Skip to content

Commit

Permalink
fix: rpc "no port provided" error by backporting cometbft/cometbft#1903
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-forbes committed Nov 15, 2024
1 parent b28d312 commit a732388
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
14 changes: 13 additions & 1 deletion rpc/jsonrpc/client/http_json_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"net/http"
"net/url"
"regexp"
"strings"

cmtsync "github.com/tendermint/tendermint/libs/sync"
Expand All @@ -24,7 +25,7 @@ const (
protoUNIX = "unix"
)

//-------------------------------------------------------------
var endsWithPortPattern = regexp.MustCompile(`:[0-9]+$`)

// Parsed URL structure
type parsedURL struct {
Expand Down Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 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",
},

"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

0 comments on commit a732388

Please sign in to comment.