48
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
644
|
4 * Copyright (C) Nginx, Inc.
|
48
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
322
|
11 #include <nginx.h>
|
48
|
12
|
|
13
|
122
|
14 static ngx_int_t ngx_http_variable_request(ngx_http_request_t *r,
|
|
15 ngx_http_variable_value_t *v, uintptr_t data);
|
324
|
16 static void ngx_http_variable_request_set(ngx_http_request_t *r,
|
|
17 ngx_http_variable_value_t *v, uintptr_t data);
|
540
|
18 static ngx_int_t ngx_http_variable_request_get_size(ngx_http_request_t *r,
|
|
19 ngx_http_variable_value_t *v, uintptr_t data);
|
186
|
20 static void ngx_http_variable_request_set_size(ngx_http_request_t *r,
|
|
21 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
22 static ngx_int_t ngx_http_variable_header(ngx_http_request_t *r,
|
|
23 ngx_http_variable_value_t *v, uintptr_t data);
|
|
24 static ngx_int_t ngx_http_variable_headers(ngx_http_request_t *r,
|
|
25 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
26
|
|
27 static ngx_int_t ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
|
122
|
28 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
29 static ngx_int_t ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
|
|
30 ngx_http_variable_value_t *v, uintptr_t data);
|
528
|
31 static ngx_int_t ngx_http_variable_request_line(ngx_http_request_t *r,
|
|
32 ngx_http_variable_value_t *v, uintptr_t data);
|
424
|
33 static ngx_int_t ngx_http_variable_cookie(ngx_http_request_t *r,
|
|
34 ngx_http_variable_value_t *v, uintptr_t data);
|
392
|
35 static ngx_int_t ngx_http_variable_argument(ngx_http_request_t *r,
|
|
36 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
37
|
122
|
38 static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r,
|
|
39 ngx_http_variable_value_t *v, uintptr_t data);
|
272
|
40 static ngx_int_t ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
|
|
41 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
42 static ngx_int_t ngx_http_variable_remote_addr(ngx_http_request_t *r,
|
|
43 ngx_http_variable_value_t *v, uintptr_t data);
|
|
44 static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r,
|
|
45 ngx_http_variable_value_t *v, uintptr_t data);
|
|
46 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r,
|
|
47 ngx_http_variable_value_t *v, uintptr_t data);
|
|
48 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r,
|
|
49 ngx_http_variable_value_t *v, uintptr_t data);
|
238
|
50 static ngx_int_t ngx_http_variable_scheme(ngx_http_request_t *r,
|
|
51 ngx_http_variable_value_t *v, uintptr_t data);
|
324
|
52 static ngx_int_t ngx_http_variable_is_args(ngx_http_request_t *r,
|
|
53 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
54 static ngx_int_t ngx_http_variable_document_root(ngx_http_request_t *r,
|
|
55 ngx_http_variable_value_t *v, uintptr_t data);
|
412
|
56 static ngx_int_t ngx_http_variable_realpath_root(ngx_http_request_t *r,
|
|
57 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
58 static ngx_int_t ngx_http_variable_request_filename(ngx_http_request_t *r,
|
|
59 ngx_http_variable_value_t *v, uintptr_t data);
|
362
|
60 static ngx_int_t ngx_http_variable_server_name(ngx_http_request_t *r,
|
|
61 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
62 static ngx_int_t ngx_http_variable_request_method(ngx_http_request_t *r,
|
|
63 ngx_http_variable_value_t *v, uintptr_t data);
|
|
64 static ngx_int_t ngx_http_variable_remote_user(ngx_http_request_t *r,
|
|
65 ngx_http_variable_value_t *v, uintptr_t data);
|
160
|
66 static ngx_int_t ngx_http_variable_body_bytes_sent(ngx_http_request_t *r,
|
|
67 ngx_http_variable_value_t *v, uintptr_t data);
|
178
|
68 static ngx_int_t ngx_http_variable_request_completion(ngx_http_request_t *r,
|
|
69 ngx_http_variable_value_t *v, uintptr_t data);
|
492
|
70 static ngx_int_t ngx_http_variable_request_body(ngx_http_request_t *r,
|
|
71 ngx_http_variable_value_t *v, uintptr_t data);
|
244
|
72 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r,
|
|
73 ngx_http_variable_value_t *v, uintptr_t data);
|
48
|
74
|
190
|
75 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r,
|
|
76 ngx_http_variable_value_t *v, uintptr_t data);
|
|
77 static ngx_int_t ngx_http_variable_sent_content_length(ngx_http_request_t *r,
|
|
78 ngx_http_variable_value_t *v, uintptr_t data);
|
442
|
79 static ngx_int_t ngx_http_variable_sent_location(ngx_http_request_t *r,
|
|
80 ngx_http_variable_value_t *v, uintptr_t data);
|
190
|
81 static ngx_int_t ngx_http_variable_sent_last_modified(ngx_http_request_t *r,
|
|
82 ngx_http_variable_value_t *v, uintptr_t data);
|
|
83 static ngx_int_t ngx_http_variable_sent_connection(ngx_http_request_t *r,
|
|
84 ngx_http_variable_value_t *v, uintptr_t data);
|
|
85 static ngx_int_t ngx_http_variable_sent_keep_alive(ngx_http_request_t *r,
|
|
86 ngx_http_variable_value_t *v, uintptr_t data);
|
|
87 static ngx_int_t ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
|
|
88 ngx_http_variable_value_t *v, uintptr_t data);
|
|
89
|
322
|
90 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r,
|
|
91 ngx_http_variable_value_t *v, uintptr_t data);
|
376
|
92 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r,
|
|
93 ngx_http_variable_value_t *v, uintptr_t data);
|
410
|
94 static ngx_int_t ngx_http_variable_pid(ngx_http_request_t *r,
|
|
95 ngx_http_variable_value_t *v, uintptr_t data);
|
48
|
96
|
58
|
97 /*
|
|
98 * TODO:
|
|
99 * Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED
|
|
100 * REMOTE_HOST (null), REMOTE_IDENT (null),
|
|
101 * SERVER_SOFTWARE
|
|
102 *
|
120
|
103 * Apache SSI: DOCUMENT_NAME, LAST_MODIFIED, USER_NAME (file owner)
|
58
|
104 */
|
48
|
105
|
190
|
106 /*
|
|
107 * the $http_host, $http_user_agent, $http_referer, $http_via,
|
|
108 * and $http_x_forwarded_for variables may be handled by generic
|
|
109 * ngx_http_variable_unknown_header_in(), but for perfomance reasons
|
|
110 * they are handled using dedicated entries
|
|
111 */
|
|
112
|
58
|
113 static ngx_http_variable_t ngx_http_core_variables[] = {
|
|
114
|
186
|
115 { ngx_string("http_host"), NULL, ngx_http_variable_header,
|
82
|
116 offsetof(ngx_http_request_t, headers_in.host), 0, 0 },
|
48
|
117
|
186
|
118 { ngx_string("http_user_agent"), NULL, ngx_http_variable_header,
|
82
|
119 offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },
|
58
|
120
|
186
|
121 { ngx_string("http_referer"), NULL, ngx_http_variable_header,
|
82
|
122 offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },
|
48
|
123
|
|
124 #if (NGX_HTTP_GZIP)
|
186
|
125 { ngx_string("http_via"), NULL, ngx_http_variable_header,
|
82
|
126 offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
|
48
|
127 #endif
|
|
128
|
286
|
129 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
|
186
|
130 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header,
|
82
|
131 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
|
48
|
132 #endif
|
|
133
|
186
|
134 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
|
82
|
135 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },
|
64
|
136
|
186
|
137 { ngx_string("content_length"), NULL, ngx_http_variable_header,
|
82
|
138 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 },
|
58
|
139
|
186
|
140 { ngx_string("content_type"), NULL, ngx_http_variable_header,
|
82
|
141 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },
|
58
|
142
|
186
|
143 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },
|
58
|
144
|
272
|
145 { ngx_string("binary_remote_addr"), NULL,
|
|
146 ngx_http_variable_binary_remote_addr, 0, 0, 0 },
|
|
147
|
186
|
148 { ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 },
|
48
|
149
|
186
|
150 { ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 },
|
58
|
151
|
186
|
152 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },
|
58
|
153
|
186
|
154 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },
|
58
|
155
|
186
|
156 { ngx_string("server_protocol"), NULL, ngx_http_variable_request,
|
82
|
157 offsetof(ngx_http_request_t, http_protocol), 0, 0 },
|
58
|
158
|
238
|
159 { ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 },
|
|
160
|
186
|
161 { ngx_string("request_uri"), NULL, ngx_http_variable_request,
|
82
|
162 offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },
|
48
|
163
|
186
|
164 { ngx_string("uri"), NULL, ngx_http_variable_request,
|
|
165 offsetof(ngx_http_request_t, uri),
|
340
|
166 NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
186
|
167
|
|
168 { ngx_string("document_uri"), NULL, ngx_http_variable_request,
|
122
|
169 offsetof(ngx_http_request_t, uri),
|
340
|
170 NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
122
|
171
|
528
|
172 { ngx_string("request"), NULL, ngx_http_variable_request_line, 0, 0, 0 },
|
118
|
173
|
186
|
174 { ngx_string("document_root"), NULL,
|
340
|
175 ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
58
|
176
|
412
|
177 { ngx_string("realpath_root"), NULL,
|
|
178 ngx_http_variable_realpath_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
|
179
|
186
|
180 { ngx_string("query_string"), NULL, ngx_http_variable_request,
|
58
|
181 offsetof(ngx_http_request_t, args),
|
340
|
182 NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
48
|
183
|
324
|
184 { ngx_string("args"),
|
|
185 ngx_http_variable_request_set,
|
|
186 ngx_http_variable_request,
|
138
|
187 offsetof(ngx_http_request_t, args),
|
340
|
188 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
324
|
189
|
|
190 { ngx_string("is_args"), NULL, ngx_http_variable_is_args,
|
340
|
191 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
138
|
192
|
186
|
193 { ngx_string("request_filename"), NULL,
|
|
194 ngx_http_variable_request_filename, 0,
|
340
|
195 NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
58
|
196
|
362
|
197 { ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0, 0, 0 },
|
58
|
198
|
186
|
199 { ngx_string("request_method"), NULL,
|
564
|
200 ngx_http_variable_request_method, 0,
|
|
201 NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
58
|
202
|
186
|
203 { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 },
|
58
|
204
|
186
|
205 { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent,
|
160
|
206 0, 0, 0 },
|
|
207
|
186
|
208 { ngx_string("request_completion"), NULL,
|
|
209 ngx_http_variable_request_completion,
|
178
|
210 0, 0, 0 },
|
|
211
|
492
|
212 { ngx_string("request_body"), NULL,
|
|
213 ngx_http_variable_request_body,
|
|
214 0, 0, 0 },
|
|
215
|
244
|
216 { ngx_string("request_body_file"), NULL,
|
|
217 ngx_http_variable_request_body_file,
|
|
218 0, 0, 0 },
|
|
219
|
190
|
220 { ngx_string("sent_http_content_type"), NULL,
|
|
221 ngx_http_variable_sent_content_type, 0, 0, 0 },
|
|
222
|
|
223 { ngx_string("sent_http_content_length"), NULL,
|
|
224 ngx_http_variable_sent_content_length, 0, 0, 0 },
|
|
225
|
442
|
226 { ngx_string("sent_http_location"), NULL,
|
|
227 ngx_http_variable_sent_location, 0, 0, 0 },
|
|
228
|
190
|
229 { ngx_string("sent_http_last_modified"), NULL,
|
|
230 ngx_http_variable_sent_last_modified, 0, 0, 0 },
|
|
231
|
|
232 { ngx_string("sent_http_connection"), NULL,
|
|
233 ngx_http_variable_sent_connection, 0, 0, 0 },
|
|
234
|
|
235 { ngx_string("sent_http_keep_alive"), NULL,
|
|
236 ngx_http_variable_sent_keep_alive, 0, 0, 0 },
|
|
237
|
|
238 { ngx_string("sent_http_transfer_encoding"), NULL,
|
|
239 ngx_http_variable_sent_transfer_encoding, 0, 0, 0 },
|
|
240
|
|
241 { ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers,
|
|
242 offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 },
|
|
243
|
186
|
244 { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
|
540
|
245 ngx_http_variable_request_get_size,
|
186
|
246 offsetof(ngx_http_request_t, limit_rate),
|
340
|
247 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
|
186
|
248
|
322
|
249 { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
|
|
250 0, 0, 0 },
|
|
251
|
376
|
252 { ngx_string("hostname"), NULL, ngx_http_variable_hostname,
|
|
253 0, 0, 0 },
|
|
254
|
410
|
255 { ngx_string("pid"), NULL, ngx_http_variable_pid,
|
|
256 0, 0, 0 },
|
|
257
|
186
|
258 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
48
|
259 };
|
|
260
|
|
261
|
126
|
262 ngx_http_variable_value_t ngx_http_variable_null_value =
|
|
263 ngx_http_variable("");
|
|
264 ngx_http_variable_value_t ngx_http_variable_true_value =
|
|
265 ngx_http_variable("1");
|
|
266
|
|
267
|
48
|
268 ngx_http_variable_t *
|
58
|
269 ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
|
|
270 {
|
160
|
271 ngx_int_t rc;
|
58
|
272 ngx_uint_t i;
|
160
|
273 ngx_hash_key_t *key;
|
58
|
274 ngx_http_variable_t *v;
|
|
275 ngx_http_core_main_conf_t *cmcf;
|
|
276
|
|
277 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
278
|
160
|
279 key = cmcf->variables_keys->keys.elts;
|
|
280 for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {
|
|
281 if (name->len != key[i].key.len
|
|
282 || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)
|
58
|
283 {
|
|
284 continue;
|
|
285 }
|
|
286
|
160
|
287 v = key[i].value;
|
|
288
|
340
|
289 if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {
|
58
|
290 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
291 "the duplicate \"%V\" variable", name);
|
|
292 return NULL;
|
|
293 }
|
|
294
|
160
|
295 return v;
|
58
|
296 }
|
|
297
|
160
|
298 v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));
|
58
|
299 if (v == NULL) {
|
|
300 return NULL;
|
|
301 }
|
|
302
|
|
303 v->name.len = name->len;
|
382
|
304 v->name.data = ngx_pnalloc(cf->pool, name->len);
|
58
|
305 if (v->name.data == NULL) {
|
|
306 return NULL;
|
|
307 }
|
|
308
|
392
|
309 ngx_strlow(v->name.data, name->data, name->len);
|
58
|
310
|
186
|
311 v->set_handler = NULL;
|
|
312 v->get_handler = NULL;
|
58
|
313 v->data = 0;
|
|
314 v->flags = flags;
|
82
|
315 v->index = 0;
|
58
|
316
|
160
|
317 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);
|
|
318
|
|
319 if (rc == NGX_ERROR) {
|
|
320 return NULL;
|
|
321 }
|
|
322
|
|
323 if (rc == NGX_BUSY) {
|
|
324 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
325 "conflicting variable name \"%V\"", name);
|
|
326 return NULL;
|
|
327 }
|
|
328
|
58
|
329 return v;
|
|
330 }
|
|
331
|
|
332
|
|
333 ngx_int_t
|
|
334 ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name)
|
48
|
335 {
|
50
|
336 ngx_uint_t i;
|
|
337 ngx_http_variable_t *v;
|
48
|
338 ngx_http_core_main_conf_t *cmcf;
|
|
339
|
|
340 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
341
|
50
|
342 v = cmcf->variables.elts;
|
|
343
|
|
344 if (v == NULL) {
|
48
|
345 if (ngx_array_init(&cmcf->variables, cf->pool, 4,
|
454
|
346 sizeof(ngx_http_variable_t))
|
|
347 != NGX_OK)
|
48
|
348 {
|
58
|
349 return NGX_ERROR;
|
48
|
350 }
|
50
|
351
|
|
352 } else {
|
|
353 for (i = 0; i < cmcf->variables.nelts; i++) {
|
|
354 if (name->len != v[i].name.len
|
|
355 || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)
|
|
356 {
|
|
357 continue;
|
|
358 }
|
|
359
|
58
|
360 return i;
|
50
|
361 }
|
48
|
362 }
|
|
363
|
50
|
364 v = ngx_array_push(&cmcf->variables);
|
|
365 if (v == NULL) {
|
58
|
366 return NGX_ERROR;
|
48
|
367 }
|
|
368
|
50
|
369 v->name.len = name->len;
|
382
|
370 v->name.data = ngx_pnalloc(cf->pool, name->len);
|
50
|
371 if (v->name.data == NULL) {
|
58
|
372 return NGX_ERROR;
|
50
|
373 }
|
48
|
374
|
392
|
375 ngx_strlow(v->name.data, name->data, name->len);
|
50
|
376
|
186
|
377 v->set_handler = NULL;
|
|
378 v->get_handler = NULL;
|
50
|
379 v->data = 0;
|
58
|
380 v->flags = 0;
|
82
|
381 v->index = cmcf->variables.nelts - 1;
|
50
|
382
|
58
|
383 return cmcf->variables.nelts - 1;
|
48
|
384 }
|
|
385
|
|
386
|
|
387 ngx_http_variable_value_t *
|
|
388 ngx_http_get_indexed_variable(ngx_http_request_t *r, ngx_uint_t index)
|
|
389 {
|
50
|
390 ngx_http_variable_t *v;
|
48
|
391 ngx_http_core_main_conf_t *cmcf;
|
|
392
|
|
393 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
394
|
102
|
395 if (cmcf->variables.nelts <= index) {
|
48
|
396 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
397 "unknown variable index: %d", index);
|
|
398 return NULL;
|
|
399 }
|
|
400
|
122
|
401 if (r->variables[index].not_found || r->variables[index].valid) {
|
|
402 return &r->variables[index];
|
50
|
403 }
|
48
|
404
|
50
|
405 v = cmcf->variables.elts;
|
48
|
406
|
186
|
407 if (v[index].get_handler(r, &r->variables[index], v[index].data)
|
|
408 == NGX_OK)
|
|
409 {
|
340
|
410 if (v[index].flags & NGX_HTTP_VAR_NOCACHEABLE) {
|
|
411 r->variables[index].no_cacheable = 1;
|
48
|
412 }
|
122
|
413
|
|
414 return &r->variables[index];
|
48
|
415 }
|
|
416
|
206
|
417 r->variables[index].valid = 0;
|
|
418 r->variables[index].not_found = 1;
|
|
419
|
122
|
420 return NULL;
|
|
421 }
|
|
422
|
|
423
|
|
424 ngx_http_variable_value_t *
|
|
425 ngx_http_get_flushed_variable(ngx_http_request_t *r, ngx_uint_t index)
|
|
426 {
|
294
|
427 ngx_http_variable_value_t *v;
|
122
|
428
|
|
429 v = &r->variables[index];
|
|
430
|
646
|
431 if (v->valid || v->not_found) {
|
340
|
432 if (!v->no_cacheable) {
|
122
|
433 return v;
|
|
434 }
|
|
435
|
|
436 v->valid = 0;
|
|
437 v->not_found = 0;
|
58
|
438 }
|
50
|
439
|
122
|
440 return ngx_http_get_indexed_variable(r, index);
|
48
|
441 }
|
|
442
|
|
443
|
|
444 ngx_http_variable_value_t *
|
568
|
445 ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key)
|
48
|
446 {
|
50
|
447 ngx_http_variable_t *v;
|
122
|
448 ngx_http_variable_value_t *vv;
|
48
|
449 ngx_http_core_main_conf_t *cmcf;
|
|
450
|
|
451 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
452
|
160
|
453 v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len);
|
48
|
454
|
160
|
455 if (v) {
|
|
456 if (v->flags & NGX_HTTP_VAR_INDEXED) {
|
568
|
457 return ngx_http_get_flushed_variable(r, v->index);
|
68
|
458
|
|
459 } else {
|
48
|
460
|
122
|
461 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
|
50
|
462
|
186
|
463 if (vv && v->get_handler(r, vv, v->data) == NGX_OK) {
|
122
|
464 return vv;
|
|
465 }
|
58
|
466
|
122
|
467 return NULL;
|
|
468 }
|
58
|
469 }
|
|
470
|
|
471 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
|
|
472 if (vv == NULL) {
|
|
473 return NULL;
|
|
474 }
|
|
475
|
122
|
476 if (ngx_strncmp(name->data, "http_", 5) == 0) {
|
48
|
477
|
126
|
478 if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name)
|
|
479 == NGX_OK)
|
|
480 {
|
|
481 return vv;
|
|
482 }
|
|
483
|
|
484 return NULL;
|
|
485 }
|
|
486
|
|
487 if (ngx_strncmp(name->data, "sent_http_", 10) == 0) {
|
|
488
|
|
489 if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name)
|
|
490 == NGX_OK)
|
122
|
491 {
|
|
492 return vv;
|
|
493 }
|
48
|
494
|
|
495 return NULL;
|
|
496 }
|
|
497
|
564
|
498 if (ngx_strncmp(name->data, "upstream_http_", 14) == 0) {
|
296
|
499
|
|
500 if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name)
|
|
501 == NGX_OK)
|
|
502 {
|
|
503 return vv;
|
|
504 }
|
|
505
|
|
506 return NULL;
|
|
507 }
|
|
508
|
420
|
509 if (ngx_strncmp(name->data, "cookie_", 7) == 0) {
|
|
510
|
424
|
511 if (ngx_http_variable_cookie(r, vv, (uintptr_t) name) == NGX_OK) {
|
420
|
512 return vv;
|
|
513 }
|
|
514
|
|
515 return NULL;
|
|
516 }
|
|
517
|
392
|
518 if (ngx_strncmp(name->data, "arg_", 4) == 0) {
|
|
519
|
|
520 if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) {
|
|
521 return vv;
|
|
522 }
|
|
523
|
|
524 return NULL;
|
|
525 }
|
|
526
|
184
|
527 vv->not_found = 1;
|
122
|
528
|
50
|
529 return vv;
|
48
|
530 }
|
|
531
|
|
532
|
122
|
533 static ngx_int_t
|
|
534 ngx_http_variable_request(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
535 uintptr_t data)
|
|
536 {
|
|
537 ngx_str_t *s;
|
|
538
|
|
539 s = (ngx_str_t *) ((char *) r + data);
|
|
540
|
|
541 if (s->data) {
|
|
542 v->len = s->len;
|
|
543 v->valid = 1;
|
340
|
544 v->no_cacheable = 0;
|
122
|
545 v->not_found = 0;
|
|
546 v->data = s->data;
|
|
547
|
|
548 } else {
|
|
549 v->not_found = 1;
|
|
550 }
|
|
551
|
|
552 return NGX_OK;
|
|
553 }
|
|
554
|
|
555
|
186
|
556 static void
|
324
|
557 ngx_http_variable_request_set(ngx_http_request_t *r,
|
|
558 ngx_http_variable_value_t *v, uintptr_t data)
|
|
559 {
|
|
560 ngx_str_t *s;
|
|
561
|
|
562 s = (ngx_str_t *) ((char *) r + data);
|
|
563
|
|
564 s->len = v->len;
|
|
565 s->data = v->data;
|
|
566 }
|
|
567
|
|
568
|
540
|
569 static ngx_int_t
|
|
570 ngx_http_variable_request_get_size(ngx_http_request_t *r,
|
|
571 ngx_http_variable_value_t *v, uintptr_t data)
|
|
572 {
|
|
573 size_t *sp;
|
|
574
|
|
575 sp = (size_t *) ((char *) r + data);
|
|
576
|
|
577 v->data = ngx_pnalloc(r->pool, NGX_SIZE_T_LEN);
|
|
578 if (v->data == NULL) {
|
|
579 return NGX_ERROR;
|
|
580 }
|
|
581
|
|
582 v->len = ngx_sprintf(v->data, "%uz", *sp) - v->data;
|
|
583 v->valid = 1;
|
|
584 v->no_cacheable = 0;
|
|
585 v->not_found = 0;
|
|
586
|
|
587 return NGX_OK;
|
|
588 }
|
|
589
|
|
590
|
324
|
591 static void
|
186
|
592 ngx_http_variable_request_set_size(ngx_http_request_t *r,
|
|
593 ngx_http_variable_value_t *v, uintptr_t data)
|
|
594 {
|
|
595 ssize_t s, *sp;
|
|
596 ngx_str_t val;
|
|
597
|
320
|
598 val.len = v->len;
|
186
|
599 val.data = v->data;
|
|
600
|
|
601 s = ngx_parse_size(&val);
|
|
602
|
|
603 if (s == NGX_ERROR) {
|
|
604 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
605 "invalid size \"%V\"", &val);
|
|
606 return;
|
|
607 }
|
|
608
|
|
609 sp = (ssize_t *) ((char *) r + data);
|
|
610
|
|
611 *sp = s;
|
|
612
|
|
613 return;
|
|
614 }
|
|
615
|
|
616
|
122
|
617 static ngx_int_t
|
|
618 ngx_http_variable_header(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
619 uintptr_t data)
|
64
|
620 {
|
122
|
621 ngx_table_elt_t *h;
|
|
622
|
|
623 h = *(ngx_table_elt_t **) ((char *) r + data);
|
|
624
|
|
625 if (h) {
|
|
626 v->len = h->value.len;
|
|
627 v->valid = 1;
|
340
|
628 v->no_cacheable = 0;
|
122
|
629 v->not_found = 0;
|
|
630 v->data = h->value.data;
|
|
631
|
|
632 } else {
|
|
633 v->not_found = 1;
|
|
634 }
|
|
635
|
|
636 return NGX_OK;
|
|
637 }
|
|
638
|
|
639
|
|
640 static ngx_int_t
|
|
641 ngx_http_variable_headers(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
642 uintptr_t data)
|
|
643 {
|
644
|
644 size_t len;
|
|
645 u_char *p, *end;
|
216
|
646 ngx_uint_t i, n;
|
122
|
647 ngx_array_t *a;
|
|
648 ngx_table_elt_t **h;
|
64
|
649
|
|
650 a = (ngx_array_t *) ((char *) r + data);
|
|
651
|
216
|
652 n = a->nelts;
|
644
|
653 h = a->elts;
|
|
654
|
|
655 len = 0;
|
|
656
|
|
657 for (i = 0; i < n; i++) {
|
|
658
|
|
659 if (h[i]->hash == 0) {
|
|
660 continue;
|
|
661 }
|
|
662
|
|
663 len += h[i]->value.len + sizeof("; ") - 1;
|
|
664 }
|
|
665
|
|
666 if (len == 0) {
|
122
|
667 v->not_found = 1;
|
|
668 return NGX_OK;
|
64
|
669 }
|
|
670
|
644
|
671 len -= sizeof("; ") - 1;
|
|
672
|
122
|
673 v->valid = 1;
|
340
|
674 v->no_cacheable = 0;
|
122
|
675 v->not_found = 0;
|
64
|
676
|
216
|
677 if (n == 1) {
|
122
|
678 v->len = (*h)->value.len;
|
|
679 v->data = (*h)->value.data;
|
64
|
680
|
122
|
681 return NGX_OK;
|
64
|
682 }
|
|
683
|
382
|
684 p = ngx_pnalloc(r->pool, len);
|
122
|
685 if (p == NULL) {
|
|
686 return NGX_ERROR;
|
|
687 }
|
|
688
|
|
689 v->len = len;
|
|
690 v->data = p;
|
64
|
691
|
644
|
692 end = p + len;
|
|
693
|
64
|
694 for (i = 0; /* void */ ; i++) {
|
644
|
695
|
|
696 if (h[i]->hash == 0) {
|
|
697 continue;
|
|
698 }
|
|
699
|
122
|
700 p = ngx_copy(p, h[i]->value.data, h[i]->value.len);
|
64
|
701
|
644
|
702 if (p == end) {
|
64
|
703 break;
|
|
704 }
|
|
705
|
|
706 *p++ = ';'; *p++ = ' ';
|
|
707 }
|
|
708
|
122
|
709 return NGX_OK;
|
64
|
710 }
|
|
711
|
|
712
|
122
|
713 static ngx_int_t
|
126
|
714 ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
|
|
715 ngx_http_variable_value_t *v, uintptr_t data)
|
|
716 {
|
|
717 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
|
|
718 &r->headers_in.headers.part,
|
|
719 sizeof("http_") - 1);
|
|
720 }
|
|
721
|
|
722
|
|
723 static ngx_int_t
|
|
724 ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
|
122
|
725 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
726 {
|
126
|
727 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
|
|
728 &r->headers_out.headers.part,
|
|
729 sizeof("sent_http_") - 1);
|
|
730 }
|
50
|
731
|
126
|
732
|
296
|
733 ngx_int_t
|
126
|
734 ngx_http_variable_unknown_header(ngx_http_variable_value_t *v, ngx_str_t *var,
|
|
735 ngx_list_part_t *part, size_t prefix)
|
|
736 {
|
122
|
737 u_char ch;
|
|
738 ngx_uint_t i, n;
|
|
739 ngx_table_elt_t *header;
|
48
|
740
|
|
741 header = part->elts;
|
|
742
|
|
743 for (i = 0; /* void */ ; i++) {
|
|
744
|
|
745 if (i >= part->nelts) {
|
|
746 if (part->next == NULL) {
|
|
747 break;
|
|
748 }
|
|
749
|
|
750 part = part->next;
|
|
751 header = part->elts;
|
|
752 i = 0;
|
|
753 }
|
|
754
|
644
|
755 if (header[i].hash == 0) {
|
|
756 continue;
|
|
757 }
|
|
758
|
126
|
759 for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) {
|
48
|
760 ch = header[i].key.data[n];
|
|
761
|
58
|
762 if (ch >= 'A' && ch <= 'Z') {
|
|
763 ch |= 0x20;
|
48
|
764
|
|
765 } else if (ch == '-') {
|
|
766 ch = '_';
|
|
767 }
|
|
768
|
126
|
769 if (var->data[n + prefix] != ch) {
|
48
|
770 break;
|
|
771 }
|
|
772 }
|
|
773
|
242
|
774 if (n + prefix == var->len && n == header[i].key.len) {
|
122
|
775 v->len = header[i].value.len;
|
|
776 v->valid = 1;
|
340
|
777 v->no_cacheable = 0;
|
122
|
778 v->not_found = 0;
|
|
779 v->data = header[i].value.data;
|
48
|
780
|
122
|
781 return NGX_OK;
|
48
|
782 }
|
|
783 }
|
|
784
|
122
|
785 v->not_found = 1;
|
|
786
|
|
787 return NGX_OK;
|
58
|
788 }
|
|
789
|
|
790
|
122
|
791 static ngx_int_t
|
528
|
792 ngx_http_variable_request_line(ngx_http_request_t *r,
|
|
793 ngx_http_variable_value_t *v, uintptr_t data)
|
|
794 {
|
|
795 u_char *p, *s;
|
|
796
|
|
797 s = r->request_line.data;
|
|
798
|
|
799 if (s == NULL) {
|
|
800 s = r->request_start;
|
|
801
|
|
802 if (s == NULL) {
|
|
803 v->not_found = 1;
|
|
804 return NGX_OK;
|
|
805 }
|
|
806
|
|
807 for (p = s; p < r->header_in->last; p++) {
|
|
808 if (*p == CR || *p == LF) {
|
|
809 break;
|
|
810 }
|
|
811 }
|
|
812
|
|
813 r->request_line.len = p - s;
|
|
814 r->request_line.data = s;
|
|
815 }
|
|
816
|
|
817 v->len = r->request_line.len;
|
|
818 v->valid = 1;
|
|
819 v->no_cacheable = 0;
|
|
820 v->not_found = 0;
|
|
821 v->data = s;
|
|
822
|
|
823 return NGX_OK;
|
|
824 }
|
|
825
|
|
826
|
|
827 static ngx_int_t
|
420
|
828 ngx_http_variable_cookie(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
829 uintptr_t data)
|
|
830 {
|
|
831 ngx_str_t *name = (ngx_str_t *) data;
|
|
832
|
|
833 ngx_str_t cookie, s;
|
|
834
|
|
835 s.len = name->len - (sizeof("cookie_") - 1);
|
|
836 s.data = name->data + sizeof("cookie_") - 1;
|
|
837
|
|
838 if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &s, &cookie)
|
|
839 == NGX_DECLINED)
|
|
840 {
|
|
841 v->not_found = 1;
|
|
842 return NGX_OK;
|
|
843 }
|
|
844
|
|
845 v->len = cookie.len;
|
|
846 v->valid = 1;
|
|
847 v->no_cacheable = 0;
|
|
848 v->not_found = 0;
|
|
849 v->data = cookie.data;
|
|
850
|
|
851 return NGX_OK;
|
|
852 }
|
|
853
|
|
854
|
|
855 static ngx_int_t
|
392
|
856 ngx_http_variable_argument(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
857 uintptr_t data)
|
|
858 {
|
|
859 ngx_str_t *name = (ngx_str_t *) data;
|
|
860
|
432
|
861 u_char *arg;
|
|
862 size_t len;
|
|
863 ngx_str_t value;
|
392
|
864
|
432
|
865 len = name->len - (sizeof("arg_") - 1);
|
|
866 arg = name->data + sizeof("arg_") - 1;
|
|
867
|
|
868 if (ngx_http_arg(r, arg, len, &value) != NGX_OK) {
|
392
|
869 v->not_found = 1;
|
|
870 return NGX_OK;
|
|
871 }
|
|
872
|
432
|
873 v->data = value.data;
|
|
874 v->len = value.len;
|
|
875 v->valid = 1;
|
|
876 v->no_cacheable = 0;
|
|
877 v->not_found = 0;
|
392
|
878
|
|
879 return NGX_OK;
|
|
880 }
|
|
881
|
|
882
|
|
883 static ngx_int_t
|
122
|
884 ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
885 uintptr_t data)
|
58
|
886 {
|
362
|
887 ngx_http_core_srv_conf_t *cscf;
|
|
888
|
376
|
889 if (r->headers_in.server.len) {
|
|
890 v->len = r->headers_in.server.len;
|
|
891 v->data = r->headers_in.server.data;
|
270
|
892
|
|
893 } else {
|
376
|
894 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
895
|
|
896 v->len = cscf->server_name.len;
|
|
897 v->data = cscf->server_name.data;
|
58
|
898 }
|
|
899
|
122
|
900 v->valid = 1;
|
340
|
901 v->no_cacheable = 0;
|
122
|
902 v->not_found = 0;
|
58
|
903
|
122
|
904 return NGX_OK;
|
48
|
905 }
|
|
906
|
|
907
|
122
|
908 static ngx_int_t
|
272
|
909 ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
|
|
910 ngx_http_variable_value_t *v, uintptr_t data)
|
|
911 {
|
448
|
912 struct sockaddr_in *sin;
|
|
913 #if (NGX_HAVE_INET6)
|
|
914 struct sockaddr_in6 *sin6;
|
|
915 #endif
|
272
|
916
|
448
|
917 switch (r->connection->sockaddr->sa_family) {
|
272
|
918
|
448
|
919 #if (NGX_HAVE_INET6)
|
|
920 case AF_INET6:
|
|
921 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
|
272
|
922
|
448
|
923 v->len = sizeof(struct in6_addr);
|
|
924 v->valid = 1;
|
|
925 v->no_cacheable = 0;
|
|
926 v->not_found = 0;
|
540
|
927 v->data = sin6->sin6_addr.s6_addr;
|
448
|
928
|
|
929 break;
|
|
930 #endif
|
|
931
|
|
932 default: /* AF_INET */
|
|
933 sin = (struct sockaddr_in *) r->connection->sockaddr;
|
|
934
|
|
935 v->len = sizeof(in_addr_t);
|
|
936 v->valid = 1;
|
|
937 v->no_cacheable = 0;
|
|
938 v->not_found = 0;
|
|
939 v->data = (u_char *) &sin->sin_addr;
|
|
940
|
|
941 break;
|
|
942 }
|
272
|
943
|
|
944 return NGX_OK;
|
|
945 }
|
|
946
|
|
947
|
|
948 static ngx_int_t
|
122
|
949 ngx_http_variable_remote_addr(ngx_http_request_t *r,
|
|
950 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
951 {
|
122
|
952 v->len = r->connection->addr_text.len;
|
|
953 v->valid = 1;
|
340
|
954 v->no_cacheable = 0;
|
122
|
955 v->not_found = 0;
|
|
956 v->data = r->connection->addr_text.data;
|
48
|
957
|
122
|
958 return NGX_OK;
|
48
|
959 }
|
|
960
|
|
961
|
122
|
962 static ngx_int_t
|
|
963 ngx_http_variable_remote_port(ngx_http_request_t *r,
|
|
964 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
965 {
|
448
|
966 ngx_uint_t port;
|
|
967 struct sockaddr_in *sin;
|
|
968 #if (NGX_HAVE_INET6)
|
|
969 struct sockaddr_in6 *sin6;
|
|
970 #endif
|
58
|
971
|
122
|
972 v->len = 0;
|
|
973 v->valid = 1;
|
340
|
974 v->no_cacheable = 0;
|
122
|
975 v->not_found = 0;
|
58
|
976
|
382
|
977 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
|
122
|
978 if (v->data == NULL) {
|
|
979 return NGX_ERROR;
|
58
|
980 }
|
|
981
|
448
|
982 switch (r->connection->sockaddr->sa_family) {
|
126
|
983
|
448
|
984 #if (NGX_HAVE_INET6)
|
|
985 case AF_INET6:
|
|
986 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
|
|
987 port = ntohs(sin6->sin6_port);
|
|
988 break;
|
|
989 #endif
|
126
|
990
|
448
|
991 default: /* AF_INET */
|
|
992 sin = (struct sockaddr_in *) r->connection->sockaddr;
|
58
|
993 port = ntohs(sin->sin_port);
|
448
|
994 break;
|
|
995 }
|
126
|
996
|
448
|
997 if (port > 0 && port < 65536) {
|
|
998 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
|
58
|
999 }
|
|
1000
|
122
|
1001 return NGX_OK;
|
58
|
1002 }
|
|
1003
|
|
1004
|
122
|
1005 static ngx_int_t
|
|
1006 ngx_http_variable_server_addr(ngx_http_request_t *r,
|
|
1007 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1008 {
|
362
|
1009 ngx_str_t s;
|
448
|
1010 u_char addr[NGX_SOCKADDR_STRLEN];
|
48
|
1011
|
448
|
1012 s.len = NGX_SOCKADDR_STRLEN;
|
|
1013 s.data = addr;
|
|
1014
|
492
|
1015 if (ngx_connection_local_sockaddr(r->connection, &s, 0) != NGX_OK) {
|
448
|
1016 return NGX_ERROR;
|
|
1017 }
|
|
1018
|
|
1019 s.data = ngx_pnalloc(r->pool, s.len);
|
362
|
1020 if (s.data == NULL) {
|
122
|
1021 return NGX_ERROR;
|
58
|
1022 }
|
|
1023
|
448
|
1024 ngx_memcpy(s.data, addr, s.len);
|
58
|
1025
|
362
|
1026 v->len = s.len;
|
122
|
1027 v->valid = 1;
|
340
|
1028 v->no_cacheable = 0;
|
122
|
1029 v->not_found = 0;
|
362
|
1030 v->data = s.data;
|
48
|
1031
|
122
|
1032 return NGX_OK;
|
48
|
1033 }
|
|
1034
|
|
1035
|
122
|
1036 static ngx_int_t
|
|
1037 ngx_http_variable_server_port(ngx_http_request_t *r,
|
|
1038 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1039 {
|
454
|
1040 ngx_uint_t port;
|
|
1041 struct sockaddr_in *sin;
|
|
1042 #if (NGX_HAVE_INET6)
|
|
1043 struct sockaddr_in6 *sin6;
|
|
1044 #endif
|
|
1045
|
|
1046 v->len = 0;
|
122
|
1047 v->valid = 1;
|
340
|
1048 v->no_cacheable = 0;
|
122
|
1049 v->not_found = 0;
|
454
|
1050
|
492
|
1051 if (ngx_connection_local_sockaddr(r->connection, NULL, 0) != NGX_OK) {
|
454
|
1052 return NGX_ERROR;
|
|
1053 }
|
|
1054
|
|
1055 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
|
|
1056 if (v->data == NULL) {
|
|
1057 return NGX_ERROR;
|
|
1058 }
|
|
1059
|
|
1060 switch (r->connection->local_sockaddr->sa_family) {
|
|
1061
|
|
1062 #if (NGX_HAVE_INET6)
|
|
1063 case AF_INET6:
|
|
1064 sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
|
|
1065 port = ntohs(sin6->sin6_port);
|
|
1066 break;
|
|
1067 #endif
|
|
1068
|
|
1069 default: /* AF_INET */
|
|
1070 sin = (struct sockaddr_in *) r->connection->local_sockaddr;
|
|
1071 port = ntohs(sin->sin_port);
|
|
1072 break;
|
|
1073 }
|
|
1074
|
|
1075 if (port > 0 && port < 65536) {
|
|
1076 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
|
|
1077 }
|
58
|
1078
|
122
|
1079 return NGX_OK;
|
58
|
1080 }
|
|
1081
|
|
1082
|
122
|
1083 static ngx_int_t
|
238
|
1084 ngx_http_variable_scheme(ngx_http_request_t *r,
|
|
1085 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1086 {
|
|
1087 #if (NGX_HTTP_SSL)
|
|
1088
|
|
1089 if (r->connection->ssl) {
|
|
1090 v->len = sizeof("https") - 1;
|
|
1091 v->valid = 1;
|
340
|
1092 v->no_cacheable = 0;
|
238
|
1093 v->not_found = 0;
|
|
1094 v->data = (u_char *) "https";
|
|
1095
|
|
1096 return NGX_OK;
|
|
1097 }
|
|
1098
|
|
1099 #endif
|
|
1100
|
|
1101 v->len = sizeof("http") - 1;
|
|
1102 v->valid = 1;
|
340
|
1103 v->no_cacheable = 0;
|
238
|
1104 v->not_found = 0;
|
|
1105 v->data = (u_char *) "http";
|
|
1106
|
|
1107 return NGX_OK;
|
|
1108 }
|
|
1109
|
|
1110
|
|
1111 static ngx_int_t
|
324
|
1112 ngx_http_variable_is_args(ngx_http_request_t *r,
|
|
1113 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1114 {
|
|
1115 v->valid = 1;
|
340
|
1116 v->no_cacheable = 0;
|
324
|
1117 v->not_found = 0;
|
|
1118
|
|
1119 if (r->args.len == 0) {
|
|
1120 v->len = 0;
|
|
1121 v->data = NULL;
|
|
1122 return NGX_OK;
|
|
1123 }
|
|
1124
|
|
1125 v->len = 1;
|
|
1126 v->data = (u_char *) "?";
|
|
1127
|
|
1128 return NGX_OK;
|
|
1129 }
|
|
1130
|
|
1131
|
|
1132 static ngx_int_t
|
122
|
1133 ngx_http_variable_document_root(ngx_http_request_t *r,
|
|
1134 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
1135 {
|
220
|
1136 ngx_str_t path;
|
122
|
1137 ngx_http_core_loc_conf_t *clcf;
|
58
|
1138
|
|
1139 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1140
|
220
|
1141 if (clcf->root_lengths == NULL) {
|
|
1142 v->len = clcf->root.len;
|
|
1143 v->valid = 1;
|
340
|
1144 v->no_cacheable = 0;
|
220
|
1145 v->not_found = 0;
|
|
1146 v->data = clcf->root.data;
|
|
1147
|
|
1148 } else {
|
|
1149 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 0,
|
|
1150 clcf->root_values->elts)
|
|
1151 == NULL)
|
|
1152 {
|
|
1153 return NGX_ERROR;
|
|
1154 }
|
|
1155
|
454
|
1156 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path, 0) != NGX_OK) {
|
220
|
1157 return NGX_ERROR;
|
|
1158 }
|
|
1159
|
|
1160 v->len = path.len;
|
|
1161 v->valid = 1;
|
340
|
1162 v->no_cacheable = 0;
|
220
|
1163 v->not_found = 0;
|
|
1164 v->data = path.data;
|
|
1165 }
|
58
|
1166
|
122
|
1167 return NGX_OK;
|
58
|
1168 }
|
|
1169
|
|
1170
|
122
|
1171 static ngx_int_t
|
412
|
1172 ngx_http_variable_realpath_root(ngx_http_request_t *r,
|
|
1173 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1174 {
|
|
1175 size_t len;
|
|
1176 ngx_str_t path;
|
|
1177 ngx_http_core_loc_conf_t *clcf;
|
|
1178 u_char real[NGX_MAX_PATH];
|
|
1179
|
|
1180 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1181
|
|
1182 if (clcf->root_lengths == NULL) {
|
|
1183 path = clcf->root;
|
|
1184
|
|
1185 } else {
|
|
1186 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 1,
|
|
1187 clcf->root_values->elts)
|
|
1188 == NULL)
|
|
1189 {
|
|
1190 return NGX_ERROR;
|
|
1191 }
|
|
1192
|
|
1193 path.data[path.len - 1] = '\0';
|
|
1194
|
454
|
1195 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path, 0) != NGX_OK) {
|
412
|
1196 return NGX_ERROR;
|
|
1197 }
|
|
1198 }
|
|
1199
|
|
1200 if (ngx_realpath(path.data, real) == NULL) {
|
|
1201 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
1202 ngx_realpath_n " \"%s\" failed", path.data);
|
|
1203 return NGX_ERROR;
|
|
1204 }
|
|
1205
|
|
1206 len = ngx_strlen(real);
|
|
1207
|
|
1208 v->data = ngx_pnalloc(r->pool, len);
|
|
1209 if (v->data == NULL) {
|
|
1210 return NGX_ERROR;
|
|
1211 }
|
|
1212
|
|
1213 v->len = len;
|
|
1214 v->valid = 1;
|
|
1215 v->no_cacheable = 0;
|
|
1216 v->not_found = 0;
|
|
1217
|
|
1218 ngx_memcpy(v->data, real, len);
|
|
1219
|
|
1220 return NGX_OK;
|
|
1221 }
|
|
1222
|
|
1223
|
|
1224 static ngx_int_t
|
122
|
1225 ngx_http_variable_request_filename(ngx_http_request_t *r,
|
|
1226 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1227 {
|
248
|
1228 size_t root;
|
122
|
1229 ngx_str_t path;
|
48
|
1230
|
248
|
1231 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
|
122
|
1232 return NGX_ERROR;
|
106
|
1233 }
|
58
|
1234
|
106
|
1235 /* ngx_http_map_uri_to_path() allocates memory for terminating '\0' */
|
58
|
1236
|
122
|
1237 v->len = path.len - 1;
|
|
1238 v->valid = 1;
|
340
|
1239 v->no_cacheable = 0;
|
122
|
1240 v->not_found = 0;
|
|
1241 v->data = path.data;
|
48
|
1242
|
122
|
1243 return NGX_OK;
|
48
|
1244 }
|
|
1245
|
|
1246
|
122
|
1247 static ngx_int_t
|
362
|
1248 ngx_http_variable_server_name(ngx_http_request_t *r,
|
|
1249 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1250 {
|
|
1251 ngx_http_core_srv_conf_t *cscf;
|
|
1252
|
|
1253 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1254
|
|
1255 v->len = cscf->server_name.len;
|
|
1256 v->valid = 1;
|
|
1257 v->no_cacheable = 0;
|
|
1258 v->not_found = 0;
|
|
1259 v->data = cscf->server_name.data;
|
|
1260
|
|
1261 return NGX_OK;
|
|
1262 }
|
|
1263
|
|
1264
|
|
1265 static ngx_int_t
|
122
|
1266 ngx_http_variable_request_method(ngx_http_request_t *r,
|
|
1267 ngx_http_variable_value_t *v, uintptr_t data)
|
110
|
1268 {
|
196
|
1269 if (r->main->method_name.data) {
|
|
1270 v->len = r->main->method_name.len;
|
122
|
1271 v->valid = 1;
|
340
|
1272 v->no_cacheable = 0;
|
122
|
1273 v->not_found = 0;
|
196
|
1274 v->data = r->main->method_name.data;
|
122
|
1275
|
|
1276 } else {
|
|
1277 v->not_found = 1;
|
110
|
1278 }
|
|
1279
|
122
|
1280 return NGX_OK;
|
110
|
1281 }
|
|
1282
|
|
1283
|
122
|
1284 static ngx_int_t
|
|
1285 ngx_http_variable_remote_user(ngx_http_request_t *r,
|
|
1286 ngx_http_variable_value_t *v, uintptr_t data)
|
88
|
1287 {
|
122
|
1288 ngx_int_t rc;
|
88
|
1289
|
|
1290 rc = ngx_http_auth_basic_user(r);
|
|
1291
|
|
1292 if (rc == NGX_DECLINED) {
|
122
|
1293 v->not_found = 1;
|
|
1294 return NGX_OK;
|
88
|
1295 }
|
|
1296
|
|
1297 if (rc == NGX_ERROR) {
|
122
|
1298 return NGX_ERROR;
|
88
|
1299 }
|
|
1300
|
122
|
1301 v->len = r->headers_in.user.len;
|
|
1302 v->valid = 1;
|
340
|
1303 v->no_cacheable = 0;
|
122
|
1304 v->not_found = 0;
|
|
1305 v->data = r->headers_in.user.data;
|
120
|
1306
|
122
|
1307 return NGX_OK;
|
120
|
1308 }
|
|
1309
|
|
1310
|
160
|
1311 static ngx_int_t
|
|
1312 ngx_http_variable_body_bytes_sent(ngx_http_request_t *r,
|
|
1313 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1314 {
|
|
1315 off_t sent;
|
|
1316 u_char *p;
|
|
1317
|
|
1318 sent = r->connection->sent - r->header_size;
|
|
1319
|
|
1320 if (sent < 0) {
|
|
1321 sent = 0;
|
|
1322 }
|
|
1323
|
382
|
1324 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);
|
160
|
1325 if (p == NULL) {
|
|
1326 return NGX_ERROR;
|
|
1327 }
|
|
1328
|
|
1329 v->len = ngx_sprintf(p, "%O", sent) - p;
|
|
1330 v->valid = 1;
|
340
|
1331 v->no_cacheable = 0;
|
160
|
1332 v->not_found = 0;
|
|
1333 v->data = p;
|
|
1334
|
|
1335 return NGX_OK;
|
|
1336 }
|
|
1337
|
|
1338
|
178
|
1339 static ngx_int_t
|
190
|
1340 ngx_http_variable_sent_content_type(ngx_http_request_t *r,
|
|
1341 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1342 {
|
|
1343 if (r->headers_out.content_type.len) {
|
|
1344 v->len = r->headers_out.content_type.len;
|
|
1345 v->valid = 1;
|
340
|
1346 v->no_cacheable = 0;
|
190
|
1347 v->not_found = 0;
|
|
1348 v->data = r->headers_out.content_type.data;
|
|
1349
|
|
1350 } else {
|
|
1351 v->not_found = 1;
|
|
1352 }
|
|
1353
|
|
1354 return NGX_OK;
|
|
1355 }
|
|
1356
|
|
1357
|
|
1358 static ngx_int_t
|
|
1359 ngx_http_variable_sent_content_length(ngx_http_request_t *r,
|
|
1360 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1361 {
|
|
1362 u_char *p;
|
|
1363
|
|
1364 if (r->headers_out.content_length) {
|
|
1365 v->len = r->headers_out.content_length->value.len;
|
|
1366 v->valid = 1;
|
340
|
1367 v->no_cacheable = 0;
|
190
|
1368 v->not_found = 0;
|
|
1369 v->data = r->headers_out.content_length->value.data;
|
|
1370
|
|
1371 return NGX_OK;
|
|
1372 }
|
|
1373
|
|
1374 if (r->headers_out.content_length_n >= 0) {
|
382
|
1375 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);
|
190
|
1376 if (p == NULL) {
|
|
1377 return NGX_ERROR;
|
|
1378 }
|
|
1379
|
|
1380 v->len = ngx_sprintf(p, "%O", r->headers_out.content_length_n) - p;
|
|
1381 v->valid = 1;
|
340
|
1382 v->no_cacheable = 0;
|
190
|
1383 v->not_found = 0;
|
|
1384 v->data = p;
|
|
1385
|
|
1386 return NGX_OK;
|
|
1387 }
|
|
1388
|
|
1389 v->not_found = 1;
|
|
1390
|
|
1391 return NGX_OK;
|
|
1392 }
|
|
1393
|
|
1394
|
|
1395 static ngx_int_t
|
442
|
1396 ngx_http_variable_sent_location(ngx_http_request_t *r,
|
|
1397 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1398 {
|
454
|
1399 ngx_str_t name;
|
|
1400
|
442
|
1401 if (r->headers_out.location) {
|
|
1402 v->len = r->headers_out.location->value.len;
|
|
1403 v->valid = 1;
|
|
1404 v->no_cacheable = 0;
|
|
1405 v->not_found = 0;
|
|
1406 v->data = r->headers_out.location->value.data;
|
|
1407
|
|
1408 return NGX_OK;
|
|
1409 }
|
|
1410
|
570
|
1411 ngx_str_set(&name, "sent_http_location");
|
454
|
1412
|
|
1413 return ngx_http_variable_unknown_header(v, &name,
|
442
|
1414 &r->headers_out.headers.part,
|
|
1415 sizeof("sent_http_") - 1);
|
|
1416 }
|
|
1417
|
|
1418
|
|
1419 static ngx_int_t
|
190
|
1420 ngx_http_variable_sent_last_modified(ngx_http_request_t *r,
|
|
1421 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1422 {
|
|
1423 u_char *p;
|
|
1424
|
|
1425 if (r->headers_out.last_modified) {
|
|
1426 v->len = r->headers_out.last_modified->value.len;
|
|
1427 v->valid = 1;
|
340
|
1428 v->no_cacheable = 0;
|
190
|
1429 v->not_found = 0;
|
|
1430 v->data = r->headers_out.last_modified->value.data;
|
|
1431
|
|
1432 return NGX_OK;
|
|
1433 }
|
|
1434
|
|
1435 if (r->headers_out.last_modified_time >= 0) {
|
382
|
1436 p = ngx_pnalloc(r->pool,
|
190
|
1437 sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT") - 1);
|
|
1438 if (p == NULL) {
|
|
1439 return NGX_ERROR;
|
|
1440 }
|
|
1441
|
|
1442 v->len = ngx_http_time(p, r->headers_out.last_modified_time) - p;
|
|
1443 v->valid = 1;
|
340
|
1444 v->no_cacheable = 0;
|
190
|
1445 v->not_found = 0;
|
|
1446 v->data = p;
|
|
1447
|
|
1448 return NGX_OK;
|
|
1449 }
|
|
1450
|
|
1451 v->not_found = 1;
|
|
1452
|
|
1453 return NGX_OK;
|
|
1454 }
|
|
1455
|
|
1456
|
|
1457 static ngx_int_t
|
|
1458 ngx_http_variable_sent_connection(ngx_http_request_t *r,
|
|
1459 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1460 {
|
|
1461 size_t len;
|
|
1462 char *p;
|
|
1463
|
|
1464 if (r->keepalive) {
|
|
1465 len = sizeof("keep-alive") - 1;
|
|
1466 p = "keep-alive";
|
|
1467
|
|
1468 } else {
|
|
1469 len = sizeof("close") - 1;
|
|
1470 p = "close";
|
|
1471 }
|
|
1472
|
|
1473 v->len = len;
|
|
1474 v->valid = 1;
|
340
|
1475 v->no_cacheable = 0;
|
190
|
1476 v->not_found = 0;
|
|
1477 v->data = (u_char *) p;
|
|
1478
|
|
1479 return NGX_OK;
|
|
1480 }
|
|
1481
|
|
1482
|
|
1483 static ngx_int_t
|
|
1484 ngx_http_variable_sent_keep_alive(ngx_http_request_t *r,
|
|
1485 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1486 {
|
|
1487 u_char *p;
|
|
1488 ngx_http_core_loc_conf_t *clcf;
|
|
1489
|
|
1490 if (r->keepalive) {
|
|
1491 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1492
|
|
1493 if (clcf->keepalive_header) {
|
|
1494
|
382
|
1495 p = ngx_pnalloc(r->pool, sizeof("timeout=") - 1 + NGX_TIME_T_LEN);
|
190
|
1496 if (p == NULL) {
|
|
1497 return NGX_ERROR;
|
|
1498 }
|
|
1499
|
|
1500 v->len = ngx_sprintf(p, "timeout=%T", clcf->keepalive_header) - p;
|
|
1501 v->valid = 1;
|
340
|
1502 v->no_cacheable = 0;
|
190
|
1503 v->not_found = 0;
|
|
1504 v->data = p;
|
|
1505
|
|
1506 return NGX_OK;
|
|
1507 }
|
|
1508 }
|
|
1509
|
|
1510 v->not_found = 1;
|
|
1511
|
|
1512 return NGX_OK;
|
|
1513 }
|
|
1514
|
|
1515
|
|
1516 static ngx_int_t
|
|
1517 ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
|
|
1518 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1519 {
|
|
1520 if (r->chunked) {
|
|
1521 v->len = sizeof("chunked") - 1;
|
|
1522 v->valid = 1;
|
340
|
1523 v->no_cacheable = 0;
|
190
|
1524 v->not_found = 0;
|
|
1525 v->data = (u_char *) "chunked";
|
|
1526
|
|
1527 } else {
|
|
1528 v->not_found = 1;
|
|
1529 }
|
|
1530
|
|
1531 return NGX_OK;
|
|
1532 }
|
|
1533
|
|
1534
|
|
1535 static ngx_int_t
|
178
|
1536 ngx_http_variable_request_completion(ngx_http_request_t *r,
|
|
1537 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1538 {
|
|
1539 if (r->request_complete) {
|
|
1540 v->len = 2;
|
|
1541 v->valid = 1;
|
340
|
1542 v->no_cacheable = 0;
|
178
|
1543 v->not_found = 0;
|
|
1544 v->data = (u_char *) "OK";
|
|
1545
|
|
1546 return NGX_OK;
|
|
1547 }
|
|
1548
|
|
1549 v->len = 0;
|
|
1550 v->valid = 1;
|
340
|
1551 v->no_cacheable = 0;
|
178
|
1552 v->not_found = 0;
|
|
1553 v->data = (u_char *) "";
|
|
1554
|
|
1555 return NGX_OK;
|
|
1556 }
|
|
1557
|
|
1558
|
244
|
1559 static ngx_int_t
|
492
|
1560 ngx_http_variable_request_body(ngx_http_request_t *r,
|
|
1561 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1562 {
|
|
1563 u_char *p;
|
|
1564 size_t len;
|
|
1565 ngx_buf_t *buf, *next;
|
|
1566 ngx_chain_t *cl;
|
|
1567
|
494
|
1568 if (r->request_body == NULL
|
|
1569 || r->request_body->bufs == NULL
|
|
1570 || r->request_body->temp_file)
|
|
1571 {
|
492
|
1572 v->not_found = 1;
|
|
1573
|
|
1574 return NGX_OK;
|
|
1575 }
|
|
1576
|
|
1577 cl = r->request_body->bufs;
|
|
1578 buf = cl->buf;
|
|
1579
|
|
1580 if (cl->next == NULL) {
|
|
1581 v->len = buf->last - buf->pos;
|
|
1582 v->valid = 1;
|
|
1583 v->no_cacheable = 0;
|
|
1584 v->not_found = 0;
|
|
1585 v->data = buf->pos;
|
|
1586
|
|
1587 return NGX_OK;
|
|
1588 }
|
|
1589
|
|
1590 next = cl->next->buf;
|
|
1591 len = (buf->last - buf->pos) + (next->last - next->pos);
|
|
1592
|
|
1593 p = ngx_pnalloc(r->pool, len);
|
|
1594 if (p == NULL) {
|
|
1595 return NGX_ERROR;
|
|
1596 }
|
|
1597
|
|
1598 v->data = p;
|
|
1599
|
|
1600 p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
|
|
1601 ngx_memcpy(p, next->pos, next->last - next->pos);
|
|
1602
|
|
1603 v->len = len;
|
|
1604 v->valid = 1;
|
|
1605 v->no_cacheable = 0;
|
|
1606 v->not_found = 0;
|
|
1607
|
|
1608 return NGX_OK;
|
|
1609 }
|
|
1610
|
|
1611
|
|
1612 static ngx_int_t
|
244
|
1613 ngx_http_variable_request_body_file(ngx_http_request_t *r,
|
|
1614 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1615 {
|
|
1616 if (r->request_body == NULL || r->request_body->temp_file == NULL) {
|
246
|
1617 v->not_found = 1;
|
244
|
1618
|
|
1619 return NGX_OK;
|
|
1620 }
|
|
1621
|
|
1622 v->len = r->request_body->temp_file->file.name.len;
|
|
1623 v->valid = 1;
|
340
|
1624 v->no_cacheable = 0;
|
244
|
1625 v->not_found = 0;
|
|
1626 v->data = r->request_body->temp_file->file.name.data;
|
|
1627
|
|
1628 return NGX_OK;
|
|
1629 }
|
|
1630
|
|
1631
|
322
|
1632 static ngx_int_t
|
|
1633 ngx_http_variable_nginx_version(ngx_http_request_t *r,
|
|
1634 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1635 {
|
|
1636 v->len = sizeof(NGINX_VERSION) - 1;
|
|
1637 v->valid = 1;
|
340
|
1638 v->no_cacheable = 0;
|
322
|
1639 v->not_found = 0;
|
|
1640 v->data = (u_char *) NGINX_VERSION;
|
|
1641
|
|
1642 return NGX_OK;
|
|
1643 }
|
|
1644
|
|
1645
|
376
|
1646 static ngx_int_t
|
|
1647 ngx_http_variable_hostname(ngx_http_request_t *r,
|
|
1648 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1649 {
|
|
1650 v->len = ngx_cycle->hostname.len;
|
|
1651 v->valid = 1;
|
|
1652 v->no_cacheable = 0;
|
|
1653 v->not_found = 0;
|
|
1654 v->data = ngx_cycle->hostname.data;
|
|
1655
|
|
1656 return NGX_OK;
|
|
1657 }
|
|
1658
|
|
1659
|
410
|
1660 static ngx_int_t
|
|
1661 ngx_http_variable_pid(ngx_http_request_t *r,
|
|
1662 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1663 {
|
|
1664 u_char *p;
|
|
1665
|
|
1666 p = ngx_pnalloc(r->pool, NGX_INT64_LEN);
|
|
1667 if (p == NULL) {
|
|
1668 return NGX_ERROR;
|
|
1669 }
|
|
1670
|
|
1671 v->len = ngx_sprintf(p, "%P", ngx_pid) - p;
|
|
1672 v->valid = 1;
|
|
1673 v->no_cacheable = 0;
|
|
1674 v->not_found = 0;
|
|
1675 v->data = p;
|
|
1676
|
|
1677 return NGX_OK;
|
|
1678 }
|
|
1679
|
|
1680
|
616
|
1681 void *
|
628
|
1682 ngx_http_map_find(ngx_http_request_t *r, ngx_http_map_t *map, ngx_str_t *match)
|
616
|
1683 {
|
628
|
1684 void *value;
|
|
1685 u_char *low;
|
|
1686 size_t len;
|
|
1687 ngx_uint_t key;
|
|
1688
|
|
1689 len = match->len;
|
|
1690
|
|
1691 if (len) {
|
|
1692 low = ngx_pnalloc(r->pool, len);
|
|
1693 if (low == NULL) {
|
|
1694 return NULL;
|
|
1695 }
|
|
1696
|
|
1697 } else {
|
|
1698 low = NULL;
|
|
1699 }
|
|
1700
|
|
1701 key = ngx_hash_strlow(low, match->data, len);
|
|
1702
|
|
1703 value = ngx_hash_find_combined(&map->hash, key, low, len);
|
|
1704 if (value) {
|
|
1705 return value;
|
616
|
1706 }
|
|
1707
|
|
1708 #if (NGX_PCRE)
|
|
1709
|
|
1710 if (len && map->nregex) {
|
|
1711 ngx_int_t n;
|
|
1712 ngx_uint_t i;
|
|
1713 ngx_http_map_regex_t *reg;
|
|
1714
|
|
1715 reg = map->regex;
|
|
1716
|
|
1717 for (i = 0; i < map->nregex; i++) {
|
|
1718
|
|
1719 n = ngx_http_regex_exec(r, reg[i].regex, match);
|
|
1720
|
|
1721 if (n == NGX_OK) {
|
|
1722 return reg[i].value;
|
|
1723 }
|
|
1724
|
|
1725 if (n == NGX_DECLINED) {
|
|
1726 continue;
|
|
1727 }
|
|
1728
|
|
1729 /* NGX_ERROR */
|
|
1730
|
|
1731 return NULL;
|
|
1732 }
|
|
1733 }
|
|
1734
|
|
1735 #endif
|
|
1736
|
|
1737 return NULL;
|
|
1738 }
|
|
1739
|
|
1740
|
552
|
1741 #if (NGX_PCRE)
|
|
1742
|
546
|
1743 static ngx_int_t
|
|
1744 ngx_http_variable_not_found(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
1745 uintptr_t data)
|
|
1746 {
|
|
1747 v->not_found = 1;
|
|
1748 return NGX_OK;
|
|
1749 }
|
|
1750
|
|
1751
|
|
1752 ngx_http_regex_t *
|
|
1753 ngx_http_regex_compile(ngx_conf_t *cf, ngx_regex_compile_t *rc)
|
|
1754 {
|
|
1755 u_char *p;
|
|
1756 size_t size;
|
|
1757 ngx_str_t name;
|
|
1758 ngx_uint_t i, n;
|
|
1759 ngx_http_variable_t *v;
|
|
1760 ngx_http_regex_t *re;
|
|
1761 ngx_http_regex_variable_t *rv;
|
|
1762 ngx_http_core_main_conf_t *cmcf;
|
|
1763
|
|
1764 rc->pool = cf->pool;
|
|
1765
|
|
1766 if (ngx_regex_compile(rc) != NGX_OK) {
|
|
1767 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc->err);
|
|
1768 return NULL;
|
|
1769 }
|
|
1770
|
|
1771 re = ngx_pcalloc(cf->pool, sizeof(ngx_http_regex_t));
|
|
1772 if (re == NULL) {
|
|
1773 return NULL;
|
|
1774 }
|
|
1775
|
|
1776 re->regex = rc->regex;
|
|
1777 re->ncaptures = rc->captures;
|
|
1778
|
|
1779 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1780 cmcf->ncaptures = ngx_max(cmcf->ncaptures, re->ncaptures);
|
|
1781
|
|
1782 n = (ngx_uint_t) rc->named_captures;
|
|
1783
|
|
1784 if (n == 0) {
|
|
1785 return re;
|
|
1786 }
|
|
1787
|
|
1788 rv = ngx_palloc(rc->pool, n * sizeof(ngx_http_regex_variable_t));
|
|
1789 if (rv == NULL) {
|
|
1790 return NULL;
|
|
1791 }
|
|
1792
|
|
1793 re->variables = rv;
|
|
1794 re->nvariables = n;
|
|
1795 re->name = rc->pattern;
|
|
1796
|
|
1797 size = rc->name_size;
|
|
1798 p = rc->names;
|
|
1799
|
|
1800 for (i = 0; i < n; i++) {
|
|
1801 rv[i].capture = 2 * ((p[0] << 8) + p[1]);
|
|
1802
|
|
1803 name.data = &p[2];
|
|
1804 name.len = ngx_strlen(name.data);
|
|
1805
|
|
1806 v = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
|
|
1807 if (v == NULL) {
|
|
1808 return NULL;
|
|
1809 }
|
|
1810
|
|
1811 rv[i].index = ngx_http_get_variable_index(cf, &name);
|
|
1812 if (rv[i].index == NGX_ERROR) {
|
|
1813 return NULL;
|
|
1814 }
|
|
1815
|
|
1816 v->get_handler = ngx_http_variable_not_found;
|
|
1817
|
560
|
1818 p += size;
|
546
|
1819 }
|
|
1820
|
|
1821 return re;
|
|
1822 }
|
|
1823
|
|
1824
|
|
1825 ngx_int_t
|
|
1826 ngx_http_regex_exec(ngx_http_request_t *r, ngx_http_regex_t *re, ngx_str_t *s)
|
|
1827 {
|
|
1828 ngx_int_t rc, index;
|
|
1829 ngx_uint_t i, n, len;
|
|
1830 ngx_http_variable_value_t *vv;
|
|
1831 ngx_http_core_main_conf_t *cmcf;
|
|
1832
|
|
1833 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
1834
|
|
1835 if (re->ncaptures) {
|
548
|
1836 len = cmcf->ncaptures;
|
546
|
1837
|
|
1838 if (r->captures == NULL) {
|
|
1839 r->captures = ngx_palloc(r->pool, len * sizeof(int));
|
|
1840 if (r->captures == NULL) {
|
|
1841 return NGX_ERROR;
|
|
1842 }
|
|
1843 }
|
|
1844
|
|
1845 } else {
|
|
1846 len = 0;
|
|
1847 }
|
|
1848
|
|
1849 rc = ngx_regex_exec(re->regex, s, r->captures, len);
|
|
1850
|
|
1851 if (rc == NGX_REGEX_NO_MATCHED) {
|
|
1852 return NGX_DECLINED;
|
|
1853 }
|
|
1854
|
|
1855 if (rc < 0) {
|
|
1856 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
1857 ngx_regex_exec_n " failed: %i on \"%V\" using \"%V\"",
|
|
1858 rc, s, &re->name);
|
|
1859 return NGX_ERROR;
|
|
1860 }
|
|
1861
|
|
1862 for (i = 0; i < re->nvariables; i++) {
|
|
1863
|
|
1864 n = re->variables[i].capture;
|
|
1865 index = re->variables[i].index;
|
|
1866 vv = &r->variables[index];
|
|
1867
|
|
1868 vv->len = r->captures[n + 1] - r->captures[n];
|
|
1869 vv->valid = 1;
|
|
1870 vv->no_cacheable = 0;
|
|
1871 vv->not_found = 0;
|
|
1872 vv->data = &s->data[r->captures[n]];
|
|
1873
|
|
1874 #if (NGX_DEBUG)
|
|
1875 {
|
|
1876 ngx_http_variable_t *v;
|
|
1877
|
|
1878 v = cmcf->variables.elts;
|
|
1879
|
|
1880 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1881 "http regex set $%V to \"%*s\"",
|
|
1882 &v[index].name, vv->len, vv->data);
|
|
1883 }
|
|
1884 #endif
|
|
1885 }
|
|
1886
|
548
|
1887 r->ncaptures = rc * 2;
|
546
|
1888 r->captures_data = s->data;
|
|
1889
|
|
1890 return NGX_OK;
|
|
1891 }
|
|
1892
|
552
|
1893 #endif
|
|
1894
|
546
|
1895
|
48
|
1896 ngx_int_t
|
58
|
1897 ngx_http_variables_add_core_vars(ngx_conf_t *cf)
|
48
|
1898 {
|
160
|
1899 ngx_int_t rc;
|
|
1900 ngx_http_variable_t *v;
|
50
|
1901 ngx_http_core_main_conf_t *cmcf;
|
48
|
1902
|
58
|
1903 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1904
|
160
|
1905 cmcf->variables_keys = ngx_pcalloc(cf->temp_pool,
|
|
1906 sizeof(ngx_hash_keys_arrays_t));
|
|
1907 if (cmcf->variables_keys == NULL) {
|
|
1908 return NGX_ERROR;
|
|
1909 }
|
|
1910
|
|
1911 cmcf->variables_keys->pool = cf->pool;
|
|
1912 cmcf->variables_keys->temp_pool = cf->pool;
|
|
1913
|
|
1914 if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL)
|
|
1915 != NGX_OK)
|
58
|
1916 {
|
48
|
1917 return NGX_ERROR;
|
|
1918 }
|
|
1919
|
160
|
1920 for (v = ngx_http_core_variables; v->name.len; v++) {
|
|
1921 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v,
|
|
1922 NGX_HASH_READONLY_KEY);
|
|
1923
|
|
1924 if (rc == NGX_OK) {
|
|
1925 continue;
|
48
|
1926 }
|
|
1927
|
160
|
1928 if (rc == NGX_BUSY) {
|
|
1929 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1930 "conflicting variable name \"%V\"", &v->name);
|
|
1931 }
|
|
1932
|
|
1933 return NGX_ERROR;
|
48
|
1934 }
|
|
1935
|
|
1936 return NGX_OK;
|
|
1937 }
|
58
|
1938
|
|
1939
|
|
1940 ngx_int_t
|
|
1941 ngx_http_variables_init_vars(ngx_conf_t *cf)
|
|
1942 {
|
|
1943 ngx_uint_t i, n;
|
160
|
1944 ngx_hash_key_t *key;
|
|
1945 ngx_hash_init_t hash;
|
58
|
1946 ngx_http_variable_t *v, *av;
|
|
1947 ngx_http_core_main_conf_t *cmcf;
|
|
1948
|
|
1949 /* set the handlers for the indexed http variables */
|
|
1950
|
|
1951 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1952
|
|
1953 v = cmcf->variables.elts;
|
160
|
1954 key = cmcf->variables_keys->keys.elts;
|
58
|
1955
|
|
1956 for (i = 0; i < cmcf->variables.nelts; i++) {
|
|
1957
|
160
|
1958 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
|
58
|
1959
|
300
|
1960 av = key[n].value;
|
|
1961
|
|
1962 if (av->get_handler
|
|
1963 && v[i].name.len == key[n].key.len
|
160
|
1964 && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len)
|
58
|
1965 == 0)
|
|
1966 {
|
186
|
1967 v[i].get_handler = av->get_handler;
|
160
|
1968 v[i].data = av->data;
|
76
|
1969
|
160
|
1970 av->flags |= NGX_HTTP_VAR_INDEXED;
|
|
1971 v[i].flags = av->flags;
|
58
|
1972
|
160
|
1973 av->index = i;
|
82
|
1974
|
58
|
1975 goto next;
|
|
1976 }
|
|
1977 }
|
|
1978
|
|
1979 if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) {
|
186
|
1980 v[i].get_handler = ngx_http_variable_unknown_header_in;
|
126
|
1981 v[i].data = (uintptr_t) &v[i].name;
|
|
1982
|
|
1983 continue;
|
|
1984 }
|
|
1985
|
|
1986 if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) {
|
186
|
1987 v[i].get_handler = ngx_http_variable_unknown_header_out;
|
58
|
1988 v[i].data = (uintptr_t) &v[i].name;
|
|
1989
|
|
1990 continue;
|
|
1991 }
|
|
1992
|
296
|
1993 if (ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0) {
|
|
1994 v[i].get_handler = ngx_http_upstream_header_variable;
|
|
1995 v[i].data = (uintptr_t) &v[i].name;
|
340
|
1996 v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;
|
296
|
1997
|
|
1998 continue;
|
|
1999 }
|
|
2000
|
420
|
2001 if (ngx_strncmp(v[i].name.data, "cookie_", 7) == 0) {
|
|
2002 v[i].get_handler = ngx_http_variable_cookie;
|
|
2003 v[i].data = (uintptr_t) &v[i].name;
|
|
2004
|
|
2005 continue;
|
|
2006 }
|
|
2007
|
392
|
2008 if (ngx_strncmp(v[i].name.data, "arg_", 4) == 0) {
|
|
2009 v[i].get_handler = ngx_http_variable_argument;
|
|
2010 v[i].data = (uintptr_t) &v[i].name;
|
568
|
2011 v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;
|
392
|
2012
|
|
2013 continue;
|
|
2014 }
|
|
2015
|
58
|
2016 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
2017 "unknown \"%V\" variable", &v[i].name);
|
|
2018
|
|
2019 return NGX_ERROR;
|
|
2020
|
|
2021 next:
|
|
2022 continue;
|
|
2023 }
|
|
2024
|
|
2025
|
160
|
2026 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
|
|
2027 av = key[n].value;
|
|
2028
|
|
2029 if (av->flags & NGX_HTTP_VAR_NOHASH) {
|
|
2030 key[n].key.data = NULL;
|
132
|
2031 }
|
|
2032 }
|
|
2033
|
|
2034
|
160
|
2035 hash.hash = &cmcf->variables_hash;
|
|
2036 hash.key = ngx_hash_key;
|
|
2037 hash.max_size = cmcf->variables_hash_max_size;
|
|
2038 hash.bucket_size = cmcf->variables_hash_bucket_size;
|
|
2039 hash.name = "variables_hash";
|
|
2040 hash.pool = cf->pool;
|
|
2041 hash.temp_pool = NULL;
|
58
|
2042
|
160
|
2043 if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts,
|
|
2044 cmcf->variables_keys->keys.nelts)
|
130
|
2045 != NGX_OK)
|
58
|
2046 {
|
|
2047 return NGX_ERROR;
|
|
2048 }
|
|
2049
|
160
|
2050 cmcf->variables_keys = NULL;
|
58
|
2051
|
|
2052 return NGX_OK;
|
|
2053 }
|