Browse Source

p2p: enable scheme-less parsing of IPv6 strings (#6158)

pull/6164/head
Callum Waters 4 years ago
committed by GitHub
parent
commit
42f6c40751
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions
  1. +5
    -3
      p2p/address.go
  2. +10
    -0
      p2p/address_test.go

+ 5
- 3
p2p/address.go View File

@ -24,8 +24,10 @@ var (
// reNodeID is a regexp for valid node IDs.
reNodeID = regexp.MustCompile(`^[0-9a-f]{40}$`)
// reHasScheme tries to detect URLs with schemes. It looks for a : before a / (if any).
reHasScheme = regexp.MustCompile(`^[^/]+:`)
// stringHasScheme tries to detect URLs with schemes. It looks for a : before a / (if any).
stringHasScheme = func(str string) bool {
return strings.Contains(str, "://")
}
// reSchemeIsHost tries to detect URLs where the scheme part is instead a
// hostname, i.e. of the form "host:80/path" where host: is a hostname.
@ -95,7 +97,7 @@ func ParseNodeAddress(urlString string) (NodeAddress, error) {
// we try to apply a default scheme.
url, err := url.Parse(urlString)
if (err != nil || url.Scheme == "") &&
(!reHasScheme.MatchString(urlString) || reSchemeIsHost.MatchString(urlString)) {
(!stringHasScheme(urlString) || reSchemeIsHost.MatchString(urlString)) {
url, err = url.Parse(string(defaultProtocol) + "://" + urlString)
}
if err != nil {


+ 10
- 0
p2p/address_test.go View File

@ -161,6 +161,16 @@ func TestParseNodeAddress(t *testing.T) {
p2p.NodeAddress{Protocol: "memory", NodeID: id},
true,
},
{
user + "@[1::]",
p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "1::"},
true,
},
{
"mconn://" + user + "@[fd80:b10c::2]:26657",
p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "fd80:b10c::2", Port: 26657},
true,
},
// Invalid addresses.
{"", p2p.NodeAddress{}, false},


Loading…
Cancel
Save