@marcel91200 / caddy Mirror · read-only / commits / 085df25c
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 15:30 UTC

reverseproxy: Support 1xx status codes (HTTP early hints) (#4882)

Kévin Dunglas committed Aug 9, 2022 at 18:53 UTC 085df25c7e82b5bdd198787232d559f2ac1e72eb
1 file changed +25
modules/caddyhttp/reverseproxy/reverseproxy.go
+25
@@ -23,9 +23,11 @@ import (
23 "io"
24 "net"
25 "net/http"
26 + "net/http/httptrace"
27 "net/textproto"
28 "net/url"
29 "regexp"
30 + "runtime"
31 "strconv"
32 "strings"
33 "sync"
@@ -40,7 +42,11 @@ import (
42 "golang.org/x/net/http/httpguts"
43 )
44
45 +var supports1xx bool
46 +
47 func init() {
48 + supports1xx = !regexp.MustCompile(`^go1\.1(?:7|8)\.`).Match([]byte(runtime.Version()))
49 +
50 caddy.RegisterModule(Handler{})
51 }
52
@@ -732,6 +738,25 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
738 server := req.Context().Value(caddyhttp.ServerCtxKey).(*caddyhttp.Server)
739 shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials
740
741 + if supports1xx {
742 + // Forward 1xx status codes, backported from https://github.com/golang/go/pull/53164
743 + trace := &httptrace.ClientTrace{
744 + Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
745 + h := rw.Header()
746 + copyHeader(h, http.Header(header))
747 + rw.WriteHeader(code)
748 +
749 + // Clear headers, it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses
750 + for k := range h {
751 + delete(h, k)
752 + }
753 +
754 + return nil
755 + },
756 + }
757 + req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
758 + }
759 +
760 // do the round-trip; emit debug log with values we know are
761 // safe, or if there is no error, emit fuller log entry
762 start := time.Now()