mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 06:03:48 +03:00
reverseproxy: Sync changes from stdlib for 1xx handling (#6656)
* reverseproxy: Sync changes from stdlib for 1xx handling Sourced from960654be0c
* Use clear()3bc28402fa
This commit is contained in:
parent
5e6024c48d
commit
fbf0f4c425
2 changed files with 22 additions and 8 deletions
|
@ -200,9 +200,7 @@ func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
|
||||||
for _, fieldName := range ops.Delete {
|
for _, fieldName := range ops.Delete {
|
||||||
fieldName = repl.ReplaceKnown(fieldName, "")
|
fieldName = repl.ReplaceKnown(fieldName, "")
|
||||||
if fieldName == "*" {
|
if fieldName == "*" {
|
||||||
for existingField := range hdr {
|
clear(hdr)
|
||||||
delete(hdr, existingField)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -807,17 +807,26 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
|
||||||
shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials
|
shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials
|
||||||
|
|
||||||
// Forward 1xx status codes, backported from https://github.com/golang/go/pull/53164
|
// Forward 1xx status codes, backported from https://github.com/golang/go/pull/53164
|
||||||
|
var (
|
||||||
|
roundTripMutex sync.Mutex
|
||||||
|
roundTripDone bool
|
||||||
|
)
|
||||||
trace := &httptrace.ClientTrace{
|
trace := &httptrace.ClientTrace{
|
||||||
Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
|
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()
|
h := rw.Header()
|
||||||
copyHeader(h, http.Header(header))
|
copyHeader(h, http.Header(header))
|
||||||
rw.WriteHeader(code)
|
rw.WriteHeader(code)
|
||||||
|
|
||||||
// Clear headers coming from the backend
|
// Clear headers coming from the backend
|
||||||
// (it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses)
|
// (it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses)
|
||||||
for k := range header {
|
clear(h)
|
||||||
delete(h, k)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
@ -833,11 +842,18 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
|
||||||
req = req.WithContext(context.WithoutCancel(req.Context()))
|
req = req.WithContext(context.WithoutCancel(req.Context()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// do the round-trip; emit debug log with values we know are
|
// do the round-trip
|
||||||
// safe, or if there is no error, emit fuller log entry
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
res, err := h.Transport.RoundTrip(req)
|
res, err := h.Transport.RoundTrip(req)
|
||||||
duration := time.Since(start)
|
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(
|
logger := h.logger.With(
|
||||||
zap.String("upstream", di.Upstream.String()),
|
zap.String("upstream", di.Upstream.String()),
|
||||||
zap.Duration("duration", duration),
|
zap.Duration("duration", duration),
|
||||||
|
|
Loading…
Reference in a new issue