review feedback
This commit is contained in:
1 parent
157910ec9a
commit
6d7e784c25
2 files changed
+25
-10
No files matched your search
+8
-10
@@ -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
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user