[#883] network: parse URI properly
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
f836e7c1dc
commit
2f149f95d4
2 changed files with 46 additions and 19 deletions
|
@ -55,14 +55,19 @@ func (a *Address) FromString(s string) error {
|
||||||
|
|
||||||
a.ma, err = multiaddr.NewMultiaddr(s)
|
a.ma, err = multiaddr.NewMultiaddr(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var u uri
|
var (
|
||||||
|
host string
|
||||||
|
hasTLS bool
|
||||||
|
)
|
||||||
|
host, hasTLS, err = parseURI(s)
|
||||||
|
if err != nil {
|
||||||
|
host = s
|
||||||
|
}
|
||||||
|
|
||||||
u.parse(s)
|
s, err = multiaddrStringFromHostAddr(host)
|
||||||
|
|
||||||
s, err = multiaddrStringFromHostAddr(u.host)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
a.ma, err = multiaddr.NewMultiaddr(s)
|
a.ma, err = multiaddr.NewMultiaddr(s)
|
||||||
if err == nil && u.tls {
|
if err == nil && hasTLS {
|
||||||
a.ma = a.ma.Encapsulate(tls)
|
a.ma = a.ma.Encapsulate(tls)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,30 +76,41 @@ func (a *Address) FromString(s string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type uri struct {
|
const (
|
||||||
host string
|
grpcScheme = "grpc"
|
||||||
tls bool
|
grpcTLSScheme = "grpcs"
|
||||||
}
|
)
|
||||||
|
|
||||||
const grpcTLSScheme = "grpcs"
|
// parseURI parses s as address and returns a host and a flag
|
||||||
|
// indicating that TLS is enabled. If multiaddress is provided
|
||||||
func (u *uri) parse(s string) {
|
// the argument is returned unchanged.
|
||||||
|
func parseURI(s string) (string, bool, error) {
|
||||||
// TODO: code is copy-pasted from client.WithURIAddress function.
|
// TODO: code is copy-pasted from client.WithURIAddress function.
|
||||||
// Would be nice to share the code.
|
// Would be nice to share the code.
|
||||||
uri, err := url.ParseRequestURI(s)
|
uri, err := url.ParseRequestURI(s)
|
||||||
isURI := err == nil
|
if err != nil {
|
||||||
|
return s, false, nil
|
||||||
if isURI && uri.Host != "" {
|
|
||||||
u.host = uri.Host
|
|
||||||
} else {
|
|
||||||
u.host = s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if passed string was parsed correctly
|
// check if passed string was parsed correctly
|
||||||
// URIs that do not start with a slash after the scheme are interpreted as:
|
// URIs that do not start with a slash after the scheme are interpreted as:
|
||||||
// `scheme:opaque` => if `opaque` is not empty, then it is supposed that URI
|
// `scheme:opaque` => if `opaque` is not empty, then it is supposed that URI
|
||||||
// is in `host:port` format
|
// is in `host:port` format
|
||||||
u.tls = isURI && uri.Opaque == "" && uri.Scheme == grpcTLSScheme
|
if uri.Host == "" {
|
||||||
|
uri.Host = uri.Scheme
|
||||||
|
uri.Scheme = grpcScheme // assume GRPC by default
|
||||||
|
if uri.Opaque != "" {
|
||||||
|
uri.Host = net.JoinHostPort(uri.Host, uri.Opaque)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch uri.Scheme {
|
||||||
|
case grpcTLSScheme, grpcScheme:
|
||||||
|
default:
|
||||||
|
return "", false, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
return uri.Host, uri.Scheme == grpcTLSScheme, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// multiaddrStringFromHostAddr converts "localhost:8080" to "/dns4/localhost/tcp/8080"
|
// multiaddrStringFromHostAddr converts "localhost:8080" to "/dns4/localhost/tcp/8080"
|
||||||
|
|
|
@ -29,6 +29,17 @@ func TestAddressFromString(t *testing.T) {
|
||||||
require.Equal(t, testcase.exp, addr.ma, testcase.inp)
|
require.Equal(t, testcase.exp, addr.ma, testcase.inp)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
t.Run("invalid addresses", func(t *testing.T) {
|
||||||
|
testCases := []string{
|
||||||
|
"wtf://example.com:123", // wrong scheme
|
||||||
|
"grpc://example.com", // missing port
|
||||||
|
}
|
||||||
|
|
||||||
|
var addr Address
|
||||||
|
for _, tc := range testCases {
|
||||||
|
require.Error(t, addr.FromString(tc))
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddress_HostAddrString(t *testing.T) {
|
func TestAddress_HostAddrString(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue