48
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_http.h>
|
318
|
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);
|
320
|
16 static void ngx_http_variable_request_set(ngx_http_request_t *r,
|
|
17 ngx_http_variable_value_t *v, uintptr_t data);
|
186
|
18 static void ngx_http_variable_request_set_size(ngx_http_request_t *r,
|
|
19 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
20 static ngx_int_t ngx_http_variable_header(ngx_http_request_t *r,
|
|
21 ngx_http_variable_value_t *v, uintptr_t data);
|
|
22 static ngx_int_t ngx_http_variable_headers(ngx_http_request_t *r,
|
|
23 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
24
|
|
25 static ngx_int_t ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
|
122
|
26 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
27 static ngx_int_t ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
|
|
28 ngx_http_variable_value_t *v, uintptr_t data);
|
|
29
|
122
|
30 static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r,
|
|
31 ngx_http_variable_value_t *v, uintptr_t data);
|
272
|
32 static ngx_int_t ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
|
|
33 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
34 static ngx_int_t ngx_http_variable_remote_addr(ngx_http_request_t *r,
|
|
35 ngx_http_variable_value_t *v, uintptr_t data);
|
|
36 static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r,
|
|
37 ngx_http_variable_value_t *v, uintptr_t data);
|
|
38 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r,
|
|
39 ngx_http_variable_value_t *v, uintptr_t data);
|
|
40 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r,
|
|
41 ngx_http_variable_value_t *v, uintptr_t data);
|
238
|
42 static ngx_int_t ngx_http_variable_scheme(ngx_http_request_t *r,
|
|
43 ngx_http_variable_value_t *v, uintptr_t data);
|
320
|
44 static ngx_int_t ngx_http_variable_is_args(ngx_http_request_t *r,
|
|
45 ngx_http_variable_value_t *v, uintptr_t data);
|
122
|
46 static ngx_int_t ngx_http_variable_document_root(ngx_http_request_t *r,
|
|
47 ngx_http_variable_value_t *v, uintptr_t data);
|
|
48 static ngx_int_t ngx_http_variable_request_filename(ngx_http_request_t *r,
|
|
49 ngx_http_variable_value_t *v, uintptr_t data);
|
|
50 static ngx_int_t ngx_http_variable_request_method(ngx_http_request_t *r,
|
|
51 ngx_http_variable_value_t *v, uintptr_t data);
|
|
52 static ngx_int_t ngx_http_variable_remote_user(ngx_http_request_t *r,
|
|
53 ngx_http_variable_value_t *v, uintptr_t data);
|
160
|
54 static ngx_int_t ngx_http_variable_body_bytes_sent(ngx_http_request_t *r,
|
|
55 ngx_http_variable_value_t *v, uintptr_t data);
|
178
|
56 static ngx_int_t ngx_http_variable_request_completion(ngx_http_request_t *r,
|
|
57 ngx_http_variable_value_t *v, uintptr_t data);
|
244
|
58 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r,
|
|
59 ngx_http_variable_value_t *v, uintptr_t data);
|
48
|
60
|
190
|
61 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r,
|
|
62 ngx_http_variable_value_t *v, uintptr_t data);
|
|
63 static ngx_int_t ngx_http_variable_sent_content_length(ngx_http_request_t *r,
|
|
64 ngx_http_variable_value_t *v, uintptr_t data);
|
|
65 static ngx_int_t ngx_http_variable_sent_last_modified(ngx_http_request_t *r,
|
|
66 ngx_http_variable_value_t *v, uintptr_t data);
|
|
67 static ngx_int_t ngx_http_variable_sent_connection(ngx_http_request_t *r,
|
|
68 ngx_http_variable_value_t *v, uintptr_t data);
|
|
69 static ngx_int_t ngx_http_variable_sent_keep_alive(ngx_http_request_t *r,
|
|
70 ngx_http_variable_value_t *v, uintptr_t data);
|
|
71 static ngx_int_t ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
|
|
72 ngx_http_variable_value_t *v, uintptr_t data);
|
|
73
|
318
|
74 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r,
|
|
75 ngx_http_variable_value_t *v, uintptr_t data);
|
48
|
76
|
58
|
77 /*
|
|
78 * TODO:
|
|
79 * Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED
|
|
80 * REMOTE_HOST (null), REMOTE_IDENT (null),
|
|
81 * SERVER_SOFTWARE
|
|
82 *
|
120
|
83 * Apache SSI: DOCUMENT_NAME, LAST_MODIFIED, USER_NAME (file owner)
|
58
|
84 */
|
48
|
85
|
190
|
86 /*
|
|
87 * the $http_host, $http_user_agent, $http_referer, $http_via,
|
|
88 * and $http_x_forwarded_for variables may be handled by generic
|
|
89 * ngx_http_variable_unknown_header_in(), but for perfomance reasons
|
|
90 * they are handled using dedicated entries
|
|
91 */
|
|
92
|
58
|
93 static ngx_http_variable_t ngx_http_core_variables[] = {
|
|
94
|
186
|
95 { ngx_string("http_host"), NULL, ngx_http_variable_header,
|
82
|
96 offsetof(ngx_http_request_t, headers_in.host), 0, 0 },
|
48
|
97
|
186
|
98 { ngx_string("http_user_agent"), NULL, ngx_http_variable_header,
|
82
|
99 offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },
|
58
|
100
|
186
|
101 { ngx_string("http_referer"), NULL, ngx_http_variable_header,
|
82
|
102 offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },
|
48
|
103
|
|
104 #if (NGX_HTTP_GZIP)
|
186
|
105 { ngx_string("http_via"), NULL, ngx_http_variable_header,
|
82
|
106 offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
|
48
|
107 #endif
|
|
108
|
286
|
109 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
|
186
|
110 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header,
|
82
|
111 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
|
48
|
112 #endif
|
|
113
|
186
|
114 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
|
82
|
115 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },
|
64
|
116
|
186
|
117 { ngx_string("content_length"), NULL, ngx_http_variable_header,
|
82
|
118 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 },
|
58
|
119
|
186
|
120 { ngx_string("content_type"), NULL, ngx_http_variable_header,
|
82
|
121 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },
|
58
|
122
|
186
|
123 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },
|
58
|
124
|
272
|
125 { ngx_string("binary_remote_addr"), NULL,
|
|
126 ngx_http_variable_binary_remote_addr, 0, 0, 0 },
|
|
127
|
186
|
128 { ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 },
|
48
|
129
|
186
|
130 { ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 },
|
58
|
131
|
186
|
132 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },
|
58
|
133
|
186
|
134 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },
|
58
|
135
|
186
|
136 { ngx_string("server_protocol"), NULL, ngx_http_variable_request,
|
82
|
137 offsetof(ngx_http_request_t, http_protocol), 0, 0 },
|
58
|
138
|
238
|
139 { ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 },
|
|
140
|
186
|
141 { ngx_string("request_uri"), NULL, ngx_http_variable_request,
|
82
|
142 offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },
|
48
|
143
|
186
|
144 { ngx_string("uri"), NULL, ngx_http_variable_request,
|
|
145 offsetof(ngx_http_request_t, uri),
|
|
146 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
|
147
|
|
148 { ngx_string("document_uri"), NULL, ngx_http_variable_request,
|
122
|
149 offsetof(ngx_http_request_t, uri),
|
|
150 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
|
151
|
186
|
152 { ngx_string("request"), NULL, ngx_http_variable_request,
|
118
|
153 offsetof(ngx_http_request_t, request_line), 0, 0 },
|
|
154
|
186
|
155 { ngx_string("document_root"), NULL,
|
220
|
156 ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHABLE, 0 },
|
58
|
157
|
186
|
158 { ngx_string("query_string"), NULL, ngx_http_variable_request,
|
58
|
159 offsetof(ngx_http_request_t, args),
|
82
|
160 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
48
|
161
|
320
|
162 { ngx_string("args"),
|
|
163 ngx_http_variable_request_set,
|
|
164 ngx_http_variable_request,
|
138
|
165 offsetof(ngx_http_request_t, args),
|
320
|
166 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOCACHABLE, 0 },
|
|
167
|
|
168 { ngx_string("is_args"), NULL, ngx_http_variable_is_args,
|
|
169 0, NGX_HTTP_VAR_NOCACHABLE, 0 },
|
138
|
170
|
186
|
171 { ngx_string("request_filename"), NULL,
|
|
172 ngx_http_variable_request_filename, 0,
|
82
|
173 NGX_HTTP_VAR_NOCACHABLE, 0 },
|
58
|
174
|
186
|
175 { ngx_string("server_name"), NULL, ngx_http_variable_request,
|
82
|
176 offsetof(ngx_http_request_t, server_name), 0, 0 },
|
58
|
177
|
186
|
178 { ngx_string("request_method"), NULL,
|
|
179 ngx_http_variable_request_method, 0, 0, 0 },
|
58
|
180
|
186
|
181 { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 },
|
58
|
182
|
186
|
183 { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent,
|
160
|
184 0, 0, 0 },
|
|
185
|
186
|
186 { ngx_string("request_completion"), NULL,
|
|
187 ngx_http_variable_request_completion,
|
178
|
188 0, 0, 0 },
|
|
189
|
244
|
190 { ngx_string("request_body_file"), NULL,
|
|
191 ngx_http_variable_request_body_file,
|
|
192 0, 0, 0 },
|
|
193
|
190
|
194 { ngx_string("sent_http_content_type"), NULL,
|
|
195 ngx_http_variable_sent_content_type, 0, 0, 0 },
|
|
196
|
|
197 { ngx_string("sent_http_content_length"), NULL,
|
|
198 ngx_http_variable_sent_content_length, 0, 0, 0 },
|
|
199
|
|
200 { ngx_string("sent_http_last_modified"), NULL,
|
|
201 ngx_http_variable_sent_last_modified, 0, 0, 0 },
|
|
202
|
|
203 { ngx_string("sent_http_connection"), NULL,
|
|
204 ngx_http_variable_sent_connection, 0, 0, 0 },
|
|
205
|
|
206 { ngx_string("sent_http_keep_alive"), NULL,
|
|
207 ngx_http_variable_sent_keep_alive, 0, 0, 0 },
|
|
208
|
|
209 { ngx_string("sent_http_transfer_encoding"), NULL,
|
|
210 ngx_http_variable_sent_transfer_encoding, 0, 0, 0 },
|
|
211
|
|
212 { ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers,
|
|
213 offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 },
|
|
214
|
186
|
215 { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
|
|
216 ngx_http_variable_request,
|
|
217 offsetof(ngx_http_request_t, limit_rate),
|
|
218 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOCACHABLE, 0 },
|
|
219
|
318
|
220 { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
|
|
221 0, 0, 0 },
|
|
222
|
186
|
223 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
48
|
224 };
|
|
225
|
|
226
|
126
|
227 ngx_http_variable_value_t ngx_http_variable_null_value =
|
|
228 ngx_http_variable("");
|
|
229 ngx_http_variable_value_t ngx_http_variable_true_value =
|
|
230 ngx_http_variable("1");
|
|
231
|
|
232
|
48
|
233 ngx_http_variable_t *
|
58
|
234 ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
|
|
235 {
|
160
|
236 ngx_int_t rc;
|
58
|
237 ngx_uint_t i;
|
160
|
238 ngx_hash_key_t *key;
|
58
|
239 ngx_http_variable_t *v;
|
|
240 ngx_http_core_main_conf_t *cmcf;
|
|
241
|
|
242 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
243
|
160
|
244 key = cmcf->variables_keys->keys.elts;
|
|
245 for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {
|
|
246 if (name->len != key[i].key.len
|
|
247 || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)
|
58
|
248 {
|
|
249 continue;
|
|
250 }
|
|
251
|
160
|
252 v = key[i].value;
|
|
253
|
|
254 if (!(v->flags & NGX_HTTP_VAR_CHANGABLE)) {
|
58
|
255 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
256 "the duplicate \"%V\" variable", name);
|
|
257 return NULL;
|
|
258 }
|
|
259
|
160
|
260 return v;
|
58
|
261 }
|
|
262
|
160
|
263 v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));
|
58
|
264 if (v == NULL) {
|
|
265 return NULL;
|
|
266 }
|
|
267
|
|
268 v->name.len = name->len;
|
|
269 v->name.data = ngx_palloc(cf->pool, name->len);
|
|
270 if (v->name.data == NULL) {
|
|
271 return NULL;
|
|
272 }
|
|
273
|
|
274 for (i = 0; i < name->len; i++) {
|
|
275 v->name.data[i] = ngx_tolower(name->data[i]);
|
|
276 }
|
|
277
|
186
|
278 v->set_handler = NULL;
|
|
279 v->get_handler = NULL;
|
58
|
280 v->data = 0;
|
|
281 v->flags = flags;
|
82
|
282 v->index = 0;
|
58
|
283
|
160
|
284 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);
|
|
285
|
|
286 if (rc == NGX_ERROR) {
|
|
287 return NULL;
|
|
288 }
|
|
289
|
|
290 if (rc == NGX_BUSY) {
|
|
291 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
292 "conflicting variable name \"%V\"", name);
|
|
293 return NULL;
|
|
294 }
|
|
295
|
58
|
296 return v;
|
|
297 }
|
|
298
|
|
299
|
|
300 ngx_int_t
|
|
301 ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name)
|
48
|
302 {
|
50
|
303 ngx_uint_t i;
|
|
304 ngx_http_variable_t *v;
|
48
|
305 ngx_http_core_main_conf_t *cmcf;
|
|
306
|
|
307 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
308
|
50
|
309 v = cmcf->variables.elts;
|
|
310
|
|
311 if (v == NULL) {
|
48
|
312 if (ngx_array_init(&cmcf->variables, cf->pool, 4,
|
|
313 sizeof(ngx_http_variable_t)) == NGX_ERROR)
|
|
314 {
|
58
|
315 return NGX_ERROR;
|
48
|
316 }
|
50
|
317
|
|
318 } else {
|
|
319 for (i = 0; i < cmcf->variables.nelts; i++) {
|
|
320 if (name->len != v[i].name.len
|
|
321 || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)
|
|
322 {
|
|
323 continue;
|
|
324 }
|
|
325
|
58
|
326 return i;
|
50
|
327 }
|
48
|
328 }
|
|
329
|
50
|
330 v = ngx_array_push(&cmcf->variables);
|
|
331 if (v == NULL) {
|
58
|
332 return NGX_ERROR;
|
48
|
333 }
|
|
334
|
50
|
335 v->name.len = name->len;
|
|
336 v->name.data = ngx_palloc(cf->pool, name->len);
|
|
337 if (v->name.data == NULL) {
|
58
|
338 return NGX_ERROR;
|
50
|
339 }
|
48
|
340
|
50
|
341 for (i = 0; i < name->len; i++) {
|
58
|
342 v->name.data[i] = ngx_tolower(name->data[i]);
|
50
|
343 }
|
|
344
|
186
|
345 v->set_handler = NULL;
|
|
346 v->get_handler = NULL;
|
50
|
347 v->data = 0;
|
58
|
348 v->flags = 0;
|
82
|
349 v->index = cmcf->variables.nelts - 1;
|
50
|
350
|
58
|
351 return cmcf->variables.nelts - 1;
|
48
|
352 }
|
|
353
|
|
354
|
|
355 ngx_http_variable_value_t *
|
|
356 ngx_http_get_indexed_variable(ngx_http_request_t *r, ngx_uint_t index)
|
|
357 {
|
50
|
358 ngx_http_variable_t *v;
|
48
|
359 ngx_http_core_main_conf_t *cmcf;
|
|
360
|
|
361 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
362
|
102
|
363 if (cmcf->variables.nelts <= index) {
|
48
|
364 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
365 "unknown variable index: %d", index);
|
|
366 return NULL;
|
|
367 }
|
|
368
|
122
|
369 if (r->variables[index].not_found || r->variables[index].valid) {
|
|
370 return &r->variables[index];
|
50
|
371 }
|
48
|
372
|
50
|
373 v = cmcf->variables.elts;
|
48
|
374
|
186
|
375 if (v[index].get_handler(r, &r->variables[index], v[index].data)
|
|
376 == NGX_OK)
|
|
377 {
|
122
|
378 if (v[index].flags & NGX_HTTP_VAR_NOCACHABLE) {
|
|
379 r->variables[index].no_cachable = 1;
|
48
|
380 }
|
122
|
381
|
|
382 return &r->variables[index];
|
48
|
383 }
|
|
384
|
206
|
385 r->variables[index].valid = 0;
|
|
386 r->variables[index].not_found = 1;
|
|
387
|
122
|
388 return NULL;
|
|
389 }
|
|
390
|
|
391
|
|
392 ngx_http_variable_value_t *
|
|
393 ngx_http_get_flushed_variable(ngx_http_request_t *r, ngx_uint_t index)
|
|
394 {
|
294
|
395 ngx_http_variable_value_t *v;
|
122
|
396
|
|
397 v = &r->variables[index];
|
|
398
|
|
399 if (v->valid) {
|
|
400 if (!v->no_cachable) {
|
|
401 return v;
|
|
402 }
|
|
403
|
|
404 v->valid = 0;
|
|
405 v->not_found = 0;
|
58
|
406 }
|
50
|
407
|
122
|
408 return ngx_http_get_indexed_variable(r, index);
|
48
|
409 }
|
|
410
|
|
411
|
|
412 ngx_http_variable_value_t *
|
184
|
413 ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key,
|
|
414 ngx_uint_t nowarn)
|
48
|
415 {
|
50
|
416 ngx_http_variable_t *v;
|
122
|
417 ngx_http_variable_value_t *vv;
|
48
|
418 ngx_http_core_main_conf_t *cmcf;
|
|
419
|
|
420 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
421
|
160
|
422 v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len);
|
48
|
423
|
160
|
424 if (v) {
|
|
425 if (v->flags & NGX_HTTP_VAR_INDEXED) {
|
|
426 return ngx_http_get_indexed_variable(r, v->index);
|
68
|
427
|
|
428 } else {
|
48
|
429
|
122
|
430 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
|
50
|
431
|
186
|
432 if (vv && v->get_handler(r, vv, v->data) == NGX_OK) {
|
122
|
433 return vv;
|
|
434 }
|
58
|
435
|
122
|
436 return NULL;
|
|
437 }
|
58
|
438 }
|
|
439
|
|
440 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
|
|
441 if (vv == NULL) {
|
|
442 return NULL;
|
|
443 }
|
|
444
|
122
|
445 if (ngx_strncmp(name->data, "http_", 5) == 0) {
|
48
|
446
|
126
|
447 if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name)
|
|
448 == NGX_OK)
|
|
449 {
|
|
450 return vv;
|
|
451 }
|
|
452
|
|
453 return NULL;
|
|
454 }
|
|
455
|
|
456 if (ngx_strncmp(name->data, "sent_http_", 10) == 0) {
|
|
457
|
|
458 if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name)
|
|
459 == NGX_OK)
|
122
|
460 {
|
|
461 return vv;
|
|
462 }
|
48
|
463
|
|
464 return NULL;
|
|
465 }
|
|
466
|
296
|
467 if (ngx_strncmp(name->data, "upstream_http_", 10) == 0) {
|
|
468
|
|
469 if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name)
|
|
470 == NGX_OK)
|
|
471 {
|
|
472 return vv;
|
|
473 }
|
|
474
|
|
475 return NULL;
|
|
476 }
|
|
477
|
184
|
478 vv->not_found = 1;
|
122
|
479
|
184
|
480 if (nowarn == 0) {
|
|
481 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
482 "unknown \"%V\" variable", name);
|
|
483 }
|
48
|
484
|
50
|
485 return vv;
|
48
|
486 }
|
|
487
|
|
488
|
122
|
489 static ngx_int_t
|
|
490 ngx_http_variable_request(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
491 uintptr_t data)
|
|
492 {
|
|
493 ngx_str_t *s;
|
|
494
|
|
495 s = (ngx_str_t *) ((char *) r + data);
|
|
496
|
|
497 if (s->data) {
|
|
498 v->len = s->len;
|
|
499 v->valid = 1;
|
|
500 v->no_cachable = 0;
|
|
501 v->not_found = 0;
|
|
502 v->data = s->data;
|
|
503
|
|
504 } else {
|
|
505 v->not_found = 1;
|
|
506 }
|
|
507
|
|
508 return NGX_OK;
|
|
509 }
|
|
510
|
|
511
|
186
|
512 static void
|
320
|
513 ngx_http_variable_request_set(ngx_http_request_t *r,
|
|
514 ngx_http_variable_value_t *v, uintptr_t data)
|
|
515 {
|
|
516 ngx_str_t *s;
|
|
517
|
|
518 s = (ngx_str_t *) ((char *) r + data);
|
|
519
|
|
520 s->len = v->len;
|
|
521 s->data = v->data;
|
|
522 }
|
|
523
|
|
524
|
|
525 static void
|
186
|
526 ngx_http_variable_request_set_size(ngx_http_request_t *r,
|
|
527 ngx_http_variable_value_t *v, uintptr_t data)
|
|
528 {
|
|
529 ssize_t s, *sp;
|
|
530 ngx_str_t val;
|
|
531
|
318
|
532 val.len = v->len;
|
186
|
533 val.data = v->data;
|
|
534
|
|
535 s = ngx_parse_size(&val);
|
|
536
|
|
537 if (s == NGX_ERROR) {
|
|
538 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
539 "invalid size \"%V\"", &val);
|
|
540 return;
|
|
541 }
|
|
542
|
|
543 sp = (ssize_t *) ((char *) r + data);
|
|
544
|
|
545 *sp = s;
|
|
546
|
|
547 return;
|
|
548 }
|
|
549
|
|
550
|
122
|
551 static ngx_int_t
|
|
552 ngx_http_variable_header(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
553 uintptr_t data)
|
64
|
554 {
|
122
|
555 ngx_table_elt_t *h;
|
|
556
|
|
557 h = *(ngx_table_elt_t **) ((char *) r + data);
|
|
558
|
|
559 if (h) {
|
|
560 v->len = h->value.len;
|
|
561 v->valid = 1;
|
|
562 v->no_cachable = 0;
|
|
563 v->not_found = 0;
|
|
564 v->data = h->value.data;
|
|
565
|
|
566 } else {
|
|
567 v->not_found = 1;
|
|
568 }
|
|
569
|
|
570 return NGX_OK;
|
|
571 }
|
|
572
|
|
573
|
|
574 static ngx_int_t
|
|
575 ngx_http_variable_headers(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
576 uintptr_t data)
|
|
577 {
|
216
|
578 ssize_t len;
|
122
|
579 u_char *p;
|
216
|
580 ngx_uint_t i, n;
|
122
|
581 ngx_array_t *a;
|
|
582 ngx_table_elt_t **h;
|
64
|
583
|
|
584 a = (ngx_array_t *) ((char *) r + data);
|
|
585
|
216
|
586 n = a->nelts;
|
|
587
|
|
588 if (n == 0) {
|
122
|
589 v->not_found = 1;
|
|
590 return NGX_OK;
|
64
|
591 }
|
|
592
|
122
|
593 v->valid = 1;
|
|
594 v->no_cachable = 0;
|
|
595 v->not_found = 0;
|
64
|
596
|
|
597 h = a->elts;
|
|
598
|
216
|
599 if (n == 1) {
|
122
|
600 v->len = (*h)->value.len;
|
|
601 v->data = (*h)->value.data;
|
64
|
602
|
122
|
603 return NGX_OK;
|
64
|
604 }
|
|
605
|
216
|
606 len = - (ssize_t) (sizeof("; ") - 1);
|
122
|
607
|
216
|
608 for (i = 0; i < n; i++) {
|
122
|
609 len += h[i]->value.len + sizeof("; ") - 1;
|
64
|
610 }
|
|
611
|
122
|
612 p = ngx_palloc(r->pool, len);
|
|
613 if (p == NULL) {
|
|
614 return NGX_ERROR;
|
|
615 }
|
|
616
|
|
617 v->len = len;
|
|
618 v->data = p;
|
64
|
619
|
|
620 for (i = 0; /* void */ ; i++) {
|
122
|
621 p = ngx_copy(p, h[i]->value.data, h[i]->value.len);
|
64
|
622
|
216
|
623 if (i == n - 1) {
|
64
|
624 break;
|
|
625 }
|
|
626
|
|
627 *p++ = ';'; *p++ = ' ';
|
|
628 }
|
|
629
|
122
|
630 return NGX_OK;
|
64
|
631 }
|
|
632
|
|
633
|
122
|
634 static ngx_int_t
|
126
|
635 ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
|
|
636 ngx_http_variable_value_t *v, uintptr_t data)
|
|
637 {
|
|
638 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
|
|
639 &r->headers_in.headers.part,
|
|
640 sizeof("http_") - 1);
|
|
641 }
|
|
642
|
|
643
|
|
644 static ngx_int_t
|
|
645 ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
|
122
|
646 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
647 {
|
126
|
648 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
|
|
649 &r->headers_out.headers.part,
|
|
650 sizeof("sent_http_") - 1);
|
|
651 }
|
50
|
652
|
126
|
653
|
296
|
654 ngx_int_t
|
126
|
655 ngx_http_variable_unknown_header(ngx_http_variable_value_t *v, ngx_str_t *var,
|
|
656 ngx_list_part_t *part, size_t prefix)
|
|
657 {
|
122
|
658 u_char ch;
|
|
659 ngx_uint_t i, n;
|
|
660 ngx_table_elt_t *header;
|
48
|
661
|
|
662 header = part->elts;
|
|
663
|
|
664 for (i = 0; /* void */ ; i++) {
|
|
665
|
|
666 if (i >= part->nelts) {
|
|
667 if (part->next == NULL) {
|
|
668 break;
|
|
669 }
|
|
670
|
|
671 part = part->next;
|
|
672 header = part->elts;
|
|
673 i = 0;
|
|
674 }
|
|
675
|
126
|
676 for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) {
|
48
|
677 ch = header[i].key.data[n];
|
|
678
|
58
|
679 if (ch >= 'A' && ch <= 'Z') {
|
|
680 ch |= 0x20;
|
48
|
681
|
|
682 } else if (ch == '-') {
|
|
683 ch = '_';
|
|
684 }
|
|
685
|
126
|
686 if (var->data[n + prefix] != ch) {
|
48
|
687 break;
|
|
688 }
|
|
689 }
|
|
690
|
242
|
691 if (n + prefix == var->len && n == header[i].key.len) {
|
122
|
692 v->len = header[i].value.len;
|
|
693 v->valid = 1;
|
|
694 v->no_cachable = 0;
|
|
695 v->not_found = 0;
|
|
696 v->data = header[i].value.data;
|
48
|
697
|
122
|
698 return NGX_OK;
|
48
|
699 }
|
|
700 }
|
|
701
|
122
|
702 v->not_found = 1;
|
|
703
|
|
704 return NGX_OK;
|
58
|
705 }
|
|
706
|
|
707
|
122
|
708 static ngx_int_t
|
|
709 ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
710 uintptr_t data)
|
58
|
711 {
|
258
|
712 if (r->host_start == NULL) {
|
|
713
|
|
714 if (r->headers_in.host) {
|
|
715 v->len = r->headers_in.host_name_len;
|
|
716 v->data = r->headers_in.host->value.data;
|
|
717
|
|
718 } else {
|
|
719 v->len = r->server_name.len;
|
|
720 v->data = r->server_name.data;
|
|
721 }
|
58
|
722
|
270
|
723 } else if (r->host_end) {
|
258
|
724 v->len = r->host_end - r->host_start;
|
|
725 v->data = r->host_start;
|
270
|
726
|
|
727 } else {
|
|
728 v->not_found = 1;
|
|
729 return NGX_OK;
|
58
|
730 }
|
|
731
|
122
|
732 v->valid = 1;
|
|
733 v->no_cachable = 0;
|
|
734 v->not_found = 0;
|
58
|
735
|
122
|
736 return NGX_OK;
|
48
|
737 }
|
|
738
|
|
739
|
122
|
740 static ngx_int_t
|
272
|
741 ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
|
|
742 ngx_http_variable_value_t *v, uintptr_t data)
|
|
743 {
|
|
744 struct sockaddr_in *sin;
|
|
745
|
|
746 /* AF_INET only */
|
|
747
|
|
748 sin = (struct sockaddr_in *) r->connection->sockaddr;
|
|
749
|
|
750 v->len = sizeof(in_addr_t);
|
|
751 v->valid = 1;
|
|
752 v->no_cachable = 0;
|
|
753 v->not_found = 0;
|
|
754 v->data = (u_char *) &sin->sin_addr.s_addr;
|
|
755
|
|
756 return NGX_OK;
|
|
757 }
|
|
758
|
|
759
|
|
760 static ngx_int_t
|
122
|
761 ngx_http_variable_remote_addr(ngx_http_request_t *r,
|
|
762 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
763 {
|
122
|
764 v->len = r->connection->addr_text.len;
|
|
765 v->valid = 1;
|
|
766 v->no_cachable = 0;
|
|
767 v->not_found = 0;
|
|
768 v->data = r->connection->addr_text.data;
|
48
|
769
|
122
|
770 return NGX_OK;
|
48
|
771 }
|
|
772
|
|
773
|
122
|
774 static ngx_int_t
|
|
775 ngx_http_variable_remote_port(ngx_http_request_t *r,
|
|
776 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
777 {
|
122
|
778 ngx_uint_t port;
|
|
779 struct sockaddr_in *sin;
|
58
|
780
|
122
|
781 v->len = 0;
|
|
782 v->valid = 1;
|
|
783 v->no_cachable = 0;
|
|
784 v->not_found = 0;
|
58
|
785
|
122
|
786 v->data = ngx_palloc(r->pool, sizeof("65535") - 1);
|
|
787 if (v->data == NULL) {
|
|
788 return NGX_ERROR;
|
58
|
789 }
|
|
790
|
|
791 /* AF_INET only */
|
126
|
792
|
58
|
793 if (r->connection->sockaddr->sa_family == AF_INET) {
|
|
794 sin = (struct sockaddr_in *) r->connection->sockaddr;
|
126
|
795
|
58
|
796 port = ntohs(sin->sin_port);
|
126
|
797
|
58
|
798 if (port > 0 && port < 65536) {
|
122
|
799 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
|
58
|
800 }
|
|
801 }
|
|
802
|
122
|
803 return NGX_OK;
|
58
|
804 }
|
|
805
|
|
806
|
122
|
807 static ngx_int_t
|
|
808 ngx_http_variable_server_addr(ngx_http_request_t *r,
|
|
809 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
810 {
|
122
|
811 socklen_t len;
|
|
812 ngx_connection_t *c;
|
|
813 struct sockaddr_in sin;
|
48
|
814
|
122
|
815 v->data = ngx_palloc(r->pool, INET_ADDRSTRLEN);
|
|
816 if (v->data == NULL) {
|
|
817 return NGX_ERROR;
|
58
|
818 }
|
|
819
|
|
820 c = r->connection;
|
|
821
|
|
822 if (r->in_addr == 0) {
|
|
823 len = sizeof(struct sockaddr_in);
|
|
824 if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
|
250
|
825 ngx_connection_error(c, ngx_socket_errno, "getsockname() failed");
|
122
|
826 return NGX_ERROR;
|
58
|
827 }
|
|
828
|
|
829 r->in_addr = sin.sin_addr.s_addr;
|
|
830 }
|
|
831
|
122
|
832 v->len = ngx_inet_ntop(c->listening->family, &r->in_addr,
|
|
833 v->data, INET_ADDRSTRLEN);
|
|
834 v->valid = 1;
|
|
835 v->no_cachable = 0;
|
|
836 v->not_found = 0;
|
48
|
837
|
122
|
838 return NGX_OK;
|
48
|
839 }
|
|
840
|
|
841
|
122
|
842 static ngx_int_t
|
|
843 ngx_http_variable_server_port(ngx_http_request_t *r,
|
|
844 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
845 {
|
122
|
846 v->len = r->port_text->len - 1;
|
|
847 v->valid = 1;
|
|
848 v->no_cachable = 0;
|
|
849 v->not_found = 0;
|
|
850 v->data = r->port_text->data + 1;
|
58
|
851
|
122
|
852 return NGX_OK;
|
58
|
853 }
|
|
854
|
|
855
|
122
|
856 static ngx_int_t
|
238
|
857 ngx_http_variable_scheme(ngx_http_request_t *r,
|
|
858 ngx_http_variable_value_t *v, uintptr_t data)
|
|
859 {
|
|
860 #if (NGX_HTTP_SSL)
|
|
861
|
|
862 if (r->connection->ssl) {
|
|
863 v->len = sizeof("https") - 1;
|
|
864 v->valid = 1;
|
|
865 v->no_cachable = 0;
|
|
866 v->not_found = 0;
|
|
867 v->data = (u_char *) "https";
|
|
868
|
|
869 return NGX_OK;
|
|
870 }
|
|
871
|
|
872 #endif
|
|
873
|
|
874 v->len = sizeof("http") - 1;
|
|
875 v->valid = 1;
|
|
876 v->no_cachable = 0;
|
|
877 v->not_found = 0;
|
|
878 v->data = (u_char *) "http";
|
|
879
|
|
880 return NGX_OK;
|
|
881 }
|
|
882
|
|
883
|
|
884 static ngx_int_t
|
320
|
885 ngx_http_variable_is_args(ngx_http_request_t *r,
|
|
886 ngx_http_variable_value_t *v, uintptr_t data)
|
|
887 {
|
|
888 v->valid = 1;
|
|
889 v->no_cachable = 0;
|
|
890 v->not_found = 0;
|
|
891
|
|
892 if (r->args.len == 0) {
|
|
893 v->len = 0;
|
|
894 v->data = NULL;
|
|
895 return NGX_OK;
|
|
896 }
|
|
897
|
|
898 v->len = 1;
|
|
899 v->data = (u_char *) "?";
|
|
900
|
|
901 return NGX_OK;
|
|
902 }
|
|
903
|
|
904
|
|
905 static ngx_int_t
|
122
|
906 ngx_http_variable_document_root(ngx_http_request_t *r,
|
|
907 ngx_http_variable_value_t *v, uintptr_t data)
|
48
|
908 {
|
220
|
909 ngx_str_t path;
|
122
|
910 ngx_http_core_loc_conf_t *clcf;
|
58
|
911
|
|
912 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
913
|
220
|
914 if (clcf->root_lengths == NULL) {
|
|
915 v->len = clcf->root.len;
|
|
916 v->valid = 1;
|
|
917 v->no_cachable = 0;
|
|
918 v->not_found = 0;
|
|
919 v->data = clcf->root.data;
|
|
920
|
|
921 } else {
|
|
922 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 0,
|
|
923 clcf->root_values->elts)
|
|
924 == NULL)
|
|
925 {
|
|
926 return NGX_ERROR;
|
|
927 }
|
|
928
|
|
929 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path) == NGX_ERROR) {
|
|
930 return NGX_ERROR;
|
|
931 }
|
|
932
|
|
933 v->len = path.len;
|
|
934 v->valid = 1;
|
|
935 v->no_cachable = 0;
|
|
936 v->not_found = 0;
|
|
937 v->data = path.data;
|
|
938 }
|
58
|
939
|
122
|
940 return NGX_OK;
|
58
|
941 }
|
|
942
|
|
943
|
122
|
944 static ngx_int_t
|
|
945 ngx_http_variable_request_filename(ngx_http_request_t *r,
|
|
946 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
947 {
|
248
|
948 size_t root;
|
122
|
949 ngx_str_t path;
|
48
|
950
|
248
|
951 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
|
122
|
952 return NGX_ERROR;
|
106
|
953 }
|
58
|
954
|
106
|
955 /* ngx_http_map_uri_to_path() allocates memory for terminating '\0' */
|
58
|
956
|
122
|
957 v->len = path.len - 1;
|
|
958 v->valid = 1;
|
|
959 v->no_cachable = 0;
|
|
960 v->not_found = 0;
|
|
961 v->data = path.data;
|
48
|
962
|
122
|
963 return NGX_OK;
|
48
|
964 }
|
|
965
|
|
966
|
122
|
967 static ngx_int_t
|
|
968 ngx_http_variable_request_method(ngx_http_request_t *r,
|
|
969 ngx_http_variable_value_t *v, uintptr_t data)
|
110
|
970 {
|
196
|
971 if (r->main->method_name.data) {
|
|
972 v->len = r->main->method_name.len;
|
122
|
973 v->valid = 1;
|
|
974 v->no_cachable = 0;
|
|
975 v->not_found = 0;
|
196
|
976 v->data = r->main->method_name.data;
|
122
|
977
|
|
978 } else {
|
|
979 v->not_found = 1;
|
110
|
980 }
|
|
981
|
122
|
982 return NGX_OK;
|
110
|
983 }
|
|
984
|
|
985
|
122
|
986 static ngx_int_t
|
|
987 ngx_http_variable_remote_user(ngx_http_request_t *r,
|
|
988 ngx_http_variable_value_t *v, uintptr_t data)
|
88
|
989 {
|
122
|
990 ngx_int_t rc;
|
88
|
991
|
|
992 rc = ngx_http_auth_basic_user(r);
|
|
993
|
|
994 if (rc == NGX_DECLINED) {
|
122
|
995 v->not_found = 1;
|
|
996 return NGX_OK;
|
88
|
997 }
|
|
998
|
|
999 if (rc == NGX_ERROR) {
|
122
|
1000 return NGX_ERROR;
|
88
|
1001 }
|
|
1002
|
122
|
1003 v->len = r->headers_in.user.len;
|
|
1004 v->valid = 1;
|
|
1005 v->no_cachable = 0;
|
|
1006 v->not_found = 0;
|
|
1007 v->data = r->headers_in.user.data;
|
120
|
1008
|
122
|
1009 return NGX_OK;
|
120
|
1010 }
|
|
1011
|
|
1012
|
160
|
1013 static ngx_int_t
|
|
1014 ngx_http_variable_body_bytes_sent(ngx_http_request_t *r,
|
|
1015 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1016 {
|
|
1017 off_t sent;
|
|
1018 u_char *p;
|
|
1019
|
|
1020 sent = r->connection->sent - r->header_size;
|
|
1021
|
|
1022 if (sent < 0) {
|
|
1023 sent = 0;
|
|
1024 }
|
|
1025
|
|
1026 p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
|
|
1027 if (p == NULL) {
|
|
1028 return NGX_ERROR;
|
|
1029 }
|
|
1030
|
|
1031 v->len = ngx_sprintf(p, "%O", sent) - p;
|
|
1032 v->valid = 1;
|
|
1033 v->no_cachable = 0;
|
|
1034 v->not_found = 0;
|
|
1035 v->data = p;
|
|
1036
|
|
1037 return NGX_OK;
|
|
1038 }
|
|
1039
|
|
1040
|
178
|
1041 static ngx_int_t
|
190
|
1042 ngx_http_variable_sent_content_type(ngx_http_request_t *r,
|
|
1043 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1044 {
|
|
1045 if (r->headers_out.content_type.len) {
|
|
1046 v->len = r->headers_out.content_type.len;
|
|
1047 v->valid = 1;
|
|
1048 v->no_cachable = 0;
|
|
1049 v->not_found = 0;
|
|
1050 v->data = r->headers_out.content_type.data;
|
|
1051
|
|
1052 } else {
|
|
1053 v->not_found = 1;
|
|
1054 }
|
|
1055
|
|
1056 return NGX_OK;
|
|
1057 }
|
|
1058
|
|
1059
|
|
1060 static ngx_int_t
|
|
1061 ngx_http_variable_sent_content_length(ngx_http_request_t *r,
|
|
1062 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1063 {
|
|
1064 u_char *p;
|
|
1065
|
|
1066 if (r->headers_out.content_length) {
|
|
1067 v->len = r->headers_out.content_length->value.len;
|
|
1068 v->valid = 1;
|
|
1069 v->no_cachable = 0;
|
|
1070 v->not_found = 0;
|
|
1071 v->data = r->headers_out.content_length->value.data;
|
|
1072
|
|
1073 return NGX_OK;
|
|
1074 }
|
|
1075
|
|
1076 if (r->headers_out.content_length_n >= 0) {
|
|
1077 p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
|
|
1078 if (p == NULL) {
|
|
1079 return NGX_ERROR;
|
|
1080 }
|
|
1081
|
|
1082 v->len = ngx_sprintf(p, "%O", r->headers_out.content_length_n) - p;
|
|
1083 v->valid = 1;
|
|
1084 v->no_cachable = 0;
|
|
1085 v->not_found = 0;
|
|
1086 v->data = p;
|
|
1087
|
|
1088 return NGX_OK;
|
|
1089 }
|
|
1090
|
|
1091 v->not_found = 1;
|
|
1092
|
|
1093 return NGX_OK;
|
|
1094 }
|
|
1095
|
|
1096
|
|
1097 static ngx_int_t
|
|
1098 ngx_http_variable_sent_last_modified(ngx_http_request_t *r,
|
|
1099 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1100 {
|
|
1101 u_char *p;
|
|
1102
|
|
1103 if (r->headers_out.last_modified) {
|
|
1104 v->len = r->headers_out.last_modified->value.len;
|
|
1105 v->valid = 1;
|
|
1106 v->no_cachable = 0;
|
|
1107 v->not_found = 0;
|
|
1108 v->data = r->headers_out.last_modified->value.data;
|
|
1109
|
|
1110 return NGX_OK;
|
|
1111 }
|
|
1112
|
|
1113 if (r->headers_out.last_modified_time >= 0) {
|
|
1114 p = ngx_palloc(r->pool,
|
|
1115 sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT") - 1);
|
|
1116 if (p == NULL) {
|
|
1117 return NGX_ERROR;
|
|
1118 }
|
|
1119
|
|
1120 v->len = ngx_http_time(p, r->headers_out.last_modified_time) - p;
|
|
1121 v->valid = 1;
|
|
1122 v->no_cachable = 0;
|
|
1123 v->not_found = 0;
|
|
1124 v->data = p;
|
|
1125
|
|
1126 return NGX_OK;
|
|
1127 }
|
|
1128
|
|
1129 v->not_found = 1;
|
|
1130
|
|
1131 return NGX_OK;
|
|
1132 }
|
|
1133
|
|
1134
|
|
1135 static ngx_int_t
|
|
1136 ngx_http_variable_sent_connection(ngx_http_request_t *r,
|
|
1137 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1138 {
|
|
1139 size_t len;
|
|
1140 char *p;
|
|
1141
|
|
1142 if (r->keepalive) {
|
|
1143 len = sizeof("keep-alive") - 1;
|
|
1144 p = "keep-alive";
|
|
1145
|
|
1146 } else {
|
|
1147 len = sizeof("close") - 1;
|
|
1148 p = "close";
|
|
1149 }
|
|
1150
|
|
1151 v->len = len;
|
|
1152 v->valid = 1;
|
|
1153 v->no_cachable = 0;
|
|
1154 v->not_found = 0;
|
|
1155 v->data = (u_char *) p;
|
|
1156
|
|
1157 return NGX_OK;
|
|
1158 }
|
|
1159
|
|
1160
|
|
1161 static ngx_int_t
|
|
1162 ngx_http_variable_sent_keep_alive(ngx_http_request_t *r,
|
|
1163 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1164 {
|
|
1165 u_char *p;
|
|
1166 ngx_http_core_loc_conf_t *clcf;
|
|
1167
|
|
1168 if (r->keepalive) {
|
|
1169 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1170
|
|
1171 if (clcf->keepalive_header) {
|
|
1172
|
|
1173 p = ngx_palloc(r->pool, sizeof("timeout=") - 1 + NGX_TIME_T_LEN);
|
|
1174 if (p == NULL) {
|
|
1175 return NGX_ERROR;
|
|
1176 }
|
|
1177
|
|
1178 v->len = ngx_sprintf(p, "timeout=%T", clcf->keepalive_header) - p;
|
|
1179 v->valid = 1;
|
|
1180 v->no_cachable = 0;
|
|
1181 v->not_found = 0;
|
|
1182 v->data = p;
|
|
1183
|
|
1184 return NGX_OK;
|
|
1185 }
|
|
1186 }
|
|
1187
|
|
1188 v->not_found = 1;
|
|
1189
|
|
1190 return NGX_OK;
|
|
1191 }
|
|
1192
|
|
1193
|
|
1194 static ngx_int_t
|
|
1195 ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
|
|
1196 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1197 {
|
|
1198 if (r->chunked) {
|
|
1199 v->len = sizeof("chunked") - 1;
|
|
1200 v->valid = 1;
|
|
1201 v->no_cachable = 0;
|
|
1202 v->not_found = 0;
|
|
1203 v->data = (u_char *) "chunked";
|
|
1204
|
|
1205 } else {
|
|
1206 v->not_found = 1;
|
|
1207 }
|
|
1208
|
|
1209 return NGX_OK;
|
|
1210 }
|
|
1211
|
|
1212
|
|
1213 static ngx_int_t
|
178
|
1214 ngx_http_variable_request_completion(ngx_http_request_t *r,
|
|
1215 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1216 {
|
|
1217 if (r->request_complete) {
|
|
1218 v->len = 2;
|
|
1219 v->valid = 1;
|
|
1220 v->no_cachable = 0;
|
|
1221 v->not_found = 0;
|
|
1222 v->data = (u_char *) "OK";
|
|
1223
|
|
1224 return NGX_OK;
|
|
1225 }
|
|
1226
|
|
1227 v->len = 0;
|
|
1228 v->valid = 1;
|
|
1229 v->no_cachable = 0;
|
|
1230 v->not_found = 0;
|
|
1231 v->data = (u_char *) "";
|
|
1232
|
|
1233 return NGX_OK;
|
|
1234 }
|
|
1235
|
|
1236
|
244
|
1237 static ngx_int_t
|
|
1238 ngx_http_variable_request_body_file(ngx_http_request_t *r,
|
|
1239 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1240 {
|
|
1241 if (r->request_body == NULL || r->request_body->temp_file == NULL) {
|
246
|
1242 v->not_found = 1;
|
244
|
1243
|
|
1244 return NGX_OK;
|
|
1245 }
|
|
1246
|
|
1247 v->len = r->request_body->temp_file->file.name.len;
|
|
1248 v->valid = 1;
|
|
1249 v->no_cachable = 0;
|
|
1250 v->not_found = 0;
|
|
1251 v->data = r->request_body->temp_file->file.name.data;
|
|
1252
|
|
1253 return NGX_OK;
|
|
1254 }
|
|
1255
|
|
1256
|
318
|
1257 static ngx_int_t
|
|
1258 ngx_http_variable_nginx_version(ngx_http_request_t *r,
|
|
1259 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1260 {
|
|
1261 v->len = sizeof(NGINX_VERSION) - 1;
|
|
1262 v->valid = 1;
|
|
1263 v->no_cachable = 0;
|
|
1264 v->not_found = 0;
|
|
1265 v->data = (u_char *) NGINX_VERSION;
|
|
1266
|
|
1267 return NGX_OK;
|
|
1268 }
|
|
1269
|
|
1270
|
48
|
1271 ngx_int_t
|
58
|
1272 ngx_http_variables_add_core_vars(ngx_conf_t *cf)
|
48
|
1273 {
|
160
|
1274 ngx_int_t rc;
|
|
1275 ngx_http_variable_t *v;
|
50
|
1276 ngx_http_core_main_conf_t *cmcf;
|
48
|
1277
|
58
|
1278 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1279
|
160
|
1280 cmcf->variables_keys = ngx_pcalloc(cf->temp_pool,
|
|
1281 sizeof(ngx_hash_keys_arrays_t));
|
|
1282 if (cmcf->variables_keys == NULL) {
|
|
1283 return NGX_ERROR;
|
|
1284 }
|
|
1285
|
|
1286 cmcf->variables_keys->pool = cf->pool;
|
|
1287 cmcf->variables_keys->temp_pool = cf->pool;
|
|
1288
|
|
1289 if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL)
|
|
1290 != NGX_OK)
|
58
|
1291 {
|
48
|
1292 return NGX_ERROR;
|
|
1293 }
|
|
1294
|
160
|
1295 for (v = ngx_http_core_variables; v->name.len; v++) {
|
|
1296 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v,
|
|
1297 NGX_HASH_READONLY_KEY);
|
|
1298
|
|
1299 if (rc == NGX_OK) {
|
|
1300 continue;
|
48
|
1301 }
|
|
1302
|
160
|
1303 if (rc == NGX_BUSY) {
|
|
1304 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1305 "conflicting variable name \"%V\"", &v->name);
|
|
1306 }
|
|
1307
|
|
1308 return NGX_ERROR;
|
48
|
1309 }
|
|
1310
|
|
1311 return NGX_OK;
|
|
1312 }
|
58
|
1313
|
|
1314
|
|
1315 ngx_int_t
|
|
1316 ngx_http_variables_init_vars(ngx_conf_t *cf)
|
|
1317 {
|
|
1318 ngx_uint_t i, n;
|
160
|
1319 ngx_hash_key_t *key;
|
|
1320 ngx_hash_init_t hash;
|
58
|
1321 ngx_http_variable_t *v, *av;
|
|
1322 ngx_http_core_main_conf_t *cmcf;
|
|
1323
|
|
1324 /* set the handlers for the indexed http variables */
|
|
1325
|
|
1326 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1327
|
|
1328 v = cmcf->variables.elts;
|
160
|
1329 key = cmcf->variables_keys->keys.elts;
|
58
|
1330
|
|
1331 for (i = 0; i < cmcf->variables.nelts; i++) {
|
|
1332
|
160
|
1333 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
|
58
|
1334
|
300
|
1335 av = key[n].value;
|
|
1336
|
|
1337 if (av->get_handler
|
|
1338 && v[i].name.len == key[n].key.len
|
160
|
1339 && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len)
|
58
|
1340 == 0)
|
|
1341 {
|
186
|
1342 v[i].get_handler = av->get_handler;
|
160
|
1343 v[i].data = av->data;
|
76
|
1344
|
160
|
1345 av->flags |= NGX_HTTP_VAR_INDEXED;
|
|
1346 v[i].flags = av->flags;
|
58
|
1347
|
160
|
1348 av->index = i;
|
82
|
1349
|
58
|
1350 goto next;
|
|
1351 }
|
|
1352 }
|
|
1353
|
|
1354 if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) {
|
186
|
1355 v[i].get_handler = ngx_http_variable_unknown_header_in;
|
126
|
1356 v[i].data = (uintptr_t) &v[i].name;
|
|
1357
|
|
1358 continue;
|
|
1359 }
|
|
1360
|
|
1361 if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) {
|
186
|
1362 v[i].get_handler = ngx_http_variable_unknown_header_out;
|
58
|
1363 v[i].data = (uintptr_t) &v[i].name;
|
|
1364
|
|
1365 continue;
|
|
1366 }
|
|
1367
|
296
|
1368 if (ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0) {
|
|
1369 v[i].get_handler = ngx_http_upstream_header_variable;
|
|
1370 v[i].data = (uintptr_t) &v[i].name;
|
|
1371 v[i].flags = NGX_HTTP_VAR_NOCACHABLE;
|
|
1372
|
|
1373 continue;
|
|
1374 }
|
|
1375
|
58
|
1376 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
1377 "unknown \"%V\" variable", &v[i].name);
|
|
1378
|
|
1379 return NGX_ERROR;
|
|
1380
|
|
1381 next:
|
|
1382 continue;
|
|
1383 }
|
|
1384
|
|
1385
|
160
|
1386 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
|
|
1387 av = key[n].value;
|
|
1388
|
|
1389 if (av->flags & NGX_HTTP_VAR_NOHASH) {
|
|
1390 key[n].key.data = NULL;
|
132
|
1391 }
|
|
1392 }
|
|
1393
|
|
1394
|
160
|
1395 hash.hash = &cmcf->variables_hash;
|
|
1396 hash.key = ngx_hash_key;
|
|
1397 hash.max_size = cmcf->variables_hash_max_size;
|
|
1398 hash.bucket_size = cmcf->variables_hash_bucket_size;
|
|
1399 hash.name = "variables_hash";
|
|
1400 hash.pool = cf->pool;
|
|
1401 hash.temp_pool = NULL;
|
58
|
1402
|
160
|
1403 if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts,
|
|
1404 cmcf->variables_keys->keys.nelts)
|
130
|
1405 != NGX_OK)
|
58
|
1406 {
|
|
1407 return NGX_ERROR;
|
|
1408 }
|
|
1409
|
160
|
1410 cmcf->variables_keys = NULL;
|
58
|
1411
|
|
1412 return NGX_OK;
|
|
1413 }
|