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