Fixed transfer-encoding for empty chunked payload (#1536)

This commit is contained in:
Abe Saiovici
2023-04-12 16:51:30 -04:00
committed by GitHub
parent 6b958c2c22
commit d76662b46d
2 changed files with 23 additions and 1 deletions
+4 -1
View File
@@ -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)
+19
View File
@@ -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()