@@ -26,6 +26,7 @@ import (
26
26
"io/ioutil"
27
27
"mime"
28
28
"net/http"
29
+ "net/url"
29
30
"sync"
30
31
"time"
31
32
)
@@ -40,9 +41,11 @@ var acceptedContentTypes = []string{contentType, "application/json-rpc", "applic
40
41
41
42
type httpConn struct {
42
43
client * http.Client
43
- req * http. Request
44
+ url string
44
45
closeOnce sync.Once
45
46
closeCh chan interface {}
47
+ mu sync.Mutex // protects headers
48
+ headers http.Header
46
49
}
47
50
48
51
// httpConn is treated specially by Client.
@@ -51,7 +54,7 @@ func (hc *httpConn) writeJSON(context.Context, interface{}) error {
51
54
}
52
55
53
56
func (hc * httpConn ) remoteAddr () string {
54
- return hc .req . URL . String ()
57
+ return hc .url
55
58
}
56
59
57
60
func (hc * httpConn ) readBatch () ([]* jsonrpcMessage , bool , error ) {
@@ -102,16 +105,24 @@ var DefaultHTTPTimeouts = HTTPTimeouts{
102
105
// DialHTTPWithClient creates a new RPC client that connects to an RPC server over HTTP
103
106
// using the provided HTTP Client.
104
107
func DialHTTPWithClient (endpoint string , client * http.Client ) (* Client , error ) {
105
- req , err := http .NewRequest (http .MethodPost , endpoint , nil )
108
+ // Sanity check URL so we don't end up with a client that will fail every request.
109
+ _ , err := url .Parse (endpoint )
106
110
if err != nil {
107
111
return nil , err
108
112
}
109
- req .Header .Set ("Content-Type" , contentType )
110
- req .Header .Set ("Accept" , contentType )
111
113
112
114
initctx := context .Background ()
115
+ headers := make (http.Header , 2 )
116
+ headers .Set ("accept" , contentType )
117
+ headers .Set ("content-type" , contentType )
113
118
return newClient (initctx , func (context.Context ) (ServerCodec , error ) {
114
- return & httpConn {client : client , req : req , closeCh : make (chan interface {})}, nil
119
+ hc := & httpConn {
120
+ client : client ,
121
+ headers : headers ,
122
+ url : endpoint ,
123
+ closeCh : make (chan interface {}),
124
+ }
125
+ return hc , nil
115
126
})
116
127
}
117
128
@@ -131,7 +142,7 @@ func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) e
131
142
if respBody != nil {
132
143
buf := new (bytes.Buffer )
133
144
if _ , err2 := buf .ReadFrom (respBody ); err2 == nil {
134
- return fmt .Errorf ("%v %v" , err , buf .String ())
145
+ return fmt .Errorf ("%v: %v" , err , buf .String ())
135
146
}
136
147
}
137
148
return err
@@ -166,10 +177,18 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos
166
177
if err != nil {
167
178
return nil , err
168
179
}
169
- req := hc .req .WithContext (ctx )
170
- req .Body = ioutil .NopCloser (bytes .NewReader (body ))
180
+ req , err := http .NewRequestWithContext (ctx , "POST" , hc .url , ioutil .NopCloser (bytes .NewReader (body )))
181
+ if err != nil {
182
+ return nil , err
183
+ }
171
184
req .ContentLength = int64 (len (body ))
172
185
186
+ // set headers
187
+ hc .mu .Lock ()
188
+ req .Header = hc .headers .Clone ()
189
+ hc .mu .Unlock ()
190
+
191
+ // do request
173
192
resp , err := hc .client .Do (req )
174
193
if err != nil {
175
194
return nil , err
0 commit comments