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;
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;
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;
+}
+
/* ---------------------------------------------------------------------------
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),
"<html><head><title>%d %s</title></head><body>\n"
"<h1>%s</h1>\n" /* errname */
"%s\n" /* reason */
"<hr>\n"
- "Generated by %s on %s\n"
+ "Generated by %s"
"</body></html>\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);
}
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");
}