Skip to content

Commit fbf0f4c

Browse files
reverseproxy: Sync changes from stdlib for 1xx handling (#6656)
* reverseproxy: Sync changes from stdlib for 1xx handling Sourced from golang/go@960654b * Use clear() golang/go@3bc2840
1 parent 5e6024c commit fbf0f4c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

modules/caddyhttp/headers/headers.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
200200
for _, fieldName := range ops.Delete {
201201
fieldName = repl.ReplaceKnown(fieldName, "")
202202
if fieldName == "*" {
203-
for existingField := range hdr {
204-
delete(hdr, existingField)
205-
}
203+
clear(hdr)
206204
}
207205
}
208206

modules/caddyhttp/reverseproxy/reverseproxy.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -807,17 +807,26 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
807807
shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials
808808

809809
// Forward 1xx status codes, backported from https://github.com/golang/go/pull/53164
810+
var (
811+
roundTripMutex sync.Mutex
812+
roundTripDone bool
813+
)
810814
trace := &httptrace.ClientTrace{
811815
Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
816+
roundTripMutex.Lock()
817+
defer roundTripMutex.Unlock()
818+
if roundTripDone {
819+
// If RoundTrip has returned, don't try to further modify
820+
// the ResponseWriter's header map.
821+
return nil
822+
}
812823
h := rw.Header()
813824
copyHeader(h, http.Header(header))
814825
rw.WriteHeader(code)
815826

816827
// Clear headers coming from the backend
817828
// (it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses)
818-
for k := range header {
819-
delete(h, k)
820-
}
829+
clear(h)
821830

822831
return nil
823832
},
@@ -833,11 +842,18 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
833842
req = req.WithContext(context.WithoutCancel(req.Context()))
834843
}
835844

836-
// do the round-trip; emit debug log with values we know are
837-
// safe, or if there is no error, emit fuller log entry
845+
// do the round-trip
838846
start := time.Now()
839847
res, err := h.Transport.RoundTrip(req)
840848
duration := time.Since(start)
849+
850+
// record that the round trip is done for the 1xx response handler
851+
roundTripMutex.Lock()
852+
roundTripDone = true
853+
roundTripMutex.Unlock()
854+
855+
// emit debug log with values we know are safe,
856+
// or if there is no error, emit fuller log entry
841857
logger := h.logger.With(
842858
zap.String("upstream", di.Upstream.String()),
843859
zap.Duration("duration", duration),

0 commit comments

Comments
 (0)