Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 07:00 UTC
reverseproxy: Sync changes from stdlib for 1xx handling (#6656)
* reverseproxy: Sync changes from stdlib for 1xx handling Sourced from https://github.com/golang/go/commit/960654be0c4ad7918376e2e1d47491c9bc7520e0 * Use clear() https://github.com/golang/go/commit/3bc28402fae2a1646e4d2756344b5eb34994d25f
Francis Lavoie committed
Oct 22, 2024 at 14:10 UTC
fbf0f4c425b23c39dbddd975d36df506cca5f45b
2 files changed
+22
-8
modules/caddyhttp/headers/headers.go
+1
-3
@@ -200,9 +200,7 @@ func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
200
for _, fieldName := range ops.Delete {
201
fieldName = repl.ReplaceKnown(fieldName, "")
202
if fieldName == "*" {
203
- for existingField := range hdr {
204
- delete(hdr, existingField)
205
- }
203
+ clear(hdr)
204
}
205
}
206
modules/caddyhttp/reverseproxy/reverseproxy.go
+21
-5
@@ -807,17 +807,26 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
807
shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials
808
809
// Forward 1xx status codes, backported from https://github.com/golang/go/pull/53164
810
+ var (
811
+ roundTripMutex sync.Mutex
812
+ roundTripDone bool
813
+ )
814
trace := &httptrace.ClientTrace{
815
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
+ }
823
h := rw.Header()
824
copyHeader(h, http.Header(header))
825
rw.WriteHeader(code)
826
827
// Clear headers coming from the backend
828
// (it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses)
818
- for k := range header {
819
- delete(h, k)
820
- }
829
+ clear(h)
830
831
return nil
832
},
@@ -833,11 +842,18 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
842
req = req.WithContext(context.WithoutCancel(req.Context()))
843
}
844
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
846
start := time.Now()
847
res, err := h.Transport.RoundTrip(req)
848
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
857
logger := h.logger.With(
858
zap.String("upstream", di.Upstream.String()),
859
zap.Duration("duration", duration),