Skip to content

Commit

Permalink
Fixed transfer-encoding for empty chunked payload (#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
asaio001 authored Apr 12, 2023
1 parent 6b958c2 commit d76662b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d76662b

Please sign in to comment.