diff --git a/rpc/jsonrpc/client/http_json_client.go b/rpc/jsonrpc/client/http_json_client.go index 76fa5063f9..864d04eb97 100644 --- a/rpc/jsonrpc/client/http_json_client.go +++ b/rpc/jsonrpc/client/http_json_client.go @@ -9,6 +9,7 @@ import ( "net" "net/http" "net/url" + "regexp" "strings" cmtsync "github.com/tendermint/tendermint/libs/sync" @@ -24,7 +25,7 @@ const ( protoUNIX = "unix" ) -//------------------------------------------------------------- +var endsWithPortPattern = regexp.MustCompile(`:[0-9]+$`) // Parsed URL structure type parsedURL struct { @@ -91,6 +92,17 @@ func (u parsedURL) GetTrimmedHostWithPath() string { func (u parsedURL) GetDialAddress() string { // if it's not a unix socket we return the host, example: localhost:443 if !u.isUnixSocket { + hasPort := endsWithPortPattern.MatchString(u.Host) + if !hasPort { + // http and ws default to port 80, https and wss default to port 443 + // https://www.rfc-editor.org/rfc/rfc9110#section-4.2 + // https://www.rfc-editor.org/rfc/rfc6455.html#section-3 + if u.Scheme == protoHTTP || u.Scheme == protoWS { + return u.Host + `:80` + } else if u.Scheme == protoHTTPS || u.Scheme == protoWSS { + return u.Host + `:443` + } + } return u.Host } // otherwise we return the path of the unix socket, ex /tmp/socket diff --git a/rpc/jsonrpc/client/http_json_client_test.go b/rpc/jsonrpc/client/http_json_client_test.go index 03134dff58..c2a722141b 100644 --- a/rpc/jsonrpc/client/http_json_client_test.go +++ b/rpc/jsonrpc/client/http_json_client_test.go @@ -44,6 +44,19 @@ func Test_parsedURL(t *testing.T) { } tests := map[string]test{ + "http endpoint": { + url: "http://example.com", + expectedURL: "http://example.com", + expectedHostWithPath: "example.com", + expectedDialAddress: "example.com:80", + }, + + "http endpoint with port": { + url: "http://example.com:8080", + expectedURL: "http://example.com:8080", + expectedHostWithPath: "example.com:8080", + expectedDialAddress: "example.com:8080", + }, "unix endpoint": { url: "unix:///tmp/test", expectedURL: "unix://.tmp.test", @@ -51,21 +64,21 @@ func Test_parsedURL(t *testing.T) { expectedDialAddress: "/tmp/test", }, - "http endpoint": { + "https endpoint": { url: "https://example.com", expectedURL: "https://example.com", expectedHostWithPath: "example.com", expectedDialAddress: "example.com", }, - "http endpoint with port": { + "https endpoint with port": { url: "https://example.com:8080", expectedURL: "https://example.com:8080", expectedHostWithPath: "example.com:8080", expectedDialAddress: "example.com:8080", }, - "http path routed endpoint": { + "https path routed endpoint": { url: "https://example.com:8080/rpc", expectedURL: "https://example.com:8080/rpc", expectedHostWithPath: "example.com:8080/rpc",