Properly handle keep-alive connections

HTTP/1.1 connections are keep-alive by default,
HTTP/1.0 are KA only if explicitly requested.

PUBLISHED_FROM=cb2070c2d4e4be6beeab4ae5914b8a01b04bc0cb
This commit is contained in:
Deomid Ryabkov 2016-04-25 16:40:38 +01:00 committed by rojer
parent 69bb96113b
commit 61672c7805
2 changed files with 32 additions and 26 deletions

View File

@ -35,7 +35,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
} else if (mg_vcmp(&hm->uri, "/printcontent") == 0) {
char buf[100] = {0};
memcpy(buf, hm->body.p,
sizeof(buf) - 1 < hm->body.len? sizeof(buf) - 1 : hm->body.len);
sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len);
printf("%s\n", buf);
} else {
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
@ -57,6 +57,12 @@ int main(int argc, char *argv[]) {
mg_mgr_init(&mgr, NULL);
/* Use current binary directory as document root */
if (argc > 0 && ((cp = strrchr(argv[0], DIRSEP)) != NULL)) {
*cp = '\0';
s_http_server_opts.document_root = argv[0];
}
/* Process command line options to customize HTTP server */
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-D") == 0 && i + 1 < argc) {
@ -110,17 +116,10 @@ int main(int argc, char *argv[]) {
#endif
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = ".";
s_http_server_opts.enable_directory_listing = "yes";
/* Use current binary directory as document root */
if (argc > 0 && ((cp = strrchr(argv[0], '/')) != NULL ||
(cp = strrchr(argv[0], '/')) != NULL)) {
*cp = '\0';
s_http_server_opts.document_root = argv[0];
}
printf("Starting RESTful server on port %s\n", s_http_port);
printf("Starting RESTful server on port %s, serving %s\n", s_http_port,
s_http_server_opts.document_root);
for (;;) {
mg_mgr_poll(&mgr, 1000);
}

View File

@ -4130,6 +4130,7 @@ struct mg_http_proto_data_file {
FILE *fp; /* Opened file. */
int64_t cl; /* Content-Length. How many bytes to send. */
int64_t sent; /* How many bytes have been already sent. */
int keepalive; /* Keep connection open after sending. */
enum mg_http_proto_data_type type;
};
@ -4812,10 +4813,8 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) {
mg_send(nc, buf, n);
pd->file.sent += n;
} else {
if (!pd->file.keepalive) nc->flags |= MG_F_SEND_AND_CLOSE;
mg_http_free_proto_data_file(&pd->file);
#ifdef MG_DISABLE_HTTP_KEEP_ALIVE
nc->flags |= MG_F_SEND_AND_CLOSE;
#endif
}
} else if (pd->file.type == DATA_PUT) {
struct mbuf *io = &nc->recv_mbuf;
@ -4827,10 +4826,8 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) {
pd->file.sent += n;
}
if (n == 0 || pd->file.sent >= pd->file.cl) {
if (!pd->file.keepalive) nc->flags |= MG_F_SEND_AND_CLOSE;
mg_http_free_proto_data_file(&pd->file);
#ifdef MG_DISABLE_HTTP_KEEP_ALIVE
nc->flags |= MG_F_SEND_AND_CLOSE;
#endif
}
}
#ifndef MG_DISABLE_CGI
@ -5946,6 +5943,17 @@ static void mg_http_send_file2(struct mg_connection *nc, const char *path,
}
}
#ifndef MG_DISABLE_HTTP_KEEP_ALIVE
{
struct mg_str *conn_hdr = mg_get_http_header(hm, "Connection");
if (conn_hdr != NULL) {
pd->file.keepalive = (mg_vcasecmp(conn_hdr, "keep-alive") == 0);
} else {
pd->file.keepalive = (mg_vcmp(&hm->proto, "HTTP/1.1") == 0);
}
}
#endif
mg_http_construct_etag(etag, sizeof(etag), st);
mg_gmt_time_string(current_time, sizeof(current_time), &t);
mg_gmt_time_string(last_modified, sizeof(last_modified), &st->st_mtime);
@ -5963,14 +5971,13 @@ static void mg_http_send_file2(struct mg_connection *nc, const char *path,
"Last-Modified: %s\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Type: %.*s\r\n"
#ifdef MG_DISABLE_HTTP_KEEP_ALIVE
"Connection: close\r\n"
#endif
"Connection: %s\r\n"
"Content-Length: %" SIZE_T_FMT
"\r\n"
"%sEtag: %s\r\n\r\n",
current_time, last_modified, (int) mime_type.len, mime_type.p,
(size_t) cl, range, etag);
(pd->file.keepalive ? "keep-alive" : "close"), (size_t) cl, range,
etag);
pd->file.cl = cl;
pd->file.type = DATA_FILE;