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