From 532ab661c7d35ea5534f19c843ce2898ecca5d2e Mon Sep 17 00:00:00 2001 From: Carter Date: Thu, 11 Aug 2016 07:03:14 -0400 Subject: [PATCH] Fully read and close the request body --- caddyhttp/httpserver/replacer.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/caddyhttp/httpserver/replacer.go b/caddyhttp/httpserver/replacer.go index f07c25cc6..5b84c88e9 100644 --- a/caddyhttp/httpserver/replacer.go +++ b/caddyhttp/httpserver/replacer.go @@ -163,17 +163,21 @@ func canLogRequest(r *http.Request) (canLog bool) { // readRequestBody reads the request body and sets a // new io.ReadCloser that has not yet been read. func readRequestBody(r *http.Request, n int64) ([]byte, error) { + defer r.Body.Close() + body, err := ioutil.ReadAll(io.LimitReader(r.Body, n)) if err != nil { return nil, err } - mr := io.MultiReader( - bytes.NewBuffer(body), - r.Body, - ) + // Read the remaining bytes + remaining, err := ioutil.ReadAll(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 }