From 6d7e784c25a864ac3d5bd49346fe5c2db513c08a Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Mon, 10 Aug 2015 11:02:51 +0100 Subject: [PATCH] review feedback --- proxy/chunked.go | 18 ++++++++---------- proxy/chunked_test.go | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/proxy/chunked.go b/proxy/chunked.go index e472e628..0e731186 100644 --- a/proxy/chunked.go +++ b/proxy/chunked.go @@ -10,8 +10,6 @@ import ( "strconv" ) -const maxLineLength = 4096 // assumed <= bufio.defaultBufSize - var ( ErrLineTooLong = errors.New("header line too long") ErrInvalidChunkLength = errors.New("invalid byte in chunk length") @@ -89,22 +87,25 @@ func (cr *ChunkedReader) Err() error { return cr.err } -func (cr *ChunkedReader) beginChunk() (n uint64) { +func (cr *ChunkedReader) beginChunk() uint64 { + var ( + line []byte + n uint64 + ) // chunk-size CRLF - var line []byte line, cr.err = readLine(cr.r) if cr.err != nil { - return + return 0 } n, cr.err = strconv.ParseUint(string(line), 16, 64) if cr.err != nil { cr.err = ErrInvalidChunkLength } - return + return n } // Read a line of bytes (up to \n) from b. -// Give up if the line exceeds maxLineLength. +// Give up if the line exceeds the buffer size. // The returned bytes are a pointer into storage in // the bufio, so they are only valid until the next bufio read. func readLine(b *bufio.Reader) (p []byte, err error) { @@ -118,8 +119,5 @@ func readLine(b *bufio.Reader) (p []byte, err error) { } return nil, err } - if len(p) >= maxLineLength { - return nil, ErrLineTooLong - } return bytes.TrimRight(p, " \t\n\r"), nil } diff --git a/proxy/chunked_test.go b/proxy/chunked_test.go index 7d649b8a..a39fa9a7 100644 --- a/proxy/chunked_test.go +++ b/proxy/chunked_test.go @@ -100,6 +100,23 @@ func TestInvalidChunkSize(t *testing.T) { assertError(t, r, "invalid byte in chunk length") } +func TestChunkSizeLineTooLong(t *testing.T) { + var ( + maxLineLength = 4096 + chunkSize string + ) + for i := 0; i < maxLineLength; i++ { + chunkSize = chunkSize + "0" + } + chunkSize = chunkSize + "7" + + r := NewChunkedReader(bytes.NewBufferString( + chunkSize + "\r\nhello, \r\n0\r\n", + )) + + assertError(t, r, "header line too long") +} + func TestBytesAfterLastChunkAreIgnored(t *testing.T) { r := NewChunkedReader(bytes.NewBufferString( "7\r\nhello, \r\n0\r\nGARBAGEBYTES",