[mod_openssl] remove erroneous SSL_set_shutdown()
[lighttpd.git] / src / mod_expire.c
blobe40780abf7a1512af56bf910cb12efc3809738eb
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
6 #include "response.h"
8 #include "plugin.h"
9 #include "stat_cache.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
15 /**
16 * this is a expire module for a lighttpd
18 * set 'Expires:' HTTP Headers on demand
23 /* plugin config for all request/connections */
25 typedef struct {
26 array *expire_url;
27 array *expire_mimetypes;
28 } plugin_config;
30 typedef struct {
31 PLUGIN_DATA;
33 buffer *expire_tstmp;
35 plugin_config **config_storage;
37 plugin_config conf;
38 } plugin_data;
40 /* init the plugin data */
41 INIT_FUNC(mod_expire_init) {
42 plugin_data *p;
44 p = calloc(1, sizeof(*p));
46 p->expire_tstmp = buffer_init();
48 buffer_string_prepare_copy(p->expire_tstmp, 255);
50 return p;
53 /* detroy the plugin data */
54 FREE_FUNC(mod_expire_free) {
55 plugin_data *p = p_d;
57 UNUSED(srv);
59 if (!p) return HANDLER_GO_ON;
61 buffer_free(p->expire_tstmp);
63 if (p->config_storage) {
64 size_t i;
65 for (i = 0; i < srv->config_context->used; i++) {
66 plugin_config *s = p->config_storage[i];
68 if (NULL == s) continue;
70 array_free(s->expire_url);
71 array_free(s->expire_mimetypes);
72 free(s);
74 free(p->config_storage);
77 free(p);
79 return HANDLER_GO_ON;
82 static int mod_expire_get_offset(server *srv, plugin_data *p, buffer *expire, time_t *offset) {
83 char *ts;
84 int type = -1;
85 time_t retts = 0;
87 UNUSED(p);
90 * parse
92 * '(access|now|modification) [plus] {<num> <type>}*'
94 * e.g. 'access 1 years'
97 if (buffer_string_is_empty(expire)) {
98 log_error_write(srv, __FILE__, __LINE__, "s",
99 "empty:");
100 return -1;
103 ts = expire->ptr;
105 if (0 == strncmp(ts, "access ", 7)) {
106 type = 0;
107 ts += 7;
108 } else if (0 == strncmp(ts, "now ", 4)) {
109 type = 0;
110 ts += 4;
111 } else if (0 == strncmp(ts, "modification ", 13)) {
112 type = 1;
113 ts += 13;
114 } else {
115 /* invalid type-prefix */
116 log_error_write(srv, __FILE__, __LINE__, "ss",
117 "invalid <base>:", ts);
118 return -1;
121 if (0 == strncmp(ts, "plus ", 5)) {
122 /* skip the optional plus */
123 ts += 5;
126 /* the rest is just <number> (years|months|weeks|days|hours|minutes|seconds) */
127 while (1) {
128 char *space, *err;
129 int num;
131 if (NULL == (space = strchr(ts, ' '))) {
132 log_error_write(srv, __FILE__, __LINE__, "ss",
133 "missing space after <num>:", ts);
134 return -1;
137 num = strtol(ts, &err, 10);
138 if (*err != ' ') {
139 log_error_write(srv, __FILE__, __LINE__, "ss",
140 "missing <type> after <num>:", ts);
141 return -1;
144 ts = space + 1;
146 if (NULL != (space = strchr(ts, ' '))) {
147 int slen;
148 /* */
150 slen = space - ts;
152 if (slen == 5 &&
153 0 == strncmp(ts, "years", slen)) {
154 num *= 60 * 60 * 24 * 30 * 12;
155 } else if (slen == 6 &&
156 0 == strncmp(ts, "months", slen)) {
157 num *= 60 * 60 * 24 * 30;
158 } else if (slen == 5 &&
159 0 == strncmp(ts, "weeks", slen)) {
160 num *= 60 * 60 * 24 * 7;
161 } else if (slen == 4 &&
162 0 == strncmp(ts, "days", slen)) {
163 num *= 60 * 60 * 24;
164 } else if (slen == 5 &&
165 0 == strncmp(ts, "hours", slen)) {
166 num *= 60 * 60;
167 } else if (slen == 7 &&
168 0 == strncmp(ts, "minutes", slen)) {
169 num *= 60;
170 } else if (slen == 7 &&
171 0 == strncmp(ts, "seconds", slen)) {
172 num *= 1;
173 } else {
174 log_error_write(srv, __FILE__, __LINE__, "ss",
175 "unknown type:", ts);
176 return -1;
179 retts += num;
181 ts = space + 1;
182 } else {
183 if (0 == strcmp(ts, "years")) {
184 num *= 60 * 60 * 24 * 30 * 12;
185 } else if (0 == strcmp(ts, "months")) {
186 num *= 60 * 60 * 24 * 30;
187 } else if (0 == strcmp(ts, "weeks")) {
188 num *= 60 * 60 * 24 * 7;
189 } else if (0 == strcmp(ts, "days")) {
190 num *= 60 * 60 * 24;
191 } else if (0 == strcmp(ts, "hours")) {
192 num *= 60 * 60;
193 } else if (0 == strcmp(ts, "minutes")) {
194 num *= 60;
195 } else if (0 == strcmp(ts, "seconds")) {
196 num *= 1;
197 } else {
198 log_error_write(srv, __FILE__, __LINE__, "ss",
199 "unknown type:", ts);
200 return -1;
203 retts += num;
205 break;
209 if (offset != NULL) *offset = retts;
211 return type;
215 /* handle plugin config and check values */
217 SETDEFAULTS_FUNC(mod_expire_set_defaults) {
218 plugin_data *p = p_d;
219 size_t i = 0, k;
221 config_values_t cv[] = {
222 { "expire.url", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
223 { "expire.mimetypes", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
224 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
227 if (!p) return HANDLER_ERROR;
229 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
231 for (i = 0; i < srv->config_context->used; i++) {
232 data_config const* config = (data_config const*)srv->config_context->data[i];
233 plugin_config *s;
235 s = calloc(1, sizeof(plugin_config));
236 s->expire_url = array_init();
237 s->expire_mimetypes = array_init();
239 cv[0].destination = s->expire_url;
240 cv[1].destination = s->expire_mimetypes;
242 p->config_storage[i] = s;
244 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
245 return HANDLER_ERROR;
248 if (!array_is_kvstring(s->expire_url)) {
249 log_error_write(srv, __FILE__, __LINE__, "s",
250 "unexpected value for expire.url; expected list of \"urlpath\" => \"expiration\"");
251 return HANDLER_ERROR;
254 for (k = 0; k < s->expire_url->used; k++) {
255 data_string *ds = (data_string *)s->expire_url->data[k];
257 /* parse lines */
258 if (-1 == mod_expire_get_offset(srv, p, ds->value, NULL)) {
259 log_error_write(srv, __FILE__, __LINE__, "sb",
260 "parsing expire.url failed:", ds->value);
261 return HANDLER_ERROR;
265 if (!array_is_kvstring(s->expire_mimetypes)) {
266 log_error_write(srv, __FILE__, __LINE__, "s",
267 "unexpected value for expire.mimetypes; expected list of \"mimetype\" => \"expiration\"");
268 return HANDLER_ERROR;
271 for (k = 0; k < s->expire_mimetypes->used; k++) {
272 data_string *ds = (data_string *)s->expire_mimetypes->data[k];
274 /* parse lines */
275 if (-1 == mod_expire_get_offset(srv, p, ds->value, NULL)) {
276 log_error_write(srv, __FILE__, __LINE__, "sb",
277 "parsing expire.mimetypes failed:", ds->value);
278 return HANDLER_ERROR;
284 return HANDLER_GO_ON;
287 #define PATCH(x) \
288 p->conf.x = s->x;
289 static int mod_expire_patch_connection(server *srv, connection *con, plugin_data *p) {
290 size_t i, j;
291 plugin_config *s = p->config_storage[0];
293 PATCH(expire_url);
294 PATCH(expire_mimetypes);
296 /* skip the first, the global context */
297 for (i = 1; i < srv->config_context->used; i++) {
298 data_config *dc = (data_config *)srv->config_context->data[i];
299 s = p->config_storage[i];
301 /* condition didn't match */
302 if (!config_check_cond(srv, con, dc)) continue;
304 /* merge config */
305 for (j = 0; j < dc->value->used; j++) {
306 data_unset *du = dc->value->data[j];
308 if (buffer_is_equal_string(du->key, CONST_STR_LEN("expire.url"))) {
309 PATCH(expire_url);
310 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("expire.mimetypes"))) {
311 PATCH(expire_mimetypes);
316 return 0;
318 #undef PATCH
320 CONNECTION_FUNC(mod_expire_handler) {
321 plugin_data *p = p_d;
322 data_string *ds = NULL;
323 size_t s_len;
324 size_t k;
326 /* Add caching headers only to http_status 200 OK or 206 Partial Content */
327 if (con->http_status != 200 && con->http_status != 206) return HANDLER_GO_ON;
328 /* Add caching headers only to GET or HEAD requests */
329 if ( con->request.http_method != HTTP_METHOD_GET
330 && con->request.http_method != HTTP_METHOD_HEAD) return HANDLER_GO_ON;
331 /* Add caching headers only if not already present */
332 ds = (data_string *)array_get_element(con->response.headers, "Cache-Control");
333 if (NULL != ds && !buffer_string_is_empty(ds->value)) return HANDLER_GO_ON;
335 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
337 mod_expire_patch_connection(srv, con, p);
339 s_len = buffer_string_length(con->uri.path);
341 /* check expire.url */
342 for (k = 0; k < p->conf.expire_url->used; k++) {
343 size_t ct_len;
344 ds = (data_string *)p->conf.expire_url->data[k];
345 ct_len = buffer_string_length(ds->key);
347 if (ct_len > s_len) continue;
348 if (buffer_is_empty(ds->key)) continue;
350 if (0 == strncmp(con->uri.path->ptr, ds->key->ptr, ct_len)) {
351 break;
354 /* check expire.mimetypes (if no match with expire.url) */
355 if (k == p->conf.expire_url->used) {
356 const char *mimetype;
357 ds = (data_string *)array_get_element(con->response.headers, "Content-Type");
358 if (NULL != ds && !buffer_string_is_empty(ds->value)) {
359 mimetype = ds->value->ptr;
360 s_len = buffer_string_length(ds->value);
361 } else {
362 mimetype = "";
363 s_len = 0;
365 for (k = 0; k < p->conf.expire_mimetypes->used; k++) {
366 size_t ct_len;
367 ds = (data_string *)p->conf.expire_mimetypes->data[k];
368 ct_len = buffer_string_length(ds->key);
370 if (ct_len > s_len) continue;
371 if (buffer_is_empty(ds->key)) continue;
373 /*(omit trailing '*', if present, from prefix match)*/
374 if (ds->key->ptr[ct_len-1] == '*') --ct_len;
376 if (0 == strncmp(mimetype, ds->key->ptr, ct_len)) {
377 break;
380 if (k == p->conf.expire_mimetypes->used) {
381 ds = NULL;
385 if (NULL != ds) {
386 time_t ts, expires;
387 stat_cache_entry *sce = NULL;
389 /* if stat fails => sce == NULL, ignore return value */
390 (void) stat_cache_get_entry(srv, con, con->physical.path, &sce);
392 switch(mod_expire_get_offset(srv, p, ds->value, &ts)) {
393 case 0:
394 /* access */
395 expires = (ts + srv->cur_ts);
396 break;
397 case 1:
398 /* modification */
400 /* can't set modification based expire header if
401 * mtime is not available
403 if (NULL == sce) return HANDLER_GO_ON;
405 expires = (ts + sce->st.st_mtime);
406 break;
407 default:
408 /* -1 is handled at parse-time */
409 return HANDLER_ERROR;
412 /* expires should be at least srv->cur_ts */
413 if (expires < srv->cur_ts) expires = srv->cur_ts;
415 buffer_string_prepare_copy(p->expire_tstmp, 255);
416 buffer_append_strftime(p->expire_tstmp, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(expires)));
418 /* HTTP/1.0 */
419 response_header_overwrite(srv, con, CONST_STR_LEN("Expires"), CONST_BUF_LEN(p->expire_tstmp));
421 /* HTTP/1.1 */
422 buffer_copy_string_len(p->expire_tstmp, CONST_STR_LEN("max-age="));
423 buffer_append_int(p->expire_tstmp, expires - srv->cur_ts); /* as expires >= srv->cur_ts the difference is >= 0 */
425 response_header_append(srv, con, CONST_STR_LEN("Cache-Control"), CONST_BUF_LEN(p->expire_tstmp));
427 return HANDLER_GO_ON;
430 /* not found */
431 return HANDLER_GO_ON;
434 /* this function is called at dlopen() time and inits the callbacks */
436 int mod_expire_plugin_init(plugin *p);
437 int mod_expire_plugin_init(plugin *p) {
438 p->version = LIGHTTPD_VERSION_ID;
439 p->name = buffer_init_string("expire");
441 p->init = mod_expire_init;
442 p->handle_response_start = mod_expire_handler;
443 p->set_defaults = mod_expire_set_defaults;
444 p->cleanup = mod_expire_free;
446 p->data = NULL;
448 return 0;