all repos — homestead @ 5193a9aa5b7f7784b9ca419afd6953020a8e53c0

Code for my website

server: handle bracketed IPv6 addresses correctly

Alan Pearce
commit

5193a9aa5b7f7784b9ca419afd6953020a8e53c0

parent

a7628f8431b184a695c49cbc1085428523e41504

1 file changed, 16 insertions(+), 5 deletions(-)

jump to
M internal/server/tcp.gointernal/server/tcp.go
@@ -8,18 +8,29 @@ "go.alanpearce.eu/x/listenfd"
) func (s *Server) serveTCP() error { + addr := joinHostPort(s.options.ListenAddress, s.options.Port) s.log.Debug( "fallback listener", "addr", - net.JoinHostPort(s.options.ListenAddress, strconv.Itoa(s.options.Port)), + addr, ) - l, err := listenfd.GetListener(0, - net.JoinHostPort(s.options.ListenAddress, strconv.Itoa(s.options.Port)), - s.log.Named("tcp.listenfd"), - ) + l, err := listenfd.GetListener(0, addr, s.log.Named("tcp.listenfd")) if err != nil { return err } return s.server.Serve(l) } + +func joinHostPort(host string, port int) string { + if len(host) < 1 { + return "" + } + + portString := strconv.Itoa(port) + if host[0] == '[' && host[len(host)-1] == ']' { + return host + ":" + portString + } + + return net.JoinHostPort(host, portString) +}