caddyhttp: Serve http2 when listener wrapper doesn't return *tls.Conn (#4929)

* Serve http2 when listener wrapper doesn't return *tls.Conn

* close conn when h2server serveConn returns

* merge from upstream

* rebase from latest

* run New and Closed ConnState hook for h2 conns

* go fmt

* fix lint

* Add comments

* reorder import
This commit is contained in:
WeidiDeng
2023-04-11 01:05:02 +08:00
committed by GitHub
parent f8b59e77f8
commit d8d87a378f
3 changed files with 153 additions and 5 deletions

View File

@ -198,6 +198,7 @@ type Server struct {
server *http.Server
h3server *http3.Server
h3listeners []net.PacketConn // TODO: we have to hold these because quic-go won't close listeners it didn't create
h2listeners []*http2Listener
addresses []caddy.NetworkAddress
trustedProxies IPRangeSource
@ -213,6 +214,16 @@ type Server struct {
// ServeHTTP is the entry point for all HTTP requests.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// If there are listener wrappers that process tls connections but don't return a *tls.Conn, this field will be nil.
// Can be removed if https://github.com/golang/go/pull/56110 is ever merged.
if r.TLS == nil {
conn := r.Context().Value(ConnCtxKey).(net.Conn)
if csc, ok := conn.(connectionStateConn); ok {
r.TLS = new(tls.ConnectionState)
*r.TLS = csc.ConnectionState()
}
}
w.Header().Set("Server", "Caddy")
// advertise HTTP/3, if enabled
@ -870,6 +881,9 @@ const (
// originally came into the server's entry handler
OriginalRequestCtxKey caddy.CtxKey = "original_request"
// For referencing underlying net.Conn
ConnCtxKey caddy.CtxKey = "conn"
// For tracking whether the client is a trusted proxy
TrustedProxyVarKey string = "trusted_proxy"