forked from astaxie/bat
/
http.go
206 lines (180 loc) · 5.1 KB
/
http.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
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/bingoohuang/gg/pkg/iox"
"github.com/bingoohuang/gg/pkg/man"
"github.com/bingoohuang/gg/pkg/osx"
"github.com/bingoohuang/gg/pkg/v"
"github.com/bingoohuang/jj"
)
var defaultSetting = Settings{
DumpRequest: true,
UserAgent: "curl/" + v.AppVersion,
ConnectTimeout: 60 * time.Second,
DumpBody: true,
}
var keyReg = regexp.MustCompile(`^([\w_.\-]*)(==|:=|=|:|@)(.*)`)
func getHTTP(method, url string, args []string, timeout time.Duration) (r *Request) {
if confirmNum > 0 {
timeout = 0
}
r = NewRequest(url, method)
r.DisableKeepAlives = disableKeepAlive
r.Setting = defaultSetting
r.Setting.ConnectTimeout = timeout
r.DryRequest = strings.HasPrefix(url, DryRequestURL)
r.Timeout = timeout
r.Header("Accept-Encoding", "gzip, deflate")
if form || method == "GET" {
r.Header("Accept", "*/*")
} else {
r.Header("Accept", "application/json")
}
r.Header("Gurl-Date", time.Now().UTC().Format(http.TimeFormat))
// https://httpie.io/docs#request-items
// Item Type Description
// HTTP Headers Name:Value Arbitrary HTTP header, e.g. X-API-Token:123
// URL parameters name==value Appends the given name/value pair as a querystring parameter to the URL. The == separator is used.
// Data Fields field=value, field=@file.txt Request data fields to be serialized as a JSON object (default), to be form-encoded (with --form, -f), or to be serialized as multipart/form-data (with --multipart)
// Raw JSON field:=json Useful when sending JSON and one or more fields need to be a Boolean, Number, nested Object, or an Array, e.g., meals:='["ham","spam"]' or pies:=[1,2,3] (note the quotes)
// File upload fields field@/dir/file, field@file;type=mime Only available with --form, -f and --multipart. For example screenshot@~/Pictures/img.png, or 'cv@cv.txt;type=text/markdown'. With --form, the presence of a file field results in a --multipart request
for i := range args {
arg := args[i]
subs := keyReg.FindStringSubmatch(arg)
if len(subs) == 0 {
continue
}
k, op, val := subs[1], subs[2], subs[3]
if k == "" && op != "@" {
log.Fatalf("Unsupported argument %s", arg)
}
switch op {
case ":=": // Json raws
if dat, fn, err := readFile(val); err != nil {
log.Fatal("Read File", fn, err)
} else if fn != "" {
var j interface{}
if err := json.Unmarshal(dat, &j); err != nil {
log.Fatal("Read from File", fn, "Unmarshal", err)
}
jsonmap[k] = j
} else {
jsonmap[k] = json.RawMessage(dat)
}
case "==": // Queries
r.Query(k, tryReadFile(val))
case "=": // Params
if val = tryReadFile(val); form || method == "GET" {
r.Param(k, val) // As Query parameter,
} else {
jsonmap[k] = val // body will be eval later
}
case ":": // Headers
if k == "Host" {
r.SetHost(val)
} else {
if strings.EqualFold(k, "Accept") && strings.EqualFold(val, "JSON") {
r.Header("Accept", "application/json")
} else {
r.Header(k, val)
}
}
case "@": // files
if k != "" {
r.PostFile(k, val)
} else {
r.Body(arg)
}
}
}
if !form && len(jsonmap) > 0 {
r.Body(jsonmap)
}
return
}
func tryReadFile(s string) string {
dat, _, err := readFile(s)
if err != nil {
log.Fatal("Read File", s, err)
}
return string(dat)
}
func readFile(s string) (data []byte, fn string, e error) {
if !strings.HasPrefix(s, "@") {
return []byte(s), "", nil
}
filename := s[1:]
if _, err := os.Stat(filename); os.IsNotExist(err) {
eval, err := Eval(s)
if err != nil {
return []byte(s), "", nil
}
return []byte(eval), "", nil
}
f, err := os.Open(filename)
if err != nil {
return nil, filename, err
}
defer iox.Close(f)
content, err := io.ReadAll(f)
if err != nil {
return nil, filename, err
}
return content, filename, nil
}
const (
MaxPayloadSize = "MAX_PAYLOAD_SIZE"
DefaultMaxPayloadSize = 1024 * 4
)
func formatResponseBody(r *Request, pretty, ugly, freeInnerJSON, influxDB bool) string {
dat, err := r.Bytes()
if err != nil {
log.Fatalln("can't get the url", err)
}
if influxTablePrint(ugly, influxDB, dat) {
return ""
}
if saveTempFile(dat, MaxPayloadSize, ugly) {
return ""
}
return formatBytes(dat, pretty, ugly, freeInnerJSON)
}
func saveTempFile(dat []byte, envName string, ugly bool) bool {
if ugly {
return false
}
if m := osx.EnvSize(envName, DefaultMaxPayloadSize); m > 0 && len(dat) > m {
if t := iox.WriteTempFile(iox.WithTempContent(dat)); t.Err == nil {
log.Printf("body is too large, %d / %s > %d / %s (set $%s), write to file: %s",
len(dat), man.Bytes(uint64(len(dat))), m, man.Bytes(uint64(m)), envName, t.Name)
return true
}
}
return false
}
func formatBytes(body []byte, pretty, ugly, freeInnerJSON bool) string {
body = bytes.TrimSpace(body)
isJSON := jj.ParseBytes(body).IsJSON()
if isJSON {
if freeInnerJSON {
body = jj.FreeInnerJSON(body)
}
if ugly {
body = jj.Ugly(body)
} else if pretty {
body = jj.Pretty(body, jj.DefaultOptions)
}
}
if hasStdoutDevice {
return ColorfulResponse(string(body), isJSON)
}
return string(body)
}