|
4 | 4 | "context"
|
5 | 5 | "encoding/hex"
|
6 | 6 | "fmt"
|
| 7 | + "net/http" |
| 8 | + "net/url" |
7 | 9 | "time"
|
8 | 10 |
|
9 | 11 | "github.com/renproject/multichain/api/account"
|
@@ -100,7 +102,17 @@ type Client struct {
|
100 | 102 |
|
101 | 103 | // NewClient returns a new Client.
|
102 | 104 | 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 | + }) |
104 | 116 | if err != nil {
|
105 | 117 | panic(err)
|
106 | 118 | }
|
@@ -213,3 +225,29 @@ func (client *Client) AccountBalance(_ context.Context, addr address.Address) (p
|
213 | 225 |
|
214 | 226 | return pack.NewU256FromInt(balance), nil
|
215 | 227 | }
|
| 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