Skip to content

Commit

Permalink
feat: ability to read body separate from header (#1130)
Browse files Browse the repository at this point in the history
* Adding ConvertHTTPRequest and renaming ConvertRequest to ConvertFastRequest

* Removing forServer boolean from ConvertHTTPRequest

* Preparing for PR

* Reverting adaptor changes

* Fixing godoc, adding req.ReadBody function as well

* Update comment to be more clear

As per @erikdubbelboer suggestion

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
  • Loading branch information
ShivanshVij and erikdubbelboer committed Oct 22, 2021
1 parent 556aa81 commit 528dd62
Showing 1 changed file with 29 additions and 9 deletions.
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 resp.SkipBody is true then it skips reading the response body.
//
// 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

0 comments on commit 528dd62

Please sign in to comment.