comparison src/http/modules/ngx_http_headers_filter_module.c @ 174:3080c5392b89 NGINX_0_3_34

nginx 0.3.34 *) Feature: the "add_header" directive supports the variables.
author Igor Sysoev <http://sysoev.ru>
date Tue, 21 Mar 2006 00:00:00 +0300
parents 36af50a5582d
children 56688ed172c8
comparison
equal deleted inserted replaced
173:298e7ea28d33 174:3080c5392b89
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 #include <ngx_http.h> 9 #include <ngx_http.h>
10 10
11 11
12 typedef struct { 12 typedef struct {
13 time_t expires; 13 ngx_table_elt_t value;
14 ngx_str_t cache_control; 14 ngx_array_t *lengths;
15 ngx_array_t *headers; 15 ngx_array_t *values;
16 } ngx_http_header_val_t;
17
18
19 typedef struct {
20 time_t expires;
21 ngx_str_t cache_control;
22 ngx_array_t *headers;
16 } ngx_http_headers_conf_t; 23 } ngx_http_headers_conf_t;
17 24
18 25
19 #define NGX_HTTP_EXPIRES_UNSET -2147483647 26 #define NGX_HTTP_EXPIRES_UNSET -2147483647
20 #define NGX_HTTP_EXPIRES_OFF -2147483646 27 #define NGX_HTTP_EXPIRES_OFF -2147483646
90 static ngx_int_t 97 static ngx_int_t
91 ngx_http_headers_filter(ngx_http_request_t *r) 98 ngx_http_headers_filter(ngx_http_request_t *r)
92 { 99 {
93 size_t len; 100 size_t len;
94 ngx_uint_t i; 101 ngx_uint_t i;
95 ngx_table_elt_t *expires, *cc, **ccp, *h, *out; 102 ngx_table_elt_t *expires, *cc, **ccp, *out;
103 ngx_http_header_val_t *h;
96 ngx_http_headers_conf_t *conf; 104 ngx_http_headers_conf_t *conf;
97 105
98 if ((r->headers_out.status != NGX_HTTP_OK 106 if ((r->headers_out.status != NGX_HTTP_OK
99 && r->headers_out.status != NGX_HTTP_NOT_MODIFIED) 107 && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)
100 || r != r->main) 108 || r != r->main)
240 out = ngx_list_push(&r->headers_out.headers); 248 out = ngx_list_push(&r->headers_out.headers);
241 if (out == NULL) { 249 if (out == NULL) {
242 return NGX_ERROR; 250 return NGX_ERROR;
243 } 251 }
244 252
245 *out = h[i]; 253 out->hash = h[i].value.hash;
254 out->key = h[i].value.key;
255
256 if (h[i].lengths == NULL) {
257 out->value = h[i].value.value;
258 continue;
259 }
260
261 if (ngx_http_script_run(r, &out->value, h[i].lengths->elts, 0,
262 h[i].values->elts)
263 == NULL)
264 {
265 return NGX_ERROR;
266 }
246 } 267 }
247 } 268 }
248 269
249 return ngx_http_next_header_filter(r); 270 return ngx_http_next_header_filter(r);
250 } 271 }
366 static char * 387 static char *
367 ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 388 ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
368 { 389 {
369 ngx_http_headers_conf_t *hcf = conf; 390 ngx_http_headers_conf_t *hcf = conf;
370 391
371 ngx_str_t *value; 392 ngx_int_t n;
372 ngx_table_elt_t *h; 393 ngx_str_t *value;
394 ngx_http_header_val_t *h;
395 ngx_http_script_compile_t sc;
373 396
374 value = cf->args->elts; 397 value = cf->args->elts;
375 398
376 if (ngx_strcasecmp(value[1].data, "cache-control") == 0) { 399 if (ngx_strcasecmp(value[1].data, "cache-control") == 0) {
377 hcf->cache_control = value[2]; 400 hcf->cache_control = value[2];
378 return NGX_CONF_OK; 401 return NGX_CONF_OK;
379 } 402 }
380 403
381 if (hcf->headers == NULL) { 404 if (hcf->headers == NULL) {
382 hcf->headers = ngx_array_create(cf->pool, 1, sizeof(ngx_table_elt_t)); 405 hcf->headers = ngx_array_create(cf->pool, 1,
406 sizeof(ngx_http_header_val_t));
383 if (hcf->headers == NULL) { 407 if (hcf->headers == NULL) {
384 return NGX_CONF_ERROR; 408 return NGX_CONF_ERROR;
385 } 409 }
386 } 410 }
387 411
388 h = ngx_array_push(hcf->headers); 412 h = ngx_array_push(hcf->headers);
389 if (h == NULL) { 413 if (h == NULL) {
390 return NGX_CONF_ERROR; 414 return NGX_CONF_ERROR;
391 } 415 }
392 416
393 h->hash = 1; 417 h->value.hash = 1;
394 h->key = value[1]; 418 h->value.key = value[1];
395 h->value = value[2]; 419 h->value.value = value[2];
420 h->lengths = NULL;
421 h->values = NULL;
422
423 n = ngx_http_script_variables_count(&value[2]);
424
425 if (n == 0) {
426 return NGX_CONF_OK;
427 }
428
429 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
430
431 sc.cf = cf;
432 sc.source = &value[2];
433 sc.lengths = &h->lengths;
434 sc.values = &h->values;
435 sc.variables = n;
436 sc.complete_lengths = 1;
437 sc.complete_values = 1;
438
439 if (ngx_http_script_compile(&sc) != NGX_OK) {
440 return NGX_CONF_ERROR;
441 }
396 442
397 return NGX_CONF_OK; 443 return NGX_CONF_OK;
398 } 444 }