Skip to content

Commit 17b07fa

Browse files
authored
Merge pull request #51 from renproject/fix/cosmos-url-parsing
Fix Cosmos URL parsing
2 parents 07b2020 + 0ae6cb8 commit 17b07fa

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

chain/cosmos/client.go

+39-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"encoding/hex"
66
"fmt"
7+
"net/http"
8+
"net/url"
79
"time"
810

911
"github.com/renproject/multichain/api/account"
@@ -100,7 +102,17 @@ type Client struct {
100102

101103
// NewClient returns a new Client.
102104
func NewClient(opts ClientOptions, cdc *codec.Codec, hrp string) *Client {
103-
httpClient, err := rpchttp.NewWithTimeout(opts.Host.String(), "websocket", uint(opts.Timeout/time.Second))
105+
httpClient, err := rpchttp.NewWithClient(
106+
string(opts.Host),
107+
"websocket",
108+
&http.Client{
109+
Timeout: opts.Timeout,
110+
111+
// We override the transport layer with a custom implementation as
112+
// there is an issue with the Cosmos SDK that causes it to
113+
// incorrectly parse URLs.
114+
Transport: newTransport(string(opts.Host), &http.Transport{}),
115+
})
104116
if err != nil {
105117
panic(err)
106118
}
@@ -213,3 +225,29 @@ func (client *Client) AccountBalance(_ context.Context, addr address.Address) (p
213225

214226
return pack.NewU256FromInt(balance), nil
215227
}
228+
229+
type transport struct {
230+
remote string
231+
proxy http.RoundTripper
232+
}
233+
234+
// newTransport returns a custom implementation of http.RoundTripper that
235+
// overrides the request URL prior to sending the request.
236+
func newTransport(remote string, proxy http.RoundTripper) *transport {
237+
return &transport{
238+
remote: remote,
239+
proxy: proxy,
240+
}
241+
}
242+
243+
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
244+
u, err := url.Parse(t.remote)
245+
if err != nil {
246+
return nil, err
247+
}
248+
req.URL = u
249+
req.Host = u.Host
250+
251+
// Proxy request.
252+
return t.proxy.RoundTrip(req)
253+
}

0 commit comments

Comments
 (0)