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