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