From 60bb868abb956f0ebc2ee3f077e965e42be84ba5 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 13 Oct 2013 21:47:19 +0100 Subject: [PATCH] add expires header for static assets (right now 1 week in the future) --- include/http/response.h | 2 +- src/http/response/asset.c | 9 ++++++++- src/http/worker/get_asset.c | 6 +++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/http/response.h b/include/http/response.h index 57a7da3..329b945 100644 --- a/include/http/response.h +++ b/include/http/response.h @@ -49,7 +49,7 @@ HttpResponse httpResponse403(); HttpResponse httpResponse404(); HttpResponse httpResponse500(); HttpResponse httpResponseJson(const char *, size_t); -HttpResponse httpResponseAsset(const char *, size_t); +HttpResponse httpResponseAsset(const char *, size_t, time_t); #endif // __HTTP_RESPONSE_H__ diff --git a/src/http/response/asset.c b/src/http/response/asset.c index 29e727a..a003e9e 100644 --- a/src/http/response/asset.c +++ b/src/http/response/asset.c @@ -49,17 +49,20 @@ #include "http/header.h" #include "utils/memory.h" +#include "utils/http.h" #include "hash.h" #include "asset.h" HttpResponse -httpResponseAsset(const char * fname, size_t nfname) +httpResponseAsset(const char * fname, size_t nfname, time_t exptime) { HttpResponse response; HttpMessage message; Asset asset = assetPoolGet(fname, nfname); + char expires[200]; + size_t nexpires; if (NULL == asset) { return NULL; @@ -72,11 +75,15 @@ httpResponseAsset(const char * fname, size_t nfname) message->body = asset->data; message->nbody = asset->size; + nexpires = rfc1123Gmt(expires, sizeof(expires), &exptime); + hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), asset->mime_type, asset->nmime_type)); hashAdd(message->header, new(HttpHeader, CSTRA("ETag"), asset->etag, asset->netag)); + hashAdd(message->header, + new(HttpHeader, CSTRA("Expires"), expires, nexpires)); hashAdd(message->header, new(HttpHeader, CSTRA("Last-Modified"), asset->mtime, asset->nmtime)); diff --git a/src/http/worker/get_asset.c b/src/http/worker/get_asset.c index bcc82dd..3569e0d 100644 --- a/src/http/worker/get_asset.c +++ b/src/http/worker/get_asset.c @@ -21,6 +21,7 @@ */ #include +#include #include "http/header.h" #include "http/message.h" @@ -54,7 +55,10 @@ httpWorkerGetAsset(HttpWorker this, const char * fname) nmatch = (header->nvalue)[0]; } - message = (HttpMessage)httpResponseAsset(fname, nfname); + message = (HttpMessage)httpResponseAsset( + fname, + nfname, + time(NULL) + 604800); if (NULL == message) { return (HttpMessage)httpResponse404();