From d9ff7b18726e21327b21b5e30b0ebd90f9023b39 Mon Sep 17 00:00:00 2001 From: Fred Cox Date: Tue, 9 Jan 2024 19:59:30 +0000 Subject: [PATCH] reverseproxy: Only change Content-Length when full request is buffered (#5830) fixes: https://github.com/caddyserver/caddy/issues/5829 Signed-off-by: Fred Cox --- modules/caddyhttp/reverseproxy/reverseproxy.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index 1a76aef4c..be67b4f59 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -589,8 +589,12 @@ func (h Handler) prepareRequest(req *http.Request, repl *caddy.Replacer) (*http. // feature if absolutely required, if read timeouts are // set, and if body size is limited if h.RequestBuffers != 0 && req.Body != nil { - req.Body, req.ContentLength = h.bufferedBody(req.Body, h.RequestBuffers) - req.Header.Set("Content-Length", strconv.FormatInt(req.ContentLength, 10)) + var readBytes int64 + req.Body, readBytes = h.bufferedBody(req.Body, h.RequestBuffers) + if h.RequestBuffers == -1 { + req.ContentLength = readBytes + req.Header.Set("Content-Length", strconv.FormatInt(req.ContentLength, 10)) + } } if req.ContentLength == 0 {