Skip to content

Commit

Permalink
Add support for request body
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-dds authored Aug 4, 2019
1 parent 2468f11 commit 65c0768
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (s saveStatusArgs) Includes(search int) bool {
}

type config struct {
body string
concurrency int
delay int
headers headerArgs
Expand All @@ -60,6 +61,11 @@ type config struct {

func processArgs() config {

// body param
body := ""
flag.StringVar(&body, "body", "", "")
flag.StringVar(&body, "b", "", "")

// concurrency param
concurrency := 20
flag.IntVar(&concurrency, "concurrency", 20, "")
Expand Down Expand Up @@ -136,6 +142,7 @@ func processArgs() config {
}

return config{
body: body,
concurrency: concurrency,
delay: delay,
headers: headers,
Expand All @@ -160,6 +167,7 @@ func init() {
h += " meg [path|pathsFile] [hostsFile] [outputDir]\n\n"

h += "Options:\n"
h += " -b, --body <val> Set the request body\n"
h += " -c, --concurrency <val> Set the concurrency level (default: 20)\n"
h += " -d, --delay <millis> Milliseconds between requests to the same host (default: 5000)\n"
h += " -H, --header <header> Send a custom HTTP header\n"
Expand Down
10 changes: 9 additions & 1 deletion gohttp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -33,7 +34,14 @@ func goRequest(r request) response {
}
}

req, err := http.NewRequest(r.method, r.URL(), nil)
var req *http.Request
var err error
if r.body != "" {
req, err = http.NewRequest(r.method, r.URL(), bytes.NewBuffer([]byte(r.body)))
} else {
req, err = http.NewRequest(r.method, r.URL(), nil)
}

if err != nil {
return response{request: r, err: err}
}
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,
body: c.body,
timeout: time.Duration(c.timeout * 1000000),
}
}
Expand Down
8 changes: 8 additions & 0 deletions rawhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func rawRequest(r request) response {
req.AddHeader(h)
}

if r.body != "" {
req.Body = r.body
}

if !r.HasHeader("Content-Length") {
req.AutoSetContentLength()
}

resp, err := rawhttp.Do(req)
if err != nil {
return response{request: r, err: err}
Expand Down
1 change: 1 addition & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type request struct {
path string
host string
headers []string
body string

followLocation bool
timeout time.Duration
Expand Down

0 comments on commit 65c0768

Please sign in to comment.