@marcel91200 / caddy Mirror · read-only / commits / 6a41b62e
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, 01:45 UTC

caddyhttp: Support custom network for HTTP/3 (#5573)

Allow registering a custom network mapping for HTTP/3. This is useful if the original network for HTTP/1.1 and HTTP/2 is not a standard `unix`, `tcp4`, or `tcp6` network. To keep backwards compatibility, we fall back to `udp` if the original network is not registered in the mapping. Fixes #5555

Dominik Roos committed Jun 14, 2023 at 03:33 UTC 6a41b62e70f867eff30b38a8ff33e90108077b0f
1 file changed +28 -11
modules/caddyhttp/server.go
+28 -11
@@ -520,17 +520,7 @@ func (s *Server) findLastRouteWithHostMatcher() int {
520 // not already done, and then uses that server to serve HTTP/3 over
521 // the listener, with Server s as the handler.
522 func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error {
523 - switch addr.Network {
524 - case "unix":
525 - addr.Network = "unixgram"
526 - case "tcp4":
527 - addr.Network = "udp4"
528 - case "tcp6":
529 - addr.Network = "udp6"
530 - default:
531 - addr.Network = "udp" // TODO: Maybe a better default is to not enable HTTP/3 if we do not know the network?
532 - }
533 -
523 + addr.Network = getHTTP3Network(addr.Network)
524 lnAny, err := addr.Listen(s.ctx, 0, net.ListenConfig{})
525 if err != nil {
526 return err
@@ -918,3 +908,30 @@ const (
908 // For tracking the real client IP (affected by trusted_proxy)
909 ClientIPVarKey string = "client_ip"
910 )
911 +
912 +var networkTypesHTTP3 = map[string]string{
913 + "unix": "unixgram",
914 + "tcp4": "udp4",
915 + "tcp6": "udp6",
916 +}
917 +
918 +// RegisterNetworkHTTP3 registers a mapping from non-HTTP/3 network to HTTP/3
919 +// network. This should be called during init() and will panic if the network
920 +// type is standard, reserved, or already registered.
921 +//
922 +// EXPERIMENTAL: Subject to change.
923 +func RegisterNetworkHTTP3(originalNetwork, h3Network string) {
924 + if _, ok := networkTypesHTTP3[strings.ToLower(originalNetwork)]; ok {
925 + panic("network type " + originalNetwork + " is already registered")
926 + }
927 + networkTypesHTTP3[originalNetwork] = h3Network
928 +}
929 +
930 +func getHTTP3Network(originalNetwork string) string {
931 + h3Network, ok := networkTypesHTTP3[strings.ToLower(originalNetwork)]
932 + if !ok {
933 + // TODO: Maybe a better default is to not enable HTTP/3 if we do not know the network?
934 + return "udp"
935 + }
936 + return h3Network
937 +}