Skip to content

Commit 0182fb8

Browse files
authored
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
1 parent 1391e8e commit 0182fb8

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

caddyconfig/httpcaddyfile/addresses.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,12 @@ func (st *ServerType) listenersForServerBlockAddress(sblock serverBlock, addr Ad
329329
// use a map to prevent duplication
330330
listeners := map[string]map[string]struct{}{}
331331
for _, lnCfgVal := range lnCfgVals {
332-
for _, lnHost := range lnCfgVal.addresses {
333-
networkAddr, err := caddy.ParseNetworkAddressFromHostPort(lnHost, lnPort)
332+
for _, lnAddr := range lnCfgVal.addresses {
333+
lnNetw, lnHost, _, err := caddy.SplitNetworkAddress(lnAddr)
334+
if err != nil {
335+
return nil, fmt.Errorf("splitting listener address: %v", err)
336+
}
337+
networkAddr, err := caddy.ParseNetworkAddress(caddy.JoinNetworkAddress(lnNetw, lnHost, lnPort))
334338
if err != nil {
335339
return nil, fmt.Errorf("parsing network address: %v", err)
336340
}

listeners.go

+18-34
Original file line numberDiff line numberDiff line change
@@ -305,25 +305,6 @@ func IsFdNetwork(netw string) bool {
305305
return strings.HasPrefix(netw, "fd")
306306
}
307307

308-
// normally we would simply append the port,
309-
// but if host is IPv6, we need to ensure it
310-
// is enclosed in [ ]; net.JoinHostPort does
311-
// this for us, but host might also have a
312-
// network type in front (e.g. "tcp/") leading
313-
// to "[tcp/::1]" which causes parsing failures
314-
// later; what we need is "tcp/[::1]", so we have
315-
// to split the network and host, then re-combine
316-
func ParseNetworkAddressFromHostPort(host, port string) (NetworkAddress, error) {
317-
network, addr, ok := strings.Cut(host, "/")
318-
if !ok {
319-
addr = network
320-
network = ""
321-
}
322-
addr = strings.Trim(addr, "[]") // IPv6
323-
networkAddr := JoinNetworkAddress(network, addr, port)
324-
return ParseNetworkAddress(networkAddr)
325-
}
326-
327308
// ParseNetworkAddress parses addr into its individual
328309
// components. The input string is expected to be of
329310
// the form "network/host:port-range" where any part is
@@ -399,25 +380,28 @@ func SplitNetworkAddress(a string) (network, host, port string, err error) {
399380
if slashFound {
400381
network = strings.ToLower(strings.TrimSpace(beforeSlash))
401382
a = afterSlash
383+
if IsUnixNetwork(network) || IsFdNetwork(network) {
384+
host = a
385+
return
386+
}
402387
}
403-
if IsUnixNetwork(network) || IsFdNetwork(network) {
404-
host = a
405-
return
406-
}
388+
407389
host, port, err = net.SplitHostPort(a)
408-
if err == nil || a == "" {
409-
return
410-
}
411-
// in general, if there was an error, it was likely "missing port",
412-
// so try adding a bogus port to take advantage of standard library's
413-
// robust parser, then strip the artificial port before returning
414-
// (don't overwrite original error though; might still be relevant)
415-
var err2 error
416-
host, port, err2 = net.SplitHostPort(a + ":0")
417-
if err2 == nil {
418-
err = nil
390+
firstErr := err
391+
392+
if err != nil {
393+
// in general, if there was an error, it was likely "missing port",
394+
// so try removing square brackets around an IPv6 host, adding a bogus
395+
// port to take advantage of standard library's robust parser, then
396+
// strip the artificial port.
397+
host, _, err = net.SplitHostPort(net.JoinHostPort(strings.Trim(a, "[]"), "0"))
419398
port = ""
420399
}
400+
401+
if err != nil {
402+
err = errors.Join(firstErr, err)
403+
}
404+
421405
return
422406
}
423407

listeners_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestSplitNetworkAddress(t *testing.T) {
3131
}{
3232
{
3333
input: "",
34-
expectErr: true,
34+
expectHost: "",
3535
},
3636
{
3737
input: "foo",
@@ -42,7 +42,7 @@ func TestSplitNetworkAddress(t *testing.T) {
4242
},
4343
{
4444
input: "::",
45-
expectErr: true,
45+
expectHost: "::",
4646
},
4747
{
4848
input: "[::]",
@@ -77,7 +77,7 @@ func TestSplitNetworkAddress(t *testing.T) {
7777
{
7878
input: "udp/",
7979
expectNetwork: "udp",
80-
expectErr: true,
80+
expectHost: "",
8181
},
8282
{
8383
input: "unix//foo/bar",
@@ -185,7 +185,8 @@ func TestParseNetworkAddress(t *testing.T) {
185185
}{
186186
{
187187
input: "",
188-
expectErr: true,
188+
expectAddr: NetworkAddress{
189+
},
189190
},
190191
{
191192
input: ":",
@@ -311,7 +312,8 @@ func TestParseNetworkAddressWithDefaults(t *testing.T) {
311312
}{
312313
{
313314
input: "",
314-
expectErr: true,
315+
expectAddr: NetworkAddress{
316+
},
315317
},
316318
{
317319
input: ":",

0 commit comments

Comments
 (0)