Skip to content

Commit

Permalink
fix(tm2): make HTTPClient support https (gnolang#1158)
Browse files Browse the repository at this point in the history
Co-authored-by: Manfred Touron <[email protected]>
  • Loading branch information
2 people authored and gfanton committed Nov 9, 2023
1 parent 5824315 commit 5db6c11
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
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)")
}
}

0 comments on commit 5db6c11

Please sign in to comment.