-
Notifications
You must be signed in to change notification settings - Fork 220
/
http.go
129 lines (122 loc) · 3.03 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/astaxie/bat/httplib"
)
var defaultSetting = httplib.BeegoHttpSettings{
ShowDebug: true,
UserAgent: "bat/" + version,
ConnectTimeout: 60 * time.Second,
ReadWriteTimeout: 60 * time.Second,
Gzip: true,
DumpBody: true,
}
func getHTTP(method string, url string, args []string) (r *httplib.BeegoHttpRequest) {
r = httplib.NewBeegoRequest(url, method)
r.Setting(defaultSetting)
r.Header("Accept-Encoding", "gzip, deflate")
if *isjson {
r.Header("Accept", "application/json")
} else if form || method == "GET" {
r.Header("Accept", "*/*")
} else {
r.Header("Accept", "application/json")
}
for i := range args {
// Headers
strs := strings.Split(args[i], ":")
if len(strs) >= 2 {
if strs[0] == "Host" {
r.SetHost(strings.Join(strs[1:], ":"))
}
r.Header(strs[0], strings.Join(strs[1:], ":"))
continue
}
// files
strs = strings.SplitN(args[i], "@", 2)
if !*isjson && len(strs) == 2 {
if !form {
log.Fatal("file upload only support in forms style: -f=true")
}
r.PostFile(strs[0], strs[1])
continue
}
// Json raws
strs = strings.SplitN(args[i], ":=", 2)
if len(strs) == 2 {
if strings.HasPrefix(strs[1], "@") {
f, err := os.Open(strings.TrimLeft(strs[1], "@"))
if err != nil {
log.Fatal("Read File", strings.TrimLeft(strs[1], "@"), err)
}
content, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal("ReadAll from File", strings.TrimLeft(strs[1], "@"), err)
}
var j interface{}
err = json.Unmarshal(content, &j)
if err != nil {
log.Fatal("Read from File", strings.TrimLeft(strs[1], "@"), "Unmarshal", err)
}
jsonmap[strs[0]] = j
continue
}
jsonmap[strs[0]] = toRealType(strs[1])
continue
}
// Params
strs = strings.SplitN(args[i], "=", 2)
if len(strs) == 2 {
if strings.HasPrefix(strs[1], "@") {
f, err := os.Open(strings.TrimLeft(strs[1], "@"))
if err != nil {
log.Fatal("Read File", strings.TrimLeft(strs[1], "@"), err)
}
content, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal("ReadAll from File", strings.TrimLeft(strs[1], "@"), err)
}
strs[1] = string(content)
}
if form || method == "GET" {
r.Param(strs[0], strs[1])
} else {
jsonmap[strs[0]] = strs[1]
}
continue
}
}
if !form && len(jsonmap) > 0 {
r.JsonBody(jsonmap)
}
return
}
func formatResponseBody(res *http.Response, httpreq *httplib.BeegoHttpRequest, pretty bool) string {
body, err := httpreq.Bytes()
if err != nil {
log.Fatalln("can't get the url", err)
}
fmt.Println("")
match, err := regexp.MatchString(contentJsonRegex, res.Header.Get("Content-Type"))
if err != nil {
log.Fatalln("failed to compile regex", err)
}
if pretty && match {
var output bytes.Buffer
err := json.Indent(&output, body, "", " ")
if err != nil {
log.Fatal("Response Json Indent: ", err)
}
return output.String()
}
return string(body)
}