]> git.kaiwu.me - nginx.git/commitdiff
HTTP/3: fixed handling request body eof.
authorRoman Arutyunyan <arut@nginx.com>
Wed, 16 Sep 2020 17:59:25 +0000 (18:59 +0100)
committerRoman Arutyunyan <arut@nginx.com>
Wed, 16 Sep 2020 17:59:25 +0000 (18:59 +0100)
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.

src/http/ngx_http_parse.c
src/http/ngx_http_request_body.c
src/http/v3/ngx_http_v3_request.c

index a351f2b27d3701f699dd5846a6456387867ea32a..2015f56b2a1a8c96714df2222e4f367fef1b84c0 100644 (file)
@@ -2377,11 +2377,6 @@ ngx_http_parse_chunked(ngx_http_request_t *r, ngx_buf_t *b,
         }
     }
 
-    if (b->last_buf) {
-        /* XXX client prematurely closed connection */
-        return NGX_ERROR;
-    }
-
 data:
 
     ctx->state = state;
index 7a9bbdd5d4793c154740863f7257db7f9d86b555..a6dbcf502f7074ed56c022b1aa621b22743372df 100644 (file)
@@ -960,6 +960,15 @@ ngx_http_request_body_length_filter(ngx_http_request_t *r, ngx_chain_t *in)
             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_http_request_t *r, ngx_chain_t *in)
         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_http_request_t *r, ngx_chain_t *in)
             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_http_request_t *r, ngx_chain_t *in)
                 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 */
index 24ad771d646b8f62672867b86b00be4971d5dc63..fe3c79bf0835dbcab47c534505465f43bac656b8 100644 (file)
@@ -379,6 +379,10 @@ ngx_http_v3_parse_request_body(ngx_http_request_t *r, ngx_buf_t *b,
     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_request_t *r, ngx_buf_t *b,
         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_request_t *r, ngx_buf_t *b,
         }
 
         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: