changeset 8548:9ffef6054abf quic

HTTP/3: fixed handling request body eof. While for HTTP/1 unexpected eof always means an error, for HTTP/3 an eof right after a DATA frame end means the end of the request body. For this reason, since adding HTTP/3 support, eof no longer produced an error right after recv() but was passed to filters which would make a decision. This decision was made in ngx_http_parse_chunked() and ngx_http_v3_parse_request_body() based on the b->last_buf flag. Now that since 0f7f1a509113 (1.19.2) rb->chunked->length is a lower threshold for the expected number of bytes, it can be set to zero to indicate that more bytes may or may not follow. Now it's possible to move the check for eof from parser functions to ngx_http_request_body_chunked_filter() and clean up the parsing code. Also, in the default branch, in case of eof, the following three things happened, which were replaced with returning NGX_ERROR while implementing HTTP/3: - "client prematurely closed connection" message was logged - c->error flag was set - NGX_HTTP_BAD_REQUEST was returned The change brings back this behavior for HTTP/1 as well as HTTP/3.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 16 Sep 2020 18:59:25 +0100
parents 57e5393e5d40
children d70a38acaea0
files src/http/ngx_http_parse.c src/http/ngx_http_request_body.c src/http/v3/ngx_http_v3_request.c
diffstat 3 files changed, 35 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -2377,11 +2377,6 @@ ngx_http_parse_chunked(ngx_http_request_
         }
     }
 
-    if (b->last_buf) {
-        /* XXX client prematurely closed connection */
-        return NGX_ERROR;
-    }
-
 data:
 
     ctx->state = state;
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -960,6 +960,15 @@ ngx_http_request_body_length_filter(ngx_
             break;
         }
 
+        size = cl->buf->last - cl->buf->pos;
+
+        if (cl->buf->last_buf && (off_t) size < rb->rest) {
+            ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
+                          "client prematurely closed connection");
+            r->connection->error = 1;
+            return NGX_HTTP_BAD_REQUEST;
+        }
+
         tl = ngx_chain_get_free_buf(r->pool, &rb->free);
         if (tl == NULL) {
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -977,8 +986,6 @@ ngx_http_request_body_length_filter(ngx_
         b->end = cl->buf->end;
         b->flush = r->request_body_no_buffering;
 
-        size = cl->buf->last - cl->buf->pos;
-
         if ((off_t) size < rb->rest) {
             cl->buf->pos = cl->buf->last;
             rb->rest -= size;
@@ -990,11 +997,6 @@ ngx_http_request_body_length_filter(ngx_
             b->last_buf = 1;
         }
 
-        if (cl->buf->last_buf && rb->rest > 0) {
-            /* XXX client prematurely closed connection */
-            return NGX_ERROR;
-        }
-
         *ll = tl;
         ll = &tl->next;
     }
@@ -1148,6 +1150,20 @@ ngx_http_request_body_chunked_filter(ngx
                 continue;
             }
 
+            if (rc == NGX_AGAIN && cl->buf->last_buf) {
+
+                /* last body buffer */
+
+                if (rb->chunked->length > 0) {
+                    ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
+                                  "client prematurely closed connection");
+                    r->connection->error = 1;
+                    return NGX_HTTP_BAD_REQUEST;
+                }
+
+                rc = NGX_DONE;
+            }
+
             if (rc == NGX_DONE) {
 
                 /* a whole response has been parsed successfully */
--- a/src/http/v3/ngx_http_v3_request.c
+++ b/src/http/v3/ngx_http_v3_request.c
@@ -379,6 +379,10 @@ ngx_http_v3_parse_request_body(ngx_http_
     ngx_int_t                  rc;
     ngx_connection_t          *c;
     ngx_http_v3_parse_data_t  *st;
+    enum {
+        sw_start = 0,
+        sw_skip
+    };
 
     c = r->connection;
     st = ctx->h3_parse;
@@ -395,12 +399,8 @@ ngx_http_v3_parse_request_body(ngx_http_
         ctx->h3_parse = st;
     }
 
-    if (ctx->size) {
-        ctx->length = ctx->size + 1;
-        return (b->pos == b->last) ? NGX_AGAIN : NGX_OK;
-    }
+    while (b->pos < b->last && ctx->size == 0) {
 
-    while (b->pos < b->last) {
         rc = ngx_http_v3_parse_data(c, st, *b->pos++);
 
         if (rc > 0) {
@@ -414,27 +414,27 @@ ngx_http_v3_parse_request_body(ngx_http_
         }
 
         if (rc == NGX_AGAIN) {
+            ctx->state = sw_skip;
             continue;
         }
 
         /* rc == NGX_DONE */
 
         ctx->size = st->length;
-        return NGX_OK;
+        ctx->state = sw_start;
     }
 
-    if (!b->last_buf) {
+    if (ctx->state == sw_skip) {
         ctx->length = 1;
         return NGX_AGAIN;
     }
 
-    if (st->state) {
-        goto failed;
+    if (b->pos == b->last) {
+        ctx->length = ctx->size;
+        return NGX_AGAIN;
     }
 
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header done");
-
-    return NGX_DONE;
+    return NGX_OK;
 
 failed: