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