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