diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index 615006d..8844b66 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -80,7 +80,7 @@ struct connection int header_dont_free, header_only, http_code; enum { REPLY_GENERATED, REPLY_FROMFILE } reply_type; - char *reply, *lastmod; /* reply lastmod, not request if-mod-since */ + char *reply; int reply_dont_free; FILE *reply_file; size_t reply_length, reply_sent; @@ -665,7 +665,6 @@ static struct connection *new_connection(void) conn->header_only = 0; conn->http_code = 0; conn->reply = NULL; - conn->lastmod = NULL; conn->reply_dont_free = 0; conn->reply_file = NULL; conn->reply_sent = 0; @@ -725,7 +724,6 @@ static void free_connection(struct connection *conn) if (conn->user_agent != NULL) free(conn->user_agent); if (conn->header != NULL && !conn->header_dont_free) free(conn->header); if (conn->reply != NULL && !conn->reply_dont_free) free(conn->reply); - if (conn->lastmod != NULL) free(conn->lastmod); if (conn->reply_file != NULL) fclose(conn->reply_file); } @@ -761,17 +759,17 @@ static void poll_check_timeout(struct connection *conn) /* --------------------------------------------------------------------------- - * Build an RFC1123 date in the static buffer _date[] and return it. + * Format [when] as an RFC1123 date, stored in the specified buffer. The same + * buffer is returned for convenience. */ -#define MAX_DATE_LENGTH 29 /* strlen("Fri, 28 Feb 2003 00:02:08 GMT") */ -static char _date[MAX_DATE_LENGTH + 1]; -static char *rfc1123_date(const time_t when) +#define DATE_LEN 30 /* strlen("Fri, 28 Feb 2003 00:02:08 GMT")+1 */ +static char *rfc1123_date(char *dest, const time_t when) { time_t now = when; - if (strftime(_date, MAX_DATE_LENGTH, + if (strftime(dest, DATE_LEN, "%a, %d %b %Y %H:%M:%S %Z", gmtime(&now) ) == 0) - errx(1, "strftime() failed"); - return _date; + errx(1, "strftime() failed [%s]", dest); + return dest; } @@ -823,13 +821,16 @@ static char *urldecode(const char *url) static void default_reply(struct connection *conn, const int errcode, const char *errname, const char *format, ...) { - char *reason; + char *reason, date[DATE_LEN]; va_list va; va_start(va, format); xvasprintf(&reason, format, va); va_end(va); + /* Only really need to calculate the date once. */ + (void)rfc1123_date(date, time(NULL)); + conn->reply_length = xasprintf(&(conn->reply), "%d %s\n" "

%s

\n" /* errname */ @@ -837,7 +838,7 @@ static void default_reply(struct connection *conn, "
\n" "Generated by %s on %s\n" "\n", - errcode, errname, errname, reason, pkgname, rfc1123_date(time(NULL))); + errcode, errname, errname, reason, pkgname, date); free(reason); conn->header_length = xasprintf(&(conn->header), @@ -848,8 +849,7 @@ static void default_reply(struct connection *conn, "Content-Length: %d\r\n" "Content-Type: text/html\r\n" "\r\n", - errcode, errname, rfc1123_date(time(NULL)), pkgname, - conn->reply_length); + errcode, errname, date, pkgname, conn->reply_length); conn->reply_type = REPLY_GENERATED; conn->http_code = errcode; @@ -924,6 +924,7 @@ static void parse_request(struct connection *conn) static void process_get(struct connection *conn) { char *decoded_url, *safe_url, *target, *if_mod_since; + char date[DATE_LEN], lastmod[DATE_LEN]; const char *mimetype = NULL; struct stat filestat; @@ -986,12 +987,12 @@ static void process_get(struct connection *conn) conn->reply_type = REPLY_FROMFILE; conn->reply_length = filestat.st_size; - conn->lastmod = xstrdup(rfc1123_date(filestat.st_mtime)); + (void) rfc1123_date(lastmod, filestat.st_mtime); /* check for If-Modified-Since, may not have to send */ if_mod_since = parse_field(conn, "If-Modified-Since: "); if (if_mod_since != NULL && - strcmp(if_mod_since, conn->lastmod) == 0) + strcmp(if_mod_since, lastmod) == 0) { debugf("not modified since %s\n", if_mod_since); default_reply(conn, 304, "Not Modified", ""); @@ -1009,8 +1010,8 @@ static void process_get(struct connection *conn) "Last-Modified: %s\r\n" "\r\n" , - rfc1123_date(time(NULL)), pkgname, conn->reply_length, - mimetype, conn->lastmod + rfc1123_date(date, time(NULL)), pkgname, conn->reply_length, + mimetype, lastmod ); conn->http_code = 200; }