# HG changeset patch # User Roman Arutyunyan # Date 1600279165 -3600 # Node ID 9ffef6054abfd4d36fbd059218cf5d1dec37e669 # Parent 57e5393e5d40772d26aa05920992d2eb0a46bd56 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. diff -r 57e5393e5d40 -r 9ffef6054abf src/http/ngx_http_parse.c --- a/src/http/ngx_http_parse.c Fri Sep 11 10:56:05 2020 +0300 +++ b/src/http/ngx_http_parse.c Wed Sep 16 18:59:25 2020 +0100 @@ -2377,11 +2377,6 @@ } } - if (b->last_buf) { - /* XXX client prematurely closed connection */ - return NGX_ERROR; - } - data: ctx->state = state; diff -r 57e5393e5d40 -r 9ffef6054abf src/http/ngx_http_request_body.c --- a/src/http/ngx_http_request_body.c Fri Sep 11 10:56:05 2020 +0300 +++ b/src/http/ngx_http_request_body.c Wed Sep 16 18:59:25 2020 +0100 @@ -960,6 +960,15 @@ 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 @@ 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 @@ 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 @@ 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 */ diff -r 57e5393e5d40 -r 9ffef6054abf src/http/v3/ngx_http_v3_request.c --- a/src/http/v3/ngx_http_v3_request.c Fri Sep 11 10:56:05 2020 +0300 +++ b/src/http/v3/ngx_http_v3_request.c Wed Sep 16 18:59:25 2020 +0100 @@ -379,6 +379,10 @@ 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 @@ 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 @@ } 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: