Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Clean up regular expressions for headers and auth and fix | being allowed for headers, allow any characters for auth password

Beat Richartzfe57084a1284
diff --git a/boom.go b/boom.go
index fe80c2e..1f86b0b 100644
--- a/boom.go
+++ b/boom.go
@@ -15,6 +15,7 @@
package main
import (
+ "errors"
"flag"
"fmt"
"net"
@@ -27,6 +28,11 @@ import (
"github.com/rakyll/boom/boomer"
)
+const (
+ headerRegexp = "^([\\w-]+):\\s*(.+)"
+ authRegexp = "^([\\w-\\.]+):(.+)"
+)
+
var (
flagMethod = flag.String("m", "GET", "")
flagHeaders = flag.String("h", "", "")
@@ -119,12 +125,11 @@ func main() {
if *flagHeaders != "" {
headers := strings.Split(*flagHeaders, ";")
for _, h := range headers {
- re := regexp.MustCompile("([\\w|-]+):(.+)")
- matches := re.FindAllStringSubmatch(h, -1)
- if len(matches) < 1 {
- usageAndExit("")
+ match, err := parseInputWithRegexp(h, headerRegexp)
+ if err != nil {
+ usageAndExit(err.Error())
}
- header.Set(matches[0][1], matches[0][2])
+ header.Set(match[1], match[2])
}
}
@@ -134,13 +139,11 @@ func main() {
// set basic auth if set
if *flagAuth != "" {
- re := regexp.MustCompile("([\\w|\\-|_|\\.]+):(\\w+)")
- matches := re.FindAllStringSubmatch(*flagAuth, -1)
- if len(matches) < 1 {
- usageAndExit("")
+ match, err := parseInputWithRegexp(*flagAuth, authRegexp)
+ if err != nil {
+ usageAndExit(err.Error())
}
- username = matches[0][1]
- password = matches[0][2]
+ username, password = match[1], match[2]
}
if *flagOutput != "csv" && *flagOutput != "" {
@@ -219,3 +222,13 @@ func usageAndExit(message string) {
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
+
+func parseInputWithRegexp(input, regx string) (matches []string, err error) {
+ re := regexp.MustCompile(regx)
+ matches = re.FindStringSubmatch(input)
+ if len(matches) < 1 {
+ err = errors.New("Could not parse provided input")
+ }
+
+ return
+}
diff --git a/boom_test.go b/boom_test.go
index dbc24d1..3e7bac4 100644
--- a/boom_test.go
+++ b/boom_test.go
@@ -77,3 +77,37 @@ func TestParseUrl_IPv6AndPort(t *testing.T) {
t.Errorf("URL is expected to be http://[2a00:1450:400a:806::1007]:80, %v is found.", u)
}
}
+
+func TestParseValidHeaderFlag(t *testing.T) {
+ match, err := parseInputWithRegexp("X-Something: !Y10K:;(He@poverflow?)", headerRegexp)
+ if err != nil {
+ t.Errorf("A valid header was not parsed correctly: %v", err.Error())
+ }
+ if match[1] != "X-Something" || match[2] != "!Y10K:;(He@poverflow?)" {
+ t.Errorf("A valid header was not parsed correctly, parsed values: %v %v", match[1], match[2])
+ }
+}
+
+func TestParseInvalidHeaderFlag(t *testing.T) {
+ _, err := parseInputWithRegexp("X|oh|bad-input: badbadbad", headerRegexp)
+ if err == nil {
+ t.Errorf("An invalid header passed parsing")
+ }
+}
+
+func TestParseValidAuthFlag(t *testing.T) {
+ match, err := parseInputWithRegexp("_coo-kie_:!!bigmonster@1969sid", authRegexp)
+ if err != nil {
+ t.Errorf("A valid auth flag was not parsed correctly: %v", err.Error())
+ }
+ if match[1] != "_coo-kie_" || match[2] != "!!bigmonster@1969sid" {
+ t.Errorf("A valid auth flag was not parsed correctly, parsed values: %v %v", match[1], match[2])
+ }
+}
+
+func TestParseInvalidAuthFlag(t *testing.T) {
+ _, err := parseInputWithRegexp("X|oh|bad-input: badbadbad", authRegexp)
+ if err == nil {
+ t.Errorf("An invalid header passed parsing")
+ }
+}