Mercurial > hg > nginx
comparison src/stream/ngx_stream_handler.c @ 6680:7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 06 Sep 2016 21:28:16 +0300 |
parents | 40e8ce405859 |
children | db422604ceb0 |
comparison
equal
deleted
inserted
replaced
6679:40e8ce405859 | 6680:7357abd1fa8c |
---|---|
11 #include <ngx_stream.h> | 11 #include <ngx_stream.h> |
12 | 12 |
13 | 13 |
14 static void ngx_stream_close_connection(ngx_connection_t *c); | 14 static void ngx_stream_close_connection(ngx_connection_t *c); |
15 static u_char *ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len); | 15 static u_char *ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len); |
16 static void ngx_stream_proxy_protocol_handler(ngx_event_t *rev); | |
16 static void ngx_stream_init_session_handler(ngx_event_t *rev); | 17 static void ngx_stream_init_session_handler(ngx_event_t *rev); |
17 static void ngx_stream_init_session(ngx_connection_t *c); | 18 static void ngx_stream_init_session(ngx_connection_t *c); |
18 | 19 |
19 #if (NGX_STREAM_SSL) | 20 #if (NGX_STREAM_SSL) |
20 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c); | 21 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c); |
169 s->start_msec = tp->msec; | 170 s->start_msec = tp->msec; |
170 | 171 |
171 rev = c->read; | 172 rev = c->read; |
172 rev->handler = ngx_stream_init_session_handler; | 173 rev->handler = ngx_stream_init_session_handler; |
173 | 174 |
175 if (addr_conf->proxy_protocol) { | |
176 c->log->action = "reading PROXY protocol"; | |
177 | |
178 rev->handler = ngx_stream_proxy_protocol_handler; | |
179 | |
180 if (!rev->ready) { | |
181 ngx_add_timer(rev, cscf->proxy_protocol_timeout); | |
182 | |
183 if (ngx_handle_read_event(rev, 0) != NGX_OK) { | |
184 ngx_stream_finalize_session(s, | |
185 NGX_STREAM_INTERNAL_SERVER_ERROR); | |
186 } | |
187 | |
188 return; | |
189 } | |
190 } | |
191 | |
174 if (ngx_use_accept_mutex) { | 192 if (ngx_use_accept_mutex) { |
175 ngx_post_event(rev, &ngx_posted_events); | 193 ngx_post_event(rev, &ngx_posted_events); |
176 return; | 194 return; |
177 } | 195 } |
178 | 196 |
179 rev->handler(rev); | 197 rev->handler(rev); |
198 } | |
199 | |
200 | |
201 static void | |
202 ngx_stream_proxy_protocol_handler(ngx_event_t *rev) | |
203 { | |
204 u_char *p, buf[NGX_PROXY_PROTOCOL_MAX_HEADER]; | |
205 size_t size; | |
206 ssize_t n; | |
207 ngx_err_t err; | |
208 ngx_connection_t *c; | |
209 ngx_stream_session_t *s; | |
210 ngx_stream_core_srv_conf_t *cscf; | |
211 | |
212 c = rev->data; | |
213 s = c->data; | |
214 | |
215 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, | |
216 "stream PROXY protocol handler"); | |
217 | |
218 if (rev->timedout) { | |
219 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); | |
220 ngx_stream_finalize_session(s, NGX_STREAM_OK); | |
221 return; | |
222 } | |
223 | |
224 n = recv(c->fd, (char *) buf, sizeof(buf), MSG_PEEK); | |
225 | |
226 err = ngx_socket_errno; | |
227 | |
228 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "recv(): %z", n); | |
229 | |
230 if (n == -1) { | |
231 if (err == NGX_EAGAIN) { | |
232 rev->ready = 0; | |
233 | |
234 if (!rev->timer_set) { | |
235 cscf = ngx_stream_get_module_srv_conf(s, | |
236 ngx_stream_core_module); | |
237 | |
238 ngx_add_timer(rev, cscf->proxy_protocol_timeout); | |
239 } | |
240 | |
241 if (ngx_handle_read_event(rev, 0) != NGX_OK) { | |
242 ngx_stream_finalize_session(s, | |
243 NGX_STREAM_INTERNAL_SERVER_ERROR); | |
244 } | |
245 | |
246 return; | |
247 } | |
248 | |
249 ngx_connection_error(c, err, "recv() failed"); | |
250 | |
251 ngx_stream_finalize_session(s, NGX_STREAM_OK); | |
252 return; | |
253 } | |
254 | |
255 if (rev->timer_set) { | |
256 ngx_del_timer(rev); | |
257 } | |
258 | |
259 p = ngx_proxy_protocol_read(c, buf, buf + n); | |
260 | |
261 if (p == NULL) { | |
262 ngx_stream_finalize_session(s, NGX_STREAM_BAD_REQUEST); | |
263 return; | |
264 } | |
265 | |
266 size = p - buf; | |
267 | |
268 if (c->recv(c, buf, size) != (ssize_t) size) { | |
269 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); | |
270 return; | |
271 } | |
272 | |
273 ngx_stream_init_session_handler(rev); | |
180 } | 274 } |
181 | 275 |
182 | 276 |
183 static void | 277 static void |
184 ngx_stream_init_session_handler(ngx_event_t *rev) | 278 ngx_stream_init_session_handler(ngx_event_t *rev) |