Skip to content

Commit

Permalink
Adds timeout param; fixes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Jan 8, 2018
1 parent d466028 commit 37475bf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
10 changes: 9 additions & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type config struct {
followLocation bool
method string
saveStatus int
timeout int
verbose bool

paths string
Expand Down Expand Up @@ -66,6 +67,11 @@ func processArgs() config {
flag.IntVar(&saveStatus, "savestatus", 0, "")
flag.IntVar(&saveStatus, "s", 0, "")

// timeout param
timeout := 10000
flag.IntVar(&timeout, "timeout", 10000, "")
flag.IntVar(&timeout, "t", 10000, "")

// rawhttp param
rawHTTP := false
flag.BoolVar(&rawHTTP, "rawhttp", false, "")
Expand Down Expand Up @@ -109,6 +115,7 @@ func processArgs() config {
followLocation: followLocation,
method: method,
saveStatus: saveStatus,
timeout: timeout,
requester: requesterFn,
verbose: verbose,
paths: paths,
Expand All @@ -126,11 +133,12 @@ func init() {

h += "Options:\n"
h += " -c, --concurrency <val> Set the concurrency level (defaut: 20)\n"
h += " -d, --delay <val> Milliseconds between requests to the same host (defaut: 5000)\n"
h += " -d, --delay <millis> Milliseconds between requests to the same host (defaut: 5000)\n"
h += " -H, --header <header> Send a custom HTTP header\n"
h += " -L, --location Follow redirects / location header\n"
h += " -r, --rawhttp Use the rawhttp library for requests (experimental)\n"
h += " -s, --savestatus <status> Save only responses with specific status code\n"
h += " -t, --timeout <millis> Set the HTTP timeout (default: 10000)\n"
h += " -v, --verbose Verbose mode\n"
h += " -X, --method <method> HTTP method (default: GET)\n\n"

Expand Down
3 changes: 1 addition & 2 deletions gohttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"net/http"
"strings"
"time"
)

var transport = &http.Transport{
Expand All @@ -15,10 +14,10 @@ var transport = &http.Transport{

var httpClient = &http.Client{
Transport: transport,
Timeout: time.Second * 10,
}

func goRequest(r request) response {
httpClient.Timeout = r.timeout

if !r.followLocation {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func main() {
path: prefixedPath,
headers: c.headers,
followLocation: c.followLocation,
timeout: time.Duration(c.timeout * 1000000),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"net/url"
"strings"
"time"
)

// a request is a wrapper for a URL that we want to request
Expand All @@ -13,6 +14,7 @@ type request struct {
headers []string

followLocation bool
timeout time.Duration
}

// Hostname returns the hostname part of the request
Expand Down

0 comments on commit 37475bf

Please sign in to comment.