From 36541669ccb459563b22eb7a82fa65b07fef4f76 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Thu, 9 Jan 2020 12:24:05 -0800 Subject: [PATCH] RPC: Accept gzip encoding --- rpc/http.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/rpc/http.go b/rpc/http.go index 0de127c808..dbbec8081a 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -18,6 +18,7 @@ package rpc import ( "bytes" + "compress/gzip" "context" "encoding/json" "errors" @@ -38,6 +39,7 @@ import ( const ( maxRequestContentLength = 1024 * 1024 * 5 contentType = "application/json" + acceptContentEncoding = "gzip" ) // https://www.jsonrpc.org/historical/json-rpc-over-http.html#id13 @@ -113,6 +115,7 @@ func DialHTTPWithClient(endpoint string, client *http.Client) (*Client, error) { } req.Header.Set("Content-Type", contentType) req.Header.Set("Accept", contentType) + req.Header.Set("Accept-Encoding", acceptContentEncoding) initctx := context.Background() return newClient(initctx, func(context.Context) (ServerCodec, error) { @@ -179,10 +182,17 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos if err != nil { return nil, err } - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - return resp.Body, errors.New(resp.Status) + b := resp.Body + if !resp.Uncompressed && resp.Header.Get("Content-Encoding") == "gzip" { + b, err = gzip.NewReader(resp.Body) + if err != nil { + return nil, err + } } - return resp.Body, nil + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return b, errors.New(resp.Status) + } + return b, nil } // httpServerConn turns a HTTP connection into a Conn.