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