From ec3956ba48f2212cd23cfdb88d4e777b51441146 Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Thu, 15 May 2008 01:53:48 +0000 Subject: [PATCH] fix connection keep-alive behavior for HTTP/1.0 svn:r822 --- ChangeLog | 2 +- http.c | 32 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 38b52b3c..7d4ea87e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -99,7 +99,7 @@ Changes in current version: o Make configure script work on IRIX. o provide a method for canceling ongoing http requests. o Make vsnprintf() returns consistent on win32. - + o Fix connection keep-alive behavior for HTTP/1.0 Changes in 1.4.0: o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr. diff --git a/http.c b/http.c index 82d6a895..e3618be9 100644 --- a/http.c +++ b/http.c @@ -397,24 +397,34 @@ static void evhttp_make_header_response(struct evhttp_connection *evcon, struct evhttp_request *req) { + int is_keepalive = evhttp_is_connection_keepalive(req->input_headers); evbuffer_add_printf(bufferevent_get_output(evcon->bufev), "HTTP/%d.%d %d %s\r\n", req->major, req->minor, req->response_code, req->response_code_line); - if (req->major == 1 && req->minor == 1) - evhttp_maybe_add_date_header(req->output_headers); + if (req->major == 1) { + if (req->minor == 1) + evhttp_maybe_add_date_header(req->output_headers); - if (req->major == 1 && - (req->minor == 1 || - evhttp_is_connection_keepalive(req->input_headers))) { - /* - * we need to add the content length if the user did - * not give it, this is required for persistent - * connections to work. + /* + * if the protocol is 1.0; and the connection was keep-alive + * we need to add a keep-alive header, too. */ - evhttp_maybe_add_content_length_header(req->output_headers, - (long)EVBUFFER_LENGTH(req->output_buffer)); + if (req->minor == 0 && is_keepalive) + evhttp_add_header(req->output_headers, + "Connection", "keep-alive"); + + if (req->minor == 1 || is_keepalive) { + /* + * we need to add the content length if the + * user did not give it, this is required for + * persistent connections to work. + */ + evhttp_maybe_add_content_length_header( + req->output_headers, + (long)EVBUFFER_LENGTH(req->output_buffer)); + } } /* Potentially add headers for unidentified content. */