]> git.kaiwu.me - nginx.git/commitdiff
HTTP/2: made it possible to flush response headers (ticket #1743).
authorMaxim Dounin <mdounin@mdounin.ru>
Wed, 2 Feb 2022 22:44:38 +0000 (01:44 +0300)
committerMaxim Dounin <mdounin@mdounin.ru>
Wed, 2 Feb 2022 22:44:38 +0000 (01:44 +0300)
Response headers can be buffered in the SSL buffer.  But stream's fake
connection buffered flag did not reflect this, so any attempts to flush
the buffer without sending additional data were stopped by the write filter.

It does not seem to be possible to reflect this in fc->buffered though, as
we never known if main connection's c->buffered corresponds to the particular
stream or not.  As such, fc->buffered might prevent request finalization
due to sending data on some other stream.

Fix is to implement handling of flush buffers when the c->need_flush_buf
flag is set, similarly to the existing last buffer handling.  The same
flag is now used for UDP sockets in the stream module instead of explicit
checking of c->type.

src/core/ngx_connection.h
src/event/ngx_event_connect.c
src/event/ngx_event_udp.c
src/http/ngx_http_write_filter_module.c
src/http/v2/ngx_http_v2_filter_module.c
src/stream/ngx_stream_write_filter_module.c

index 8cc14755ad46cbc82a1bf5464c043c2baa6c9ee2..6d3348d9c26d28cbd535a2bca44395d2e12bdebd 100644 (file)
@@ -184,6 +184,7 @@ struct ngx_connection_s {
     unsigned            tcp_nopush:2;    /* ngx_connection_tcp_nopush_e */
 
     unsigned            need_last_buf:1;
+    unsigned            need_flush_buf:1;
 
 #if (NGX_HAVE_SENDFILE_NODISKIO || NGX_COMPAT)
     unsigned            busy_count:2;
index adbbde6336e9c021f71791fb016b3c84df273150..668084a7bb919ece3406e34aea7d8a23734ebce0 100644 (file)
@@ -179,6 +179,8 @@ ngx_event_connect_peer(ngx_peer_connection_t *pc)
         c->recv = ngx_udp_recv;
         c->send = ngx_send;
         c->send_chain = ngx_udp_send_chain;
+
+        c->need_flush_buf = 1;
     }
 
     c->log_error = pc->log_error;
index 640b37757a7ed611ab0cd3c8ed70a3b9dc5de97b..1ac82c7f5e2e1a9af104f7e00c6756e700482813 100644 (file)
@@ -246,6 +246,8 @@ ngx_event_recvmsg(ngx_event_t *ev)
         c->send = ngx_udp_send;
         c->send_chain = ngx_udp_send_chain;
 
+        c->need_flush_buf = 1;
+
         c->log = log;
         c->pool->log = log;
         c->listening = ls;
index 932f26dd3055c5da5f173aa1e0bcfc40db27895c..89ab49e124647674dae05512414828ddc8028c90 100644 (file)
@@ -227,7 +227,8 @@ ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
 
     if (size == 0
         && !(c->buffered & NGX_LOWLEVEL_BUFFERED)
-        && !(last && c->need_last_buf))
+        && !(last && c->need_last_buf)
+        && !(flush && c->need_flush_buf))
     {
         if (last || flush || sync) {
             for (cl = r->out; cl; /* void */) {
index 9ffb155dfd16343d7117236ffd8873838759385c..62b6a047ca7ae8cadd55247114e5a5ccb98c762e 100644 (file)
@@ -665,6 +665,7 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
 
     fc->send_chain = ngx_http_v2_send_chain;
     fc->need_last_buf = 1;
+    fc->need_flush_buf = 1;
 
     return ngx_http_v2_filter_send(fc, stream);
 }
@@ -1815,7 +1816,11 @@ ngx_http_v2_waiting_queue(ngx_http_v2_connection_t *h2c,
 static ngx_inline ngx_int_t
 ngx_http_v2_filter_send(ngx_connection_t *fc, ngx_http_v2_stream_t *stream)
 {
-    if (stream->queued == 0) {
+    ngx_connection_t  *c;
+
+    c = stream->connection->connection;
+
+    if (stream->queued == 0 && !c->buffered) {
         fc->buffered &= ~NGX_HTTP_V2_BUFFERED;
         return NGX_OK;
     }
index 156a61c3d5ac65f658b548e3c02e89eb4c27e4a2..07dc7b52e394dcd8ba2bce000d77ad2cdd811afc 100644 (file)
@@ -235,7 +235,7 @@ ngx_stream_write_filter(ngx_stream_session_t *s, ngx_chain_t *in,
     if (size == 0
         && !(c->buffered & NGX_LOWLEVEL_BUFFERED)
         && !(last && c->need_last_buf)
-        && !(c->type == SOCK_DGRAM && flush))
+        && !(flush && c->need_flush_buf))
     {
         if (last || flush || sync) {
             for (cl = *out; cl; /* void */) {