50
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
50
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
326
|
13 typedef struct ngx_http_header_val_s ngx_http_header_val_t;
|
|
14
|
|
15 typedef ngx_int_t (*ngx_http_set_header_pt)(ngx_http_request_t *r,
|
|
16 ngx_http_header_val_t *hv, ngx_str_t *value);
|
174
|
17
|
|
18
|
|
19 typedef struct {
|
464
|
20 ngx_str_t name;
|
|
21 ngx_uint_t offset;
|
|
22 ngx_http_set_header_pt handler;
|
326
|
23 } ngx_http_set_header_t;
|
|
24
|
|
25
|
|
26 struct ngx_http_header_val_s {
|
464
|
27 ngx_http_complex_value_t value;
|
|
28 ngx_str_t key;
|
|
29 ngx_http_set_header_pt handler;
|
|
30 ngx_uint_t offset;
|
326
|
31 };
|
|
32
|
|
33
|
668
|
34 typedef enum {
|
|
35 NGX_HTTP_EXPIRES_OFF,
|
|
36 NGX_HTTP_EXPIRES_EPOCH,
|
|
37 NGX_HTTP_EXPIRES_MAX,
|
|
38 NGX_HTTP_EXPIRES_ACCESS,
|
|
39 NGX_HTTP_EXPIRES_MODIFIED,
|
|
40 NGX_HTTP_EXPIRES_DAILY,
|
|
41 NGX_HTTP_EXPIRES_UNSET
|
|
42 } ngx_http_expires_t;
|
376
|
43
|
|
44
|
326
|
45 typedef struct {
|
668
|
46 ngx_http_expires_t expires;
|
376
|
47 time_t expires_time;
|
326
|
48 ngx_array_t *headers;
|
50
|
49 } ngx_http_headers_conf_t;
|
|
50
|
|
51
|
326
|
52 static ngx_int_t ngx_http_set_expires(ngx_http_request_t *r,
|
|
53 ngx_http_headers_conf_t *conf);
|
|
54 static ngx_int_t ngx_http_add_cache_control(ngx_http_request_t *r,
|
|
55 ngx_http_header_val_t *hv, ngx_str_t *value);
|
668
|
56 static ngx_int_t ngx_http_add_header(ngx_http_request_t *r,
|
|
57 ngx_http_header_val_t *hv, ngx_str_t *value);
|
326
|
58 static ngx_int_t ngx_http_set_last_modified(ngx_http_request_t *r,
|
|
59 ngx_http_header_val_t *hv, ngx_str_t *value);
|
680
|
60 static ngx_int_t ngx_http_set_response_header(ngx_http_request_t *r,
|
|
61 ngx_http_header_val_t *hv, ngx_str_t *value);
|
326
|
62
|
50
|
63 static void *ngx_http_headers_create_conf(ngx_conf_t *cf);
|
|
64 static char *ngx_http_headers_merge_conf(ngx_conf_t *cf,
|
122
|
65 void *parent, void *child);
|
230
|
66 static ngx_int_t ngx_http_headers_filter_init(ngx_conf_t *cf);
|
50
|
67 static char *ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
68 void *conf);
|
122
|
69 static char *ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
70 void *conf);
|
50
|
71
|
|
72
|
326
|
73 static ngx_http_set_header_t ngx_http_set_headers[] = {
|
|
74
|
|
75 { ngx_string("Cache-Control"), 0, ngx_http_add_cache_control },
|
|
76
|
694
|
77 { ngx_string("Last-Modified"),
|
|
78 offsetof(ngx_http_headers_out_t, last_modified),
|
|
79 ngx_http_set_last_modified },
|
680
|
80
|
|
81 { ngx_string("ETag"),
|
|
82 offsetof(ngx_http_headers_out_t, etag),
|
|
83 ngx_http_set_response_header },
|
326
|
84
|
|
85 { ngx_null_string, 0, NULL }
|
|
86 };
|
|
87
|
|
88
|
50
|
89 static ngx_command_t ngx_http_headers_filter_commands[] = {
|
|
90
|
|
91 { ngx_string("expires"),
|
122
|
92 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
376
|
93 |NGX_CONF_TAKE12,
|
50
|
94 ngx_http_headers_expires,
|
|
95 NGX_HTTP_LOC_CONF_OFFSET,
|
|
96 0,
|
|
97 NULL},
|
|
98
|
122
|
99 { ngx_string("add_header"),
|
|
100 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
101 |NGX_CONF_TAKE2,
|
|
102 ngx_http_headers_add,
|
|
103 NGX_HTTP_LOC_CONF_OFFSET,
|
|
104 0,
|
|
105 NULL},
|
|
106
|
50
|
107 ngx_null_command
|
|
108 };
|
|
109
|
|
110
|
|
111 static ngx_http_module_t ngx_http_headers_filter_module_ctx = {
|
58
|
112 NULL, /* preconfiguration */
|
230
|
113 ngx_http_headers_filter_init, /* postconfiguration */
|
50
|
114
|
|
115 NULL, /* create main configuration */
|
|
116 NULL, /* init main configuration */
|
|
117
|
|
118 NULL, /* create server configuration */
|
|
119 NULL, /* merge server configuration */
|
|
120
|
|
121 ngx_http_headers_create_conf, /* create location configuration */
|
|
122 ngx_http_headers_merge_conf /* merge location configuration */
|
|
123 };
|
|
124
|
|
125
|
|
126 ngx_module_t ngx_http_headers_filter_module = {
|
58
|
127 NGX_MODULE_V1,
|
50
|
128 &ngx_http_headers_filter_module_ctx, /* module context */
|
|
129 ngx_http_headers_filter_commands, /* module directives */
|
|
130 NGX_HTTP_MODULE, /* module type */
|
90
|
131 NULL, /* init master */
|
230
|
132 NULL, /* init module */
|
90
|
133 NULL, /* init process */
|
|
134 NULL, /* init thread */
|
|
135 NULL, /* exit thread */
|
|
136 NULL, /* exit process */
|
|
137 NULL, /* exit master */
|
|
138 NGX_MODULE_V1_PADDING
|
50
|
139 };
|
|
140
|
|
141
|
|
142 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
143
|
|
144
|
|
145 static ngx_int_t
|
|
146 ngx_http_headers_filter(ngx_http_request_t *r)
|
|
147 {
|
326
|
148 ngx_str_t value;
|
58
|
149 ngx_uint_t i;
|
174
|
150 ngx_http_header_val_t *h;
|
50
|
151 ngx_http_headers_conf_t *conf;
|
|
152
|
326
|
153 conf = ngx_http_get_module_loc_conf(r, ngx_http_headers_filter_module);
|
|
154
|
|
155 if ((conf->expires == NGX_HTTP_EXPIRES_OFF && conf->headers == NULL)
|
|
156 || r != r->main
|
212
|
157 || (r->headers_out.status != NGX_HTTP_OK
|
694
|
158 && r->headers_out.status != NGX_HTTP_CREATED
|
212
|
159 && r->headers_out.status != NGX_HTTP_NO_CONTENT
|
650
|
160 && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
|
212
|
161 && r->headers_out.status != NGX_HTTP_MOVED_PERMANENTLY
|
|
162 && r->headers_out.status != NGX_HTTP_MOVED_TEMPORARILY
|
664
|
163 && r->headers_out.status != NGX_HTTP_SEE_OTHER
|
|
164 && r->headers_out.status != NGX_HTTP_NOT_MODIFIED
|
|
165 && r->headers_out.status != NGX_HTTP_TEMPORARY_REDIRECT))
|
92
|
166 {
|
50
|
167 return ngx_http_next_header_filter(r);
|
|
168 }
|
|
169
|
|
170 if (conf->expires != NGX_HTTP_EXPIRES_OFF) {
|
326
|
171 if (ngx_http_set_expires(r, conf) != NGX_OK) {
|
|
172 return NGX_ERROR;
|
|
173 }
|
|
174 }
|
58
|
175
|
326
|
176 if (conf->headers) {
|
|
177 h = conf->headers->elts;
|
|
178 for (i = 0; i < conf->headers->nelts; i++) {
|
50
|
179
|
464
|
180 if (ngx_http_complex_value(r, &h[i].value, &value) != NGX_OK) {
|
|
181 return NGX_ERROR;
|
58
|
182 }
|
|
183
|
326
|
184 if (h[i].handler(r, &h[i], &value) != NGX_OK) {
|
50
|
185 return NGX_ERROR;
|
|
186 }
|
|
187 }
|
|
188 }
|
|
189
|
326
|
190 return ngx_http_next_header_filter(r);
|
|
191 }
|
|
192
|
122
|
193
|
326
|
194 static ngx_int_t
|
|
195 ngx_http_set_expires(ngx_http_request_t *r, ngx_http_headers_conf_t *conf)
|
|
196 {
|
|
197 size_t len;
|
394
|
198 time_t now, expires_time, max_age;
|
326
|
199 ngx_uint_t i;
|
|
200 ngx_table_elt_t *expires, *cc, **ccp;
|
122
|
201
|
326
|
202 expires = r->headers_out.expires;
|
|
203
|
|
204 if (expires == NULL) {
|
|
205
|
|
206 expires = ngx_list_push(&r->headers_out.headers);
|
|
207 if (expires == NULL) {
|
|
208 return NGX_ERROR;
|
|
209 }
|
122
|
210
|
326
|
211 r->headers_out.expires = expires;
|
|
212
|
|
213 expires->hash = 1;
|
570
|
214 ngx_str_set(&expires->key, "Expires");
|
326
|
215 }
|
|
216
|
|
217 len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
|
|
218 expires->value.len = len - 1;
|
|
219
|
|
220 ccp = r->headers_out.cache_control.elts;
|
|
221
|
|
222 if (ccp == NULL) {
|
|
223
|
|
224 if (ngx_array_init(&r->headers_out.cache_control, r->pool,
|
|
225 1, sizeof(ngx_table_elt_t *))
|
|
226 != NGX_OK)
|
|
227 {
|
|
228 return NGX_ERROR;
|
122
|
229 }
|
|
230
|
|
231 ccp = ngx_array_push(&r->headers_out.cache_control);
|
|
232 if (ccp == NULL) {
|
|
233 return NGX_ERROR;
|
|
234 }
|
|
235
|
|
236 cc = ngx_list_push(&r->headers_out.headers);
|
|
237 if (cc == NULL) {
|
|
238 return NGX_ERROR;
|
|
239 }
|
|
240
|
|
241 cc->hash = 1;
|
570
|
242 ngx_str_set(&cc->key, "Cache-Control");
|
122
|
243 *ccp = cc;
|
326
|
244
|
|
245 } else {
|
|
246 for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
|
|
247 ccp[i]->hash = 0;
|
|
248 }
|
|
249
|
|
250 cc = ccp[0];
|
|
251 }
|
|
252
|
|
253 if (conf->expires == NGX_HTTP_EXPIRES_EPOCH) {
|
|
254 expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
|
570
|
255 ngx_str_set(&cc->value, "no-cache");
|
326
|
256 return NGX_OK;
|
|
257 }
|
|
258
|
|
259 if (conf->expires == NGX_HTTP_EXPIRES_MAX) {
|
|
260 expires->value.data = (u_char *) "Thu, 31 Dec 2037 23:55:55 GMT";
|
|
261 /* 10 years */
|
570
|
262 ngx_str_set(&cc->value, "max-age=315360000");
|
326
|
263 return NGX_OK;
|
|
264 }
|
|
265
|
382
|
266 expires->value.data = ngx_pnalloc(r->pool, len);
|
326
|
267 if (expires->value.data == NULL) {
|
|
268 return NGX_ERROR;
|
|
269 }
|
|
270
|
646
|
271 if (conf->expires_time == 0 && conf->expires != NGX_HTTP_EXPIRES_DAILY) {
|
326
|
272 ngx_memcpy(expires->value.data, ngx_cached_http_time.data,
|
|
273 ngx_cached_http_time.len + 1);
|
570
|
274 ngx_str_set(&cc->value, "max-age=0");
|
326
|
275 return NGX_OK;
|
122
|
276 }
|
|
277
|
394
|
278 now = ngx_time();
|
|
279
|
646
|
280 if (conf->expires == NGX_HTTP_EXPIRES_DAILY) {
|
|
281 expires_time = ngx_next_time(conf->expires_time);
|
|
282 max_age = expires_time - now;
|
|
283
|
|
284 } else if (conf->expires == NGX_HTTP_EXPIRES_ACCESS
|
|
285 || r->headers_out.last_modified_time == -1)
|
376
|
286 {
|
394
|
287 expires_time = now + conf->expires_time;
|
|
288 max_age = conf->expires_time;
|
|
289
|
376
|
290 } else {
|
394
|
291 expires_time = r->headers_out.last_modified_time + conf->expires_time;
|
|
292 max_age = expires_time - now;
|
376
|
293 }
|
|
294
|
394
|
295 ngx_http_time(expires->value.data, expires_time);
|
376
|
296
|
560
|
297 if (conf->expires_time < 0 || max_age < 0) {
|
570
|
298 ngx_str_set(&cc->value, "no-cache");
|
326
|
299 return NGX_OK;
|
|
300 }
|
122
|
301
|
382
|
302 cc->value.data = ngx_pnalloc(r->pool,
|
|
303 sizeof("max-age=") + NGX_TIME_T_LEN + 1);
|
326
|
304 if (cc->value.data == NULL) {
|
|
305 return NGX_ERROR;
|
|
306 }
|
|
307
|
394
|
308 cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T", max_age)
|
326
|
309 - cc->value.data;
|
|
310
|
|
311 return NGX_OK;
|
|
312 }
|
|
313
|
|
314
|
|
315 static ngx_int_t
|
|
316 ngx_http_add_header(ngx_http_request_t *r, ngx_http_header_val_t *hv,
|
|
317 ngx_str_t *value)
|
|
318 {
|
|
319 ngx_table_elt_t *h;
|
174
|
320
|
430
|
321 if (value->len) {
|
|
322 h = ngx_list_push(&r->headers_out.headers);
|
|
323 if (h == NULL) {
|
|
324 return NGX_ERROR;
|
|
325 }
|
|
326
|
668
|
327 h->hash = 1;
|
464
|
328 h->key = hv->key;
|
430
|
329 h->value = *value;
|
326
|
330 }
|
|
331
|
|
332 return NGX_OK;
|
|
333 }
|
|
334
|
174
|
335
|
326
|
336 static ngx_int_t
|
|
337 ngx_http_add_cache_control(ngx_http_request_t *r, ngx_http_header_val_t *hv,
|
|
338 ngx_str_t *value)
|
|
339 {
|
|
340 ngx_table_elt_t *cc, **ccp;
|
|
341
|
|
342 ccp = r->headers_out.cache_control.elts;
|
|
343
|
|
344 if (ccp == NULL) {
|
|
345
|
|
346 if (ngx_array_init(&r->headers_out.cache_control, r->pool,
|
|
347 1, sizeof(ngx_table_elt_t *))
|
|
348 != NGX_OK)
|
|
349 {
|
|
350 return NGX_ERROR;
|
122
|
351 }
|
|
352 }
|
|
353
|
326
|
354 ccp = ngx_array_push(&r->headers_out.cache_control);
|
|
355 if (ccp == NULL) {
|
|
356 return NGX_ERROR;
|
|
357 }
|
|
358
|
|
359 cc = ngx_list_push(&r->headers_out.headers);
|
|
360 if (cc == NULL) {
|
|
361 return NGX_ERROR;
|
|
362 }
|
|
363
|
|
364 cc->hash = 1;
|
570
|
365 ngx_str_set(&cc->key, "Cache-Control");
|
326
|
366 cc->value = *value;
|
|
367
|
|
368 *ccp = cc;
|
|
369
|
|
370 return NGX_OK;
|
|
371 }
|
|
372
|
|
373
|
|
374 static ngx_int_t
|
|
375 ngx_http_set_last_modified(ngx_http_request_t *r, ngx_http_header_val_t *hv,
|
|
376 ngx_str_t *value)
|
|
377 {
|
694
|
378 if (ngx_http_set_response_header(r, hv, value) != NGX_OK) {
|
680
|
379 return NGX_ERROR;
|
|
380 }
|
|
381
|
694
|
382 r->headers_out.last_modified_time =
|
|
383 (value->len) ? ngx_http_parse_time(value->data, value->len) : -1;
|
680
|
384
|
|
385 return NGX_OK;
|
|
386 }
|
|
387
|
|
388
|
|
389 static ngx_int_t
|
|
390 ngx_http_set_response_header(ngx_http_request_t *r, ngx_http_header_val_t *hv,
|
|
391 ngx_str_t *value)
|
|
392 {
|
326
|
393 ngx_table_elt_t *h, **old;
|
|
394
|
668
|
395 old = (ngx_table_elt_t **) ((char *) &r->headers_out + hv->offset);
|
326
|
396
|
694
|
397 if (value->len == 0) {
|
|
398 if (*old) {
|
|
399 (*old)->hash = 0;
|
|
400 *old = NULL;
|
|
401 }
|
348
|
402
|
680
|
403 return NGX_OK;
|
|
404 }
|
668
|
405
|
694
|
406 if (*old) {
|
|
407 h = *old;
|
|
408
|
|
409 } else {
|
|
410 h = ngx_list_push(&r->headers_out.headers);
|
|
411 if (h == NULL) {
|
|
412 return NGX_ERROR;
|
|
413 }
|
|
414
|
|
415 *old = h;
|
680
|
416 }
|
348
|
417
|
668
|
418 h->hash = 1;
|
464
|
419 h->key = hv->key;
|
326
|
420 h->value = *value;
|
|
421
|
|
422 return NGX_OK;
|
50
|
423 }
|
|
424
|
|
425
|
|
426 static void *
|
|
427 ngx_http_headers_create_conf(ngx_conf_t *cf)
|
126
|
428 {
|
50
|
429 ngx_http_headers_conf_t *conf;
|
|
430
|
122
|
431 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_headers_conf_t));
|
50
|
432 if (conf == NULL) {
|
496
|
433 return NULL;
|
50
|
434 }
|
|
435
|
122
|
436 /*
|
|
437 * set by ngx_pcalloc():
|
|
438 *
|
|
439 * conf->headers = NULL;
|
376
|
440 * conf->expires_time = 0;
|
122
|
441 */
|
|
442
|
668
|
443 conf->expires = NGX_HTTP_EXPIRES_UNSET;
|
50
|
444
|
|
445 return conf;
|
|
446 }
|
|
447
|
|
448
|
|
449 static char *
|
|
450 ngx_http_headers_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
451 {
|
|
452 ngx_http_headers_conf_t *prev = parent;
|
|
453 ngx_http_headers_conf_t *conf = child;
|
|
454
|
668
|
455 if (conf->expires == NGX_HTTP_EXPIRES_UNSET) {
|
376
|
456 conf->expires = prev->expires;
|
|
457 conf->expires_time = prev->expires_time;
|
|
458
|
668
|
459 if (conf->expires == NGX_HTTP_EXPIRES_UNSET) {
|
376
|
460 conf->expires = NGX_HTTP_EXPIRES_OFF;
|
|
461 }
|
122
|
462 }
|
|
463
|
|
464 if (conf->headers == NULL) {
|
|
465 conf->headers = prev->headers;
|
|
466 }
|
|
467
|
50
|
468 return NGX_CONF_OK;
|
|
469 }
|
|
470
|
|
471
|
230
|
472 static ngx_int_t
|
|
473 ngx_http_headers_filter_init(ngx_conf_t *cf)
|
|
474 {
|
|
475 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
476 ngx_http_top_header_filter = ngx_http_headers_filter;
|
|
477
|
|
478 return NGX_OK;
|
|
479 }
|
|
480
|
|
481
|
50
|
482 static char *
|
|
483 ngx_http_headers_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
484 {
|
|
485 ngx_http_headers_conf_t *hcf = conf;
|
|
486
|
376
|
487 ngx_uint_t minus, n;
|
50
|
488 ngx_str_t *value;
|
|
489
|
668
|
490 if (hcf->expires != NGX_HTTP_EXPIRES_UNSET) {
|
50
|
491 return "is duplicate";
|
|
492 }
|
|
493
|
|
494 value = cf->args->elts;
|
|
495
|
376
|
496 if (cf->args->nelts == 2) {
|
|
497
|
|
498 if (ngx_strcmp(value[1].data, "epoch") == 0) {
|
|
499 hcf->expires = NGX_HTTP_EXPIRES_EPOCH;
|
|
500 return NGX_CONF_OK;
|
|
501 }
|
|
502
|
|
503 if (ngx_strcmp(value[1].data, "max") == 0) {
|
|
504 hcf->expires = NGX_HTTP_EXPIRES_MAX;
|
|
505 return NGX_CONF_OK;
|
|
506 }
|
50
|
507
|
376
|
508 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
509 hcf->expires = NGX_HTTP_EXPIRES_OFF;
|
|
510 return NGX_CONF_OK;
|
|
511 }
|
|
512
|
|
513 hcf->expires = NGX_HTTP_EXPIRES_ACCESS;
|
|
514
|
|
515 n = 1;
|
|
516
|
|
517 } else { /* cf->args->nelts == 3 */
|
|
518
|
|
519 if (ngx_strcmp(value[1].data, "modified") != 0) {
|
|
520 return "invalid value";
|
|
521 }
|
|
522
|
|
523 hcf->expires = NGX_HTTP_EXPIRES_MODIFIED;
|
|
524
|
|
525 n = 2;
|
238
|
526 }
|
|
527
|
394
|
528 if (value[n].data[0] == '@') {
|
|
529 value[n].data++;
|
|
530 value[n].len--;
|
|
531 minus = 0;
|
|
532
|
|
533 if (hcf->expires == NGX_HTTP_EXPIRES_MODIFIED) {
|
640
|
534 return "daily time cannot be used with \"modified\" parameter";
|
394
|
535 }
|
|
536
|
|
537 hcf->expires = NGX_HTTP_EXPIRES_DAILY;
|
|
538
|
|
539 } else if (value[n].data[0] == '+') {
|
376
|
540 value[n].data++;
|
|
541 value[n].len--;
|
50
|
542 minus = 0;
|
|
543
|
376
|
544 } else if (value[n].data[0] == '-') {
|
|
545 value[n].data++;
|
|
546 value[n].len--;
|
50
|
547 minus = 1;
|
|
548
|
|
549 } else {
|
|
550 minus = 0;
|
|
551 }
|
|
552
|
376
|
553 hcf->expires_time = ngx_parse_time(&value[n], 1);
|
50
|
554
|
662
|
555 if (hcf->expires_time == (time_t) NGX_ERROR) {
|
50
|
556 return "invalid value";
|
|
557 }
|
126
|
558
|
394
|
559 if (hcf->expires == NGX_HTTP_EXPIRES_DAILY
|
|
560 && hcf->expires_time > 24 * 60 * 60)
|
|
561 {
|
|
562 return "daily time value must be less than 24 hours";
|
|
563 }
|
|
564
|
50
|
565 if (minus) {
|
376
|
566 hcf->expires_time = - hcf->expires_time;
|
50
|
567 }
|
|
568
|
|
569 return NGX_CONF_OK;
|
|
570 }
|
122
|
571
|
|
572
|
|
573 static char *
|
|
574 ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
575 {
|
|
576 ngx_http_headers_conf_t *hcf = conf;
|
|
577
|
464
|
578 ngx_str_t *value;
|
|
579 ngx_uint_t i;
|
|
580 ngx_http_header_val_t *hv;
|
|
581 ngx_http_set_header_t *set;
|
|
582 ngx_http_compile_complex_value_t ccv;
|
122
|
583
|
|
584 value = cf->args->elts;
|
|
585
|
|
586 if (hcf->headers == NULL) {
|
174
|
587 hcf->headers = ngx_array_create(cf->pool, 1,
|
|
588 sizeof(ngx_http_header_val_t));
|
122
|
589 if (hcf->headers == NULL) {
|
|
590 return NGX_CONF_ERROR;
|
|
591 }
|
|
592 }
|
|
593
|
464
|
594 hv = ngx_array_push(hcf->headers);
|
|
595 if (hv == NULL) {
|
122
|
596 return NGX_CONF_ERROR;
|
|
597 }
|
|
598
|
464
|
599 hv->key = value[1];
|
|
600 hv->handler = ngx_http_add_header;
|
|
601 hv->offset = 0;
|
174
|
602
|
464
|
603 set = ngx_http_set_headers;
|
|
604 for (i = 0; set[i].name.len; i++) {
|
|
605 if (ngx_strcasecmp(value[1].data, set[i].name.data) != 0) {
|
326
|
606 continue;
|
|
607 }
|
|
608
|
464
|
609 hv->offset = set[i].offset;
|
|
610 hv->handler = set[i].handler;
|
|
611
|
326
|
612 break;
|
|
613 }
|
|
614
|
466
|
615 if (value[2].len == 0) {
|
|
616 ngx_memzero(&hv->value, sizeof(ngx_http_complex_value_t));
|
|
617 return NGX_CONF_OK;
|
|
618 }
|
|
619
|
464
|
620 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
174
|
621
|
464
|
622 ccv.cf = cf;
|
|
623 ccv.value = &value[2];
|
|
624 ccv.complex_value = &hv->value;
|
174
|
625
|
464
|
626 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
174
|
627 return NGX_CONF_ERROR;
|
|
628 }
|
122
|
629
|
|
630 return NGX_CONF_OK;
|
|
631 }
|