From 895f7e1decf0b5bdba18f792484f88d218bce11d Mon Sep 17 00:00:00 2001 From: Breno Loyola Date: Thu, 1 Oct 2020 17:23:06 -0300 Subject: [PATCH] support multiple headers --- README.md | 2 +- main.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a444663..b85599c 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ ali -h | `--duration` | The amount of time to issue requests to the targets. Give `0s` for an infinite attack. Press `Ctrl-C` to stop. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". | 10s | | `--timeout` | The timeout for each request. `0s` means to disable timeouts. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". | 30s | | `--method` | An HTTP request method for each request. | GET | -| `--header` | A request header to be sent. | | +| `--header` | A request header to be sent. Can be used multiple times to send multiple headers | | | `--body` | A request body to be sent. | | | `--body-file` | The path to file whose content will be set as the http request body. | | diff --git a/main.go b/main.go index 56a14e1..989f480 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ type cli struct { duration time.Duration timeout time.Duration method string - header string + headers []string body string bodyFile string @@ -68,7 +68,7 @@ func main() { flagSet.DurationVarP(&c.duration, "duration", "d", time.Second*10, "the amount of time to issue requests to the targets") flagSet.DurationVarP(&c.timeout, "timeout", "t", time.Second*30, "the timeout for each request") flagSet.StringVarP(&c.method, "method", "m", "GET", "an HTTP request method for each request") - flagSet.StringVarP(&c.header, "header", "H", "", "a request header to be sent") + flagSet.StringSliceVarP(&c.headers, "header", "H", []string{}, "a request header to be sent. can be used multiple times to send multiple headers") flagSet.StringVarP(&c.body, "body", "b", "", "a request body to be sent") flagSet.StringVarP(&c.bodyFile, "body-file", "B", "", "the file whose content will be set as the http request body") flagSet.BoolVarP(&c.version, "version", "v", false, "print the current version") @@ -123,14 +123,14 @@ func (c *cli) makeOptions() (*attacker.Options, error) { } header := make(http.Header) - if c.header != "" { - parts := strings.SplitN(c.header, ":", 2) + for _, hdr := range c.headers { + parts := strings.SplitN(hdr, ":", 2) if len(parts) != 2 { - return nil, fmt.Errorf("given header %q has a wrong format", c.header) + return nil, fmt.Errorf("given header %q has a wrong format", hdr) } key, val := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]) if key == "" || val == "" { - return nil, fmt.Errorf("given header %q has a wrong format", c.header) + return nil, fmt.Errorf("given header %q has a wrong format", hdr) } // NOTE: Add key/value directly to the http.Header (map[string][]string). // http.Header.Add() canonicalizes keys but the vegeta API is used to test systems that require case-sensitive headers.