From a45963d16ea48d419dc2a7d835a7b344cf2f3356 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Wed, 20 Oct 2021 01:49:38 -0400 Subject: [PATCH] Delete the "Content-Length" header if we aren't copying Fixes a bug where the Content-Length will mismatch the actual bytes written if we skipped copying the response, so we get a message like this when using curl: ``` curl: (18) transfer closed with 18 bytes remaining to read ``` To replicate: ``` { admin off debug } :8881 { reverse_proxy 127.0.0.1:8882 { @200 status 200 handle_response @200 { header Foo bar } } } :8882 { header Content-Type application/json respond `{"hello": "world"}` 200 } ``` --- modules/caddyhttp/reverseproxy/reverseproxy.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index fe53447e3af..1ad80a78680 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -733,6 +733,13 @@ func (h Handler) finalizeResponse( res.Header.Del(h) } + // remove the content length if we're not going to be copying + // from the response, because otherwise there'll be a mismatch + // between bytes written and the advertised length + if bodyClosed { + res.Header.Del("Content-Length") + } + // apply any response header operations if h.Headers != nil && h.Headers.Response != nil { if h.Headers.Response.Require == nil ||