[nginx] HTTP/3: postponed session creation to init() callback.

Roman Arutyunyan arut at nginx.com
Fri Sep 22 15:36:34 UTC 2023


details:   https://hg.nginx.org/nginx/rev/4939fd04737f
branches:  
changeset: 9161:4939fd04737f
user:      Roman Arutyunyan <arut at nginx.com>
date:      Thu Sep 14 14:13:43 2023 +0400
description:
HTTP/3: postponed session creation to init() callback.

Now the session object is assigned to c->data while ngx_http_connection_t
object is referenced by its http_connection field, similar to
ngx_http_v2_connection_t and ngx_http_request_t.

The change allows to eliminate v3_session field from ngx_http_connection_t.
The field was under NGX_HTTP_V3 macro, which was a source of binary
compatibility problems when nginx/module is build with/without HTTP/3 support.

Postponing is essential since c->data should retain the reference to
ngx_http_connection_t object throughout QUIC handshake, because SSL callbacks
ngx_http_ssl_servername() and ngx_http_ssl_alpn_select() rely on this.

diffstat:

 src/http/ngx_http_request.h       |  4 ----
 src/http/v3/ngx_http_v3.c         |  4 +++-
 src/http/v3/ngx_http_v3.h         |  9 ++++++---
 src/http/v3/ngx_http_v3_request.c |  9 ++++-----
 4 files changed, 13 insertions(+), 13 deletions(-)

diffs (89 lines):

diff -r dd5fd5719027 -r 4939fd04737f src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h	Thu Sep 21 19:32:38 2023 +0400
+++ b/src/http/ngx_http_request.h	Thu Sep 14 14:13:43 2023 +0400
@@ -324,10 +324,6 @@ typedef struct {
 #endif
 #endif
 
-#if (NGX_HTTP_V3 || NGX_COMPAT)
-    ngx_http_v3_session_t            *v3_session;
-#endif
-
     ngx_chain_t                      *busy;
     ngx_int_t                         nbusy;
 
diff -r dd5fd5719027 -r 4939fd04737f src/http/v3/ngx_http_v3.c
--- a/src/http/v3/ngx_http_v3.c	Thu Sep 21 19:32:38 2023 +0400
+++ b/src/http/v3/ngx_http_v3.c	Thu Sep 14 14:13:43 2023 +0400
@@ -30,6 +30,8 @@ ngx_http_v3_init_session(ngx_connection_
         goto failed;
     }
 
+    h3c->http_connection = hc;
+
     ngx_queue_init(&h3c->blocked);
 
     h3c->keepalive.log = c->log;
@@ -48,7 +50,7 @@ ngx_http_v3_init_session(ngx_connection_
     cln->handler = ngx_http_v3_cleanup_session;
     cln->data = h3c;
 
-    hc->v3_session = h3c;
+    c->data = h3c;
 
     return NGX_OK;
 
diff -r dd5fd5719027 -r 4939fd04737f src/http/v3/ngx_http_v3.h
--- a/src/http/v3/ngx_http_v3.h	Thu Sep 21 19:32:38 2023 +0400
+++ b/src/http/v3/ngx_http_v3.h	Thu Sep 14 14:13:43 2023 +0400
@@ -78,11 +78,12 @@
 #define NGX_HTTP_V3_ERR_DECODER_STREAM_ERROR       0x202
 
 
-#define ngx_http_quic_get_connection(c)                                       \
-    ((ngx_http_connection_t *) ((c)->quic ? (c)->quic->parent->data           \
+#define ngx_http_v3_get_session(c)                                            \
+    ((ngx_http_v3_session_t *) ((c)->quic ? (c)->quic->parent->data           \
                                           : (c)->data))
 
-#define ngx_http_v3_get_session(c)  ngx_http_quic_get_connection(c)->v3_session
+#define ngx_http_quic_get_connection(c)                                       \
+    (ngx_http_v3_get_session(c)->http_connection)
 
 #define ngx_http_v3_get_module_loc_conf(c, module)                            \
     ngx_http_get_module_loc_conf(ngx_http_quic_get_connection(c)->conf_ctx,   \
@@ -120,6 +121,8 @@ struct ngx_http_v3_parse_s {
 
 
 struct ngx_http_v3_session_s {
+    ngx_http_connection_t        *http_connection;
+
     ngx_http_v3_dynamic_table_t   table;
 
     ngx_event_t                   keepalive;
diff -r dd5fd5719027 -r 4939fd04737f src/http/v3/ngx_http_v3_request.c
--- a/src/http/v3/ngx_http_v3_request.c	Thu Sep 21 19:32:38 2023 +0400
+++ b/src/http/v3/ngx_http_v3_request.c	Thu Sep 14 14:13:43 2023 +0400
@@ -69,11 +69,6 @@ ngx_http_v3_init_stream(ngx_connection_t
     clcf = ngx_http_get_module_loc_conf(hc->conf_ctx, ngx_http_core_module);
 
     if (c->quic == NULL) {
-        if (ngx_http_v3_init_session(c) != NGX_OK) {
-            ngx_http_close_connection(c);
-            return;
-        }
-
         h3scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v3_module);
         h3scf->quic.idle_timeout = clcf->keepalive_timeout;
 
@@ -113,6 +108,10 @@ ngx_http_v3_init(ngx_connection_t *c)
 
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 init");
 
+    if (ngx_http_v3_init_session(c) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
     h3c = ngx_http_v3_get_session(c);
     clcf = ngx_http_v3_get_module_loc_conf(c, ngx_http_core_module);
     ngx_add_timer(&h3c->keepalive, clcf->keepalive_timeout);


More information about the nginx-devel mailing list