From 613d544a4775b831595f223028fee76b980aa003 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 22 Apr 2024 13:11:59 -0600 Subject: [PATCH] reverseproxy: Accept EOF when buffering Before this change, a read of size (let's say) < 10, into a buffer of size 10, will return EOF because we're using CopyN to limit to the size of the buffer. That resulted in the body being read from later, which should only happen if it couldn't fit in the buffer. With this change, the body is properly NOT set when it can all fit in the buffer. --- modules/caddyhttp/reverseproxy/reverseproxy.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index deba304a..048539e9 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -444,6 +444,13 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht if done { break } + if h.VerboseLogs { + var lbWait time.Duration + if h.LoadBalancing != nil { + lbWait = time.Duration(h.LoadBalancing.TryInterval) + } + h.logger.Debug("retrying", zap.Error(proxyErr), zap.Duration("after", lbWait)) + } retries++ } @@ -1131,7 +1138,7 @@ func (h Handler) bufferedBody(originalBody io.ReadCloser, limit int64) (io.ReadC buf.Reset() if limit > 0 { n, err := io.CopyN(buf, originalBody, limit) - if err != nil || n == limit { + if (err != nil && err != io.EOF) || n == limit { return bodyReadCloser{ Reader: io.MultiReader(buf, originalBody), buf: buf,