From a26a108e64ece458717c51db00cdc577b1a3ead3 Mon Sep 17 00:00:00 2001 From: Jerome Froelich Date: Tue, 8 Dec 2020 16:47:38 -0500 Subject: [PATCH 1/3] [x/net] Add Proxy field to HTTPClientOptions --- src/x/net/http/client.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/x/net/http/client.go b/src/x/net/http/client.go index 246aad0e92..fb466fdfc7 100644 --- a/src/x/net/http/client.go +++ b/src/x/net/http/client.go @@ -23,9 +23,14 @@ package xhttp import ( "net" "net/http" + "net/url" "time" ) +// ProxyFunc is a type alias for the proxy function used by the standard +// library's http.Transport struct. +type ProxyFunc func(*http.Request) (*url.URL, error) + // HTTPClientOptions specify HTTP Client options. type HTTPClientOptions struct { RequestTimeout time.Duration `yaml:"requestTimeout"` @@ -34,6 +39,7 @@ type HTTPClientOptions struct { IdleConnTimeout time.Duration `yaml:"idleConnTimeout"` MaxIdleConns int `yaml:"maxIdleConns"` DisableCompression bool `yaml:"disableCompression"` + Proxy ProxyFunc `yaml:"proxy"` } // NewHTTPClient constructs a new HTTP Client. @@ -41,7 +47,7 @@ func NewHTTPClient(o HTTPClientOptions) *http.Client { return &http.Client{ Timeout: o.RequestTimeout, Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, + Proxy: o.Proxy, Dial: (&net.Dialer{ Timeout: o.ConnectTimeout, KeepAlive: o.KeepAlive, @@ -67,5 +73,6 @@ func DefaultHTTPClientOptions() HTTPClientOptions { // DisableCompression is true by default since we have seen // a large amount of overhead with compression. DisableCompression: true, + Proxy: ProxyFunc(http.ProxyFromEnvironment), } } From 815f13d4aea64097e1270f0871b99bc62bca37a9 Mon Sep 17 00:00:00 2001 From: Jerome Froelich Date: Tue, 8 Dec 2020 18:09:29 -0500 Subject: [PATCH 2/3] Set Proxy field if it is not set --- src/x/net/http/client.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/x/net/http/client.go b/src/x/net/http/client.go index fb466fdfc7..b43795de82 100644 --- a/src/x/net/http/client.go +++ b/src/x/net/http/client.go @@ -44,6 +44,14 @@ type HTTPClientOptions struct { // NewHTTPClient constructs a new HTTP Client. func NewHTTPClient(o HTTPClientOptions) *http.Client { + // Before we added the Proxy option, we unconditionally set the field in + // the http.Transport we construct below to http.ProxyFromEnvironment. To + // keep that behavior unchanged now that we added the field, we need to + // set the option if it is nil. + if o.Proxy == nil { + o.Proxy = http.ProxyFromEnvironment + } + return &http.Client{ Timeout: o.RequestTimeout, Transport: &http.Transport{ From fa43f4e3a79a46ac5797c516ae7eaec3fbf9336c Mon Sep 17 00:00:00 2001 From: Jerome Froelich Date: Tue, 8 Dec 2020 18:18:38 -0500 Subject: [PATCH 3/3] Ensure code is gofmt'ed --- src/x/net/http/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/x/net/http/client.go b/src/x/net/http/client.go index b43795de82..9ff579346d 100644 --- a/src/x/net/http/client.go +++ b/src/x/net/http/client.go @@ -48,7 +48,7 @@ func NewHTTPClient(o HTTPClientOptions) *http.Client { // the http.Transport we construct below to http.ProxyFromEnvironment. To // keep that behavior unchanged now that we added the field, we need to // set the option if it is nil. - if o.Proxy == nil { + if o.Proxy == nil { o.Proxy = http.ProxyFromEnvironment }