diff --git a/mgmt/rest/client/client.go b/mgmt/rest/client/client.go index 3b2cf093e..ac3013efa 100644 --- a/mgmt/rest/client/client.go +++ b/mgmt/rest/client/client.go @@ -34,6 +34,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/asaskevich/govalidator" @@ -101,6 +102,17 @@ func Username(u string) metaOp { } } +var ( + secureTransport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + IdleConnTimeout: time.Second, + } + insecureTransport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, + IdleConnTimeout: time.Second, + } +) + // New returns a pointer to a snap api client // if ver is an empty string, v1 is used by default func New(url, ver string, insecure bool, opts ...metaOp) (*Client, error) { @@ -110,16 +122,18 @@ func New(url, ver string, insecure bool, opts ...metaOp) (*Client, error) { if ver == "" { ver = "v1" } + var t *http.Transport + if insecure { + t = insecureTransport + } else { + t = secureTransport + } c := &Client{ URL: url, Version: ver, http: &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: insecure, - }, - }, + Transport: t, }, } for _, opt := range opts { @@ -249,7 +263,6 @@ func httpRespToAPIResp(rsp *http.Response) (*rbody.APIResponse, error) { } resp := new(rbody.APIResponse) b, err := ioutil.ReadAll(rsp.Body) - rsp.Body.Close() if err != nil { return nil, err }