Mercurial > hg > nginx
comparison src/http/modules/ngx_http_headers_filter_module.c @ 1365:d1dc7ef7c3ce
move "Cache-Control" header code to specific handler
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 01 Aug 2007 15:24:06 +0000 |
parents | b7a8c9fe2337 |
children | 279210f24408 |
comparison
equal
deleted
inserted
replaced
1364:b7a8c9fe2337 | 1365:d1dc7ef7c3ce |
---|---|
31 }; | 31 }; |
32 | 32 |
33 | 33 |
34 typedef struct { | 34 typedef struct { |
35 time_t expires; | 35 time_t expires; |
36 ngx_str_t cache_control; | |
37 ngx_array_t *headers; | 36 ngx_array_t *headers; |
38 } ngx_http_headers_conf_t; | 37 } ngx_http_headers_conf_t; |
39 | 38 |
40 | 39 |
41 #define NGX_HTTP_EXPIRES_UNSET -2147483647 | 40 #define NGX_HTTP_EXPIRES_UNSET -2147483647 |
42 #define NGX_HTTP_EXPIRES_OFF -2147483646 | 41 #define NGX_HTTP_EXPIRES_OFF -2147483646 |
43 #define NGX_HTTP_EXPIRES_EPOCH -2147483645 | 42 #define NGX_HTTP_EXPIRES_EPOCH -2147483645 |
44 #define NGX_HTTP_EXPIRES_MAX -2147483644 | 43 #define NGX_HTTP_EXPIRES_MAX -2147483644 |
45 | 44 |
46 | 45 |
47 static ngx_int_t ngx_http_add_header(ngx_http_request_t *r, | 46 static ngx_int_t ngx_http_add_cache_control(ngx_http_request_t *r, |
48 ngx_http_header_val_t *hv, ngx_str_t *value); | 47 ngx_http_header_val_t *hv, ngx_str_t *value); |
49 static ngx_int_t ngx_http_set_last_modified(ngx_http_request_t *r, | 48 static ngx_int_t ngx_http_set_last_modified(ngx_http_request_t *r, |
50 ngx_http_header_val_t *hv, ngx_str_t *value); | 49 ngx_http_header_val_t *hv, ngx_str_t *value); |
51 | 50 |
52 static void *ngx_http_headers_create_conf(ngx_conf_t *cf); | 51 static void *ngx_http_headers_create_conf(ngx_conf_t *cf); |
58 static char *ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, | 57 static char *ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, |
59 void *conf); | 58 void *conf); |
60 | 59 |
61 | 60 |
62 static ngx_http_set_header_t ngx_http_set_headers[] = { | 61 static ngx_http_set_header_t ngx_http_set_headers[] = { |
62 | |
63 { ngx_string("Cache-Control"), 0, ngx_http_add_cache_control }, | |
63 | 64 |
64 { ngx_string("Last-Modified"), | 65 { ngx_string("Last-Modified"), |
65 offsetof(ngx_http_headers_out_t, last_modified), | 66 offsetof(ngx_http_headers_out_t, last_modified), |
66 ngx_http_set_last_modified }, | 67 ngx_http_set_last_modified }, |
67 | 68 |
249 } | 250 } |
250 } | 251 } |
251 } | 252 } |
252 } | 253 } |
253 | 254 |
254 if (conf->cache_control.len) { | |
255 | |
256 ccp = r->headers_out.cache_control.elts; | |
257 | |
258 if (ccp == NULL) { | |
259 | |
260 if (ngx_array_init(&r->headers_out.cache_control, r->pool, | |
261 1, sizeof(ngx_table_elt_t *)) | |
262 != NGX_OK) | |
263 { | |
264 return NGX_ERROR; | |
265 } | |
266 } | |
267 | |
268 ccp = ngx_array_push(&r->headers_out.cache_control); | |
269 if (ccp == NULL) { | |
270 return NGX_ERROR; | |
271 } | |
272 | |
273 cc = ngx_list_push(&r->headers_out.headers); | |
274 if (cc == NULL) { | |
275 return NGX_ERROR; | |
276 } | |
277 | |
278 cc->hash = 1; | |
279 cc->key.len = sizeof("Cache-Control") - 1; | |
280 cc->key.data = (u_char *) "Cache-Control"; | |
281 cc->value = conf->cache_control; | |
282 | |
283 *ccp = cc; | |
284 } | |
285 | |
286 if (conf->headers) { | 255 if (conf->headers) { |
287 h = conf->headers->elts; | 256 h = conf->headers->elts; |
288 for (i = 0; i < conf->headers->nelts; i++) { | 257 for (i = 0; i < conf->headers->nelts; i++) { |
289 | 258 |
290 if (h[i].lengths == NULL) { | 259 if (h[i].lengths == NULL) { |
327 return NGX_OK; | 296 return NGX_OK; |
328 } | 297 } |
329 | 298 |
330 | 299 |
331 static ngx_int_t | 300 static ngx_int_t |
301 ngx_http_add_cache_control(ngx_http_request_t *r, ngx_http_header_val_t *hv, | |
302 ngx_str_t *value) | |
303 { | |
304 ngx_table_elt_t *cc, **ccp; | |
305 | |
306 ccp = r->headers_out.cache_control.elts; | |
307 | |
308 if (ccp == NULL) { | |
309 | |
310 if (ngx_array_init(&r->headers_out.cache_control, r->pool, | |
311 1, sizeof(ngx_table_elt_t *)) | |
312 != NGX_OK) | |
313 { | |
314 return NGX_ERROR; | |
315 } | |
316 } | |
317 | |
318 ccp = ngx_array_push(&r->headers_out.cache_control); | |
319 if (ccp == NULL) { | |
320 return NGX_ERROR; | |
321 } | |
322 | |
323 cc = ngx_list_push(&r->headers_out.headers); | |
324 if (cc == NULL) { | |
325 return NGX_ERROR; | |
326 } | |
327 | |
328 cc->hash = 1; | |
329 cc->key.len = sizeof("Cache-Control") - 1; | |
330 cc->key.data = (u_char *) "Cache-Control"; | |
331 cc->value = *value; | |
332 | |
333 *ccp = cc; | |
334 | |
335 return NGX_OK; | |
336 } | |
337 | |
338 | |
339 static ngx_int_t | |
332 ngx_http_set_last_modified(ngx_http_request_t *r, ngx_http_header_val_t *hv, | 340 ngx_http_set_last_modified(ngx_http_request_t *r, ngx_http_header_val_t *hv, |
333 ngx_str_t *value) | 341 ngx_str_t *value) |
334 { | 342 { |
335 ngx_table_elt_t *h, **old; | 343 ngx_table_elt_t *h, **old; |
336 | 344 |
394 if (conf->expires == NGX_HTTP_EXPIRES_UNSET) { | 402 if (conf->expires == NGX_HTTP_EXPIRES_UNSET) { |
395 conf->expires = (prev->expires == NGX_HTTP_EXPIRES_UNSET) ? | 403 conf->expires = (prev->expires == NGX_HTTP_EXPIRES_UNSET) ? |
396 NGX_HTTP_EXPIRES_OFF : prev->expires; | 404 NGX_HTTP_EXPIRES_OFF : prev->expires; |
397 } | 405 } |
398 | 406 |
399 if (conf->cache_control.data == NULL) { | |
400 conf->cache_control = prev->cache_control; | |
401 } | |
402 | |
403 if (conf->headers == NULL) { | 407 if (conf->headers == NULL) { |
404 conf->headers = prev->headers; | 408 conf->headers = prev->headers; |
405 } | 409 } |
406 | 410 |
407 return NGX_CONF_OK; | 411 return NGX_CONF_OK; |
490 ngx_http_header_val_t *h; | 494 ngx_http_header_val_t *h; |
491 ngx_http_set_header_t *sh; | 495 ngx_http_set_header_t *sh; |
492 ngx_http_script_compile_t sc; | 496 ngx_http_script_compile_t sc; |
493 | 497 |
494 value = cf->args->elts; | 498 value = cf->args->elts; |
495 | |
496 if (ngx_strcasecmp(value[1].data, (u_char *) "cache-control") == 0) { | |
497 hcf->cache_control = value[2]; | |
498 return NGX_CONF_OK; | |
499 } | |
500 | 499 |
501 if (hcf->headers == NULL) { | 500 if (hcf->headers == NULL) { |
502 hcf->headers = ngx_array_create(cf->pool, 1, | 501 hcf->headers = ngx_array_create(cf->pool, 1, |
503 sizeof(ngx_http_header_val_t)); | 502 sizeof(ngx_http_header_val_t)); |
504 if (hcf->headers == NULL) { | 503 if (hcf->headers == NULL) { |