summaryrefslogtreecommitdiff
path: root/src/mod_expire.c
blob: a88b774d7bc4c4245e0a1f38c44a4b51eb212031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "first.h"

#include "base.h"
#include "array.h"
#include "buffer.h"
#include "log.h"
#include "http_date.h"
#include "http_header.h"

#include "plugin.h"
#include "stat_cache.h"

#include "sys-time.h"
#include <stdlib.h>
#include <string.h>

/**
 * set HTTP headers Cache-Control and Expires
 */

typedef struct {
    const array *expire_url;
    const array *expire_mimetypes;
} plugin_config;

typedef struct {
    PLUGIN_DATA;
    plugin_config defaults;
    plugin_config conf;
    time_t *toffsets;
    uint32_t tused;
} plugin_data;

INIT_FUNC(mod_expire_init) {
    return ck_calloc(1, sizeof(plugin_data));
}

FREE_FUNC(mod_expire_free) {
    plugin_data * const p = p_d;
    free(p->toffsets);
}

static time_t mod_expire_get_offset(log_error_st *errh, const buffer *expire, time_t *offset) {
	char *ts;
	time_t type;
	time_t retts = 0;

	/*
	 * parse
	 *
	 * '(access|now|modification) [plus] {<num> <type>}*'
	 *
	 * e.g. 'access 1 years'
	 */

	if (buffer_is_blank(expire)) {
		log_error(errh, __FILE__, __LINE__, "mod_expire empty string");
		return -1;
	}

	ts = expire->ptr;

	if (0 == strncmp(ts, "access ", 7)) {
		type  = 0;
		ts   += 7;
	} else if (0 == strncmp(ts, "now ", 4)) {
		type  = 0;
		ts   += 4;
	} else if (0 == strncmp(ts, "modification ", 13)) {
		type  = 1;
		ts   += 13;
	} else {
		/* invalid type-prefix */
		log_error(errh, __FILE__, __LINE__, "invalid <base>: %s", ts);
		return -1;
	}

	if (0 == strncmp(ts, "plus ", 5)) {
		/* skip the optional plus */
		ts   += 5;
	}

	/* the rest is just <number> (years|months|weeks|days|hours|minutes|seconds) */
	do {
		char *space, *err;
		int num;

		if (NULL == (space = strchr(ts, ' '))) {
			log_error(errh, __FILE__, __LINE__,
			  "missing space after <num>: %s", ts);
			return -1;
		}

		num = strtol(ts, &err, 10);
		if (*err != ' ') {
			log_error(errh, __FILE__, __LINE__,
			  "missing <type> after <num>: %s", ts);
			return -1;
		}

		ts = space + 1;

		if (NULL == (space = strchr(ts, ' ')))
			space = expire->ptr + buffer_clen(expire);

		{
			int slen;
			/* */

			slen = space - ts;
			if (ts[slen-1] == 's') --slen; /* strip plural */

			if (slen == 4 && 0 == strncmp(ts, "year", slen))
				num *= 60 * 60 * 24 * 30 * 12;
			else if (slen == 5 && 0 == strncmp(ts, "month", slen))
				num *= 60 * 60 * 24 * 30;
			else if (slen == 4 && 0 == strncmp(ts, "week", slen))
				num *= 60 * 60 * 24 * 7;
			else if (slen == 3 && 0 == strncmp(ts, "day", slen))
				num *= 60 * 60 * 24;
			else if (slen == 4 && 0 == strncmp(ts, "hour", slen))
				num *= 60 * 60;
			else if (slen == 6 && 0 == strncmp(ts, "minute", slen))
				num *= 60;
			else if (slen == 6 && 0 == strncmp(ts, "second", slen))
				num *= 1;
			else {
				log_error(errh, __FILE__, __LINE__, "unknown type: %s", ts);
				return -1;
			}

			retts += num;

			if (*space == '\0') break;
			ts = space + 1;
		}
	} while (*ts);

	*offset = retts;

	return type;
}

static void mod_expire_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
    switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
      case 0: /* expire.url */
        pconf->expire_url = cpv->v.a;
        break;
      case 1: /* expire.mimetypes */
        pconf->expire_mimetypes = cpv->v.a;
        break;
      default:/* should not happen */
        return;
    }
}

static void mod_expire_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
    do {
        mod_expire_merge_config_cpv(pconf, cpv);
    } while ((++cpv)->k_id != -1);
}

static void mod_expire_patch_config(request_st * const r, plugin_data * const p) {
    p->conf = p->defaults; /* copy small struct instead of memcpy() */
    /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
    for (int i = 1, used = p->nconfig; i < used; ++i) {
        if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
            mod_expire_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
    }
}

SETDEFAULTS_FUNC(mod_expire_set_defaults) {
    static const config_plugin_keys_t cpk[] = {
      { CONST_STR_LEN("expire.url"),
        T_CONFIG_ARRAY_KVSTRING,
        T_CONFIG_SCOPE_CONNECTION }
     ,{ CONST_STR_LEN("expire.mimetypes"),
        T_CONFIG_ARRAY_KVSTRING,
        T_CONFIG_SCOPE_CONNECTION }
     ,{ NULL, 0,
        T_CONFIG_UNSET,
        T_CONFIG_SCOPE_UNSET }
    };

    plugin_data * const p = p_d;
    if (!config_plugin_values_init(srv, p, cpk, "mod_expire"))
        return HANDLER_ERROR;

    /* process and validate config directives
     * (init i to 0 if global context; to 1 to skip empty global context) */
    for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
        const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
        for (; -1 != cpv->k_id; ++cpv) {
            const array *a = NULL;
            switch (cpv->k_id) {
              case 0: /* expire.url */
                a = cpv->v.a;
                break;
              case 1: /* expire.mimetypes */
                for (uint32_t k = 0; k < cpv->v.a->used; ++k) {
                    data_string *ds = (data_string *)cpv->v.a->data[k];
                    /*(omit trailing '*', if present, from prefix match)*/
                    /*(not usually a good idea to modify array keys
                     * since doing so might break array_get_element_klen()
                     * search; config should be consistent in using * or not)*/
                    size_t klen = buffer_clen(&ds->key);
                    if (klen && ds->key.ptr[klen-1] == '*')
                        buffer_truncate(&ds->key, klen-1);
                }
                a = cpv->v.a;
                if (!array_get_element_klen(a, CONST_STR_LEN("text/javascript"))
                    && !array_get_element_klen(a, CONST_STR_LEN("text/"))) {
                    array *m;
                    *(const array **)&m = a;
                    data_unset * const du =
                      array_extract_element_klen(m,
                        CONST_STR_LEN("application/javascript"));
                    if (du) {
                        buffer_copy_string_len(&du->key, "text/javascript", 15);
                        array_replace(m, du);
                    }
                }
                break;
              default:/* should not happen */
                continue;
            }

            /* parse array values into structured data */
            if (NULL != a && a->used) {
                ck_realloc_u32((void **)&p->toffsets, p->tused,
                               a->used*2, sizeof(*p->toffsets));
                time_t *toff = p->toffsets + p->tused;
                for (uint32_t k = 0; k < a->used; ++k, toff+=2, p->tused+=2) {
                    buffer *v = &((data_string *)a->data[k])->value;
                    *toff = mod_expire_get_offset(srv->errh, v, toff+1);
                    if (-1 == *toff) {
                        log_error(srv->errh, __FILE__, __LINE__,
                          "parsing %s failed: %s", cpk[cpv->k_id].k, v->ptr);
                        return HANDLER_ERROR;
                    }
                    /* overwrite v->used with offset int p->toffsets
                     * as v->ptr is not used by this module after config */
                    v->used = (uint32_t)p->tused;
                }
            }
        }
    }

    /* initialize p->defaults from global config context */
    if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
        const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
        if (-1 != cpv->k_id)
            mod_expire_merge_config(&p->defaults, cpv);
    }

    return HANDLER_GO_ON;
}

static handler_t
mod_expire_set_header (request_st * const r, const time_t * const off)
{
    const unix_time64_t cur_ts = log_epoch_secs;
    unix_time64_t expires = off[1];
    if (0 == off[0]) { /* access */
        expires += cur_ts;
    }
    else {             /* modification */
        const stat_cache_st * const st = stat_cache_path_stat(&r->physical.path);
        /* can't set modification-based expire if mtime is not available */
        if (NULL == st) return HANDLER_GO_ON;
        expires += TIME64_CAST(st->st_mtime);
    }

    /* expires should be at least cur_ts */
    if (expires < cur_ts) expires = cur_ts;

    /* HTTP/1.1 dictates that Cache-Control overrides Expires if both present.
     * Therefore, send only Cache-Control to HTTP/1.1 requests.  This means
     * that if an intermediary upgraded the request to HTTP/1.1, and the actual
     * client sent HTTP/1.0, then the actual client might not understand
     * Cache-Control when it may have understood Expires.  RFC 2616 HTTP/1.1
     * was released June 1999, almost 22 years ago (as this comment is written).
     * If a client today is sending HTTP/1.0, chances are the client does not
     * cache.  Avoid the overhead of formatting time for Expires to send both
     * Cache-Control and Expires when the majority of clients are HTTP/1.1 or
     * HTTP/2 (or later). */
    buffer *vb;
    if (r->http_version > HTTP_VERSION_1_0) {
        vb = http_header_response_set_ptr(r, HTTP_HEADER_CACHE_CONTROL,
                                          CONST_STR_LEN("Cache-Control"));
        buffer_append_string_len(vb, CONST_STR_LEN("max-age="));
        buffer_append_int(vb, expires - cur_ts);
    }
    else { /* HTTP/1.0 */
        vb = http_header_response_set_ptr(r, HTTP_HEADER_EXPIRES,
                                          CONST_STR_LEN("Expires"));
        http_date_time_append(vb, expires);
    }

    return HANDLER_GO_ON;
}

REQUEST_FUNC(mod_expire_handler) {
	plugin_data *p = p_d;
	buffer *vb;
	const data_string *ds;

	/* Add caching headers only to http_status
	 * 200 OK or 204 No Content or 206 Partial Content */
	if (r->http_status != 200 && r->http_status != 204 && r->http_status != 206)
		return HANDLER_GO_ON;
	/* Add caching headers only to GET, HEAD, QUERY requests */
	if (!http_method_get_head_query(r->http_method)) return HANDLER_GO_ON;
	/* Add caching headers only if not already present */
	vb = http_header_response_get(r, HTTP_HEADER_CACHE_CONTROL, CONST_STR_LEN("Cache-Control"));
	if (NULL != vb) return HANDLER_GO_ON;

	mod_expire_patch_config(r, p);

	/* check expire.url */
	ds = p->conf.expire_url
	  ? (const data_string *)array_match_key_prefix(p->conf.expire_url, &r->uri.path)
	  : NULL;
	/* check expire.mimetypes (if no match with expire.url) */
	if (NULL == ds) {
		if (NULL == p->conf.expire_mimetypes) return HANDLER_GO_ON;
		vb = http_header_response_get(r, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"));
		if (NULL != vb)
			ds = (const data_string *)
			     array_match_key_prefix(p->conf.expire_mimetypes, vb);
		if (NULL == ds) {
			ds = (const data_string *)
			     array_get_element_klen(p->conf.expire_mimetypes,
			                            CONST_STR_LEN(""));
			if (NULL == ds) return HANDLER_GO_ON;
		}
	}

	return mod_expire_set_header(r, p->toffsets + ds->value.used);
}


__attribute_cold__
__declspec_dllexport__
int mod_expire_plugin_init(plugin *p);
int mod_expire_plugin_init(plugin *p) {
	p->version     = LIGHTTPD_VERSION_ID;
	p->name        = "expire";

	p->init        = mod_expire_init;
	p->cleanup     = mod_expire_free;
	p->set_defaults= mod_expire_set_defaults;
	p->handle_response_start = mod_expire_handler;

	return 0;
}