Skip to content

Commit

Permalink
bombardier: change urls handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
codesenberg committed Dec 18, 2018
1 parent 8db045e commit e71c65b
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 443 deletions.
74 changes: 73 additions & 1 deletion args_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"fmt"
"net"
"net/url"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -200,11 +203,15 @@ func (k *kingpinParser) parse(args []string) (config, error) {
"unknown format or invalid format spec %q", k.formatSpec,
)
}
url, err := tryParseURL(k.url)
if err != nil {
return emptyConf, err
}
return config{
numConns: k.numConns,
numReqs: k.numReqs.val,
duration: k.duration.val,
url: k.url,
url: url,
headers: k.headers,
timeout: k.timeout,
method: k.method,
Expand Down Expand Up @@ -253,3 +260,68 @@ func parsePrintSpec(spec string) (bool, bool, bool, error) {
}
return pi, pp, pr, nil
}

var re = regexp.MustCompile(`^(?P<proto>.+:\/\/)?.*$`)

func tryParseURL(raw string) (string, error) {
rs := raw

// Try the parse.
m := re.FindStringSubmatch(rs)
if m == nil {
// Just in case.
return "", fmt.Errorf(
"%v does not appear to be a valid URL",
raw,
)
}

// If the URL doesn't start with a scheme, assume that the user
// meant 'http'.
proto := m[1]
if proto == "" {
rs = "http://" + rs
} else if proto != "http://" && proto != "https://" {
// We're not interested in other protocols.
return "", fmt.Errorf(
"%q is not an acceptable protocol (http, https): %v",
proto, raw,
)
}

u, err := url.Parse(rs)
if err != nil {
return "", fmt.Errorf(
"%v does not appear to be a valid URL: %v",
raw, err,
)
}

// If port is not present append a default one to the u.Host.
schemePort := map[string]string{
"http": ":80",
"https": ":443",
}
if u.Port() == "" {
u.Host = u.Host + schemePort[u.Scheme]
}

host, port, err := net.SplitHostPort(u.Host)
if err != nil {
return "", fmt.Errorf(
"%v does not appear to be a valid URL",
raw,
)
}

// If user omitted the host, assume that he meant 'localhost'.
// net/http seem to be doing this already, but fasthttp needs
// host to be specified explicitly.
if host == "" {
host = "localhost"
}

u.Host = net.JoinHostPort(host, port)

return u.String(), nil
}
40 changes: 20 additions & 20 deletions args_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "GET",
numReqs: &defaultNumberOfReqs,
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -127,7 +127,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
printLatencies: true,
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -153,7 +153,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
insecure: true,
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestArgsParsing(t *testing.T) {
method: "GET",
keyPath: "testclient.key",
certPath: "testclient.cert",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "POST",
body: "reqbody",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestArgsParsing(t *testing.T) {
{"Two", "Value two"},
},
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
rate: &ten,
printIntro: true,
printProgress: true,
Expand All @@ -318,7 +318,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
clientType: fhttp,
printIntro: true,
printProgress: true,
Expand All @@ -339,7 +339,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
clientType: nhttp1,
printIntro: true,
printProgress: true,
Expand All @@ -360,7 +360,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
clientType: nhttp2,
printIntro: true,
printProgress: true,
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "GET",
bodyFilePath: "testbody.txt",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -418,7 +418,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "GET",
stream: true,
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -437,7 +437,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -482,7 +482,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: false,
printResult: true,
Expand All @@ -552,7 +552,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: false,
printProgress: false,
printResult: false,
Expand Down Expand Up @@ -597,7 +597,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -672,7 +672,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: "https://somehost.somedomain:443",
printIntro: true,
printProgress: true,
printResult: true,
Expand Down
3 changes: 1 addition & 2 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/goware/urlx"
"github.com/valyala/fasthttp"
"golang.org/x/net/http2"
)
Expand Down Expand Up @@ -140,7 +139,7 @@ func newHTTPClient(opts *clientOpts) client {
c.headers = headersToHTTPHeaders(opts.headers)
c.method, c.body, c.bodProd = opts.method, opts.body, opts.bodProd
var err error
c.url, err = urlx.Parse(opts.url)
c.url, err = url.Parse(opts.url)
if err != nil {
// opts.url guaranteed to be valid at this point
panic(err)
Expand Down
5 changes: 2 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package main

import (
"fmt"
"net/url"
"sort"
"time"

"github.com/goware/urlx"
)

type config struct {
Expand Down Expand Up @@ -84,7 +83,7 @@ func (c *config) testType() testTyp {
}

func (c *config) checkURL() error {
url, err := urlx.Parse(c.url)
url, err := url.Parse(c.url)
if err != nil {
return err
}
Expand Down
21 changes: 0 additions & 21 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,27 +366,6 @@ func TestInvalidHTTPMethodError(t *testing.T) {
}
}

func TestParsingOfURLsWithoutScheme(t *testing.T) {
c := config{
numConns: defaultNumberOfConns,
numReqs: nil,
duration: nil,
url: "localhost:8080",
headers: new(headersList),
timeout: defaultTimeout,
method: "GET",
body: "",
}
if err := c.checkArgs(); err != nil {
t.Error(err)
return
}
exp := "http://localhost:8080"
if act := c.url; act != exp {
t.Error(exp, act)
}
}

func TestClientTypToStringConversion(t *testing.T) {
expectations := []struct {
in clientTyp
Expand Down
22 changes: 0 additions & 22 deletions vendor/github.com/goware/urlx/LICENSE

This file was deleted.

Loading

0 comments on commit e71c65b

Please sign in to comment.