diff --git a/rpc/jsonrpc/client/http_json_client.go b/rpc/jsonrpc/client/http_json_client.go index 76fa5063f9..fce584cb73 100644 --- a/rpc/jsonrpc/client/http_json_client.go +++ b/rpc/jsonrpc/client/http_json_client.go @@ -24,8 +24,6 @@ const ( protoUNIX = "unix" ) -//------------------------------------------------------------- - // Parsed URL structure type parsedURL struct { url.URL @@ -87,13 +85,25 @@ func (u parsedURL) GetTrimmedHostWithPath() string { return strings.ReplaceAll(u.GetHostWithPath(), "/", ".") } -// GetDialAddress returns the endpoint to dial for the parsed URL func (u parsedURL) GetDialAddress() string { - // if it's not a unix socket we return the host, example: localhost:443 if !u.isUnixSocket { - return u.Host + host := u.Host + // Check if the host already includes a port + _, _, err := net.SplitHostPort(host) + if err != nil { + // Add the default port based on the scheme + switch u.Scheme { + case protoHTTP, protoWS: + host = net.JoinHostPort(host, "80") + case protoHTTPS, protoWSS: + host = net.JoinHostPort(host, "443") + default: + // Handle unsupported schemes explicitly + panic("unsupported scheme: " + u.Scheme) + } + } + return "[" + host + "]" // Explicitly wrap IPv6 addresses in brackets } - // otherwise we return the path of the unix socket, ex /tmp/socket return u.GetHostWithPath() } diff --git a/rpc/jsonrpc/client/http_json_client_test.go b/rpc/jsonrpc/client/http_json_client_test.go index 03134dff58..86a13acb8b 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", + expectedDialAddress: "example.com:443", }, - "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",