Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Moved hostname parsing and request creation into its own function

Pascal Hakim41f9b88dcc33
diff --git a/main.go b/main.go
index 1b06cc2..39940c8 100644
--- a/main.go
+++ b/main.go
@@ -95,23 +95,11 @@ func main() {
usageAndExit(err.Error())
}
- hostParts := strings.Split(uri.Host, ":")
- servername := hostParts[0]
-
- addrs, err := net.LookupHost(hostParts[0])
- if err != nil {
- usageAndExit("Hostname " + uri.Host + " is invalid")
- }
+ req := newReq(method, uri, flagD)
- hostParts[0] = addrs[0]
- uri.Host = strings.Join(hostParts, ":")
-
- req, _ := http.NewRequest(method, uri.String(), strings.NewReader(*flagD))
// set content-type
req.Header.Set("Content-Type", *flagType)
- req.Host = servername
-
// set any other additional headers
if *flagHeaders != "" {
headers := strings.Split(*flagHeaders, ";")
@@ -149,6 +137,25 @@ func main() {
Output: *flagOutput}).Run()
}
+func newReq(method string, uri *gourl.URL, flagD *string) *http.Request {
+ hostParts := strings.Split(uri.Host, ":")
+ servername := hostParts[0]
+
+ addrs, err := net.LookupHost(hostParts[0])
+ if err != nil {
+ usageAndExit("Hostname " + uri.Host + " is invalid")
+ }
+
+ hostParts[0] = addrs[0]
+ uri.Host = strings.Join(hostParts, ":")
+
+ req, _ := http.NewRequest(method, uri.String(), strings.NewReader(*flagD))
+
+ req.Host = servername
+
+ return req
+}
+
func usageAndExit(message string) {
if message != "" {
fmt.Fprintf(os.Stderr, message)