Skip to content

Commit e71c65b

Browse files
committed
bombardier: change urls handling
This commit removes urlx from dependencies and adds a new code to parse/fix not-entirely-valid URLs. It also (hopefully) fixes the issue reported in #39. Updates #39.
1 parent 8db045e commit e71c65b

9 files changed

Lines changed: 96 additions & 443 deletions

File tree

args_parser.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package main
22

33
import (
44
"fmt"
5+
"net"
6+
"net/url"
7+
"regexp"
58
"runtime"
69
"strconv"
710
"strings"
@@ -200,11 +203,15 @@ func (k *kingpinParser) parse(args []string) (config, error) {
200203
"unknown format or invalid format spec %q", k.formatSpec,
201204
)
202205
}
206+
url, err := tryParseURL(k.url)
207+
if err != nil {
208+
return emptyConf, err
209+
}
203210
return config{
204211
numConns: k.numConns,
205212
numReqs: k.numReqs.val,
206213
duration: k.duration.val,
207-
url: k.url,
214+
url: url,
208215
headers: k.headers,
209216
timeout: k.timeout,
210217
method: k.method,
@@ -253,3 +260,68 @@ func parsePrintSpec(spec string) (bool, bool, bool, error) {
253260
}
254261
return pi, pp, pr, nil
255262
}
263+
264+
var re = regexp.MustCompile(`^(?P<proto>.+:\/\/)?.*$`)
265+
266+
func tryParseURL(raw string) (string, error) {
267+
rs := raw
268+
269+
// Try the parse.
270+
m := re.FindStringSubmatch(rs)
271+
if m == nil {
272+
// Just in case.
273+
return "", fmt.Errorf(
274+
"%v does not appear to be a valid URL",
275+
raw,
276+
)
277+
}
278+
279+
// If the URL doesn't start with a scheme, assume that the user
280+
// meant 'http'.
281+
proto := m[1]
282+
if proto == "" {
283+
rs = "http://" + rs
284+
} else if proto != "http://" && proto != "https://" {
285+
// We're not interested in other protocols.
286+
return "", fmt.Errorf(
287+
"%q is not an acceptable protocol (http, https): %v",
288+
proto, raw,
289+
)
290+
}
291+
292+
u, err := url.Parse(rs)
293+
if err != nil {
294+
return "", fmt.Errorf(
295+
"%v does not appear to be a valid URL: %v",
296+
raw, err,
297+
)
298+
}
299+
300+
// If port is not present append a default one to the u.Host.
301+
schemePort := map[string]string{
302+
"http": ":80",
303+
"https": ":443",
304+
}
305+
if u.Port() == "" {
306+
u.Host = u.Host + schemePort[u.Scheme]
307+
}
308+
309+
host, port, err := net.SplitHostPort(u.Host)
310+
if err != nil {
311+
return "", fmt.Errorf(
312+
"%v does not appear to be a valid URL",
313+
raw,
314+
)
315+
}
316+
317+
// If user omitted the host, assume that he meant 'localhost'.
318+
// net/http seem to be doing this already, but fasthttp needs
319+
// host to be specified explicitly.
320+
if host == "" {
321+
host = "localhost"
322+
}
323+
324+
u.Host = net.JoinHostPort(host, port)
325+
326+
return u.String(), nil
327+
}

args_parser_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestArgsParsing(t *testing.T) {
5757
timeout: defaultTimeout,
5858
headers: new(headersList),
5959
method: "GET",
60-
url: "https://somehost.somedomain",
60+
url: "https://somehost.somedomain:443",
6161
printIntro: true,
6262
printProgress: true,
6363
printResult: true,
@@ -101,7 +101,7 @@ func TestArgsParsing(t *testing.T) {
101101
headers: new(headersList),
102102
method: "GET",
103103
numReqs: &defaultNumberOfReqs,
104-
url: "https://somehost.somedomain",
104+
url: "https://somehost.somedomain:443",
105105
printIntro: true,
106106
printProgress: true,
107107
printResult: true,
@@ -127,7 +127,7 @@ func TestArgsParsing(t *testing.T) {
127127
headers: new(headersList),
128128
printLatencies: true,
129129
method: "GET",
130-
url: "https://somehost.somedomain",
130+
url: "https://somehost.somedomain:443",
131131
printIntro: true,
132132
printProgress: true,
133133
printResult: true,
@@ -153,7 +153,7 @@ func TestArgsParsing(t *testing.T) {
153153
headers: new(headersList),
154154
insecure: true,
155155
method: "GET",
156-
url: "https://somehost.somedomain",
156+
url: "https://somehost.somedomain:443",
157157
printIntro: true,
158158
printProgress: true,
159159
printResult: true,
@@ -182,7 +182,7 @@ func TestArgsParsing(t *testing.T) {
182182
method: "GET",
183183
keyPath: "testclient.key",
184184
certPath: "testclient.cert",
185-
url: "https://somehost.somedomain",
185+
url: "https://somehost.somedomain:443",
186186
printIntro: true,
187187
printProgress: true,
188188
printResult: true,
@@ -222,7 +222,7 @@ func TestArgsParsing(t *testing.T) {
222222
headers: new(headersList),
223223
method: "POST",
224224
body: "reqbody",
225-
url: "https://somehost.somedomain",
225+
url: "https://somehost.somedomain:443",
226226
printIntro: true,
227227
printProgress: true,
228228
printResult: true,
@@ -258,7 +258,7 @@ func TestArgsParsing(t *testing.T) {
258258
{"Two", "Value two"},
259259
},
260260
method: "GET",
261-
url: "https://somehost.somedomain",
261+
url: "https://somehost.somedomain:443",
262262
printIntro: true,
263263
printProgress: true,
264264
printResult: true,
@@ -293,7 +293,7 @@ func TestArgsParsing(t *testing.T) {
293293
timeout: defaultTimeout,
294294
headers: new(headersList),
295295
method: "GET",
296-
url: "https://somehost.somedomain",
296+
url: "https://somehost.somedomain:443",
297297
rate: &ten,
298298
printIntro: true,
299299
printProgress: true,
@@ -318,7 +318,7 @@ func TestArgsParsing(t *testing.T) {
318318
timeout: defaultTimeout,
319319
headers: new(headersList),
320320
method: "GET",
321-
url: "https://somehost.somedomain",
321+
url: "https://somehost.somedomain:443",
322322
clientType: fhttp,
323323
printIntro: true,
324324
printProgress: true,
@@ -339,7 +339,7 @@ func TestArgsParsing(t *testing.T) {
339339
timeout: defaultTimeout,
340340
headers: new(headersList),
341341
method: "GET",
342-
url: "https://somehost.somedomain",
342+
url: "https://somehost.somedomain:443",
343343
clientType: nhttp1,
344344
printIntro: true,
345345
printProgress: true,
@@ -360,7 +360,7 @@ func TestArgsParsing(t *testing.T) {
360360
timeout: defaultTimeout,
361361
headers: new(headersList),
362362
method: "GET",
363-
url: "https://somehost.somedomain",
363+
url: "https://somehost.somedomain:443",
364364
clientType: nhttp2,
365365
printIntro: true,
366366
printProgress: true,
@@ -392,7 +392,7 @@ func TestArgsParsing(t *testing.T) {
392392
headers: new(headersList),
393393
method: "GET",
394394
bodyFilePath: "testbody.txt",
395-
url: "https://somehost.somedomain",
395+
url: "https://somehost.somedomain:443",
396396
printIntro: true,
397397
printProgress: true,
398398
printResult: true,
@@ -418,7 +418,7 @@ func TestArgsParsing(t *testing.T) {
418418
headers: new(headersList),
419419
method: "GET",
420420
stream: true,
421-
url: "https://somehost.somedomain",
421+
url: "https://somehost.somedomain:443",
422422
printIntro: true,
423423
printProgress: true,
424424
printResult: true,
@@ -437,7 +437,7 @@ func TestArgsParsing(t *testing.T) {
437437
timeout: defaultTimeout,
438438
headers: new(headersList),
439439
method: "GET",
440-
url: "https://somehost.somedomain",
440+
url: "https://somehost.somedomain:443",
441441
printIntro: true,
442442
printProgress: true,
443443
printResult: true,
@@ -482,7 +482,7 @@ func TestArgsParsing(t *testing.T) {
482482
timeout: defaultTimeout,
483483
headers: new(headersList),
484484
method: "GET",
485-
url: "https://somehost.somedomain",
485+
url: "https://somehost.somedomain:443",
486486
printIntro: true,
487487
printProgress: true,
488488
printResult: true,
@@ -527,7 +527,7 @@ func TestArgsParsing(t *testing.T) {
527527
timeout: defaultTimeout,
528528
headers: new(headersList),
529529
method: "GET",
530-
url: "https://somehost.somedomain",
530+
url: "https://somehost.somedomain:443",
531531
printIntro: true,
532532
printProgress: false,
533533
printResult: true,
@@ -552,7 +552,7 @@ func TestArgsParsing(t *testing.T) {
552552
timeout: defaultTimeout,
553553
headers: new(headersList),
554554
method: "GET",
555-
url: "https://somehost.somedomain",
555+
url: "https://somehost.somedomain:443",
556556
printIntro: false,
557557
printProgress: false,
558558
printResult: false,
@@ -597,7 +597,7 @@ func TestArgsParsing(t *testing.T) {
597597
timeout: defaultTimeout,
598598
headers: new(headersList),
599599
method: "GET",
600-
url: "https://somehost.somedomain",
600+
url: "https://somehost.somedomain:443",
601601
printIntro: true,
602602
printProgress: true,
603603
printResult: true,
@@ -642,7 +642,7 @@ func TestArgsParsing(t *testing.T) {
642642
timeout: defaultTimeout,
643643
headers: new(headersList),
644644
method: "GET",
645-
url: "https://somehost.somedomain",
645+
url: "https://somehost.somedomain:443",
646646
printIntro: true,
647647
printProgress: true,
648648
printResult: true,
@@ -672,7 +672,7 @@ func TestArgsParsing(t *testing.T) {
672672
timeout: defaultTimeout,
673673
headers: new(headersList),
674674
method: "GET",
675-
url: "https://somehost.somedomain",
675+
url: "https://somehost.somedomain:443",
676676
printIntro: true,
677677
printProgress: true,
678678
printResult: true,

clients.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/goware/urlx"
1312
"github.com/valyala/fasthttp"
1413
"golang.org/x/net/http2"
1514
)
@@ -140,7 +139,7 @@ func newHTTPClient(opts *clientOpts) client {
140139
c.headers = headersToHTTPHeaders(opts.headers)
141140
c.method, c.body, c.bodProd = opts.method, opts.body, opts.bodProd
142141
var err error
143-
c.url, err = urlx.Parse(opts.url)
142+
c.url, err = url.Parse(opts.url)
144143
if err != nil {
145144
// opts.url guaranteed to be valid at this point
146145
panic(err)

config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package main
22

33
import (
44
"fmt"
5+
"net/url"
56
"sort"
67
"time"
7-
8-
"github.com/goware/urlx"
98
)
109

1110
type config struct {
@@ -84,7 +83,7 @@ func (c *config) testType() testTyp {
8483
}
8584

8685
func (c *config) checkURL() error {
87-
url, err := urlx.Parse(c.url)
86+
url, err := url.Parse(c.url)
8887
if err != nil {
8988
return err
9089
}

config_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -366,27 +366,6 @@ func TestInvalidHTTPMethodError(t *testing.T) {
366366
}
367367
}
368368

369-
func TestParsingOfURLsWithoutScheme(t *testing.T) {
370-
c := config{
371-
numConns: defaultNumberOfConns,
372-
numReqs: nil,
373-
duration: nil,
374-
url: "localhost:8080",
375-
headers: new(headersList),
376-
timeout: defaultTimeout,
377-
method: "GET",
378-
body: "",
379-
}
380-
if err := c.checkArgs(); err != nil {
381-
t.Error(err)
382-
return
383-
}
384-
exp := "http://localhost:8080"
385-
if act := c.url; act != exp {
386-
t.Error(exp, act)
387-
}
388-
}
389-
390369
func TestClientTypToStringConversion(t *testing.T) {
391370
expectations := []struct {
392371
in clientTyp

vendor/github.com/goware/urlx/LICENSE

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)