mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Fully read and close the request body
This commit is contained in:
parent
d56ac28bec
commit
532ab661c7
1 changed files with 9 additions and 5 deletions
|
@ -163,17 +163,21 @@ func canLogRequest(r *http.Request) (canLog bool) {
|
||||||
// readRequestBody reads the request body and sets a
|
// readRequestBody reads the request body and sets a
|
||||||
// new io.ReadCloser that has not yet been read.
|
// new io.ReadCloser that has not yet been read.
|
||||||
func readRequestBody(r *http.Request, n int64) ([]byte, error) {
|
func readRequestBody(r *http.Request, n int64) ([]byte, error) {
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(io.LimitReader(r.Body, n))
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, n))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mr := io.MultiReader(
|
// Read the remaining bytes
|
||||||
bytes.NewBuffer(body),
|
remaining, err := ioutil.ReadAll(r.Body)
|
||||||
r.Body,
|
if err != nil {
|
||||||
)
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
r.Body = ioutil.NopCloser(mr)
|
buf := bytes.NewBuffer(append(body, remaining...))
|
||||||
|
r.Body = ioutil.NopCloser(buf)
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue