diff options
author | Francis Lavoie <[email protected]> | 2024-10-22 11:35:04 -0400 |
---|---|---|
committer | Francis Lavoie <[email protected]> | 2024-10-22 11:35:04 -0400 |
commit | 690219f681f597701ea3b9205c39d0cef583caa4 (patch) | |
tree | 873c79a0da7d85c6a417389471e27f6789859204 | |
parent | 5e6024c48da68492761837af3806be1951fa4c24 (diff) | |
download | caddy-690219f681f597701ea3b9205c39d0cef583caa4.tar.gz caddy-690219f681f597701ea3b9205c39d0cef583caa4.zip |
reverseproxy: Sync changes from stdlib for 1xx handling
Sourced from https://github.com/golang/go/commit/960654be0c4ad7918376e2e1d47491c9bc7520e0
-rw-r--r-- | modules/caddyhttp/reverseproxy/reverseproxy.go | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index 123bf774b..a32994e04 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -807,8 +807,19 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials // Forward 1xx status codes, backported from https://github.com/golang/go/pull/53164 + var ( + roundTripMutex sync.Mutex + roundTripDone bool + ) trace := &httptrace.ClientTrace{ Got1xxResponse: func(code int, header textproto.MIMEHeader) error { + roundTripMutex.Lock() + defer roundTripMutex.Unlock() + if roundTripDone { + // If RoundTrip has returned, don't try to further modify + // the ResponseWriter's header map. + return nil + } h := rw.Header() copyHeader(h, http.Header(header)) rw.WriteHeader(code) @@ -833,11 +844,18 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe req = req.WithContext(context.WithoutCancel(req.Context())) } - // do the round-trip; emit debug log with values we know are - // safe, or if there is no error, emit fuller log entry + // do the round-trip start := time.Now() res, err := h.Transport.RoundTrip(req) duration := time.Since(start) + + // record that the round trip is done for the 1xx response handler + roundTripMutex.Lock() + roundTripDone = true + roundTripMutex.Unlock() + + // emit debug log with values we know are safe, + // or if there is no error, emit fuller log entry logger := h.logger.With( zap.String("upstream", di.Upstream.String()), zap.Duration("duration", duration), |