From e0abf3ac919ffecd891d7b7d3a4dd1c79646077b Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 10 Sep 2013 12:38:19 +0100 Subject: [PATCH] now every date header is formatted in GMT. Commment: We still have a very weak Etag implementation. --- include/utils/http.h | 3 +++ src/asset/asset.c | 7 ++++-- src/http/worker/add_common_header.c | 22 ++++-------------- src/utils/http.c | 36 +++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/include/utils/http.h b/include/utils/http.h index 5e877bb..6a16957 100644 --- a/include/utils/http.h +++ b/include/utils/http.h @@ -23,10 +23,13 @@ #ifndef __UTILS_HTTP_H__ #define __UTILS_HTTP_H__ +#include #include #include "http/message.h" +size_t rfc1123Gmt(char *, size_t, const time_t *); +size_t rfc1123GmtNow(char *, size_t); char isHttpVersion(const char *, size_t); HttpMessage httpGetMessage( const char *, size_t, diff --git a/src/asset/asset.c b/src/asset/asset.c index 9e2c8a0..4de2cc8 100644 --- a/src/asset/asset.c +++ b/src/asset/asset.c @@ -43,6 +43,7 @@ #include "utils/mime_type.h" #include "utils/hash.h" +#include "utils/http.h" static @@ -74,8 +75,10 @@ assetCtor(void * _this, va_list * params) tmp = localtime(&(st.st_mtime)); this->netag = strftime(this->etag, sizeof(this->etag), "%s", tmp); - this->nmtime = strftime( - this->mtime, sizeof(this->mtime), "%a, %d %b %Y %T %Z", tmp); + this->nmtime = rfc1123Gmt( + this->mtime, + sizeof(this->mtime), + &(st.st_mtime)); this->size = st.st_size; diff --git a/src/http/worker/add_common_header.c b/src/http/worker/add_common_header.c index 014c815..74890d8 100644 --- a/src/http/worker/add_common_header.c +++ b/src/http/worker/add_common_header.c @@ -20,7 +20,6 @@ * along with this program. If not, see . */ -#include #include #include "class.h" @@ -29,23 +28,17 @@ #include "http/header.h" #include "http/response.h" -#include "utils/memory.h" #include "hash.h" -static const char *DAY_NAMES[] = { - "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; -static const char *MONTH_NAMES[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; +#include "utils/memory.h" +#include "utils/http.h" void httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) { - time_t t; - struct tm * tmp; - char buffer[200]; - size_t nbuf; + char buffer[200]; + size_t nbuf; if (httpMessageHasKeepAlive(request)) { hashAdd(response->header, @@ -69,12 +62,7 @@ httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) new(HttpHeader, CSTRA("Content-Length"), buffer, nbuf)); } - t = time(NULL); - tmp = gmtime(&t); - nbuf = strftime(buffer, sizeof(buffer), "---, %d --- %Y %T GMT", tmp); - memcpy(buffer, DAY_NAMES[tmp->tm_wday], 3); - memcpy(buffer+8, MONTH_NAMES[tmp->tm_mon], 3); - + nbuf = rfc1123GmtNow(buffer, sizeof(buffer)); hashAdd(response->header, new(HttpHeader, CSTRA("Date"), buffer, nbuf)); } diff --git a/src/utils/http.c b/src/utils/http.c index e24ce9d..04e9ec1 100644 --- a/src/utils/http.c +++ b/src/utils/http.c @@ -20,6 +20,7 @@ * along with this program. If not, see . */ +#include #include #include #include @@ -32,6 +33,41 @@ #include "commons.h" + +static const char *DAY_NAMES[] = { + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; +static const char *MONTH_NAMES[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + + +/* + * This one is not thread save. + */ +size_t +rfc1123Gmt(char * buffer, size_t _nbuf, const time_t * t) +{ + struct tm * tmp = gmtime(t); + size_t nbuf; + + nbuf = strftime(buffer, _nbuf, "---, %d --- %Y %T GMT", tmp); + memcpy(buffer, DAY_NAMES[tmp->tm_wday], 3); + memcpy(buffer+8, MONTH_NAMES[tmp->tm_mon], 3); + + return nbuf; +} + +/* + * This one is not thread save. + */ +size_t +rfc1123GmtNow(char * buffer, size_t _nbuf) +{ + time_t t = time(NULL); + + return rfc1123Gmt(buffer, _nbuf, &t); +} + char isHttpVersion(const char * str, size_t len) {