xref: /lighttpd1.4/src/mod_expire.c (revision 66698469)
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 
34 INIT_FUNC(mod_expire_init) {
35     return calloc(1, sizeof(plugin_data));
36 }
37 
38 FREE_FUNC(mod_expire_free) {
39     plugin_data * const p = p_d;
40     free(p->toffsets);
41 }
42 
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 
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 
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 
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 
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                 break;
212               default:/* should not happen */
213                 break;
214             }
215 
216             /* parse array values into structured data */
217             if (NULL != a && a->used) {
218                 p->toffsets =
219                   realloc(p->toffsets, sizeof(time_t) * (p->tused + a->used*2));
220                 time_t *toff = p->toffsets + p->tused;
221                 for (uint32_t k = 0; k < a->used; ++k, toff+=2, p->tused+=2) {
222                     buffer *v = &((data_string *)a->data[k])->value;
223                     *toff = mod_expire_get_offset(srv->errh, v, toff+1);
224                     if (-1 == *toff) {
225                         log_error(srv->errh, __FILE__, __LINE__,
226                           "parsing %s failed: %s", cpk[cpv->k_id].k, v->ptr);
227                         return HANDLER_ERROR;
228                     }
229                     /* overwrite v->used with offset int p->toffsets
230                      * as v->ptr is not used by this module after config */
231                     v->used = (uint32_t)p->tused;
232                 }
233             }
234         }
235     }
236 
237     /* initialize p->defaults from global config context */
238     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
239         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
240         if (-1 != cpv->k_id)
241             mod_expire_merge_config(&p->defaults, cpv);
242     }
243 
244     return HANDLER_GO_ON;
245 }
246 
247 static handler_t
248 mod_expire_set_header (request_st * const r, const time_t * const off)
249 {
250     const unix_time64_t cur_ts = log_epoch_secs;
251     unix_time64_t expires = off[1];
252     if (0 == off[0]) { /* access */
253         expires += cur_ts;
254     }
255     else {             /* modification */
256         const stat_cache_st * const st = stat_cache_path_stat(&r->physical.path);
257         /* can't set modification-based expire if mtime is not available */
258         if (NULL == st) return HANDLER_GO_ON;
259         expires += TIME64_CAST(st->st_mtime);
260     }
261 
262     /* expires should be at least cur_ts */
263     if (expires < cur_ts) expires = cur_ts;
264 
265     /* HTTP/1.1 dictates that Cache-Control overrides Expires if both present.
266      * Therefore, send only Cache-Control to HTTP/1.1 requests.  This means
267      * that if an intermediary upgraded the request to HTTP/1.1, and the actual
268      * client sent HTTP/1.0, then the actual client might not understand
269      * Cache-Control when it may have understood Expires.  RFC 2616 HTTP/1.1
270      * was released June 1999, almost 22 years ago (as this comment is written).
271      * If a client today is sending HTTP/1.0, chances are the client does not
272      * cache.  Avoid the overhead of formatting time for Expires to send both
273      * Cache-Control and Expires when the majority of clients are HTTP/1.1 or
274      * HTTP/2 (or later). */
275     buffer *vb;
276     if (r->http_version > HTTP_VERSION_1_0) {
277         vb = http_header_response_set_ptr(r, HTTP_HEADER_CACHE_CONTROL,
278                                           CONST_STR_LEN("Cache-Control"));
279         buffer_append_string_len(vb, CONST_STR_LEN("max-age="));
280         buffer_append_int(vb, expires - cur_ts);
281     }
282     else { /* HTTP/1.0 */
283         vb = http_header_response_set_ptr(r, HTTP_HEADER_EXPIRES,
284                                           CONST_STR_LEN("Expires"));
285         http_date_time_append(vb, expires);
286     }
287 
288     return HANDLER_GO_ON;
289 }
290 
291 REQUEST_FUNC(mod_expire_handler) {
292 	plugin_data *p = p_d;
293 	buffer *vb;
294 	const data_string *ds;
295 
296 	/* Add caching headers only to http_status
297 	 * 200 OK or 204 No Content or 206 Partial Content */
298 	if (r->http_status != 200 && r->http_status != 204 && r->http_status != 206)
299 		return HANDLER_GO_ON;
300 	/* Add caching headers only to GET, HEAD, QUERY requests */
301 	if (!http_method_get_head_query(r->http_method)) return HANDLER_GO_ON;
302 	/* Add caching headers only if not already present */
303 	vb = http_header_response_get(r, HTTP_HEADER_CACHE_CONTROL, CONST_STR_LEN("Cache-Control"));
304 	if (NULL != vb) return HANDLER_GO_ON;
305 
306 	mod_expire_patch_config(r, p);
307 
308 	/* check expire.url */
309 	ds = p->conf.expire_url
310 	  ? (const data_string *)array_match_key_prefix(p->conf.expire_url, &r->uri.path)
311 	  : NULL;
312 	/* check expire.mimetypes (if no match with expire.url) */
313 	if (NULL == ds) {
314 		if (NULL == p->conf.expire_mimetypes) return HANDLER_GO_ON;
315 		vb = http_header_response_get(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"));
316 		if (NULL != vb)
317 			ds = (const data_string *)
318 			     array_match_key_prefix(p->conf.expire_mimetypes, vb);
319 		if (NULL == ds) {
320 			ds = (const data_string *)
321 			     array_get_element_klen(p->conf.expire_mimetypes,
322 			                            CONST_STR_LEN(""));
323 			if (NULL == ds) return HANDLER_GO_ON;
324 		}
325 	}
326 
327 	return mod_expire_set_header(r, p->toffsets + ds->value.used);
328 }
329 
330 
331 int mod_expire_plugin_init(plugin *p);
332 int mod_expire_plugin_init(plugin *p) {
333 	p->version     = LIGHTTPD_VERSION_ID;
334 	p->name        = "expire";
335 
336 	p->init        = mod_expire_init;
337 	p->cleanup     = mod_expire_free;
338 	p->set_defaults= mod_expire_set_defaults;
339 	p->handle_response_start = mod_expire_handler;
340 
341 	return 0;
342 }
343