-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (160 loc) · 4.21 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
)
const (
// escape = "\x1b"
nl = "\n"
prefixIn = ""
prefixOut = ""
protocol = "\x1b[91m%s %s %s\x1b[0m\n"
header = "%s\x1b[90m%s:\x1b[0m \x1b[94m%s\x1b[0m\n"
summary = "\x1b[90m%s:\x1b[0m \x1b[94m%s\x1b[0m\n"
)
func main() {
params := getParams(getBuildVersion(), getBuildTimestamp(), getCompiledBy())
client := http.Client{}
remote, err := url.Parse(params.optURI)
if err != nil {
log.Fatal("Unable to parse", params.optURI)
}
if remote.Scheme == "" {
remote.Scheme = "http"
remote, _ = url.Parse(remote.String()) // reparse to populate remote.Host
}
data := url.Values{}
parseMultiData(params.optData, data)
switch {
case params.optPostForm:
params.optFormURLEncode = true
fallthrough
case params.optPost:
params.optHTTPAction = http.MethodPost
}
var body bytes.Buffer // io.ReadWriter
switch {
case params.optQueryString:
remote.RawQuery = data.Encode() // force a query string with -q
case params.optReadStdin:
if params.optReadStdin {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
body.Write(scanner.Bytes())
}
}
case len(params.optBinData) > 0:
body.WriteString(params.optBinData)
case body.Len() == 0:
fallthrough
default:
body.WriteString(data.Encode()) // send the query data as the body
}
req, err := http.NewRequest(params.optHTTPAction, remote.String(), &body)
if err != nil {
log.Fatal(err)
}
// sugar for basic auth
if len(params.optBasic) > 0 {
if strings.Contains(params.optBasic, ":") {
params.optBasic = base64.StdEncoding.EncodeToString([]byte(params.optBasic))
}
req.Header.Set("Authorization", "Basic "+params.optBasic)
}
// sugar for token auth
if len(params.optToken) > 0 {
req.Header.Set("Authorization", "Token "+params.optToken)
}
// sugar for bearer auth
if len(params.optBearer) > 0 {
req.Header.Set("Authorization", "Bearer "+params.optBearer)
}
// sugar for Content-Type
if len(params.optType) > 0 {
req.Header.Set("Content-Type", params.optType)
}
req.Header.Set("User-Agent", "hurl/"+buildVersion)
req.Header.Set("Accept", "*/*")
// req.Header.Set("cache-control", "no-cache")
req.Header.Set("Host", remote.Host)
if body.Len() > 0 {
req.Header.Set("Content-Length", strconv.Itoa(body.Len()))
}
if params.optFormURLEncode {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
}
parseMultiData(params.optHeaders, req.Header)
stderr.Printf(protocol, req.Method, req.URL.RequestURI(), req.Proto)
printHeaders(req.Header)
stderr.Print(nl)
stderr.Printf("%v%s", &body, nl)
stderr.Print(nl)
start := time.Now()
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
stderr.Printf(protocol, resp.Proto, resp.Status, "")
printHeaders(resp.Header)
stderr.Print(nl)
local := os.Stdout
if params.optOutFile {
// log.Fatal(resp.Header.Get("Content-Disposition"))
fname := path.Base(remote.Path)
local, err = os.OpenFile(fname, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
}
n, err := io.Copy(local, resp.Body)
if err != nil {
log.Fatal(err)
}
stderr.Println(nl)
if params.optSummary {
// stderr.Println("\x1b[91m//------------------------------------------------------------------------//\x1b[0m")
stderr.Println("\x1b[91m---------------\x1b[0m")
stderr.Printf(summary, "Content-Length", strconv.FormatInt(resp.ContentLength, 10))
stderr.Printf(summary, "Bytes Received", strconv.FormatInt(n, 10))
stderr.Printf(summary, "Request Duration", time.Since(start).String())
}
}
type adder interface {
Add(string, string)
}
func parseMultiData(src multiParams, dest adder) {
for name, slice := range src {
for _, v := range slice {
dest.Add(name, v)
}
}
}
func printHeaders(headers map[string][]string) {
for k, v := range headers {
for _, v := range v {
stderr.Printf(header, prefixOut, k, v)
}
}
}
func (d multiParams) String() string {
return fmt.Sprintf("%d", len(d))
}
func (d multiParams) Set(value string) error {
v := strings.SplitN(value, "=", 2)
if len(v) == 2 {
d[v[0]] = append(d[v[0]], v[1])
}
return nil
}