@@ -305,25 +305,6 @@ func IsFdNetwork(netw string) bool {
305
305
return strings .HasPrefix (netw , "fd" )
306
306
}
307
307
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
-
327
308
// ParseNetworkAddress parses addr into its individual
328
309
// components. The input string is expected to be of
329
310
// the form "network/host:port-range" where any part is
@@ -399,25 +380,28 @@ func SplitNetworkAddress(a string) (network, host, port string, err error) {
399
380
if slashFound {
400
381
network = strings .ToLower (strings .TrimSpace (beforeSlash ))
401
382
a = afterSlash
383
+ if IsUnixNetwork (network ) || IsFdNetwork (network ) {
384
+ host = a
385
+ return
386
+ }
402
387
}
403
- if IsUnixNetwork (network ) || IsFdNetwork (network ) {
404
- host = a
405
- return
406
- }
388
+
407
389
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" ))
419
398
port = ""
420
399
}
400
+
401
+ if err != nil {
402
+ err = errors .Join (firstErr , err )
403
+ }
404
+
421
405
return
422
406
}
423
407
0 commit comments