This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.go
152 lines (140 loc) · 3.87 KB
/
body.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 pica
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/jerloo/funny"
"github.com/pkg/errors"
)
func CreateHttpRequest(req *ApiRequest, runner *APIRunner) (httpReq *http.Request, err error) {
var bodyParams map[string]funny.Value
if req.Method != "GET" && req.Method != "DELETE" {
bodyParams = runner.vm.Lookup(strings.ToLower(req.Method)).(map[string]funny.Value)
}
headers := runner.vm.LookupDefault("headers", nil).(map[string]funny.Value)
contentType := "unknow content type"
if headers != nil {
contentType = headers["Content-Type"].(string)
} else {
contentType = req.Headers["Content-Type"][0]
}
switch contentType {
case "application/x-www-form-urlencoded":
fmt.Printf("create %s", "application/x-www-form-urlencoded")
httpReq, err = createFormUrlEncodedRequest(req, runner, bodyParams)
if err != nil {
return nil, err
}
break
case "multipart/form-data":
httpReq, err = createFormDataRequest(req, runner, bodyParams)
if err != nil {
return nil, err
}
break
case "application/json":
httpReq, err = createJsonRequest(req, runner, bodyParams)
if err != nil {
return nil, err
}
break
default:
if req.Method == "GET" || req.Method == "DELETE" {
targetUrl, err := getTargetURL(req, runner)
if err != nil {
return nil, err
}
httpReq, err = http.NewRequest(req.Method, targetUrl, nil)
if err != nil {
return nil, err
}
}
return nil, errors.New("unknow http method")
}
if headers != nil {
httpReq.Header = VmMap2HttpHeaders(headers)
} else {
httpReq.Header = req.Headers
}
return
}
func getValue(val funny.Value) string {
switch val := val.(type) {
case int:
return fmt.Sprint(val)
case string:
return val
default:
panic(fmt.Errorf("unsupport type [%s], only support [int][string]", funny.Typing(val)))
}
}
func getTargetURL(req *ApiRequest, runner *APIRunner) (string, error) {
baseURL := runner.vm.Lookup("baseUrl").(string)
targetUrl, query, err := CompileURL(baseURL+req.Url, runner.vm)
req.Query = query
return targetUrl, err
}
func createFormUrlEncodedRequest(req *ApiRequest, runner *APIRunner, bodyParams map[string]funny.Value) (*http.Request, error) {
v := url.Values{}
for key, val := range bodyParams {
v.Set(key, getValue(val))
}
u := ioutil.NopCloser(strings.NewReader(v.Encode()))
fmt.Printf("application/x-www-form-urlencoded %s\n", v.Encode())
targetUrl, err := getTargetURL(req, runner)
if err != nil {
return nil, err
}
return http.NewRequest(req.Method, targetUrl, u)
}
func createFormDataRequest(req *ApiRequest, runner *APIRunner, bodyParams map[string]funny.Value) (*http.Request, error) {
buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)
for key, val := range bodyParams {
v := getValue(val)
if strings.HasPrefix(v, "@") {
fullFileName := v[1:]
_, filename := filepath.Split(fullFileName)
formFile, err := writer.CreateFormFile(key, filename)
if err != nil {
return nil, errors.New("Create form file failed: %s\n")
}
srcFile, err := os.Open(fullFileName)
if err != nil {
return nil, errors.New("%Open source file failed: s\n")
}
defer srcFile.Close()
_, err = io.Copy(formFile, srcFile)
if err != nil {
return nil, errors.New("Write to form file falied: %s\n")
}
} else {
writer.WriteField(key, v)
}
}
writer.Close()
targetUrl, err := getTargetURL(req, runner)
if err != nil {
return nil, err
}
return http.NewRequest(req.Method, targetUrl, buf)
}
func createJsonRequest(req *ApiRequest, runner *APIRunner, bodyParams map[string]funny.Value) (*http.Request, error) {
targetUrl, err := getTargetURL(req, runner)
if err != nil {
return nil, err
}
jsonContent, err := json.Marshal(bodyParams)
if err != nil {
return nil, err
}
return http.NewRequest(req.Method, targetUrl, bytes.NewBuffer(jsonContent))
}