Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rpc "no port provided" error by backporting #1528

Draft
wants to merge 5 commits into
base: v0.34.x-celestia
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions rpc/jsonrpc/client/http_json_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const (
protoUNIX = "unix"
)

//-------------------------------------------------------------

// Parsed URL structure
type parsedURL struct {
url.URL
Expand Down Expand Up @@ -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()
}

Expand Down
21 changes: 17 additions & 4 deletions rpc/jsonrpc/client/http_json_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,41 @@ 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",
expectedHostWithPath: "/tmp/test",
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",
Expand Down
Loading