Skip to content

Commit

Permalink
Don't reinitialize the http options and transport when creating a cli…
Browse files Browse the repository at this point in the history
…ent. Use the golang url class to parse urls
  • Loading branch information
ffurano committed Jun 17, 2021
1 parent 338598b commit b6ff7a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
33 changes: 14 additions & 19 deletions pkg/eosclient/eosgrpc/eos_http/eoshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -232,11 +234,13 @@ func (c *Client) getRespError(rsp *http.Response, err error) error {

// From the basepath and the file path... build an url
func (c *Client) buildFullURL(urlpath, uid, gid string) (string, error) {
s := c.opt.BaseURL
if len(urlpath) > 0 && urlpath[0] != '/' {
s += "/"

u, err := url.Parse(c.opt.BaseURL)
if err != nil {
return "", err
}
s += urlpath

u.Path = path.Join(u.Path, urlpath)

// I feel safer putting here a check, to prohibit malicious users to
// inject a false uid/gid into the url
Expand All @@ -250,25 +254,16 @@ func (c *Client) buildFullURL(urlpath, uid, gid string) (string, error) {
return "", errtypes.PermissionDenied("Illegal malicious url " + urlpath)
}

eosuidgid := ""
v := u.Query()

if len(uid) > 0 {
eosuidgid += "eos.ruid=" + uid
v.Set("eos.ruid", uid)
}
if len(gid) > 0 {
if len(eosuidgid) > 0 {
eosuidgid += "&"
}
eosuidgid += "eos.rgid=" + gid
}

if strings.Contains(urlpath, "?") {
s += "&"
} else {
s += "?"
v.Set("eos.rgid", gid)
}
s += eosuidgid

return s, nil
return u.String(), nil
}

// GETFile does an entire GET to download a full file. Returns a stream to read the content from
Expand Down Expand Up @@ -308,7 +303,7 @@ func (c *Client) GETFile(ctx context.Context, httptransport *http.Transport, rem
resp, err := c.cl.Do(req)

// Let's support redirections... and if we retry we have to retry at the same FST, avoid going back to the MGM
if resp != nil && (resp.StatusCode == 307 || resp.StatusCode == 302) {
if resp != nil && (resp.StatusCode == http.StatusFound || resp.StatusCode == http.StatusTemporaryRedirect) {

// io.Copy(ioutil.Discard, resp.Body)
// resp.Body.Close()
Expand Down
16 changes: 4 additions & 12 deletions pkg/eosclient/eosgrpc/eosgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,15 @@ func (opt *Options) init() {
// using the EOS GRPC interface.
type Client struct {
opt *Options
htopts ehttp.Options
httptransport *http.Transport
cl erpc.EosClient
}

// GetHTTPCl creates an http client for immediate usage, using the already instantiated resources
func (c *Client) GetHTTPCl() *ehttp.Client {
var htopts ehttp.Options

t, err := htopts.Init()
if err != nil {
panic("Cant't init the EOS http client options")
}
htopts.BaseURL = c.opt.URL

return ehttp.New(&htopts, t)
return ehttp.New(&c.htopts, c.httptransport)
}

// Create and connect a grpc eos Client
Expand Down Expand Up @@ -192,14 +186,12 @@ func New(opt *Options) *Client {
c := new(Client)
c.opt = opt

var htopts ehttp.Options

t, err := htopts.Init()
t, err := c.htopts.Init()
if err != nil {
panic("Cant't init the EOS http client options")
}
c.httptransport = t
htopts.BaseURL = c.opt.URL
c.htopts.BaseURL = c.opt.URL

tctx := appctx.WithLogger(context.Background(), &tlog)
ccl, err := newgrpc(tctx, opt)
Expand Down

0 comments on commit b6ff7a4

Please sign in to comment.