#include bool response_cacheable(evapp_ctx *ctx, enum cacheable_t type, size_t lifetime) { char header[64]; // XXX: long enough? struct tm tm; time_t t; struct evhttp_request *request = evapp_request(ctx); switch (type) { case CACHE_PUBLIC: t = time(NULL) + lifetime; gmtime_r(&t, &tm); /* Add the expires in the future. Let evhttp stamp the date */ if (strftime(header, sizeof(header), "%a, %d %b %Y %H:%M:%S GMT", &tm) != 0) { evhttp_add_header(request->output_headers, "Expires", header); } snprintf(header, sizeof(header), "public, max-age=%zu", lifetime); evhttp_add_header(request->output_headers, "Cache-Control", header); break; case CACHE_PRIVATE: /* Disallow HTTP/1.0 caching by setting * the expires == date. Not setting it in the past * due to a firefox bug */ t = time(NULL); gmtime_r(&t, &tm); if (strftime(header, sizeof(header), "%a, %d %b %Y %H:%M:%S GMT", &tm) != 0) { evhttp_add_header(request->output_headers, "Expires", header); evhttp_add_header(request->output_headers, "Date", header); } snprintf(header, sizeof(header), "private, max-age=%zu", lifetime); evhttp_add_header(request->output_headers, "Cache-Control", header); break; case CACHE_NONE: t = time(NULL); gmtime_r(&t, &tm); if (strftime(header, sizeof(header), "%a, %d %b %Y %H:%M:%S GMT", &tm) != 0) { evhttp_add_header(request->output_headers, "Expires", header); evhttp_add_header(request->output_headers, "Date", header); } evhttp_add_header(request->output_headers, "Cache-Control", "no-cache"); break; default: return false; break; } return true; }