Skip to content

Commit 085df25

Browse files
authored
reverseproxy: Support 1xx status codes (HTTP early hints) (#4882)
1 parent fe61209 commit 085df25

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

modules/caddyhttp/reverseproxy/reverseproxy.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import (
2323
"io"
2424
"net"
2525
"net/http"
26+
"net/http/httptrace"
2627
"net/textproto"
2728
"net/url"
2829
"regexp"
30+
"runtime"
2931
"strconv"
3032
"strings"
3133
"sync"
@@ -40,7 +42,11 @@ import (
4042
"golang.org/x/net/http/httpguts"
4143
)
4244

45+
var supports1xx bool
46+
4347
func init() {
48+
supports1xx = !regexp.MustCompile(`^go1\.1(?:7|8)\.`).Match([]byte(runtime.Version()))
49+
4450
caddy.RegisterModule(Handler{})
4551
}
4652

@@ -732,6 +738,25 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
732738
server := req.Context().Value(caddyhttp.ServerCtxKey).(*caddyhttp.Server)
733739
shouldLogCredentials := server.Logs != nil && server.Logs.ShouldLogCredentials
734740

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+
735760
// do the round-trip; emit debug log with values we know are
736761
// safe, or if there is no error, emit fuller log entry
737762
start := time.Now()

0 commit comments

Comments
 (0)