-
Notifications
You must be signed in to change notification settings - Fork 10
/
ngx_http_set_header.c
326 lines (253 loc) · 8.18 KB
/
ngx_http_set_header.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* @Author: im-pingo
* @Copyright: pngox
* @Github: https://github.com/im-pingo
* @EMail: [email protected]
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct ngx_http_header_val_s ngx_http_header_val_t;
typedef ngx_int_t (*ngx_http_set_header_pt)(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value);
struct ngx_http_header_val_s {
ngx_http_complex_value_t value;
ngx_uint_t hash;
ngx_str_t key;
ngx_http_set_header_pt handler;
ngx_uint_t offset;
};
typedef struct {
ngx_str_t name;
ngx_uint_t offset;
ngx_http_set_header_pt handler;
} ngx_http_set_header_t;
//TODO need fill all header set in future
/* for header has no quick link in ngx_http_headers_out_t */
static ngx_int_t ngx_http_set_header_out_other(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value);
/* for header has quick link like ngx_table_elt_t* in ngx_http_headers_out_t */
static ngx_int_t ngx_http_set_header_out_builtin(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value);
/* for header has quick link like ngx_array_t in ngx_http_headers_out_t */
static ngx_int_t ngx_http_set_header_out_builtin_multi(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value);
/* for header has quick link like ngx_table_elt_t* and
* other attribute in ngx_http_headers_out_t defined below */
static ngx_int_t ngx_http_set_header_out_content_type(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value);
static ngx_http_set_header_t ngx_http_set_header_out_handlers[] = {
{ ngx_string("Server"),
offsetof(ngx_http_headers_out_t, server),
ngx_http_set_header_out_builtin },
{ ngx_string("Date"),
offsetof(ngx_http_headers_out_t, date),
ngx_http_set_header_out_builtin },
{ ngx_string("Content-Type"),
offsetof(ngx_http_headers_out_t, content_type),
ngx_http_set_header_out_content_type },
{ ngx_string("Cache-Control"),
offsetof(ngx_http_headers_out_t, cache_control),
ngx_http_set_header_out_builtin_multi },
{ ngx_null_string, 0, ngx_http_set_header_out_other }
};
static ngx_int_t
ngx_http_set_header_out_helper(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value, unsigned no_create)
{
ngx_table_elt_t *h;
ngx_list_part_t *part;
ngx_uint_t i;
part = &r->headers_out.headers.part;
h = part->elts;
for (i = 0; /* void */; ++i) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
h = part->elts;
i = 0;
}
if (h[i].hash != 0
&& h[i].key.len == hv->key.len
&& ngx_strncasecmp(hv->key.data, h[i].key.data, h[i].key.len) == 0)
/* header has been set */
{
h[i].value = *value;
if (value->len == 0) { /* if value is empty, remove header */
h[i].hash = 0;
} else {
h[i].hash = hv->hash;
}
return NGX_OK;
}
}
if (no_create && value->len == 0) { /* set header to empty but header cannot found */
return NGX_OK;
}
/* header has not been set, create it */
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_ERROR;
}
h->hash = hv->hash;
h->key = hv->key;
h->value = *value;
h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
if (h->lowcase_key == NULL) {
return NGX_ERROR;
}
ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
return NGX_OK;
}
static ngx_int_t
ngx_http_set_header_out_other(ngx_http_request_t * r,
ngx_http_header_val_t * hv,ngx_str_t * value)
{
return ngx_http_set_header_out_helper(r, hv, value, 0);
}
static ngx_int_t
ngx_http_set_header_out_builtin(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value)
{
ngx_table_elt_t *h, **old;
if (hv->offset) {
old = (ngx_table_elt_t **) ((char *) &r->headers_out + hv->offset);
} else {
old = NULL;
}
if (old == NULL || *old == NULL) {
/* user should use ngx_http_set_header_out_other but use this func to set header */
//TODO
return ngx_http_set_header_out_helper(r, hv, value, 0);
}
h = *old;
h->value = *value;
if (value->len == 0) { /* if value is empty, remove header */
h->hash = 0;
return NGX_OK;
}
h->hash = hv->hash;
h->key = hv->key;
return NGX_OK;
}
static ngx_int_t
ngx_http_set_header_out_builtin_multi(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value)
{
ngx_array_t *pa;
ngx_table_elt_t *ho, **ph;
ngx_uint_t i;
pa = (ngx_array_t *) ((char *) &r->headers_out + hv->offset);
if (pa->elts == NULL) {
if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *))
!= NGX_OK)
{
return NGX_ERROR;
}
}
if (pa->nelts > 0) {
ph = pa->elts;
for (i = 1; i < pa->nelts; i++) { /* clear old value */
ph[i]->hash = 0;
ph[i]->value.len = 0;
}
ph[0]->value = *value;
if (value->len == 0) {
ph[0]->hash = 0;
} else {
ph[0]->hash = hv->hash;
}
return NGX_OK;
}
/* header does not set */
ph = ngx_array_push(pa);
if (ph == NULL) {
return NGX_ERROR;
}
ho = ngx_list_push(&r->headers_out.headers);
if (ho == NULL) {
return NGX_ERROR;
}
ho->value = *value;
ho->hash = hv->hash;
ho->key = hv->key;
*ph = ho;
return NGX_OK;
}
static ngx_int_t
ngx_http_set_header_out_content_type(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value)
{
ngx_uint_t i;
r->headers_out.content_type_len = value->len;
for (i = 0; i < value->len; i++) {
if (value->data[i] == ';') {
r->headers_out.content_type_len = i;
break;
}
}
r->headers_out.content_type = *value;
r->headers_out.content_type_hash = hv->hash;
r->headers_out.content_type_lowcase = NULL;
value->len = 0;
return ngx_http_set_header_out_helper(r, hv, value, 1);
}
ngx_int_t
ngx_http_set_header_out(ngx_http_request_t *r, ngx_str_t *key, ngx_str_t *value)
{
ngx_http_header_val_t hv;
ngx_http_set_header_t *handlers = ngx_http_set_header_out_handlers;
ngx_uint_t i;
ngx_str_t v;
hv.hash = ngx_hash_key_lc(key->data, key->len);
hv.key = *key;
hv.offset = 0;
hv.handler = NULL;
for (i = 0; handlers[i].name.len; ++i) {
if (hv.key.len != handlers[i].name.len
|| ngx_strncasecmp(hv.key.data, handlers[i].name.data,
handlers[i].name.len) != 0)
{
continue;
}
hv.offset = handlers[i].offset;
hv.handler = handlers[i].handler;
break;
}
if (handlers[i].name.len == 0 && handlers[i].handler) {
hv.offset = handlers[i].offset;
hv.handler = handlers[i].handler;
}
v = *value;
return hv.handler(r, &hv, &v);
}
ngx_str_t *
ngx_http_get_header_in(ngx_http_request_t *r, ngx_str_t *key)
{
ngx_table_elt_t *h;
ngx_list_part_t *part;
ngx_uint_t i;
part = &r->headers_in.headers.part;
h = part->elts;
for (i = 0; /* void */; ++i) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
h = part->elts;
i = 0;
}
if (h[i].hash == 0) {
continue;
}
if (h[i].key.len == key->len
&& ngx_strncasecmp(h[i].key.data, key->data, key->len) == 0)
{
return &h[i].value;
}
}
return NULL;
}