diff --git a/http.go b/http.go index 18e5f8126c..8021a2528d 100644 --- a/http.go +++ b/http.go @@ -1122,6 +1122,14 @@ func (req *Request) ContinueReadBody(r *bufio.Reader, maxBodySize int, preParseM return nil } + return req.ReadBody(r, contentLength, maxBodySize) +} + +// ReadBody reads request body from the given r, limiting the body size. +// +// If maxBodySize > 0 and the body size exceeds maxBodySize, +// then ErrBodyTooLarge is returned. +func (req *Request) ReadBody(r *bufio.Reader, contentLength int, maxBodySize int) (err error) { bodyBuf := req.bodyBuffer() bodyBuf.Reset() bodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B) @@ -1133,7 +1141,7 @@ func (req *Request) ContinueReadBody(r *bufio.Reader, maxBodySize int, preParseM return nil } -// ContinueReadBody reads request body if request header contains +// ContinueReadBodyStream reads request body if request header contains // 'Expect: 100-continue'. // // The caller must send StatusContinue response before calling this method. @@ -1200,7 +1208,10 @@ func (resp *Response) Read(r *bufio.Reader) error { return resp.ReadLimitBody(r, 0) } -// ReadLimitBody reads response from the given r, limiting the body size. +// ReadLimitBody reads response headers from the given r, +// then reads the body using the ReadBody function and limiting the body size. +// +// If resp.SkipBody is true then it skips reading the response body. // // If maxBodySize > 0 and the body size exceeds maxBodySize, // then ErrBodyTooLarge is returned. @@ -1220,14 +1231,23 @@ func (resp *Response) ReadLimitBody(r *bufio.Reader, maxBodySize int) error { } if !resp.mustSkipBody() { - bodyBuf := resp.bodyBuffer() - bodyBuf.Reset() - bodyBuf.B, err = readBody(r, resp.Header.ContentLength(), maxBodySize, bodyBuf.B) - if err != nil { - return err - } - resp.Header.SetContentLength(len(bodyBuf.B)) + return resp.ReadBody(r, maxBodySize) + } + return nil +} + +// ReadBody reads response body from the given r, limiting the body size. +// +// If maxBodySize > 0 and the body size exceeds maxBodySize, +// then ErrBodyTooLarge is returned. +func (resp *Response) ReadBody(r *bufio.Reader, maxBodySize int) (err error) { + bodyBuf := resp.bodyBuffer() + bodyBuf.Reset() + bodyBuf.B, err = readBody(r, resp.Header.ContentLength(), maxBodySize, bodyBuf.B) + if err != nil { + return err } + resp.Header.SetContentLength(len(bodyBuf.B)) return nil }