diff options
author | Kévin Dunglas <kevin@dunglas.fr> | 2022-08-09 18:53:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-09 10:53:24 -0600 |
commit | 085df25c7e82b5bdd198787232d559f2ac1e72eb (patch) | |
tree | 8e4813524fcb1215da7cdaefe5e55e0bf2b526f9 /modules/caddyhttp | |
parent | fe61209df2d8ecbff7371703ae2dfa58342d2ed9 (diff) |
reverseproxy: Support 1xx status codes (HTTP early hints) (#4882)
Diffstat (limited to 'modules/caddyhttp')
-rw-r--r-- | modules/caddyhttp/reverseproxy/reverseproxy.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index 8887511..276616b 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -23,9 +23,11 @@ import ( "io" "net" "net/http" + "net/http/httptrace" "net/textproto" "net/url" "regexp" + "runtime" "strconv" "strings" "sync" @@ -40,7 +42,11 @@ import ( "golang.org/x/net/http/httpguts" ) +var supports1xx bool + func init() { + supports1xx = !regexp.MustCompile(`^go1\.1(?:7|8)\.`).Match([]byte(runtime.Version())) + caddy.RegisterModule(Handler{}) } @@ -732,6 +738,25 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe server := req.Context().Value(caddyhttp.ServerCtxKey).(*caddyhttp.Server) shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials + if supports1xx { + // Forward 1xx status codes, backported from https://github.com/golang/go/pull/53164 + trace := &httptrace.ClientTrace{ + Got1xxResponse: func(code int, header textproto.MIMEHeader) error { + h := rw.Header() + copyHeader(h, http.Header(header)) + rw.WriteHeader(code) + + // Clear headers, it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses + for k := range h { + delete(h, k) + } + + return nil + }, + } + req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) + } + // do the round-trip; emit debug log with values we know are // safe, or if there is no error, emit fuller log entry start := time.Now() |