1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

core: addresses.go funcs renames (#6622)

* right side in tls ln

* remove ParseNetworkAddressFromHostPort

* ignore placeholder port

* remove println

* update test cases (!!!)

* []

* comment

* Trim

* Update addresses.go
This commit is contained in:
Aaron Paterson 2024-10-21 10:02:29 -04:00 committed by GitHub
parent 1391e8ed9a
commit 0182fb87fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 41 deletions

View file

@ -329,8 +329,12 @@ func (st *ServerType) listenersForServerBlockAddress(sblock serverBlock, addr Ad
// use a map to prevent duplication // use a map to prevent duplication
listeners := map[string]map[string]struct{}{} listeners := map[string]map[string]struct{}{}
for _, lnCfgVal := range lnCfgVals { for _, lnCfgVal := range lnCfgVals {
for _, lnHost := range lnCfgVal.addresses { for _, lnAddr := range lnCfgVal.addresses {
networkAddr, err := caddy.ParseNetworkAddressFromHostPort(lnHost, lnPort) lnNetw, lnHost, _, err := caddy.SplitNetworkAddress(lnAddr)
if err != nil {
return nil, fmt.Errorf("splitting listener address: %v", err)
}
networkAddr, err := caddy.ParseNetworkAddress(caddy.JoinNetworkAddress(lnNetw, lnHost, lnPort))
if err != nil { if err != nil {
return nil, fmt.Errorf("parsing network address: %v", err) return nil, fmt.Errorf("parsing network address: %v", err)
} }

View file

@ -305,25 +305,6 @@ func IsFdNetwork(netw string) bool {
return strings.HasPrefix(netw, "fd") return strings.HasPrefix(netw, "fd")
} }
// normally we would simply append the port,
// but if host is IPv6, we need to ensure it
// is enclosed in [ ]; net.JoinHostPort does
// this for us, but host might also have a
// network type in front (e.g. "tcp/") leading
// to "[tcp/::1]" which causes parsing failures
// later; what we need is "tcp/[::1]", so we have
// to split the network and host, then re-combine
func ParseNetworkAddressFromHostPort(host, port string) (NetworkAddress, error) {
network, addr, ok := strings.Cut(host, "/")
if !ok {
addr = network
network = ""
}
addr = strings.Trim(addr, "[]") // IPv6
networkAddr := JoinNetworkAddress(network, addr, port)
return ParseNetworkAddress(networkAddr)
}
// ParseNetworkAddress parses addr into its individual // ParseNetworkAddress parses addr into its individual
// components. The input string is expected to be of // components. The input string is expected to be of
// the form "network/host:port-range" where any part is // the form "network/host:port-range" where any part is
@ -399,25 +380,28 @@ func SplitNetworkAddress(a string) (network, host, port string, err error) {
if slashFound { if slashFound {
network = strings.ToLower(strings.TrimSpace(beforeSlash)) network = strings.ToLower(strings.TrimSpace(beforeSlash))
a = afterSlash a = afterSlash
if IsUnixNetwork(network) || IsFdNetwork(network) {
host = a
return
}
} }
if IsUnixNetwork(network) || IsFdNetwork(network) {
host = a
return
}
host, port, err = net.SplitHostPort(a) host, port, err = net.SplitHostPort(a)
if err == nil || a == "" { firstErr := err
return
} if err != nil {
// in general, if there was an error, it was likely "missing port", // in general, if there was an error, it was likely "missing port",
// so try adding a bogus port to take advantage of standard library's // so try removing square brackets around an IPv6 host, adding a bogus
// robust parser, then strip the artificial port before returning // port to take advantage of standard library's robust parser, then
// (don't overwrite original error though; might still be relevant) // strip the artificial port.
var err2 error host, _, err = net.SplitHostPort(net.JoinHostPort(strings.Trim(a, "[]"), "0"))
host, port, err2 = net.SplitHostPort(a + ":0")
if err2 == nil {
err = nil
port = "" port = ""
} }
if err != nil {
err = errors.Join(firstErr, err)
}
return return
} }

View file

@ -31,7 +31,7 @@ func TestSplitNetworkAddress(t *testing.T) {
}{ }{
{ {
input: "", input: "",
expectErr: true, expectHost: "",
}, },
{ {
input: "foo", input: "foo",
@ -42,7 +42,7 @@ func TestSplitNetworkAddress(t *testing.T) {
}, },
{ {
input: "::", input: "::",
expectErr: true, expectHost: "::",
}, },
{ {
input: "[::]", input: "[::]",
@ -77,7 +77,7 @@ func TestSplitNetworkAddress(t *testing.T) {
{ {
input: "udp/", input: "udp/",
expectNetwork: "udp", expectNetwork: "udp",
expectErr: true, expectHost: "",
}, },
{ {
input: "unix//foo/bar", input: "unix//foo/bar",
@ -185,7 +185,8 @@ func TestParseNetworkAddress(t *testing.T) {
}{ }{
{ {
input: "", input: "",
expectErr: true, expectAddr: NetworkAddress{
},
}, },
{ {
input: ":", input: ":",
@ -311,7 +312,8 @@ func TestParseNetworkAddressWithDefaults(t *testing.T) {
}{ }{
{ {
input: "", input: "",
expectErr: true, expectAddr: NetworkAddress{
},
}, },
{ {
input: ":", input: ":",