comparison src/stream/ngx_stream_handler.c @ 6674:38143d1abdec

Stream: the $status variable. The stream session status is one of the following: 200 - normal completion 403 - access forbidden 500 - internal server error 502 - bad gateway 503 - limit conn
author Roman Arutyunyan <arut@nginx.com>
date Thu, 11 Aug 2016 20:22:23 +0300
parents 164a0824ce20
children 0125b151c9a5
comparison
equal deleted inserted replaced
6673:e4c1f5b32868 6674:38143d1abdec
9 #include <ngx_core.h> 9 #include <ngx_core.h>
10 #include <ngx_event.h> 10 #include <ngx_event.h>
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 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);
15 static void ngx_stream_init_session(ngx_connection_t *c); 16 static void ngx_stream_init_session(ngx_connection_t *c);
16 17
17 #if (NGX_STREAM_SSL) 18 #if (NGX_STREAM_SSL)
18 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c); 19 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c);
164 s->start_msec = tp->msec; 165 s->start_msec = tp->msec;
165 166
166 if (cmcf->limit_conn_handler) { 167 if (cmcf->limit_conn_handler) {
167 rc = cmcf->limit_conn_handler(s); 168 rc = cmcf->limit_conn_handler(s);
168 169
169 if (rc != NGX_DECLINED) { 170 if (rc == NGX_ERROR) {
170 ngx_stream_close_connection(c); 171 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
172 return;
173 }
174
175 if (rc == NGX_ABORT) {
176 ngx_stream_finalize_session(s, NGX_STREAM_SERVICE_UNAVAILABLE);
171 return; 177 return;
172 } 178 }
173 } 179 }
174 180
175 if (cmcf->access_handler) { 181 if (cmcf->access_handler) {
176 rc = cmcf->access_handler(s); 182 rc = cmcf->access_handler(s);
177 183
178 if (rc != NGX_OK && rc != NGX_DECLINED) { 184 if (rc == NGX_ERROR) {
179 ngx_stream_close_connection(c); 185 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
186 return;
187 }
188
189 if (rc == NGX_ABORT) {
190 ngx_stream_finalize_session(s, NGX_STREAM_FORBIDDEN);
180 return; 191 return;
181 } 192 }
182 } 193 }
183 194
184 if (c->type == SOCK_STREAM 195 if (c->type == SOCK_STREAM
192 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, 203 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
193 (const void *) &tcp_nodelay, sizeof(int)) == -1) 204 (const void *) &tcp_nodelay, sizeof(int)) == -1)
194 { 205 {
195 ngx_connection_error(c, ngx_socket_errno, 206 ngx_connection_error(c, ngx_socket_errno,
196 "setsockopt(TCP_NODELAY) failed"); 207 "setsockopt(TCP_NODELAY) failed");
197 ngx_stream_close_connection(c); 208 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
198 return; 209 return;
199 } 210 }
200 211
201 c->tcp_nodelay = NGX_TCP_NODELAY_SET; 212 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
202 } 213 }
213 224
214 if (sslcf->ssl.ctx == NULL) { 225 if (sslcf->ssl.ctx == NULL) {
215 ngx_log_error(NGX_LOG_ERR, c->log, 0, 226 ngx_log_error(NGX_LOG_ERR, c->log, 0,
216 "no \"ssl_certificate\" is defined " 227 "no \"ssl_certificate\" is defined "
217 "in server listening on SSL port"); 228 "in server listening on SSL port");
218 ngx_stream_close_connection(c); 229 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
219 return; 230 return;
220 } 231 }
221 232
222 ngx_stream_ssl_init_connection(&sslcf->ssl, c); 233 ngx_stream_ssl_init_connection(&sslcf->ssl, c);
223 return; 234 return;
240 251
241 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module); 252 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
242 253
243 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_stream_max_module); 254 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_stream_max_module);
244 if (s->ctx == NULL) { 255 if (s->ctx == NULL) {
245 ngx_stream_close_connection(c); 256 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
246 return; 257 return;
247 } 258 }
248 259
249 cscf->handler(s); 260 cscf->handler(s);
250 } 261 }
256 ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c) 267 ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
257 { 268 {
258 ngx_stream_session_t *s; 269 ngx_stream_session_t *s;
259 ngx_stream_ssl_conf_t *sslcf; 270 ngx_stream_ssl_conf_t *sslcf;
260 271
272 s = c->data;
273
261 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) { 274 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
262 ngx_stream_close_connection(c); 275 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
263 return; 276 return;
264 } 277 }
265 278
266 if (ngx_ssl_handshake(c) == NGX_AGAIN) { 279 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
267
268 s = c->data;
269
270 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module); 280 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module);
271 281
272 ngx_add_timer(c->read, sslcf->handshake_timeout); 282 ngx_add_timer(c->read, sslcf->handshake_timeout);
273 283
274 c->ssl->handler = ngx_stream_ssl_handshake_handler; 284 c->ssl->handler = ngx_stream_ssl_handshake_handler;
282 292
283 static void 293 static void
284 ngx_stream_ssl_handshake_handler(ngx_connection_t *c) 294 ngx_stream_ssl_handshake_handler(ngx_connection_t *c)
285 { 295 {
286 if (!c->ssl->handshaked) { 296 if (!c->ssl->handshaked) {
287 ngx_stream_close_connection(c); 297 ngx_stream_finalize_session(c->data, NGX_STREAM_INTERNAL_SERVER_ERROR);
288 return; 298 return;
289 } 299 }
290 300
291 if (c->read->timer_set) { 301 if (c->read->timer_set) {
292 ngx_del_timer(c->read); 302 ngx_del_timer(c->read);
297 307
298 #endif 308 #endif
299 309
300 310
301 void 311 void
312 ngx_stream_finalize_session(ngx_stream_session_t *s, ngx_uint_t rc)
313 {
314 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
315 "finalize stream session: %i", rc);
316
317 s->status = rc;
318
319 ngx_stream_close_connection(s->connection);
320 }
321
322
323 static void
302 ngx_stream_close_connection(ngx_connection_t *c) 324 ngx_stream_close_connection(ngx_connection_t *c)
303 { 325 {
304 ngx_pool_t *pool; 326 ngx_pool_t *pool;
305 327
306 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, 328 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0,