Skip to content

Commit

Permalink
Allow whitespace after chunk size
Browse files Browse the repository at this point in the history
There seems to be servers/load balancers that insert whitespaces
between the chunk-size number and \r\n.
  • Loading branch information
erikdubbelboer authored and kirillDanshin committed Aug 27, 2018
1 parent e277e51 commit 26aa8e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
21 changes: 14 additions & 7 deletions http.go
Expand Up @@ -1679,14 +1679,21 @@ func parseChunkSize(r *bufio.Reader) (int, error) {
if err != nil {
return -1, err
}
c, err := r.ReadByte()
if err != nil {
return -1, fmt.Errorf("cannot read '\r' char at the end of chunk size: %s", err)
}
if c != '\r' {
return -1, fmt.Errorf("unexpected char %q at the end of chunk size. Expected %q", c, '\r')
for {
c, err := r.ReadByte()
if err != nil {
return -1, fmt.Errorf("cannot read '\r' char at the end of chunk size: %s", err)
}
// Skip any trailing whitespace after chunk size.
if c == ' ' {
continue
}
if c != '\r' {
return -1, fmt.Errorf("unexpected char %q at the end of chunk size. Expected %q", c, '\r')
}
break
}
c, err = r.ReadByte()
c, err := r.ReadByte()
if err != nil {
return -1, fmt.Errorf("cannot read '\n' char at the end of chunk size: %s", err)
}
Expand Down
17 changes: 17 additions & 0 deletions http_test.go
Expand Up @@ -1300,6 +1300,23 @@ func TestRequestReadChunked(t *testing.T) {
verifyTrailer(t, rb, "trail")
}

// See: https://github.com/erikdubbelboer/fasthttp/issues/34
func TestRequestChunkedWhitespace(t *testing.T) {
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\n3 \r\nabc\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: %s", err)
}
expectedBody := "abc"
if string(req.Body()) != expectedBody {
t.Fatalf("Unexpected body %q. Expected %q", req.Body(), expectedBody)
}
}

func TestResponseReadWithoutBody(t *testing.T) {
var resp Response

Expand Down

0 comments on commit 26aa8e5

Please sign in to comment.