mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-25 09:24:30 +00:00
rpc: fix RPC client doesn't handle url's without ports (#6507)
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tmsync "github.com/tendermint/tendermint/internal/libs/sync"
|
||||
types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
|
||||
@@ -370,6 +371,7 @@ func makeHTTPDialer(remoteAddr string) (func(string, string) (net.Conn, error),
|
||||
}
|
||||
|
||||
protocol := u.Scheme
|
||||
padding := u.Scheme
|
||||
|
||||
// accept http(s) as an alias for tcp
|
||||
switch protocol {
|
||||
@@ -378,7 +380,13 @@ func makeHTTPDialer(remoteAddr string) (func(string, string) (net.Conn, error),
|
||||
}
|
||||
|
||||
dialFn := func(proto, addr string) (net.Conn, error) {
|
||||
return net.Dial(protocol, u.GetDialAddress())
|
||||
var timeout = 10 * time.Second
|
||||
if !u.isUnixSocket && strings.LastIndex(u.Host, ":") == -1 {
|
||||
u.Host = fmt.Sprintf("%s:%s", u.Host, padding)
|
||||
return net.DialTimeout(protocol, u.GetDialAddress(), timeout)
|
||||
}
|
||||
|
||||
return net.DialTimeout(protocol, u.GetDialAddress(), timeout)
|
||||
}
|
||||
|
||||
return dialFn, nil
|
||||
|
||||
@@ -84,3 +84,31 @@ func Test_parsedURL(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeHTTPDialerURL(t *testing.T) {
|
||||
remotes := []string{"https://foo-bar.com", "http://foo-bar.com"}
|
||||
|
||||
for _, remote := range remotes {
|
||||
u, err := newParsedURL(remote)
|
||||
require.NoError(t, err)
|
||||
dialFn, err := makeHTTPDialer(remote)
|
||||
require.Nil(t, err)
|
||||
|
||||
addr, err := dialFn(u.Scheme, u.GetHostWithPath())
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, addr)
|
||||
}
|
||||
|
||||
errorURLs := []string{"tcp://foo-bar.com", "ftp://foo-bar.com"}
|
||||
|
||||
for _, errorURL := range errorURLs {
|
||||
u, err := newParsedURL(errorURL)
|
||||
require.NoError(t, err)
|
||||
dialFn, err := makeHTTPDialer(errorURL)
|
||||
require.Nil(t, err)
|
||||
|
||||
addr, err := dialFn(u.Scheme, u.GetHostWithPath())
|
||||
require.Error(t, err)
|
||||
require.Nil(t, addr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user