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