-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.go
152 lines (130 loc) · 3.43 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"crypto/tls"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"slices"
"strconv"
"strings"
"github.com/armosec/utils-go/httputils"
)
type FlagParser struct {
fullURL url.URL
pathToBody string
headers string
method string
pathToOutput string
skipSSLVerify bool
}
func NewFlagParser() *FlagParser {
return &FlagParser{
fullURL: url.URL{},
}
}
func (f *FlagParser) parser() {
flag.StringVar(&f.method, "method", "", "http method (GET/POST/DELETE)")
flag.StringVar(&f.fullURL.Scheme, "scheme", "http", "request scheme")
flag.StringVar(&f.fullURL.Host, "host", "", "host")
flag.StringVar(&f.fullURL.Path, "path", "", "path")
flag.StringVar(&f.pathToBody, "path-body", "", "path to body")
flag.StringVar(&f.headers, "headers", "", "http headers")
flag.StringVar(&f.pathToOutput, "path-output", "", "path to output file")
flag.BoolVar(&f.skipSSLVerify, "skip-ssl-verify", false, "skip SSL verification")
flag.Parse()
// unquote headers
if unquote, err := strconv.Unquote(f.headers); err == nil {
f.headers = unquote
}
}
func (f *FlagParser) validate() error {
if f.fullURL.Host == "" {
return fmt.Errorf("missing host")
}
if f.method == "" {
return fmt.Errorf("missing method")
}
return nil
}
func loadHeaders(f *FlagParser) (map[string]string, error) {
headers := map[string]string{}
if f.headers != "" {
splitteedHeaders := strings.Split(f.headers, ";")
for i := range splitteedHeaders {
header := strings.Split(splitteedHeaders[i], ":")
if len(header) == 2 {
headers[header[0]] = strings.TrimLeft(header[1], " ")
}
}
return headers, nil
}
return headers, nil
}
func loadBody(f *FlagParser) ([]byte, error) {
if f.pathToBody != "" {
fmt.Printf("loading body from: %s\n", f.pathToBody)
return os.ReadFile(f.pathToBody)
}
return []byte{}, nil
}
func setHeaders(req *http.Request, headers map[string]string) {
if headers != nil && len(headers) > 0 {
for k, v := range headers {
req.Header.Set(k, v)
}
}
}
// Request run a http request
func Request(f *FlagParser) (string, error) {
var resp *http.Response
var err error
headers, e := loadHeaders(f)
if e != nil {
return "", e
}
body, e := loadBody(f)
if e != nil {
return "", e
}
method := strings.ToUpper(f.method)
if !slices.Contains([]string{http.MethodPost, http.MethodGet, http.MethodDelete}, method) {
return "", fmt.Errorf("method %s not supported", method)
}
fmt.Printf("method: %s, url: %s, headers: %v, body: %s\n", method, f.fullURL.String(), headers, body)
req, err := http.NewRequest(method, f.fullURL.String(), bytes.NewReader(body))
if err != nil {
return "", err
}
setHeaders(req, headers)
if f.skipSSLVerify {
fmt.Printf("skipping SSL verification\n")
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
resp, err = client.Do(req)
if err != nil {
return "", err
}
} else {
resp, err = http.DefaultClient.Do(req)
if err != nil {
return "", err
}
}
strResp, e := httputils.HttpRespToString(resp)
if e != nil {
return "", fmt.Errorf("failed to parse http response to string, reason: %s", e.Error())
}
if f.pathToOutput != "" {
if err := os.WriteFile(f.pathToOutput, []byte(strResp), 0644); err != nil {
return "", fmt.Errorf("error writing response to file: %s", err.Error())
}
fmt.Printf("response was written to file: %s\n", f.pathToOutput)
}
return strResp, nil
}