Skip to content

Commit

Permalink
added headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
rbarazzutti committed Dec 5, 2021
1 parent d9f6503 commit 4123cb4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Flags:
-K, --disable-keepalive disable keep-alive feature
-x, --extra-parameter extra changing parameter, add an extra changing parameter to the request to avoid being cached by reverse proxy
-H, --head perform HTTP HEAD requests instead of GETs
--header stringArray add one or more header, in the form name:value
-h, --help help for http-ping
-k, --insecure allow insecure server connections when using SSL
-i, --interval duration define the wait time between each request (default 1s)
Expand Down
6 changes: 5 additions & 1 deletion app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ type pair struct {
// Cookie is a data structure which represents the basic info about a cookie (Name and Value)
type Cookie pair

// Parameter is a data structure which represents the basic info about a cookie (Name and Value)
// Header is a data structure which represents the basic info about a HTTP header (Name and Value)
type Header pair

// Parameter is a data structure which represents a request parameter (Name and Value)
type Parameter pair

// Config defines the multiple parameters which can be sent to HTTPPing
Expand All @@ -29,6 +32,7 @@ type Config struct {
ConnTarget string
NoCheckCertificate bool
Cookies []Cookie
Headers []Header
Parameters []Parameter
IgnoreServerErrors bool
ExtraParam bool
Expand Down
12 changes: 10 additions & 2 deletions app/webclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http/cookiejar"
"net/http/httptrace"
"net/url"
"strings"
"time"
)

Expand Down Expand Up @@ -126,9 +127,16 @@ func (webClient *WebClient) DoMeasure() *Answer {
req.URL.RawQuery = q.Encode()
}

start := time.Now()
// Host is considered as a special header in net/http, for simplicity we use here a common way to handle both
for _, header := range webClient.config.Headers {
if strings.ToLower(header.Name) != "host" {
req.Header.Set(header.Name, header.Value)
} else {
req.Host = header.Value
}
}

req.Header.Set("User-Agent", webClient.config.UserAgent)
start := time.Now()

res, err := webClient.httpClient.Do(req)

Expand Down
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func prepareRootCmd() *cobra.Command {

var cookies []string

var headers []string

var parameters []string

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -95,6 +97,13 @@ func prepareRootCmd() *cobra.Command {
}
}

for _, header := range headers {
n, v := splitPair(header)
if n != "" {
config.Headers = append(config.Headers, app.Header{Name: n, Value: v})
}
}

for _, parameter := range parameters {
n, v := splitPair(parameter)
if n != "" {
Expand Down Expand Up @@ -137,6 +146,8 @@ func prepareRootCmd() *cobra.Command {

rootCmd.Flags().StringArrayVarP(&cookies, "cookie", "", []string{}, "add one or more cookies, in the form name:value")

rootCmd.Flags().StringArrayVarP(&headers, "header", "", []string{}, "add one or more header, in the form name:value")

rootCmd.Flags().StringArrayVarP(&parameters, "parameter", "", []string{}, "add one or more parameters, in the form name:value")

rootCmd.Flags().BoolVarP(&config.IgnoreServerErrors, "no-server-error", "", false, "ignore server errors (5xx), do not handle them as \"lost pings\"")
Expand Down

0 comments on commit 4123cb4

Please sign in to comment.