Browse Source

add expires header for static assets (right now 1 week in the future)

v0.1.8
Georg Hopp 12 years ago
parent
commit
60bb868abb
  1. 2
      include/http/response.h
  2. 9
      src/http/response/asset.c
  3. 6
      src/http/worker/get_asset.c

2
include/http/response.h

@ -49,7 +49,7 @@ HttpResponse httpResponse403();
HttpResponse httpResponse404(); HttpResponse httpResponse404();
HttpResponse httpResponse500(); HttpResponse httpResponse500();
HttpResponse httpResponseJson(const char *, size_t); HttpResponse httpResponseJson(const char *, size_t);
HttpResponse httpResponseAsset(const char *, size_t);
HttpResponse httpResponseAsset(const char *, size_t, time_t);
#endif // __HTTP_RESPONSE_H__ #endif // __HTTP_RESPONSE_H__

9
src/http/response/asset.c

@ -49,17 +49,20 @@
#include "http/header.h" #include "http/header.h"
#include "utils/memory.h" #include "utils/memory.h"
#include "utils/http.h"
#include "hash.h" #include "hash.h"
#include "asset.h" #include "asset.h"
HttpResponse HttpResponse
httpResponseAsset(const char * fname, size_t nfname)
httpResponseAsset(const char * fname, size_t nfname, time_t exptime)
{ {
HttpResponse response; HttpResponse response;
HttpMessage message; HttpMessage message;
Asset asset = assetPoolGet(fname, nfname); Asset asset = assetPoolGet(fname, nfname);
char expires[200];
size_t nexpires;
if (NULL == asset) { if (NULL == asset) {
return NULL; return NULL;
@ -72,11 +75,15 @@ httpResponseAsset(const char * fname, size_t nfname)
message->body = asset->data; message->body = asset->data;
message->nbody = asset->size; message->nbody = asset->size;
nexpires = rfc1123Gmt(expires, sizeof(expires), &exptime);
hashAdd(message->header, hashAdd(message->header,
new(HttpHeader, CSTRA("Content-Type"), new(HttpHeader, CSTRA("Content-Type"),
asset->mime_type, asset->nmime_type)); asset->mime_type, asset->nmime_type));
hashAdd(message->header, hashAdd(message->header,
new(HttpHeader, CSTRA("ETag"), asset->etag, asset->netag)); new(HttpHeader, CSTRA("ETag"), asset->etag, asset->netag));
hashAdd(message->header,
new(HttpHeader, CSTRA("Expires"), expires, nexpires));
hashAdd(message->header, hashAdd(message->header,
new(HttpHeader, CSTRA("Last-Modified"), new(HttpHeader, CSTRA("Last-Modified"),
asset->mtime, asset->nmtime)); asset->mtime, asset->nmtime));

6
src/http/worker/get_asset.c

@ -21,6 +21,7 @@
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <time.h>
#include "http/header.h" #include "http/header.h"
#include "http/message.h" #include "http/message.h"
@ -54,7 +55,10 @@ httpWorkerGetAsset(HttpWorker this, const char * fname)
nmatch = (header->nvalue)[0]; nmatch = (header->nvalue)[0];
} }
message = (HttpMessage)httpResponseAsset(fname, nfname);
message = (HttpMessage)httpResponseAsset(
fname,
nfname,
time(NULL) + 604800);
if (NULL == message) { if (NULL == message) {
return (HttpMessage)httpResponse404(); return (HttpMessage)httpResponse404();

Loading…
Cancel
Save