Skip to content

Commit

Permalink
#457: review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Berezhnoy committed Nov 12, 2018
1 parent 8565ef5 commit aa16fe7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
31 changes: 24 additions & 7 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,9 @@ func (req *Request) Read(r *bufio.Reader) error {

const defaultMaxInMemoryFileSize = 16 * 1024 * 1024

var errGetOnly = errors.New("non-GET request received")
// ErrGetOnly is returned when server expects only GET requests,
// but some other type of request came (Server.GetOnly option is enabled).
var ErrGetOnly = errors.New("non-GET request received")

// ReadLimitBody reads request from the given r, limiting the body size.
//
Expand Down Expand Up @@ -881,7 +883,7 @@ func (req *Request) readLimitBody(r *bufio.Reader, maxBodySize int, getOnly bool
return err
}
if getOnly && !req.Header.IsGet() {
return errGetOnly
return ErrGetOnly
}

if req.MayContinue() {
Expand Down Expand Up @@ -1652,6 +1654,11 @@ func appendBodyFixedSize(r *bufio.Reader, dst []byte, n int) ([]byte, error) {
}
}

// ErrBrokenChunks is returned when server got broken chunked body (Transfer-Encoding: chunked).
type ErrBrokenChunks struct {
error
}

func readBodyChunked(r *bufio.Reader, maxBodySize int, dst []byte) ([]byte, error) {
if len(dst) > 0 {
panic("BUG: expected zero-length buffer")
Expand All @@ -1671,7 +1678,9 @@ func readBodyChunked(r *bufio.Reader, maxBodySize int, dst []byte) ([]byte, erro
return dst, err
}
if !bytes.Equal(dst[len(dst)-strCRLFLen:], strCRLF) {
return dst, fmt.Errorf("cannot find crlf at the end of chunk")
return dst, ErrBrokenChunks{
error: fmt.Errorf("cannot find crlf at the end of chunk"),
}
}
dst = dst[:len(dst)-strCRLFLen]
if chunkSize == 0 {
Expand All @@ -1688,23 +1697,31 @@ func parseChunkSize(r *bufio.Reader) (int, error) {
for {
c, err := r.ReadByte()
if err != nil {
return -1, fmt.Errorf("cannot read '\r' char at the end of chunk size: %s", err)
return -1, ErrBrokenChunks{
error: 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')
return -1, ErrBrokenChunks{
error: fmt.Errorf("unexpected char %q at the end of chunk size. Expected %q", c, '\r'),
}
}
break
}
c, err := r.ReadByte()
if err != nil {
return -1, fmt.Errorf("cannot read '\n' char at the end of chunk size: %s", err)
return -1, ErrBrokenChunks{
error: fmt.Errorf("cannot read '\n' char at the end of chunk size: %s", err),
}
}
if c != '\n' {
return -1, fmt.Errorf("unexpected char %q at the end of chunk size. Expected %q", c, '\n')
return -1, ErrBrokenChunks{
error: fmt.Errorf("unexpected char %q at the end of chunk size. Expected %q", c, '\n'),
}
}
return n, nil
}
Expand Down
8 changes: 8 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ type Server struct {
Handler RequestHandler

// ErrorHandler for returning an error response in user-defined way.
//
// Following errors will be captured with this handler:
// * io.EOF
// * io.ErrUnexpectedEOF
// * ErrGetOnly
// * ErrSmallBuffer
// * ErrBodyTooLarge
// * ErrBrokenChunks
ErrorHandler func(ctx *RequestCtx, err error)

// Server name for sending in response headers.
Expand Down
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,8 @@ func TestServerGetOnly(t *testing.T) {
if err == nil {
t.Fatalf("expecting error")
}
if err != errGetOnly {
t.Fatalf("Unexpected error from serveConn: %s. Expecting %s", err, errGetOnly)
if err != ErrGetOnly {
t.Fatalf("Unexpected error from serveConn: %s. Expecting %s", err, ErrGetOnly)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout")
Expand Down

0 comments on commit aa16fe7

Please sign in to comment.