Skip to content

Commit

Permalink
feature: add --proxy to net/http
Browse files Browse the repository at this point in the history
This adds a new --proxy flag,
that alows to specify a proxy to use like in curl.

One could instead fix the env var mechanism
which does not work as reported
(see codesenberg#78).

But this is maybe more straigthforward and more user friendly.
  • Loading branch information
mariotrucco committed Nov 19, 2021
1 parent 9376ab4 commit 059e842
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
7 changes: 7 additions & 0 deletions args_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type kingpinParser struct {
method string
body string
bodyFilePath string
proxy string
stream bool
certPath string
keyPath string
Expand All @@ -56,6 +57,7 @@ func newKingpinParser() argsParser {
method: "GET",
body: "",
bodyFilePath: "",
proxy: "",
stream: false,
certPath: "",
keyPath: "",
Expand Down Expand Up @@ -151,6 +153,10 @@ func newKingpinParser() argsParser {
}).
Bool()

app.Flag("proxy", "Use the specified HTTP proxy").
Default("").
StringVar(&kparser.proxy)

app.Flag(
"print", "Specifies what to output. Comma-separated list of values"+
" 'intro' (short: 'i'), 'progress' (short: 'p'),"+
Expand Down Expand Up @@ -222,6 +228,7 @@ func (k *kingpinParser) parse(args []string) (config, error) {
method: k.method,
body: k.body,
bodyFilePath: k.bodyFilePath,
proxy: k.proxy,
stream: k.stream,
keyPath: k.keyPath,
certPath: k.certPath,
Expand Down
29 changes: 29 additions & 0 deletions args_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,35 @@ func TestArgsParsing(t *testing.T) {
format: knownFormat("plain-text"),
},
},
{
[][]string{
{
programName,
"--http2",
"--proxy", "http://proxyHost:proxyPort",
"https://somehost.somedomain",
},
{
programName,
"--http2",
"--proxy=http://proxyHost:proxyPort",
"https://somehost.somedomain",
},
},
config{
numConns: defaultNumberOfConns,
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain:443",
clientType: nhttp2,
printIntro: true,
printProgress: true,
printResult: true,
format: knownFormat("plain-text"),
proxy: "http://proxyHost:proxyPort",
},
},
{
[][]string{
{
Expand Down
1 change: 1 addition & 0 deletions bombardier.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func newBombardier(c config) (*bombardier, error) {
headers: c.headers,
url: c.url,
method: c.method,
proxy: c.proxy,
body: pbody,
bodProd: bsp,
bytesRead: &b.bytesRead,
Expand Down
12 changes: 10 additions & 2 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type clientOpts struct {
tlsConfig *tls.Config
disableKeepAlives bool

headers *headersList
url, method string
headers *headersList
url, method, proxy string

body *string
bodProd bodyStreamProducer
Expand Down Expand Up @@ -132,11 +132,19 @@ type httpClient struct {

func newHTTPClient(opts *clientOpts) client {
c := new(httpClient)

tr := &http.Transport{
TLSClientConfig: opts.tlsConfig,
MaxIdleConnsPerHost: int(opts.maxConns),
DisableKeepAlives: opts.disableKeepAlives,
}

if opts.proxy != "" {
url_i := url.URL{}
url_proxy, _ := url_i.Parse(opts.proxy)
tr.Proxy = http.ProxyURL(url_proxy)
}

tr.DialContext = httpDialContextFunc(opts.bytesRead, opts.bytesWritten)
if opts.HTTP2 {
_ = http2.ConfigureTransport(tr)
Expand Down
18 changes: 9 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

type config struct {
numConns uint64
numReqs *uint64
disableKeepAlives bool
duration *time.Duration
url, method, certPath, keyPath string
body, bodyFilePath string
stream bool
headers *headersList
timeout time.Duration
numConns uint64
numReqs *uint64
disableKeepAlives bool
duration *time.Duration
url, method, certPath, keyPath, proxy string
body, bodyFilePath string
stream bool
headers *headersList
timeout time.Duration
// TODO(codesenberg): printLatencies should probably be
// re(named&maked) into printPercentiles or even let
// users provide their own percentiles and not just
Expand Down

0 comments on commit 059e842

Please sign in to comment.