comparison src/stream/ngx_stream_handler.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 7357abd1fa8c
children 9cac11efb205
comparison
equal deleted inserted replaced
6681:b9f78a4e3597 6682:db422604ceb0
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_proxy_protocol_handler(ngx_event_t *rev);
17 static void ngx_stream_init_session_handler(ngx_event_t *rev); 17 static void ngx_stream_init_session_handler(ngx_event_t *rev);
18 static void ngx_stream_init_session(ngx_connection_t *c);
19 18
20 #if (NGX_STREAM_SSL) 19 #if (NGX_STREAM_SSL)
21 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c); 20 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c);
22 static void ngx_stream_ssl_handshake_handler(ngx_connection_t *c); 21 static void ngx_stream_ssl_handshake_handler(ngx_connection_t *c);
23 #endif 22 #endif
152 c->log->handler = ngx_stream_log_error; 151 c->log->handler = ngx_stream_log_error;
153 c->log->data = s; 152 c->log->data = s;
154 c->log->action = "initializing connection"; 153 c->log->action = "initializing connection";
155 c->log_error = NGX_ERROR_INFO; 154 c->log_error = NGX_ERROR_INFO;
156 155
156 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_stream_max_module);
157 if (s->ctx == NULL) {
158 ngx_stream_close_connection(c);
159 return;
160 }
161
157 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); 162 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module);
158 163
159 s->variables = ngx_pcalloc(s->connection->pool, 164 s->variables = ngx_pcalloc(s->connection->pool,
160 cmcf->variables.nelts 165 cmcf->variables.nelts
161 * sizeof(ngx_stream_variable_value_t)); 166 * sizeof(ngx_stream_variable_value_t));
363 return; 368 return;
364 } 369 }
365 } 370 }
366 #endif 371 #endif
367 372
368 ngx_stream_init_session(c); 373 c->log->action = "handling client connection";
369 } 374
370 375 cscf->handler(s);
376 }
377
378
379 #if (NGX_STREAM_SSL)
371 380
372 static void 381 static void
373 ngx_stream_init_session(ngx_connection_t *c) 382 ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
383 {
384 ngx_stream_session_t *s;
385 ngx_stream_ssl_conf_t *sslcf;
386
387 s = c->data;
388
389 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
390 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
391 return;
392 }
393
394 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
395 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module);
396
397 ngx_add_timer(c->read, sslcf->handshake_timeout);
398
399 c->ssl->handler = ngx_stream_ssl_handshake_handler;
400
401 return;
402 }
403
404 ngx_stream_ssl_handshake_handler(c);
405 }
406
407
408 static void
409 ngx_stream_ssl_handshake_handler(ngx_connection_t *c)
374 { 410 {
375 ngx_stream_session_t *s; 411 ngx_stream_session_t *s;
376 ngx_stream_core_srv_conf_t *cscf; 412 ngx_stream_core_srv_conf_t *cscf;
377 413
378 s = c->data;
379 c->log->action = "handling client connection";
380
381 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
382
383 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_stream_max_module);
384 if (s->ctx == NULL) {
385 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
386 return;
387 }
388
389 cscf->handler(s);
390 }
391
392
393 #if (NGX_STREAM_SSL)
394
395 static void
396 ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
397 {
398 ngx_stream_session_t *s;
399 ngx_stream_ssl_conf_t *sslcf;
400
401 s = c->data;
402
403 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
404 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
405 return;
406 }
407
408 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
409 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module);
410
411 ngx_add_timer(c->read, sslcf->handshake_timeout);
412
413 c->ssl->handler = ngx_stream_ssl_handshake_handler;
414
415 return;
416 }
417
418 ngx_stream_ssl_handshake_handler(c);
419 }
420
421
422 static void
423 ngx_stream_ssl_handshake_handler(ngx_connection_t *c)
424 {
425 if (!c->ssl->handshaked) { 414 if (!c->ssl->handshaked) {
426 ngx_stream_finalize_session(c->data, NGX_STREAM_INTERNAL_SERVER_ERROR); 415 ngx_stream_finalize_session(c->data, NGX_STREAM_INTERNAL_SERVER_ERROR);
427 return; 416 return;
428 } 417 }
429 418
430 if (c->read->timer_set) { 419 if (c->read->timer_set) {
431 ngx_del_timer(c->read); 420 ngx_del_timer(c->read);
432 } 421 }
433 422
434 ngx_stream_init_session(c); 423 c->log->action = "handling client connection";
424
425 s = c->data;
426
427 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
428
429 cscf->handler(s);
435 } 430 }
436 431
437 #endif 432 #endif
438 433
439 434