Hi,
Here is a patch for some cache control in httpd. I've embedded this into
the 'types' section so one can have the following in httpd.conf:
types {
text/html htm html
text/css css < 604800
image/jpeg jpeg jpg < 86400
}
Questions:
- Is there any interest?
- Is the right place (maybe this shoud be in 'location')?
Index: http.h
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/http.h,v
retrieving revision 1.12
diff -u -p -r1.12 http.h
--- http.h 11 Feb 2015 12:52:01 -0000 1.12
+++ http.h 15 Sep 2015 12:18:20 -0000
@@ -208,21 +208,22 @@ struct http_mediatype {
char *media_name;
char *media_type;
char *media_subtype;
+ int media_maxage;
};
/*
* Some default media types based on (2014-08-04 version):
* https://www.iana.org/assignments/media-types/media-types.xhtml
*/
#define MEDIA_TYPES { \
- { "css", "text", "css" }, \
- { "html", "text", "html" }, \
- { "txt", "text", "plain" }, \
- { "gif", "image", "gif" }, \
- { "jpeg", "image", "jpeg" }, \
- { "jpg", "image", "jpeg" }, \
- { "png", "image", "png" }, \
- { "svg", "image", "svg+xml" }, \
- { "js", "application", "javascript" }, \
+ { "css", "text", "css", 0 }, \
+ { "html", "text", "html", 0 }, \
+ { "txt", "text", "plain", 0 }, \
+ { "gif", "image", "gif", 0 }, \
+ { "jpeg", "image", "jpeg", 0 }, \
+ { "jpg", "image", "jpeg", 0 }, \
+ { "png", "image", "png", 0 }, \
+ { "svg", "image", "svg+xml", 0 }, \
+ { "js", "application", "javascript", 0 }, \
{ NULL } \
}
Index: httpd.h
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/httpd.h,v
retrieving revision 1.81
diff -u -p -r1.81 httpd.h
--- httpd.h 23 Feb 2015 18:43:18 -0000 1.81
+++ httpd.h 15 Sep 2015 12:18:20 -0000
@@ -457,6 +457,7 @@ struct media_type {
char media_type[MEDIATYPE_TYPEMAX];
char media_subtype[MEDIATYPE_TYPEMAX];
char *media_encoding;
+ int media_maxage;
RB_ENTRY(media_type) media_entry;
};
RB_HEAD(mediatypes, media_type);
Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/parse.y,v
retrieving revision 1.65
diff -u -p -r1.65 parse.y
--- parse.y 12 Feb 2015 04:40:23 -0000 1.65
+++ parse.y 15 Sep 2015 12:18:20 -0000
@@ -960,7 +960,15 @@ mediaoptsl : STRING '/' STRING {
}
free($1);
free($3);
- } medianames_l optsemicolon
+ } medianames_l optmaxage optsemicolon {
+ if (!loadcfg)
+ break;
+
+ if (media_add(conf->sc_mediatypes, &media) == NULL) {
+ yyerror("failed to add media type");
+ YYERROR;
+ }
+ }
| include
;
@@ -977,15 +985,18 @@ medianamesl : numberstring
{
YYERROR;
}
free($1);
+ }
+ ;
- if (!loadcfg)
- break;
-
- if (media_add(conf->sc_mediatypes, &media) == NULL) {
- yyerror("failed to add media type");
+optmaxage : '<' NUMBER {
+ if ($2 < 0) {
+ yyerror("invalid maxage: %lld", $2);
YYERROR;
}
+
+ media.media_maxage = $2;
}
+ |
;
port : PORT NUMBER {
@@ -1553,6 +1564,7 @@ load_config(const char *filename, struct
(void)strlcpy(m.media_subtype,
mediatypes[i].media_subtype,
sizeof(m.media_subtype));
+ m.media_maxage = mediatypes[i].media_maxage;
m.media_encoding = NULL;
if (media_add(conf->sc_mediatypes, &m) == NULL) {
Index: server_http.c
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/server_http.c,v
retrieving revision 1.75
diff -u -p -r1.75 server_http.c
--- server_http.c 23 Feb 2015 18:43:18 -0000 1.75
+++ server_http.c 15 Sep 2015 12:18:20 -0000
@@ -1130,7 +1130,7 @@ server_response_http(struct client *clt,
struct http_descriptor *desc = clt->clt_descreq;
struct http_descriptor *resp = clt->clt_descresp;
const char *error;
- struct kv *ct, *cl;
+ struct kv *ct, *cl, *cc;
char tmbuf[32];
if (desc == NULL || (error = server_httperror_byid(code)) == NULL)
@@ -1169,6 +1169,10 @@ server_response_http(struct client *clt,
kv_set(cl, "%ld", size) == -1)
return (-1);
+ if ((cc = kv_add(&resp->http_headers, "Cache-Control", NULL)) == NULL ||
+ kv_set(cc, "max-age=%lld", media->media_maxage) == -1)
+ return (-1);
+
/* Set last modification time */
if (server_http_time(mtime, tmbuf, sizeof(tmbuf)) <= 0 ||
kv_add(&resp->http_headers, "Last-Modified", tmbuf) == NULL)
--
Manuel Giraud