Permalink
Browse files

Allow whitespace after chunk size

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 17, 2018
1 parent e277e51 commit 26aa8e51e815bcc130b148439d174a9db986f5e8
Showing with 31 additions and 7 deletions.
  1. +14 −7 http.go
  2. +17 −0 http_test.go
View
21 http.go
@@ -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)
}
View
@@ -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

0 comments on commit 26aa8e5

Please sign in to comment.