Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_variables.c @ 6671:6b1b8c4b7a95
Realip: fixed uninitialized memory access.
Previously, the realip module could be left with uninitialized context after an
error in the ngx_http_realip_set_addr() function. That context could be later
accessed by $realip_remote_addr and $realip_remote_port variable handlers.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Thu, 01 Sep 2016 14:33:51 +0300 |
parents | c6372a40c2a7 |
children | 38143d1abdec |
rev | line source |
---|---|
6607 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 #include <nginx.h> | |
12 | |
13 | |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
14 static ngx_int_t ngx_stream_variable_binary_remote_addr( |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
15 ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
16 static ngx_int_t ngx_stream_variable_remote_addr(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
17 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
18 static ngx_int_t ngx_stream_variable_remote_port(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
19 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
20 static ngx_int_t ngx_stream_variable_server_addr(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
21 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
22 static ngx_int_t ngx_stream_variable_server_port(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
23 ngx_stream_variable_value_t *v, uintptr_t data); |
6668
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
24 static ngx_int_t ngx_stream_variable_bytes(ngx_stream_session_t *s, |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
25 ngx_stream_variable_value_t *v, uintptr_t data); |
6669
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
26 static ngx_int_t ngx_stream_variable_session_time(ngx_stream_session_t *s, |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
27 ngx_stream_variable_value_t *v, uintptr_t data); |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
28 static ngx_int_t ngx_stream_variable_connection(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
29 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
30 |
6607 | 31 static ngx_int_t ngx_stream_variable_nginx_version(ngx_stream_session_t *s, |
32 ngx_stream_variable_value_t *v, uintptr_t data); | |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
33 static ngx_int_t ngx_stream_variable_hostname(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
34 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
35 static ngx_int_t ngx_stream_variable_pid(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
36 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
37 static ngx_int_t ngx_stream_variable_msec(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
38 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
39 static ngx_int_t ngx_stream_variable_time_iso8601(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
40 ngx_stream_variable_value_t *v, uintptr_t data); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
41 static ngx_int_t ngx_stream_variable_time_local(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
42 ngx_stream_variable_value_t *v, uintptr_t data); |
6670
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
43 static ngx_int_t ngx_stream_variable_protocol(ngx_stream_session_t *s, |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
44 ngx_stream_variable_value_t *v, uintptr_t data); |
6607 | 45 |
46 | |
47 static ngx_stream_variable_t ngx_stream_core_variables[] = { | |
48 | |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
49 { ngx_string("binary_remote_addr"), NULL, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
50 ngx_stream_variable_binary_remote_addr, 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
51 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
52 { ngx_string("remote_addr"), NULL, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
53 ngx_stream_variable_remote_addr, 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
54 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
55 { ngx_string("remote_port"), NULL, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
56 ngx_stream_variable_remote_port, 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
57 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
58 { ngx_string("server_addr"), NULL, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
59 ngx_stream_variable_server_addr, 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
60 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
61 { ngx_string("server_port"), NULL, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
62 ngx_stream_variable_server_port, 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
63 |
6668
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
64 { ngx_string("bytes_sent"), NULL, ngx_stream_variable_bytes, |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
65 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
66 |
6668
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
67 { ngx_string("bytes_received"), NULL, ngx_stream_variable_bytes, |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
68 1, 0, 0 }, |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
69 |
6669
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
70 { ngx_string("session_time"), NULL, ngx_stream_variable_session_time, |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
71 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
72 |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
73 { ngx_string("connection"), NULL, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
74 ngx_stream_variable_connection, 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
75 |
6607 | 76 { ngx_string("nginx_version"), NULL, ngx_stream_variable_nginx_version, |
77 0, 0, 0 }, | |
78 | |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
79 { ngx_string("hostname"), NULL, ngx_stream_variable_hostname, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
80 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
81 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
82 { ngx_string("pid"), NULL, ngx_stream_variable_pid, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
83 0, 0, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
84 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
85 { ngx_string("msec"), NULL, ngx_stream_variable_msec, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
86 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
87 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
88 { ngx_string("time_iso8601"), NULL, ngx_stream_variable_time_iso8601, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
89 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
90 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
91 { ngx_string("time_local"), NULL, ngx_stream_variable_time_local, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
92 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
93 |
6670
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
94 { ngx_string("protocol"), NULL, |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
95 ngx_stream_variable_protocol, 0, 0, 0 }, |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
96 |
6607 | 97 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
98 }; | |
99 | |
100 | |
101 ngx_stream_variable_value_t ngx_stream_variable_null_value = | |
102 ngx_stream_variable(""); | |
103 ngx_stream_variable_value_t ngx_stream_variable_true_value = | |
104 ngx_stream_variable("1"); | |
105 | |
106 | |
107 ngx_stream_variable_t * | |
108 ngx_stream_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags) | |
109 { | |
110 ngx_int_t rc; | |
111 ngx_uint_t i; | |
112 ngx_hash_key_t *key; | |
113 ngx_stream_variable_t *v; | |
114 ngx_stream_core_main_conf_t *cmcf; | |
115 | |
116 if (name->len == 0) { | |
117 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
118 "invalid variable name \"$\""); | |
119 return NULL; | |
120 } | |
121 | |
122 cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); | |
123 | |
124 key = cmcf->variables_keys->keys.elts; | |
125 for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) { | |
126 if (name->len != key[i].key.len | |
127 || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0) | |
128 { | |
129 continue; | |
130 } | |
131 | |
132 v = key[i].value; | |
133 | |
134 if (!(v->flags & NGX_STREAM_VAR_CHANGEABLE)) { | |
135 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
136 "the duplicate \"%V\" variable", name); | |
137 return NULL; | |
138 } | |
139 | |
140 return v; | |
141 } | |
142 | |
143 v = ngx_palloc(cf->pool, sizeof(ngx_stream_variable_t)); | |
144 if (v == NULL) { | |
145 return NULL; | |
146 } | |
147 | |
148 v->name.len = name->len; | |
149 v->name.data = ngx_pnalloc(cf->pool, name->len); | |
150 if (v->name.data == NULL) { | |
151 return NULL; | |
152 } | |
153 | |
154 ngx_strlow(v->name.data, name->data, name->len); | |
155 | |
156 v->set_handler = NULL; | |
157 v->get_handler = NULL; | |
158 v->data = 0; | |
159 v->flags = flags; | |
160 v->index = 0; | |
161 | |
162 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0); | |
163 | |
164 if (rc == NGX_ERROR) { | |
165 return NULL; | |
166 } | |
167 | |
168 if (rc == NGX_BUSY) { | |
169 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
170 "conflicting variable name \"%V\"", name); | |
171 return NULL; | |
172 } | |
173 | |
174 return v; | |
175 } | |
176 | |
177 | |
178 ngx_int_t | |
179 ngx_stream_get_variable_index(ngx_conf_t *cf, ngx_str_t *name) | |
180 { | |
181 ngx_uint_t i; | |
182 ngx_stream_variable_t *v; | |
183 ngx_stream_core_main_conf_t *cmcf; | |
184 | |
185 if (name->len == 0) { | |
186 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
187 "invalid variable name \"$\""); | |
188 return NGX_ERROR; | |
189 } | |
190 | |
191 cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); | |
192 | |
193 v = cmcf->variables.elts; | |
194 | |
195 if (v == NULL) { | |
196 if (ngx_array_init(&cmcf->variables, cf->pool, 4, | |
197 sizeof(ngx_stream_variable_t)) | |
198 != NGX_OK) | |
199 { | |
200 return NGX_ERROR; | |
201 } | |
202 | |
203 } else { | |
204 for (i = 0; i < cmcf->variables.nelts; i++) { | |
205 if (name->len != v[i].name.len | |
206 || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0) | |
207 { | |
208 continue; | |
209 } | |
210 | |
211 return i; | |
212 } | |
213 } | |
214 | |
215 v = ngx_array_push(&cmcf->variables); | |
216 if (v == NULL) { | |
217 return NGX_ERROR; | |
218 } | |
219 | |
220 v->name.len = name->len; | |
221 v->name.data = ngx_pnalloc(cf->pool, name->len); | |
222 if (v->name.data == NULL) { | |
223 return NGX_ERROR; | |
224 } | |
225 | |
226 ngx_strlow(v->name.data, name->data, name->len); | |
227 | |
228 v->set_handler = NULL; | |
229 v->get_handler = NULL; | |
230 v->data = 0; | |
231 v->flags = 0; | |
232 v->index = cmcf->variables.nelts - 1; | |
233 | |
234 return v->index; | |
235 } | |
236 | |
237 | |
238 ngx_stream_variable_value_t * | |
239 ngx_stream_get_indexed_variable(ngx_stream_session_t *s, ngx_uint_t index) | |
240 { | |
241 ngx_stream_variable_t *v; | |
242 ngx_stream_core_main_conf_t *cmcf; | |
243 | |
244 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); | |
245 | |
246 if (cmcf->variables.nelts <= index) { | |
247 ngx_log_error(NGX_LOG_ALERT, s->connection->log, 0, | |
248 "unknown variable index: %ui", index); | |
249 return NULL; | |
250 } | |
251 | |
252 if (s->variables[index].not_found || s->variables[index].valid) { | |
253 return &s->variables[index]; | |
254 } | |
255 | |
256 v = cmcf->variables.elts; | |
257 | |
258 if (v[index].get_handler(s, &s->variables[index], v[index].data) | |
259 == NGX_OK) | |
260 { | |
261 if (v[index].flags & NGX_STREAM_VAR_NOCACHEABLE) { | |
262 s->variables[index].no_cacheable = 1; | |
263 } | |
264 | |
265 return &s->variables[index]; | |
266 } | |
267 | |
268 s->variables[index].valid = 0; | |
269 s->variables[index].not_found = 1; | |
270 | |
271 return NULL; | |
272 } | |
273 | |
274 | |
275 ngx_stream_variable_value_t * | |
276 ngx_stream_get_flushed_variable(ngx_stream_session_t *s, ngx_uint_t index) | |
277 { | |
278 ngx_stream_variable_value_t *v; | |
279 | |
280 v = &s->variables[index]; | |
281 | |
282 if (v->valid || v->not_found) { | |
283 if (!v->no_cacheable) { | |
284 return v; | |
285 } | |
286 | |
287 v->valid = 0; | |
288 v->not_found = 0; | |
289 } | |
290 | |
291 return ngx_stream_get_indexed_variable(s, index); | |
292 } | |
293 | |
294 | |
295 ngx_stream_variable_value_t * | |
296 ngx_stream_get_variable(ngx_stream_session_t *s, ngx_str_t *name, | |
297 ngx_uint_t key) | |
298 { | |
299 ngx_stream_variable_t *v; | |
300 ngx_stream_variable_value_t *vv; | |
301 ngx_stream_core_main_conf_t *cmcf; | |
302 | |
303 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); | |
304 | |
305 v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len); | |
306 | |
307 if (v) { | |
308 if (v->flags & NGX_STREAM_VAR_INDEXED) { | |
309 return ngx_stream_get_flushed_variable(s, v->index); | |
310 | |
311 } else { | |
312 | |
313 vv = ngx_palloc(s->connection->pool, | |
314 sizeof(ngx_stream_variable_value_t)); | |
315 | |
316 if (vv && v->get_handler(s, vv, v->data) == NGX_OK) { | |
317 return vv; | |
318 } | |
319 | |
320 return NULL; | |
321 } | |
322 } | |
323 | |
324 vv = ngx_palloc(s->connection->pool, sizeof(ngx_stream_variable_value_t)); | |
325 if (vv == NULL) { | |
326 return NULL; | |
327 } | |
328 | |
329 vv->not_found = 1; | |
330 | |
331 return vv; | |
332 } | |
333 | |
334 | |
335 static ngx_int_t | |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
336 ngx_stream_variable_binary_remote_addr(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
337 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
338 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
339 struct sockaddr_in *sin; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
340 #if (NGX_HAVE_INET6) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
341 struct sockaddr_in6 *sin6; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
342 #endif |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
343 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
344 switch (s->connection->sockaddr->sa_family) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
345 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
346 #if (NGX_HAVE_INET6) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
347 case AF_INET6: |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
348 sin6 = (struct sockaddr_in6 *) s->connection->sockaddr; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
349 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
350 v->len = sizeof(struct in6_addr); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
351 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
352 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
353 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
354 v->data = sin6->sin6_addr.s6_addr; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
355 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
356 break; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
357 #endif |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
358 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
359 default: /* AF_INET */ |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
360 sin = (struct sockaddr_in *) s->connection->sockaddr; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
361 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
362 v->len = sizeof(in_addr_t); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
363 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
364 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
365 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
366 v->data = (u_char *) &sin->sin_addr; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
367 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
368 break; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
369 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
370 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
371 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
372 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
373 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
374 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
375 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
376 ngx_stream_variable_remote_addr(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
377 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
378 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
379 v->len = s->connection->addr_text.len; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
380 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
381 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
382 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
383 v->data = s->connection->addr_text.data; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
384 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
385 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
386 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
387 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
388 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
389 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
390 ngx_stream_variable_remote_port(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
391 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
392 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
393 ngx_uint_t port; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
394 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
395 v->len = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
396 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
397 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
398 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
399 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
400 v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
401 if (v->data == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
402 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
403 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
404 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
405 port = ngx_inet_get_port(s->connection->sockaddr); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
406 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
407 if (port > 0 && port < 65536) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
408 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
409 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
410 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
411 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
412 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
413 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
414 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
415 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
416 ngx_stream_variable_server_addr(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
417 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
418 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
419 ngx_str_t str; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
420 u_char addr[NGX_SOCKADDR_STRLEN]; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
421 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
422 str.len = NGX_SOCKADDR_STRLEN; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
423 str.data = addr; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
424 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
425 if (ngx_connection_local_sockaddr(s->connection, &str, 0) != NGX_OK) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
426 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
427 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
428 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
429 str.data = ngx_pnalloc(s->connection->pool, str.len); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
430 if (str.data == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
431 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
432 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
433 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
434 ngx_memcpy(str.data, addr, str.len); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
435 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
436 v->len = str.len; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
437 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
438 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
439 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
440 v->data = str.data; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
441 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
442 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
443 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
444 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
445 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
446 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
447 ngx_stream_variable_server_port(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
448 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
449 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
450 ngx_uint_t port; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
451 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
452 v->len = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
453 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
454 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
455 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
456 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
457 if (ngx_connection_local_sockaddr(s->connection, NULL, 0) != NGX_OK) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
458 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
459 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
460 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
461 v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
462 if (v->data == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
463 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
464 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
465 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
466 port = ngx_inet_get_port(s->connection->local_sockaddr); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
467 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
468 if (port > 0 && port < 65536) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
469 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
470 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
471 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
472 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
473 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
474 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
475 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
476 static ngx_int_t |
6668
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
477 ngx_stream_variable_bytes(ngx_stream_session_t *s, |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
478 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
479 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
480 u_char *p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
481 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
482 p = ngx_pnalloc(s->connection->pool, NGX_OFF_T_LEN); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
483 if (p == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
484 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
485 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
486 |
6668
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
487 if (data == 1) { |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
488 v->len = ngx_sprintf(p, "%O", s->received) - p; |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
489 |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
490 } else { |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
491 v->len = ngx_sprintf(p, "%O", s->connection->sent) - p; |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
492 } |
5e2821c2de46
Stream: the $bytes_received variable.
Vladimir Homutov <vl@nginx.com>
parents:
6608
diff
changeset
|
493 |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
494 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
495 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
496 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
497 v->data = p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
498 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
499 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
500 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
501 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
502 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
503 static ngx_int_t |
6669
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
504 ngx_stream_variable_session_time(ngx_stream_session_t *s, |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
505 ngx_stream_variable_value_t *v, uintptr_t data) |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
506 { |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
507 u_char *p; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
508 ngx_time_t *tp; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
509 ngx_msec_int_t ms; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
510 |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
511 p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4); |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
512 if (p == NULL) { |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
513 return NGX_ERROR; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
514 } |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
515 |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
516 tp = ngx_timeofday(); |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
517 |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
518 ms = (ngx_msec_int_t) |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
519 ((tp->sec - s->start_sec) * 1000 + (tp->msec - s->start_msec)); |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
520 ms = ngx_max(ms, 0); |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
521 |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
522 v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
523 v->valid = 1; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
524 v->no_cacheable = 0; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
525 v->not_found = 0; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
526 v->data = p; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
527 |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
528 return NGX_OK; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
529 } |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
530 |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
531 |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6668
diff
changeset
|
532 static ngx_int_t |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
533 ngx_stream_variable_connection(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
534 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
535 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
536 u_char *p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
537 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
538 p = ngx_pnalloc(s->connection->pool, NGX_ATOMIC_T_LEN); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
539 if (p == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
540 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
541 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
542 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
543 v->len = ngx_sprintf(p, "%uA", s->connection->number) - p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
544 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
545 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
546 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
547 v->data = p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
548 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
549 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
550 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
551 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
552 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
553 static ngx_int_t |
6607 | 554 ngx_stream_variable_nginx_version(ngx_stream_session_t *s, |
555 ngx_stream_variable_value_t *v, uintptr_t data) | |
556 { | |
557 v->len = sizeof(NGINX_VERSION) - 1; | |
558 v->valid = 1; | |
559 v->no_cacheable = 0; | |
560 v->not_found = 0; | |
561 v->data = (u_char *) NGINX_VERSION; | |
562 | |
563 return NGX_OK; | |
564 } | |
565 | |
566 | |
6608
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
567 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
568 ngx_stream_variable_hostname(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
569 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
570 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
571 v->len = ngx_cycle->hostname.len; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
572 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
573 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
574 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
575 v->data = ngx_cycle->hostname.data; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
576 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
577 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
578 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
579 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
580 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
581 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
582 ngx_stream_variable_pid(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
583 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
584 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
585 u_char *p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
586 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
587 p = ngx_pnalloc(s->connection->pool, NGX_INT64_LEN); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
588 if (p == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
589 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
590 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
591 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
592 v->len = ngx_sprintf(p, "%P", ngx_pid) - p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
593 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
594 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
595 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
596 v->data = p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
597 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
598 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
599 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
600 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
601 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
602 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
603 ngx_stream_variable_msec(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
604 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
605 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
606 u_char *p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
607 ngx_time_t *tp; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
608 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
609 p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
610 if (p == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
611 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
612 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
613 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
614 tp = ngx_timeofday(); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
615 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
616 v->len = ngx_sprintf(p, "%T.%03M", tp->sec, tp->msec) - p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
617 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
618 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
619 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
620 v->data = p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
621 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
622 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
623 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
624 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
625 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
626 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
627 ngx_stream_variable_time_iso8601(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
628 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
629 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
630 u_char *p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
631 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
632 p = ngx_pnalloc(s->connection->pool, ngx_cached_http_log_iso8601.len); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
633 if (p == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
634 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
635 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
636 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
637 ngx_memcpy(p, ngx_cached_http_log_iso8601.data, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
638 ngx_cached_http_log_iso8601.len); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
639 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
640 v->len = ngx_cached_http_log_iso8601.len; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
641 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
642 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
643 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
644 v->data = p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
645 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
646 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
647 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
648 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
649 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
650 static ngx_int_t |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
651 ngx_stream_variable_time_local(ngx_stream_session_t *s, |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
652 ngx_stream_variable_value_t *v, uintptr_t data) |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
653 { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
654 u_char *p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
655 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
656 p = ngx_pnalloc(s->connection->pool, ngx_cached_http_log_time.len); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
657 if (p == NULL) { |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
658 return NGX_ERROR; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
659 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
660 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
661 ngx_memcpy(p, ngx_cached_http_log_time.data, ngx_cached_http_log_time.len); |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
662 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
663 v->len = ngx_cached_http_log_time.len; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
664 v->valid = 1; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
665 v->no_cacheable = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
666 v->not_found = 0; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
667 v->data = p; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
668 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
669 return NGX_OK; |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
670 } |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
671 |
eb4293155e87
Stream: core module variables.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
672 |
6670
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
673 static ngx_int_t |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
674 ngx_stream_variable_protocol(ngx_stream_session_t *s, |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
675 ngx_stream_variable_value_t *v, uintptr_t data) |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
676 { |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
677 v->len = 3; |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
678 v->valid = 1; |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
679 v->no_cacheable = 0; |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
680 v->not_found = 0; |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
681 v->data = (u_char *) (s->connection->type == SOCK_DGRAM ? "UDP" : "TCP"); |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
682 |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
683 return NGX_OK; |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
684 } |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
685 |
c6372a40c2a7
Stream: the $protocol variable.
Vladimir Homutov <vl@nginx.com>
parents:
6669
diff
changeset
|
686 |
6607 | 687 void * |
688 ngx_stream_map_find(ngx_stream_session_t *s, ngx_stream_map_t *map, | |
689 ngx_str_t *match) | |
690 { | |
691 void *value; | |
692 u_char *low; | |
693 size_t len; | |
694 ngx_uint_t key; | |
695 | |
696 len = match->len; | |
697 | |
698 if (len) { | |
699 low = ngx_pnalloc(s->connection->pool, len); | |
700 if (low == NULL) { | |
701 return NULL; | |
702 } | |
703 | |
704 } else { | |
705 low = NULL; | |
706 } | |
707 | |
708 key = ngx_hash_strlow(low, match->data, len); | |
709 | |
710 value = ngx_hash_find_combined(&map->hash, key, low, len); | |
711 if (value) { | |
712 return value; | |
713 } | |
714 | |
715 #if (NGX_PCRE) | |
716 | |
717 if (len && map->nregex) { | |
718 ngx_int_t n; | |
719 ngx_uint_t i; | |
720 ngx_stream_map_regex_t *reg; | |
721 | |
722 reg = map->regex; | |
723 | |
724 for (i = 0; i < map->nregex; i++) { | |
725 | |
726 n = ngx_stream_regex_exec(s, reg[i].regex, match); | |
727 | |
728 if (n == NGX_OK) { | |
729 return reg[i].value; | |
730 } | |
731 | |
732 if (n == NGX_DECLINED) { | |
733 continue; | |
734 } | |
735 | |
736 /* NGX_ERROR */ | |
737 | |
738 return NULL; | |
739 } | |
740 } | |
741 | |
742 #endif | |
743 | |
744 return NULL; | |
745 } | |
746 | |
747 | |
748 #if (NGX_PCRE) | |
749 | |
750 static ngx_int_t | |
751 ngx_stream_variable_not_found(ngx_stream_session_t *s, | |
752 ngx_stream_variable_value_t *v, uintptr_t data) | |
753 { | |
754 v->not_found = 1; | |
755 return NGX_OK; | |
756 } | |
757 | |
758 | |
759 ngx_stream_regex_t * | |
760 ngx_stream_regex_compile(ngx_conf_t *cf, ngx_regex_compile_t *rc) | |
761 { | |
762 u_char *p; | |
763 size_t size; | |
764 ngx_str_t name; | |
765 ngx_uint_t i, n; | |
766 ngx_stream_variable_t *v; | |
767 ngx_stream_regex_t *re; | |
768 ngx_stream_regex_variable_t *rv; | |
769 ngx_stream_core_main_conf_t *cmcf; | |
770 | |
771 rc->pool = cf->pool; | |
772 | |
773 if (ngx_regex_compile(rc) != NGX_OK) { | |
774 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc->err); | |
775 return NULL; | |
776 } | |
777 | |
778 re = ngx_pcalloc(cf->pool, sizeof(ngx_stream_regex_t)); | |
779 if (re == NULL) { | |
780 return NULL; | |
781 } | |
782 | |
783 re->regex = rc->regex; | |
784 re->ncaptures = rc->captures; | |
785 re->name = rc->pattern; | |
786 | |
787 cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); | |
788 cmcf->ncaptures = ngx_max(cmcf->ncaptures, re->ncaptures); | |
789 | |
790 n = (ngx_uint_t) rc->named_captures; | |
791 | |
792 if (n == 0) { | |
793 return re; | |
794 } | |
795 | |
796 rv = ngx_palloc(rc->pool, n * sizeof(ngx_stream_regex_variable_t)); | |
797 if (rv == NULL) { | |
798 return NULL; | |
799 } | |
800 | |
801 re->variables = rv; | |
802 re->nvariables = n; | |
803 | |
804 size = rc->name_size; | |
805 p = rc->names; | |
806 | |
807 for (i = 0; i < n; i++) { | |
808 rv[i].capture = 2 * ((p[0] << 8) + p[1]); | |
809 | |
810 name.data = &p[2]; | |
811 name.len = ngx_strlen(name.data); | |
812 | |
813 v = ngx_stream_add_variable(cf, &name, NGX_STREAM_VAR_CHANGEABLE); | |
814 if (v == NULL) { | |
815 return NULL; | |
816 } | |
817 | |
818 rv[i].index = ngx_stream_get_variable_index(cf, &name); | |
819 if (rv[i].index == NGX_ERROR) { | |
820 return NULL; | |
821 } | |
822 | |
823 v->get_handler = ngx_stream_variable_not_found; | |
824 | |
825 p += size; | |
826 } | |
827 | |
828 return re; | |
829 } | |
830 | |
831 | |
832 ngx_int_t | |
833 ngx_stream_regex_exec(ngx_stream_session_t *s, ngx_stream_regex_t *re, | |
834 ngx_str_t *str) | |
835 { | |
836 ngx_int_t rc, index; | |
837 ngx_uint_t i, n, len; | |
838 ngx_stream_variable_value_t *vv; | |
839 ngx_stream_core_main_conf_t *cmcf; | |
840 | |
841 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); | |
842 | |
843 if (re->ncaptures) { | |
844 len = cmcf->ncaptures; | |
845 | |
846 if (s->captures == NULL) { | |
847 s->captures = ngx_palloc(s->connection->pool, len * sizeof(int)); | |
848 if (s->captures == NULL) { | |
849 return NGX_ERROR; | |
850 } | |
851 } | |
852 | |
853 } else { | |
854 len = 0; | |
855 } | |
856 | |
857 rc = ngx_regex_exec(re->regex, str, s->captures, len); | |
858 | |
859 if (rc == NGX_REGEX_NO_MATCHED) { | |
860 return NGX_DECLINED; | |
861 } | |
862 | |
863 if (rc < 0) { | |
864 ngx_log_error(NGX_LOG_ALERT, s->connection->log, 0, | |
865 ngx_regex_exec_n " failed: %i on \"%V\" using \"%V\"", | |
866 rc, str, &re->name); | |
867 return NGX_ERROR; | |
868 } | |
869 | |
870 for (i = 0; i < re->nvariables; i++) { | |
871 | |
872 n = re->variables[i].capture; | |
873 index = re->variables[i].index; | |
874 vv = &s->variables[index]; | |
875 | |
876 vv->len = s->captures[n + 1] - s->captures[n]; | |
877 vv->valid = 1; | |
878 vv->no_cacheable = 0; | |
879 vv->not_found = 0; | |
880 vv->data = &str->data[s->captures[n]]; | |
881 | |
882 #if (NGX_DEBUG) | |
883 { | |
884 ngx_stream_variable_t *v; | |
885 | |
886 v = cmcf->variables.elts; | |
887 | |
888 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
889 "stream regex set $%V to \"%v\"", &v[index].name, vv); | |
890 } | |
891 #endif | |
892 } | |
893 | |
894 s->ncaptures = rc * 2; | |
895 s->captures_data = str->data; | |
896 | |
897 return NGX_OK; | |
898 } | |
899 | |
900 #endif | |
901 | |
902 | |
903 ngx_int_t | |
904 ngx_stream_variables_add_core_vars(ngx_conf_t *cf) | |
905 { | |
906 ngx_int_t rc; | |
907 ngx_stream_variable_t *cv, *v; | |
908 ngx_stream_core_main_conf_t *cmcf; | |
909 | |
910 cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); | |
911 | |
912 cmcf->variables_keys = ngx_pcalloc(cf->temp_pool, | |
913 sizeof(ngx_hash_keys_arrays_t)); | |
914 if (cmcf->variables_keys == NULL) { | |
915 return NGX_ERROR; | |
916 } | |
917 | |
918 cmcf->variables_keys->pool = cf->pool; | |
919 cmcf->variables_keys->temp_pool = cf->pool; | |
920 | |
921 if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL) | |
922 != NGX_OK) | |
923 { | |
924 return NGX_ERROR; | |
925 } | |
926 | |
927 for (cv = ngx_stream_core_variables; cv->name.len; cv++) { | |
928 v = ngx_palloc(cf->pool, sizeof(ngx_stream_variable_t)); | |
929 if (v == NULL) { | |
930 return NGX_ERROR; | |
931 } | |
932 | |
933 *v = *cv; | |
934 | |
935 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, | |
936 NGX_HASH_READONLY_KEY); | |
937 | |
938 if (rc == NGX_OK) { | |
939 continue; | |
940 } | |
941 | |
942 if (rc == NGX_BUSY) { | |
943 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
944 "conflicting variable name \"%V\"", &v->name); | |
945 } | |
946 | |
947 return NGX_ERROR; | |
948 } | |
949 | |
950 return NGX_OK; | |
951 } | |
952 | |
953 | |
954 ngx_int_t | |
955 ngx_stream_variables_init_vars(ngx_conf_t *cf) | |
956 { | |
957 ngx_uint_t i, n; | |
958 ngx_hash_key_t *key; | |
959 ngx_hash_init_t hash; | |
960 ngx_stream_variable_t *v, *av; | |
961 ngx_stream_core_main_conf_t *cmcf; | |
962 | |
963 /* set the handlers for the indexed stream variables */ | |
964 | |
965 cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); | |
966 | |
967 v = cmcf->variables.elts; | |
968 key = cmcf->variables_keys->keys.elts; | |
969 | |
970 for (i = 0; i < cmcf->variables.nelts; i++) { | |
971 | |
972 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { | |
973 | |
974 av = key[n].value; | |
975 | |
976 if (v[i].name.len == key[n].key.len | |
977 && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len) | |
978 == 0) | |
979 { | |
980 v[i].get_handler = av->get_handler; | |
981 v[i].data = av->data; | |
982 | |
983 av->flags |= NGX_STREAM_VAR_INDEXED; | |
984 v[i].flags = av->flags; | |
985 | |
986 av->index = i; | |
987 | |
988 if (av->get_handler == NULL) { | |
989 break; | |
990 } | |
991 | |
992 goto next; | |
993 } | |
994 } | |
995 | |
996 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
997 "unknown \"%V\" variable", &v[i].name); | |
998 | |
999 return NGX_ERROR; | |
1000 | |
1001 next: | |
1002 continue; | |
1003 } | |
1004 | |
1005 | |
1006 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { | |
1007 av = key[n].value; | |
1008 | |
1009 if (av->flags & NGX_STREAM_VAR_NOHASH) { | |
1010 key[n].key.data = NULL; | |
1011 } | |
1012 } | |
1013 | |
1014 | |
1015 hash.hash = &cmcf->variables_hash; | |
1016 hash.key = ngx_hash_key; | |
1017 hash.max_size = cmcf->variables_hash_max_size; | |
1018 hash.bucket_size = cmcf->variables_hash_bucket_size; | |
1019 hash.name = "variables_hash"; | |
1020 hash.pool = cf->pool; | |
1021 hash.temp_pool = NULL; | |
1022 | |
1023 if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts, | |
1024 cmcf->variables_keys->keys.nelts) | |
1025 != NGX_OK) | |
1026 { | |
1027 return NGX_ERROR; | |
1028 } | |
1029 | |
1030 cmcf->variables_keys = NULL; | |
1031 | |
1032 return NGX_OK; | |
1033 } |