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 authored and rojer committed 2016-04-26 13:30:03 +01:00
1 parent 69bb96113b
commit 61672c7805
2 files changed
+32 -26

No files matched your search

+12 -13
View File
@@ -22,7 +22,7 @@ static void handle_sum_call(struct mg_connection *nc, struct http_message *hm) {
/* Compute the result and send it back as a JSON object */
result = strtod(n1, NULL) + strtod(n2, NULL);
mg_printf_http_chunk(nc, "{ \"result\": %lf }", result);
mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
}
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
@@ -31,14 +31,14 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
switch (ev) {
case MG_EV_HTTP_REQUEST:
if (mg_vcmp(&hm->uri, "/api/v1/sum") == 0) {
handle_sum_call(nc, hm); /* Handle RESTful call */
handle_sum_call(nc, hm); /* Handle RESTful call */
} 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 */
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
}
break;
default:
@@ -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);
}