aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/http.go
diff options
context:
space:
mode:
authorBas van Kervel <bas@ethdev.com>2016-03-14 16:38:54 +0800
committerBas van Kervel <bas@ethdev.com>2016-04-12 17:02:39 +0800
commitaa9fff3e68b1def0a9a22009c233150bf9ba481f (patch)
tree926c241574d6d80dfe4ffd6d2e447a9f7f84dc8b /rpc/http.go
parent7e02105672cda92889a78db864a5701d78f45eb2 (diff)
downloaddexon-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar
dexon-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.gz
dexon-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.bz2
dexon-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.lz
dexon-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.xz
dexon-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.zst
dexon-aa9fff3e68b1def0a9a22009c233150bf9ba481f.zip
rpc: various fixes/enhancements
rpc: be less restrictive on the request id rpc: improved documentation console: upgrade web3.js to version 0.16.0 rpc: cache http connections rpc: rename wsDomains parameter to wsOrigins
Diffstat (limited to 'rpc/http.go')
-rw-r--r--rpc/http.go20
1 files changed, 6 insertions, 14 deletions
diff --git a/rpc/http.go b/rpc/http.go
index dd1ec2c01..9283ce0ec 100644
--- a/rpc/http.go
+++ b/rpc/http.go
@@ -20,13 +20,12 @@ import (
"bytes"
"encoding/json"
"fmt"
+ "io"
"io/ioutil"
"net/http"
"net/url"
"strings"
- "io"
-
"github.com/rs/cors"
)
@@ -36,8 +35,9 @@ const (
// httpClient connects to a geth RPC server over HTTP.
type httpClient struct {
- endpoint *url.URL // HTTP-RPC server endpoint
- lastRes []byte // HTTP requests are synchronous, store last response
+ endpoint *url.URL // HTTP-RPC server endpoint
+ httpClient http.Client // reuse connection
+ lastRes []byte // HTTP requests are synchronous, store last response
}
// NewHTTPClient create a new RPC clients that connection to a geth RPC server
@@ -57,30 +57,22 @@ func (client *httpClient) Send(msg interface{}) error {
var err error
client.lastRes = nil
-
if body, err = json.Marshal(msg); err != nil {
return err
}
- httpReq, err := http.NewRequest("POST", client.endpoint.String(), bytes.NewBuffer(body))
+ resp, err := client.httpClient.Post(client.endpoint.String(), "application/json", bytes.NewReader(body))
if err != nil {
return err
}
- httpReq.Header.Set("Content-Type", "application/json")
- httpClient := http.Client{}
- resp, err := httpClient.Do(httpReq)
- if err != nil {
- return err
- }
defer resp.Body.Close()
-
if resp.StatusCode == http.StatusOK {
client.lastRes, err = ioutil.ReadAll(resp.Body)
return err
}
- return fmt.Errorf("unable to handle request")
+ return fmt.Errorf("request failed: %s", resp.Status)
}
// Recv will try to deserialize the last received response into the given msg.