Skip to content

feat(sdk): support compressed response #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func NewClient(c *rpc.Client) *Client {
return &Client{c}
}

// SetHeader expose the function, in able to set http header.
func (ec *Client) SetHeader(key, value string) {
ec.c.SetHeader(key, value)
}

func (ec *Client) Close() {
ec.c.Close()
}
Expand Down
29 changes: 28 additions & 1 deletion rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package rpc

import (
"bytes"
"compress/gzip"
"compress/zlib"
"context"
"encoding/json"
"errors"
Expand All @@ -27,6 +29,7 @@ import (
"mime"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -198,7 +201,9 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos
Body: body,
}
}
return resp.Body, nil

// if encoding is set use it.
return newDecodeCompression(resp.Header.Get("Content-Encoding"), resp.Body)
}

// httpServerConn turns a HTTP connection into a Conn.
Expand All @@ -208,6 +213,28 @@ type httpServerConn struct {
r *http.Request
}

func newDecodeCompression(decoding string, rc io.ReadCloser) (io.ReadCloser, error) {
tps := strings.Split(strings.TrimSpace(strings.ToLower(decoding)), ",")
var res io.ReadCloser
switch tps[0] {
case "gzip":
gz, err := gzip.NewReader(rc)
if err != nil {
return nil, err
}
res = gz
case "deflate":
zl, err := zlib.NewReader(rc)
if err != nil {
return nil, err
}
res = zl
default:
res = rc
}
return res, nil
}

func newHTTPServerConn(r *http.Request, w http.ResponseWriter) ServerCodec {
body := io.LimitReader(r.Body, maxRequestContentLength)
conn := &httpServerConn{Reader: body, Writer: w, r: r}
Expand Down