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