kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit c64d3e7854767475dc75b15b3b9105b462c33e32
parent 07079dc8c054b4617e3239788cc95a64e09e6698
Author: Joris Vink <joris@coders.se>
Date:   Tue, 15 Oct 2013 11:10:45 +0200

Add http_keepalive_time configuration parameter.

Allows you to configure maximum amount of seconds an HTTP connection
can stay open (does not affect SPDY connections). If set to 0 it will
disable keep-alive all together.

Add some inttypes fluff.

Diffstat:
includes/http.h | 2++
modules/example/module.conf | 9++++++++-
src/config.c | 26+++++++++++++++++++++++++-
src/connection.c | 10++++++++++
src/http.c | 18+++++++++++++-----
5 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/includes/http.h b/includes/http.h @@ -17,6 +17,7 @@ #ifndef __H_HTTP_H #define __H_HTTP_H +#define HTTP_KEEPALIVE_TIME 20 #define HTTP_HSTS_ENABLE 31536000 #define HTTP_HEADER_MAX_LEN 4096 #define HTTP_POSTBODY_MAX_LEN 10240000 @@ -82,6 +83,7 @@ extern int http_request_count; extern u_int16_t http_header_max; extern u_int64_t http_postbody_max; extern u_int64_t http_hsts_enable; +extern u_int16_t http_keepalive_time; void http_init(void); void http_process(void); diff --git a/modules/example/module.conf b/modules/example/module.conf @@ -35,12 +35,19 @@ workers 4 # HTTP specific settings. # http_header_max Maximum size of HTTP headers (in bytes). +# # http_postbody_max Maximum size of an HTTP POST body (in bytes). +# +# http_keepalive_time Maximum seconds an HTTP connection can be +# kept alive by the browser. +# (Set to 0 to disable keepalive completely). +# # http_hsts_enable Send Strict Transport Security header in # all responses. Parameter is the age. -# Set age to 0 to disable sending this header. +# (Set to 0 to disable sending this header). #http_header_max 4096 #http_postbody_max 10240000 +#http_keepalive_time 20 #http_hsts_enable 31536000 # Specifies what module to be loaded. diff --git a/src/config.c b/src/config.c @@ -45,6 +45,7 @@ static int configure_kore_cb_worker(char **); static int configure_http_header_max(char **); static int configure_http_postbody_max(char **); static int configure_http_hsts_enable(char **); +static int configure_http_keepalive_time(char **); static void domain_sslstart(void); static struct { @@ -75,6 +76,7 @@ static struct { { "http_header_max", configure_http_header_max }, { "http_postbody_max", configure_http_postbody_max }, { "http_hsts_enable", configure_http_hsts_enable }, + { "http_keepalive_time", configure_http_keepalive_time }, { NULL, NULL }, }; @@ -566,7 +568,7 @@ configure_http_hsts_enable(char **argv) return (KORE_RESULT_ERROR); } - http_hsts_enable = kore_strtonum(argv[1], 10, 1, ULONG_MAX, &err); + http_hsts_enable = kore_strtonum(argv[1], 10, 0, ULONG_MAX, &err); if (err != KORE_RESULT_OK) { printf("bad http_hsts_enable value: %s\n", argv[1]); return (KORE_RESULT_ERROR); @@ -575,6 +577,28 @@ configure_http_hsts_enable(char **argv) return (KORE_RESULT_OK); } +static int +configure_http_keepalive_time(char **argv) +{ + int err; + + if (argv[1] == NULL) + return (KORE_RESULT_ERROR); + + if (http_keepalive_time != HTTP_KEEPALIVE_TIME) { + kore_debug("http_keepalive_time already set"); + return (KORE_RESULT_ERROR); + } + + http_keepalive_time = kore_strtonum(argv[1], 10, 0, USHRT_MAX, &err); + if (err != KORE_RESULT_OK) { + printf("bad http_keepalive_time value: %s\n", argv[1]); + return (KORE_RESULT_ERROR); + } + + return (KORE_RESULT_OK); +} + static void domain_sslstart(void) { diff --git a/src/connection.c b/src/connection.c @@ -152,6 +152,11 @@ kore_connection_handle(struct connection *c) NULL, spdy_frame_recv); } else if (!memcmp(data, "http/1.1", MIN(8, len))) { c->proto = CONN_PROTO_HTTP; + if (http_keepalive_time != 0) { + c->idle_timer.length = + http_keepalive_time * 1000; + } + net_recv_queue(c, http_header_max, NETBUF_CALL_CB_ALWAYS, NULL, http_header_recv); @@ -160,6 +165,11 @@ kore_connection_handle(struct connection *c) } } else { c->proto = CONN_PROTO_HTTP; + if (http_keepalive_time != 0) { + c->idle_timer.length = + http_keepalive_time * 1000; + } + net_recv_queue(c, http_header_max, NETBUF_CALL_CB_ALWAYS, NULL, http_header_recv); diff --git a/src/http.c b/src/http.c @@ -17,6 +17,7 @@ #include <sys/param.h> #include <ctype.h> +#include <inttypes.h> #include "spdy.h" #include "kore.h" @@ -33,6 +34,7 @@ static struct kore_pool http_header_pool; int http_request_count; u_int64_t http_hsts_enable = HTTP_HSTS_ENABLE; u_int16_t http_header_max = HTTP_HEADER_MAX_LEN; +u_int16_t http_keepalive_time = HTTP_KEEPALIVE_TIME; u_int64_t http_postbody_max = HTTP_POSTBODY_MAX_LEN; void @@ -262,7 +264,7 @@ http_response(struct http_request *req, int status, u_int8_t *d, u_int32_t len) if (http_hsts_enable) { snprintf(sbuf, sizeof(sbuf), - "max-age=%lu", http_hsts_enable); + "max-age=%" PRIu64, http_hsts_enable); spdy_header_block_add(hblock, ":strict-transport-security", sbuf); } @@ -298,13 +300,19 @@ http_response(struct http_request *req, int status, u_int8_t *d, u_int32_t len) kore_buf_appendf(buf, "HTTP/1.1 %d %s\r\n", status, http_status_text(status)); kore_buf_appendf(buf, "Content-length: %d\r\n", len); - kore_buf_appendf(buf, "Connection: keep-alive\r\n"); - kore_buf_appendf(buf, "Keep-Alive: timeout=20\r\n"); kore_buf_appendf(buf, "Server: %s\r\n", KORE_NAME_STRING); + if (http_keepalive_time) { + kore_buf_appendf(buf, "Connection: keep-alive\r\n"); + kore_buf_appendf(buf, "Keep-Alive: timeout=%d\r\n", + http_keepalive_time); + } else { + kore_buf_appendf(buf, "Connection: close\r\n"); + } + if (http_hsts_enable) { - kore_buf_appendf(buf, - "Strict-Transport-Security: max-age=%lu\r\n", + kore_buf_appendf(buf, "Strict-Transport-Security: "); + kore_buf_appendf(buf, "max-age=%" PRIu64 "\r\n", http_hsts_enable); }