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