From fd3f47fe130d165fba2c85d2f98324e0076a642e Mon Sep 17 00:00:00 2001 From: "Malte S. Stretz" Date: Wed, 1 Sep 2010 13:40:15 +0200 Subject: [PATCH] Consolidate HTTP header generation. Always sending a Content-Encoding (with the value "identity" if uncompressed) and a Vary doesn't really hurt but allows for cleaner header generation code (and less duplication). All we lose is the date on the error messages since I wanted to avoid to format the date twice. --- http.c | 74 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/http.c b/http.c index 8eaebcf..7755b9b 100644 --- a/http.c +++ b/http.c @@ -40,9 +40,8 @@ static const char mime_type_xml[] = "text/xml"; static const char mime_type_html[] = "text/html; charset=us-ascii"; static const char mime_type_css[] = "text/css"; static const char mime_type_js[] = "text/javascript"; -static const char encoding_gzip[] = - "Vary: Accept-Encoding\r\n" - "Content-Encoding: gzip\r\n"; +static const char encoding_identity[] = "identity"; +static const char encoding_gzip[] = "gzip"; static const char server[] = PACKAGE_NAME "/" PACKAGE_VERSION; static int idletime = 60; @@ -271,7 +270,7 @@ static struct connection *new_connection(void) conn->query = NULL; conn->header = NULL; conn->mime_type = NULL; - conn->encoding = ""; + conn->encoding = NULL; conn->header_extra = ""; conn->header_length = 0; conn->header_sent = 0; @@ -365,6 +364,36 @@ static char *rfc1123_date(char *dest, const time_t when) return (dest); } +static void generate_header(struct connection *conn, + const int code, const char *text) +{ + char date[DATE_LEN]; + + assert(conn->header == NULL); + assert(conn->mime_type != NULL); + if (conn->encoding == NULL) + conn->encoding = encoding_identity; + + verbosef("http: %d %s (%s: %d bytes)", code, text, + conn->encoding, conn->reply_length); + conn->header_length = xasprintf(&(conn->header), + "HTTP/1.1 %d %s\r\n" + "Date: %s\r\n" + "Server: %s\r\n" + "Vary: Accept-Encoding\r\n" + "Content-Type: %s\r\n" + "Content-Length: %d\r\n" + "Content-Encoding: %s\r\n" + "%s" + "\r\n" + , + code, text, + rfc1123_date(date, now), server, + conn->mime_type, conn->reply_length, conn->encoding, + conn->header_extra); + conn->http_code = code; +} + /* --------------------------------------------------------------------------- @@ -380,29 +409,21 @@ static void default_reply(struct connection *conn, xvasprintf(&reason, format, va); va_end(va); - /* Only really need to calculate the date once. */ - (void)rfc1123_date(date, now); - conn->reply_length = xasprintf(&(conn->reply), "%d %s\n" "

%s

\n" /* errname */ "%s\n" /* reason */ "
\n" - "Generated by %s on %s\n" + "Generated by %s" "\n", - errcode, errname, errname, reason, server, date); + errcode, errname, errname, reason, server); free(reason); - conn->header_length = xasprintf(&(conn->header), - "HTTP/1.1 %d %s\r\n" - "Date: %s\r\n" - "Server: %s\r\n" - "Content-Length: %d\r\n" - "Content-Type: text/html\r\n" - "\r\n", - errcode, errname, date, server, conn->reply_length); - - conn->http_code = errcode; + /* forget any dangling metadata */ + conn->mime_type = mime_type_html; + conn->encoding = encoding_identity; + + generate_header(conn, errcode, errname); } @@ -643,20 +664,7 @@ static void process_get(struct connection *conn) process_gzip(conn); assert(conn->mime_type != NULL); - conn->header_length = xasprintf(&(conn->header), - "HTTP/1.1 200 OK\r\n" - "Date: %s\r\n" - "Server: %s\r\n" - "Content-Length: %d\r\n" - "Content-Type: %s\r\n" - "%s" - "%s" - "\r\n" - , - rfc1123_date(date, now), server, - conn->reply_length, conn->mime_type, conn->encoding, - conn->header_extra); - conn->http_code = 200; + generate_header(conn, 200, "OK"); } -- 2.17.1