3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 typedef struct ngx_http_header_val_s ngx_http_header_val_t;
14 typedef ngx_int_t (*ngx_http_set_header_pt)(ngx_http_request_t *r,
15 ngx_http_header_val_t *hv, ngx_str_t *value);
21 ngx_http_set_header_pt handler;
22 } ngx_http_set_header_t;
25 struct ngx_http_header_val_s {
26 ngx_table_elt_t value;
28 ngx_http_set_header_pt handler;
34 #define NGX_HTTP_EXPIRES_OFF 0
35 #define NGX_HTTP_EXPIRES_EPOCH 1
36 #define NGX_HTTP_EXPIRES_MAX 2
37 #define NGX_HTTP_EXPIRES_ACCESS 3
38 #define NGX_HTTP_EXPIRES_MODIFIED 4
39 #define NGX_HTTP_EXPIRES_DAILY 5
46 } ngx_http_headers_conf_t;
49 static ngx_int_t ngx_http_set_expires(ngx_http_request_t *r,
50 ngx_http_headers_conf_t *conf);
51 static ngx_int_t ngx_http_add_cache_control(ngx_http_request_t *r,
52 ngx_http_header_val_t *hv, ngx_str_t *value);
53 static ngx_int_t ngx_http_set_last_modified(ngx_http_request_t *r,
54 ngx_http_header_val_t *hv, ngx_str_t *value);
56 static void *ngx_http_headers_create_conf(ngx_conf_t *cf);
57 static char *ngx_http_headers_merge_conf(ngx_conf_t *cf,
58 void *parent, void *child);
59 static ngx_int_t ngx_http_headers_filter_init(ngx_conf_t *cf);
60 static char *ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd,
62 static char *ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd,
66 static ngx_http_set_header_t ngx_http_set_headers[] = {
68 { ngx_string("Cache-Control"), 0, ngx_http_add_cache_control },
70 { ngx_string("Last-Modified"),
71 offsetof(ngx_http_headers_out_t, last_modified),
72 ngx_http_set_last_modified },
74 { ngx_null_string, 0, NULL }
78 static ngx_command_t ngx_http_headers_filter_commands[] = {
80 { ngx_string("expires"),
81 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
83 ngx_http_headers_expires,
84 NGX_HTTP_LOC_CONF_OFFSET,
88 { ngx_string("add_header"),
89 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
92 NGX_HTTP_LOC_CONF_OFFSET,
100 static ngx_http_module_t ngx_http_headers_filter_module_ctx = {
101 NULL, /* preconfiguration */
102 ngx_http_headers_filter_init, /* postconfiguration */
104 NULL, /* create main configuration */
105 NULL, /* init main configuration */
107 NULL, /* create server configuration */
108 NULL, /* merge server configuration */
110 ngx_http_headers_create_conf, /* create location configuration */
111 ngx_http_headers_merge_conf /* merge location configuration */
115 ngx_module_t ngx_http_headers_filter_module = {
117 &ngx_http_headers_filter_module_ctx, /* module context */
118 ngx_http_headers_filter_commands, /* module directives */
119 NGX_HTTP_MODULE, /* module type */
120 NULL, /* init master */
121 NULL, /* init module */
122 NULL, /* init process */
123 NULL, /* init thread */
124 NULL, /* exit thread */
125 NULL, /* exit process */
126 NULL, /* exit master */
127 NGX_MODULE_V1_PADDING
131 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
135 ngx_http_headers_filter(ngx_http_request_t *r)
139 ngx_http_header_val_t *h;
140 ngx_http_headers_conf_t *conf;
142 conf = ngx_http_get_module_loc_conf(r, ngx_http_headers_filter_module);
144 if ((conf->expires == NGX_HTTP_EXPIRES_OFF && conf->headers == NULL)
146 || (r->headers_out.status != NGX_HTTP_OK
147 && r->headers_out.status != NGX_HTTP_NO_CONTENT
148 && r->headers_out.status != NGX_HTTP_MOVED_PERMANENTLY
149 && r->headers_out.status != NGX_HTTP_MOVED_TEMPORARILY
150 && r->headers_out.status != NGX_HTTP_NOT_MODIFIED))
152 return ngx_http_next_header_filter(r);
155 if (conf->expires != NGX_HTTP_EXPIRES_OFF) {
156 if (ngx_http_set_expires(r, conf) != NGX_OK) {
162 h = conf->headers->elts;
163 for (i = 0; i < conf->headers->nelts; i++) {
165 if (h[i].lengths == NULL) {
166 value = h[i].value.value;
169 if (ngx_http_script_run(r, &value, h[i].lengths->elts, 0,
177 if (h[i].handler(r, &h[i], &value) != NGX_OK) {
183 return ngx_http_next_header_filter(r);
188 ngx_http_set_expires(ngx_http_request_t *r, ngx_http_headers_conf_t *conf)
191 time_t now, expires_time, max_age;
193 ngx_table_elt_t *expires, *cc, **ccp;
195 expires = r->headers_out.expires;
197 if (expires == NULL) {
199 expires = ngx_list_push(&r->headers_out.headers);
200 if (expires == NULL) {
204 r->headers_out.expires = expires;
207 expires->key.len = sizeof("Expires") - 1;
208 expires->key.data = (u_char *) "Expires";
211 len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
212 expires->value.len = len - 1;
214 ccp = r->headers_out.cache_control.elts;
218 if (ngx_array_init(&r->headers_out.cache_control, r->pool,
219 1, sizeof(ngx_table_elt_t *))
225 ccp = ngx_array_push(&r->headers_out.cache_control);
230 cc = ngx_list_push(&r->headers_out.headers);
236 cc->key.len = sizeof("Cache-Control") - 1;
237 cc->key.data = (u_char *) "Cache-Control";
242 for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
249 if (conf->expires == NGX_HTTP_EXPIRES_EPOCH) {
250 expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
252 cc->value.len = sizeof("no-cache") - 1;
253 cc->value.data = (u_char *) "no-cache";
258 if (conf->expires == NGX_HTTP_EXPIRES_MAX) {
259 expires->value.data = (u_char *) "Thu, 31 Dec 2037 23:55:55 GMT";
262 cc->value.len = sizeof("max-age=315360000") - 1;
263 cc->value.data = (u_char *) "max-age=315360000";
268 expires->value.data = ngx_pnalloc(r->pool, len);
269 if (expires->value.data == NULL) {
273 if (conf->expires_time == 0) {
274 ngx_memcpy(expires->value.data, ngx_cached_http_time.data,
275 ngx_cached_http_time.len + 1);
277 cc->value.len = sizeof("max-age=0") - 1;
278 cc->value.data = (u_char *) "max-age=0";
285 if (conf->expires == NGX_HTTP_EXPIRES_ACCESS
286 || r->headers_out.last_modified_time == -1)
288 expires_time = now + conf->expires_time;
289 max_age = conf->expires_time;
291 } else if (conf->expires == NGX_HTTP_EXPIRES_DAILY) {
292 expires_time = ngx_next_time(conf->expires_time);
293 max_age = expires_time - now;
296 expires_time = r->headers_out.last_modified_time + conf->expires_time;
297 max_age = expires_time - now;
300 ngx_http_time(expires->value.data, expires_time);
302 if (conf->expires_time < 0) {
303 cc->value.len = sizeof("no-cache") - 1;
304 cc->value.data = (u_char *) "no-cache";
309 cc->value.data = ngx_pnalloc(r->pool,
310 sizeof("max-age=") + NGX_TIME_T_LEN + 1);
311 if (cc->value.data == NULL) {
315 cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T", max_age)
323 ngx_http_add_header(ngx_http_request_t *r, ngx_http_header_val_t *hv,
329 h = ngx_list_push(&r->headers_out.headers);
334 h->hash = hv->value.hash;
335 h->key = hv->value.key;
344 ngx_http_add_cache_control(ngx_http_request_t *r, ngx_http_header_val_t *hv,
347 ngx_table_elt_t *cc, **ccp;
349 ccp = r->headers_out.cache_control.elts;
353 if (ngx_array_init(&r->headers_out.cache_control, r->pool,
354 1, sizeof(ngx_table_elt_t *))
361 ccp = ngx_array_push(&r->headers_out.cache_control);
366 cc = ngx_list_push(&r->headers_out.headers);
372 cc->key.len = sizeof("Cache-Control") - 1;
373 cc->key.data = (u_char *) "Cache-Control";
383 ngx_http_set_last_modified(ngx_http_request_t *r, ngx_http_header_val_t *hv,
386 ngx_table_elt_t *h, **old;
389 old = (ngx_table_elt_t **) ((char *) &r->headers_out + hv->offset);
395 r->headers_out.last_modified_time = -1;
397 if (old == NULL || *old == NULL) {
399 if (value->len == 0) {
403 h = ngx_list_push(&r->headers_out.headers);
411 if (value->len == 0) {
417 h->hash = hv->value.hash;
418 h->key = hv->value.key;
426 ngx_http_headers_create_conf(ngx_conf_t *cf)
428 ngx_http_headers_conf_t *conf;
430 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_headers_conf_t));
432 return NGX_CONF_ERROR;
436 * set by ngx_pcalloc():
438 * conf->headers = NULL;
439 * conf->expires_time = 0;
442 conf->expires = NGX_CONF_UNSET_UINT;
449 ngx_http_headers_merge_conf(ngx_conf_t *cf, void *parent, void *child)
451 ngx_http_headers_conf_t *prev = parent;
452 ngx_http_headers_conf_t *conf = child;
454 if (conf->expires == NGX_CONF_UNSET_UINT) {
455 conf->expires = prev->expires;
456 conf->expires_time = prev->expires_time;
458 if (conf->expires == NGX_CONF_UNSET_UINT) {
459 conf->expires = NGX_HTTP_EXPIRES_OFF;
463 if (conf->headers == NULL) {
464 conf->headers = prev->headers;
472 ngx_http_headers_filter_init(ngx_conf_t *cf)
474 ngx_http_next_header_filter = ngx_http_top_header_filter;
475 ngx_http_top_header_filter = ngx_http_headers_filter;
482 ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
484 ngx_http_headers_conf_t *hcf = conf;
489 if (hcf->expires != NGX_CONF_UNSET_UINT) {
490 return "is duplicate";
493 value = cf->args->elts;
495 if (cf->args->nelts == 2) {
497 if (ngx_strcmp(value[1].data, "epoch") == 0) {
498 hcf->expires = NGX_HTTP_EXPIRES_EPOCH;
502 if (ngx_strcmp(value[1].data, "max") == 0) {
503 hcf->expires = NGX_HTTP_EXPIRES_MAX;
507 if (ngx_strcmp(value[1].data, "off") == 0) {
508 hcf->expires = NGX_HTTP_EXPIRES_OFF;
512 hcf->expires = NGX_HTTP_EXPIRES_ACCESS;
516 } else { /* cf->args->nelts == 3 */
518 if (ngx_strcmp(value[1].data, "modified") != 0) {
519 return "invalid value";
522 hcf->expires = NGX_HTTP_EXPIRES_MODIFIED;
527 if (value[n].data[0] == '@') {
532 if (hcf->expires == NGX_HTTP_EXPIRES_MODIFIED) {
533 return "daily time can not be used with \"modified\" parameter";
536 hcf->expires = NGX_HTTP_EXPIRES_DAILY;
538 } else if (value[n].data[0] == '+') {
543 } else if (value[n].data[0] == '-') {
552 hcf->expires_time = ngx_parse_time(&value[n], 1);
554 if (hcf->expires_time == NGX_ERROR) {
555 return "invalid value";
558 if (hcf->expires == NGX_HTTP_EXPIRES_DAILY
559 && hcf->expires_time > 24 * 60 * 60)
561 return "daily time value must be less than 24 hours";
564 if (hcf->expires_time == NGX_PARSE_LARGE_TIME) {
565 return "value must be less than 68 years";
569 hcf->expires_time = - hcf->expires_time;
577 ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
579 ngx_http_headers_conf_t *hcf = conf;
584 ngx_http_header_val_t *h;
585 ngx_http_set_header_t *sh;
586 ngx_http_script_compile_t sc;
588 value = cf->args->elts;
590 if (hcf->headers == NULL) {
591 hcf->headers = ngx_array_create(cf->pool, 1,
592 sizeof(ngx_http_header_val_t));
593 if (hcf->headers == NULL) {
594 return NGX_CONF_ERROR;
598 h = ngx_array_push(hcf->headers);
600 return NGX_CONF_ERROR;
604 h->value.key = value[1];
605 h->value.value = value[2];
607 h->handler = ngx_http_add_header;
611 sh = ngx_http_set_headers;
612 for (i = 0; sh[i].name.len; i++) {
613 if (ngx_strcasecmp(value[1].data, sh[i].name.data) != 0) {
617 h->offset = sh[i].offset;
618 h->handler = sh[i].handler;
622 n = ngx_http_script_variables_count(&value[2]);
628 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
631 sc.source = &value[2];
632 sc.lengths = &h->lengths;
633 sc.values = &h->values;
635 sc.complete_lengths = 1;
636 sc.complete_values = 1;
638 if (ngx_http_script_compile(&sc) != NGX_OK) {
639 return NGX_CONF_ERROR;