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(tm2): make HTTPClient support https #1158

Merged
merged 4 commits into from
Oct 5, 2023
Merged
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
12 changes: 6 additions & 6 deletions tm2/pkg/bft/rpc/lib/client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ func parseRemoteAddr(remoteAddr string) (network string, s string, err error) {
return "", "", fmt.Errorf("invalid addr: %s", remoteAddr)
}

// accept http(s) as an alias for tcp
switch protocol {
case protoHTTP, protoHTTPS:
protocol = protoTCP
}

return protocol, address, nil
}

Expand All @@ -99,6 +93,12 @@ func makeHTTPDialer(remoteAddr string) func(string, string) (net.Conn, error) {
return makeErrorDialer(err)
}

// net.Dial doesn't understand http/https, so change it to TCP
switch protocol {
case protoHTTP, protoHTTPS:
protocol = protoTCP
}

return func(proto, addr string) (net.Conn, error) {
return net.Dial(protocol, address)
}
Expand Down
52 changes: 52 additions & 0 deletions tm2/pkg/bft/rpc/lib/client/http_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package rpcclient

import (
"testing"

"github.com/jaekwon/testify/assert"
)

func Test_parseRemoteAddr(t *testing.T) {
tt := []struct {
remoteAddr string
network, s, errContains string
}{
{"127.0.0.1", "tcp", "127.0.0.1", ""},
{"https://example.com", "https", "example.com", ""},
{"wss://[::1]", "wss", "[::1]", ""},
// no error cases - they cannot happen!
}

for _, tc := range tt {
n, s, err := parseRemoteAddr(tc.remoteAddr)
if tc.errContains != "" {
_ = assert.NotNil(t, err) && assert.Contains(t, err.Error(), tc.errContains)
}
assert.NoError(t, err)
assert.Equal(t, n, tc.network)
assert.Equal(t, s, tc.s)
}
}

// Following tests check that we correctly translate http/https to tcp,
// and other protocols are left intact from parseRemoteAddr()

func Test_makeHTTPDialer(t *testing.T) {
dl := makeHTTPDialer("https://.")
_, err := dl("hello", "world")
if assert.NotNil(t, err) {
e := err.Error()
assert.Contains(t, e, "dial tcp:", "should convert https to tcp")
assert.Contains(t, e, "address .:", "should have parsed the address (as incorrect)")
}
}

func Test_makeHTTPDialer_noConvert(t *testing.T) {
dl := makeHTTPDialer("udp://.")
_, err := dl("hello", "world")
if assert.NotNil(t, err) {
e := err.Error()
assert.Contains(t, e, "dial udp:", "udp protocol should remain the same")
assert.Contains(t, e, "address .:", "should have parsed the address (as incorrect)")
}
}