Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to read body separate from header #1130

Merged
merged 8 commits into from Oct 22, 2021
38 changes: 29 additions & 9 deletions http.go
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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 SkipBody is true then it skips reading the response body.
ShivanshVij marked this conversation as resolved.
Show resolved Hide resolved
//
// If maxBodySize > 0 and the body size exceeds maxBodySize,
// then ErrBodyTooLarge is returned.
Expand All @@ -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
}

Expand Down