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