diff --git a/http.go b/http.go index 0508857d9b..9f0de04377 100644 --- a/http.go +++ b/http.go @@ -1268,7 +1268,7 @@ func (req *Request) ContinueReadBody(r *bufio.Reader, maxBodySize int, preParseM return err } - if req.Header.ContentLength() == -1 { + if contentLength == -1 { err = req.Header.ReadTrailer(r) if err != nil && err != io.EOF { return err @@ -1290,6 +1290,9 @@ func (req *Request) ReadBody(r *bufio.Reader, contentLength int, maxBodySize int } else if contentLength == -1 { bodyBuf.B, err = readBodyChunked(r, maxBodySize, bodyBuf.B) + if err == nil && len(bodyBuf.B) == 0 { + req.Header.SetContentLength(0) + } } else { bodyBuf.B, err = readBodyIdentity(r, maxBodySize, bodyBuf.B) diff --git a/http_test.go b/http_test.go index 32194a84ca..42feb56967 100644 --- a/http_test.go +++ b/http_test.go @@ -2011,6 +2011,25 @@ func TestRequestReadChunked(t *testing.T) { verifyTrailer(t, rb, map[string]string{"Trail": "test"}, true) } +func TestRequestChunkedEmpty(t *testing.T) { + t.Parallel() + + var req Request + + s := "POST /foo HTTP/1.1\r\nHost: google.com\r\nTransfer-Encoding: chunked\r\nContent-Type: aa/bb\r\n\r\n0\r\n\r\n" + r := bytes.NewBufferString(s) + rb := bufio.NewReader(r) + err := req.Read(rb) + if err != nil { + t.Fatalf("Unexpected error when reading chunked request: %v", err) + } + expectedBody := "" + if string(req.Body()) != expectedBody { + t.Fatalf("Unexpected body %q. Expected %q", req.Body(), expectedBody) + } + expectRequestHeaderGet(t, &req.Header, HeaderTransferEncoding, "") +} + // See: https://github.com/erikdubbelboer/fasthttp/issues/34 func TestRequestChunkedWhitespace(t *testing.T) { t.Parallel()