-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
143 lines (108 loc) · 3.39 KB
/
http.go
File metadata and controls
143 lines (108 loc) · 3.39 KB
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
package main
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
)
type HttpOptions struct {
Hostname string `long:"hostname" short:"H" description:"Web server to query"`
Uri string `long:"uri" short:"u" description:"URI to GET or POST" default:"/"`
Method string `long:"method" short:"j" description:"HTTP method (eg. HEAD, OPTIONS, TRACE, PUT, DELETE)" default:"GET"`
Post string `long:"post" short:"P" description:"Body of POST Request"`
Authorization func(string) `long:"authorization" short:"a" description:"Basic HTTP auth (username:password)"`
Ssl bool `long:"ssl" short:"S" description:"Enforce SSL" default:"false"`
Headers map[string]string `long:"header" short:"k" description:"Key,value pairs to add as headers in HTTP request (name:value format)"`
}
var httpOpts HttpOptions
func init() {
// If Authorization flag provided, add authentication headers
httpOpts.Authorization = func(str string) {
data := base64.StdEncoding.EncodeToString([]byte(str))
httpOpts.Headers["Authorization"] = "Basic " + data
}
parser.AddGroup("HTTP Options", "HTTP", &httpOpts)
}
func buildUrl(ssl bool, hostname string, uri string) (string, error) {
if hostname == "" {
return "", errors.New("Hostname is blank")
}
// Check the URI for template text, if so send it to the handler
if templateTest(uri) {
uri = templateHndlr(uri)
}
if uri == "" {
uri = "/"
}
// If SSL enforce, add https:// in front of the URL
var protocol string
if ssl {
protocol = "https://"
} else {
protocol = "http://"
}
return fmt.Sprintf("%s%s%s", protocol, hostname, uri), nil
}
func setReqHeaders(req *http.Request, hdrs map[string]string) {
for k, v := range hdrs { // Get all, cept last values
req.Header[k] = []string{v}
}
}
func httpRequest(
method string,
urlStr string,
bodyFile string,
) (int, map[string][]string, []byte, int64) {
// Build the API Request
var req *http.Request
var err error
switch bodyFile {
case "": // Nil request body
req, err = http.NewRequest(method, urlStr, nil)
case "stdin": // Read request from standard in
bodyStm := readStdIn()
bodyStr := streamToString(bodyStm)
// Check the POST body for template text, if so send it to the handler
if templateTest(bodyStr) {
bodyStr = templateHndlr(bodyStr)
}
newBody := strings.NewReader(bodyStr)
req, err = http.NewRequest(method, urlStr, newBody)
default: // Load request body from file
bodyStm := readFile(bodyFile)
bodyStr := streamToString(bodyStm)
// Check the POST body for template text, if so send it to the handler
if templateTest(bodyStr) {
bodyStr = templateHndlr(bodyStr)
}
newBody := strings.NewReader(bodyStr)
req, err = http.NewRequest(method, urlStr, newBody)
}
check(err)
// Add the appropriate headers to the request
setReqHeaders(req, httpOpts.Headers)
// Print the request body if verbose flag set.
if opts.Verbose {
dat, err := httputil.DumpRequest(req, true)
check(err)
fmt.Printf("%s\n", dat)
}
// Make the HTTP Request
client := http.DefaultClient
resp, err := client.Do(req)
check(err)
defer resp.Body.Close()
// Print the response body if verbose flag set.
if opts.Verbose {
dat, err := httputil.DumpResponse(resp, true)
check(err)
fmt.Printf("%s\n", dat)
}
// Read the API request response
body, err := ioutil.ReadAll(resp.Body)
check(err)
return resp.StatusCode, resp.Header, body, resp.ContentLength
}