xref: /lighttpd1.4/src/mod_expire.c (revision 5e14db43)
1 #include "first.h"
2 
3 #include "base.h"
4 #include "array.h"
5 #include "buffer.h"
6 #include "log.h"
7 #include "http_date.h"
8 #include "http_header.h"
9 
10 #include "plugin.h"
11 #include "stat_cache.h"
12 
13 #include "sys-time.h"
14 #include <stdlib.h>
15 #include <string.h>
16 
17 /**
18  * set HTTP headers Cache-Control and Expires
19  */
20 
21 typedef struct {
22     const array *expire_url;
23     const array *expire_mimetypes;
24 } plugin_config;
25 
26 typedef struct {
27     PLUGIN_DATA;
28     plugin_config defaults;
29     plugin_config conf;
30     time_t *toffsets;
31     uint32_t tused;
32 } plugin_data;
33 
INIT_FUNC(mod_expire_init)34 INIT_FUNC(mod_expire_init) {
35     return ck_calloc(1, sizeof(plugin_data));
36 }
37 
FREE_FUNC(mod_expire_free)38 FREE_FUNC(mod_expire_free) {
39     plugin_data * const p = p_d;
40     free(p->toffsets);
41 }
42 
mod_expire_get_offset(log_error_st * errh,const buffer * expire,time_t * offset)43 static time_t mod_expire_get_offset(log_error_st *errh, const buffer *expire, time_t *offset) {
44 	char *ts;
45 	time_t type;
46 	time_t retts = 0;
47 
48 	/*
49 	 * parse
50 	 *
51 	 * '(access|now|modification) [plus] {<num> <type>}*'
52 	 *
53 	 * e.g. 'access 1 years'
54 	 */
55 
56 	if (buffer_is_blank(expire)) {
57 		log_error(errh, __FILE__, __LINE__, "mod_expire empty string");
58 		return -1;
59 	}
60 
61 	ts = expire->ptr;
62 
63 	if (0 == strncmp(ts, "access ", 7)) {
64 		type  = 0;
65 		ts   += 7;
66 	} else if (0 == strncmp(ts, "now ", 4)) {
67 		type  = 0;
68 		ts   += 4;
69 	} else if (0 == strncmp(ts, "modification ", 13)) {
70 		type  = 1;
71 		ts   += 13;
72 	} else {
73 		/* invalid type-prefix */
74 		log_error(errh, __FILE__, __LINE__, "invalid <base>: %s", ts);
75 		return -1;
76 	}
77 
78 	if (0 == strncmp(ts, "plus ", 5)) {
79 		/* skip the optional plus */
80 		ts   += 5;
81 	}
82 
83 	/* the rest is just <number> (years|months|weeks|days|hours|minutes|seconds) */
84 	do {
85 		char *space, *err;
86 		int num;
87 
88 		if (NULL == (space = strchr(ts, ' '))) {
89 			log_error(errh, __FILE__, __LINE__,
90 			  "missing space after <num>: %s", ts);
91 			return -1;
92 		}
93 
94 		num = strtol(ts, &err, 10);
95 		if (*err != ' ') {
96 			log_error(errh, __FILE__, __LINE__,
97 			  "missing <type> after <num>: %s", ts);
98 			return -1;
99 		}
100 
101 		ts = space + 1;
102 
103 		if (NULL == (space = strchr(ts, ' ')))
104 			space = expire->ptr + buffer_clen(expire);
105 
106 		{
107 			int slen;
108 			/* */
109 
110 			slen = space - ts;
111 			if (ts[slen-1] == 's') --slen; /* strip plural */
112 
113 			if (slen == 4 && 0 == strncmp(ts, "year", slen))
114 				num *= 60 * 60 * 24 * 30 * 12;
115 			else if (slen == 5 && 0 == strncmp(ts, "month", slen))
116 				num *= 60 * 60 * 24 * 30;
117 			else if (slen == 4 && 0 == strncmp(ts, "week", slen))
118 				num *= 60 * 60 * 24 * 7;
119 			else if (slen == 3 && 0 == strncmp(ts, "day", slen))
120 				num *= 60 * 60 * 24;
121 			else if (slen == 4 && 0 == strncmp(ts, "hour", slen))
122 				num *= 60 * 60;
123 			else if (slen == 6 && 0 == strncmp(ts, "minute", slen))
124 				num *= 60;
125 			else if (slen == 6 && 0 == strncmp(ts, "second", slen))
126 				num *= 1;
127 			else {
128 				log_error(errh, __FILE__, __LINE__, "unknown type: %s", ts);
129 				return -1;
130 			}
131 
132 			retts += num;
133 
134 			if (*space == '\0') break;
135 			ts = space + 1;
136 		}
137 	} while (*ts);
138 
139 	*offset = retts;
140 
141 	return type;
142 }
143 
mod_expire_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)144 static void mod_expire_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
145     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
146       case 0: /* expire.url */
147         pconf->expire_url = cpv->v.a;
148         break;
149       case 1: /* expire.mimetypes */
150         pconf->expire_mimetypes = cpv->v.a;
151         break;
152       default:/* should not happen */
153         return;
154     }
155 }
156 
mod_expire_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)157 static void mod_expire_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
158     do {
159         mod_expire_merge_config_cpv(pconf, cpv);
160     } while ((++cpv)->k_id != -1);
161 }
162 
mod_expire_patch_config(request_st * const r,plugin_data * const p)163 static void mod_expire_patch_config(request_st * const r, plugin_data * const p) {
164     p->conf = p->defaults; /* copy small struct instead of memcpy() */
165     /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
166     for (int i = 1, used = p->nconfig; i < used; ++i) {
167         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
168             mod_expire_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
169     }
170 }
171 
SETDEFAULTS_FUNC(mod_expire_set_defaults)172 SETDEFAULTS_FUNC(mod_expire_set_defaults) {
173     static const config_plugin_keys_t cpk[] = {
174       { CONST_STR_LEN("expire.url"),
175         T_CONFIG_ARRAY_KVSTRING,
176         T_CONFIG_SCOPE_CONNECTION }
177      ,{ CONST_STR_LEN("expire.mimetypes"),
178         T_CONFIG_ARRAY_KVSTRING,
179         T_CONFIG_SCOPE_CONNECTION }
180      ,{ NULL, 0,
181         T_CONFIG_UNSET,
182         T_CONFIG_SCOPE_UNSET }
183     };
184 
185     plugin_data * const p = p_d;
186     if (!config_plugin_values_init(srv, p, cpk, "mod_expire"))
187         return HANDLER_ERROR;
188 
189     /* process and validate config directives
190      * (init i to 0 if global context; to 1 to skip empty global context) */
191     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
192         const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
193         for (; -1 != cpv->k_id; ++cpv) {
194             const array *a = NULL;
195             switch (cpv->k_id) {
196               case 0: /* expire.url */
197                 a = cpv->v.a;
198                 break;
199               case 1: /* expire.mimetypes */
200                 for (uint32_t k = 0; k < cpv->v.a->used; ++k) {
201                     data_string *ds = (data_string *)cpv->v.a->data[k];
202                     /*(omit trailing '*', if present, from prefix match)*/
203                     /*(not usually a good idea to modify array keys
204                      * since doing so might break array_get_element_klen()
205                      * search; config should be consistent in using * or not)*/
206                     size_t klen = buffer_clen(&ds->key);
207                     if (klen && ds->key.ptr[klen-1] == '*')
208                         buffer_truncate(&ds->key, klen-1);
209                 }
210                 a = cpv->v.a;
211                 if (!array_get_element_klen(a, CONST_STR_LEN("text/javascript"))
212                     && !array_get_element_klen(a, CONST_STR_LEN("text/"))) {
213                     array *m;
214                     *(const array **)&m = a;
215                     data_unset * const du =
216                       array_extract_element_klen(m,
217                         CONST_STR_LEN("application/javascript"));
218                     if (du) {
219                         buffer_copy_string_len(&du->key, "text/javascript", 15);
220                         array_replace(m, du);
221                     }
222                 }
223                 break;
224               default:/* should not happen */
225                 continue;
226             }
227 
228             /* parse array values into structured data */
229             if (NULL != a && a->used) {
230                 ck_realloc_u32((void **)&p->toffsets, p->tused,
231                                a->used*2, sizeof(*p->toffsets));
232                 time_t *toff = p->toffsets + p->tused;
233                 for (uint32_t k = 0; k < a->used; ++k, toff+=2, p->tused+=2) {
234                     buffer *v = &((data_string *)a->data[k])->value;
235                     *toff = mod_expire_get_offset(srv->errh, v, toff+1);
236                     if (-1 == *toff) {
237                         log_error(srv->errh, __FILE__, __LINE__,
238                           "parsing %s failed: %s", cpk[cpv->k_id].k, v->ptr);
239                         return HANDLER_ERROR;
240                     }
241                     /* overwrite v->used with offset int p->toffsets
242                      * as v->ptr is not used by this module after config */
243                     v->used = (uint32_t)p->tused;
244                 }
245             }
246         }
247     }
248 
249     /* initialize p->defaults from global config context */
250     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
251         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
252         if (-1 != cpv->k_id)
253             mod_expire_merge_config(&p->defaults, cpv);
254     }
255 
256     return HANDLER_GO_ON;
257 }
258 
259 static handler_t
mod_expire_set_header(request_st * const r,const time_t * const off)260 mod_expire_set_header (request_st * const r, const time_t * const off)
261 {
262     const unix_time64_t cur_ts = log_epoch_secs;
263     unix_time64_t expires = off[1];
264     if (0 == off[0]) { /* access */
265         expires += cur_ts;
266     }
267     else {             /* modification */
268         const stat_cache_st * const st = stat_cache_path_stat(&r->physical.path);
269         /* can't set modification-based expire if mtime is not available */
270         if (NULL == st) return HANDLER_GO_ON;
271         expires += TIME64_CAST(st->st_mtime);
272     }
273 
274     /* expires should be at least cur_ts */
275     if (expires < cur_ts) expires = cur_ts;
276 
277     /* HTTP/1.1 dictates that Cache-Control overrides Expires if both present.
278      * Therefore, send only Cache-Control to HTTP/1.1 requests.  This means
279      * that if an intermediary upgraded the request to HTTP/1.1, and the actual
280      * client sent HTTP/1.0, then the actual client might not understand
281      * Cache-Control when it may have understood Expires.  RFC 2616 HTTP/1.1
282      * was released June 1999, almost 22 years ago (as this comment is written).
283      * If a client today is sending HTTP/1.0, chances are the client does not
284      * cache.  Avoid the overhead of formatting time for Expires to send both
285      * Cache-Control and Expires when the majority of clients are HTTP/1.1 or
286      * HTTP/2 (or later). */
287     buffer *vb;
288     if (r->http_version > HTTP_VERSION_1_0) {
289         vb = http_header_response_set_ptr(r, HTTP_HEADER_CACHE_CONTROL,
290                                           CONST_STR_LEN("Cache-Control"));
291         buffer_append_string_len(vb, CONST_STR_LEN("max-age="));
292         buffer_append_int(vb, expires - cur_ts);
293     }
294     else { /* HTTP/1.0 */
295         vb = http_header_response_set_ptr(r, HTTP_HEADER_EXPIRES,
296                                           CONST_STR_LEN("Expires"));
297         http_date_time_append(vb, expires);
298     }
299 
300     return HANDLER_GO_ON;
301 }
302 
REQUEST_FUNC(mod_expire_handler)303 REQUEST_FUNC(mod_expire_handler) {
304 	plugin_data *p = p_d;
305 	buffer *vb;
306 	const data_string *ds;
307 
308 	/* Add caching headers only to http_status
309 	 * 200 OK or 204 No Content or 206 Partial Content */
310 	if (r->http_status != 200 && r->http_status != 204 && r->http_status != 206)
311 		return HANDLER_GO_ON;
312 	/* Add caching headers only to GET, HEAD, QUERY requests */
313 	if (!http_method_get_head_query(r->http_method)) return HANDLER_GO_ON;
314 	/* Add caching headers only if not already present */
315 	vb = http_header_response_get(r, HTTP_HEADER_CACHE_CONTROL, CONST_STR_LEN("Cache-Control"));
316 	if (NULL != vb) return HANDLER_GO_ON;
317 
318 	mod_expire_patch_config(r, p);
319 
320 	/* check expire.url */
321 	ds = p->conf.expire_url
322 	  ? (const data_string *)array_match_key_prefix(p->conf.expire_url, &r->uri.path)
323 	  : NULL;
324 	/* check expire.mimetypes (if no match with expire.url) */
325 	if (NULL == ds) {
326 		if (NULL == p->conf.expire_mimetypes) return HANDLER_GO_ON;
327 		vb = http_header_response_get(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"));
328 		if (NULL != vb)
329 			ds = (const data_string *)
330 			     array_match_key_prefix(p->conf.expire_mimetypes, vb);
331 		if (NULL == ds) {
332 			ds = (const data_string *)
333 			     array_get_element_klen(p->conf.expire_mimetypes,
334 			                            CONST_STR_LEN(""));
335 			if (NULL == ds) return HANDLER_GO_ON;
336 		}
337 	}
338 
339 	return mod_expire_set_header(r, p->toffsets + ds->value.used);
340 }
341 
342 
343 __attribute_cold__
344 int mod_expire_plugin_init(plugin *p);
mod_expire_plugin_init(plugin * p)345 int mod_expire_plugin_init(plugin *p) {
346 	p->version     = LIGHTTPD_VERSION_ID;
347 	p->name        = "expire";
348 
349 	p->init        = mod_expire_init;
350 	p->cleanup     = mod_expire_free;
351 	p->set_defaults= mod_expire_set_defaults;
352 	p->handle_response_start = mod_expire_handler;
353 
354 	return 0;
355 }
356