From 65c0768c2d3efe5e90354eb868f6da4c7719518c Mon Sep 17 00:00:00 2001 From: jack-dds <40569500+jack-dds@users.noreply.github.com> Date: Sun, 4 Aug 2019 14:41:18 -0700 Subject: [PATCH] Add support for request body --- args.go | 8 ++++++++ gohttp.go | 10 +++++++++- main.go | 1 + rawhttp.go | 8 ++++++++ request.go | 1 + 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/args.go b/args.go index db261bf..90cca52 100644 --- a/args.go +++ b/args.go @@ -41,6 +41,7 @@ func (s saveStatusArgs) Includes(search int) bool { } type config struct { + body string concurrency int delay int headers headerArgs @@ -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, "") @@ -136,6 +142,7 @@ func processArgs() config { } return config{ + body: body, concurrency: concurrency, delay: delay, headers: headers, @@ -160,6 +167,7 @@ func init() { h += " meg [path|pathsFile] [hostsFile] [outputDir]\n\n" h += "Options:\n" + h += " -b, --body Set the request body\n" h += " -c, --concurrency Set the concurrency level (default: 20)\n" h += " -d, --delay Milliseconds between requests to the same host (default: 5000)\n" h += " -H, --header
Send a custom HTTP header\n" diff --git a/gohttp.go b/gohttp.go index 5123a60..9bb13ce 100644 --- a/gohttp.go +++ b/gohttp.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "crypto/tls" "fmt" "io/ioutil" @@ -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} } diff --git a/main.go b/main.go index a26a93d..f2a73a2 100644 --- a/main.go +++ b/main.go @@ -132,6 +132,7 @@ func main() { path: prefixedPath, headers: c.headers, followLocation: c.followLocation, + body: c.body, timeout: time.Duration(c.timeout * 1000000), } } diff --git a/rawhttp.go b/rawhttp.go index 94ef29a..4d6563f 100644 --- a/rawhttp.go +++ b/rawhttp.go @@ -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} diff --git a/request.go b/request.go index ba24083..1c33af8 100644 --- a/request.go +++ b/request.go @@ -12,6 +12,7 @@ type request struct { path string host string headers []string + body string followLocation bool timeout time.Duration