forked from levigross/grequests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
317 lines (238 loc) · 7.96 KB
/
request.go
File metadata and controls
317 lines (238 loc) · 7.96 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package grequests
import (
"bytes"
"crypto/tls"
"encoding/json"
"encoding/xml"
"errors"
"io"
"mime"
"mime/multipart"
"net"
"net/http"
"net/url"
"strings"
"time"
)
// RequestOptions is the location that of where the data
type RequestOptions struct {
// Data is a map of key values that will eventually convert into the query string of a GET request or the
// body of a POST request.
Data map[string]string
// Params is a map of query strings that may be used within a GET request
Params map[string]string
// Files is where you can include files to upload. The use of this data structure is limited to POST requests
File *FileUpload
// JSON can be used when you wish to send JSON within the request body
JSON interface{}
// XML can be used if you wish to send XML within the request body
XML interface{}
// If you want to add custom HTTP headers to the request, this is your friend
Headers map[string]string
// InsecureSkipVerify is a flag that specifies if we should validate the server's TLS certificate. It should be noted that
// Go's TLS verify mechanism doesn't validate if a certificate has been revoked
InsecureSkipVerify bool
// DisableCompression will disable gzip compression on requests
DisableCompression bool
// UserAgent allows you to set an arbitrary custom user agent
UserAgent string
// Auth allows you to specify a user name and password that you wish to use when requesting
// the URL. It will use basic HTTP authentication formatting the username and password in base64
// the format is []string{username, password}
Auth []string
// IsAjax is a flag that can be set to make the request appear to be generated by browser Javascript
IsAjax bool
// Cookies is an array of `http.Cookie` that allows you to attach cookies to your request
Cookies []http.Cookie
}
func doRequest(requestVerb, url string, ro *RequestOptions) *Response {
return buildResponse(buildRequest(requestVerb, url, ro))
}
func doAsyncRequest(requestVerb, url string, ro *RequestOptions) chan *Response {
responseChan := make(chan *Response, 1)
go func() {
responseChan <- doRequest(requestVerb, url, ro)
}()
return responseChan
}
// buildRequest is where most of the magic happens for request processing
func buildRequest(httpMethod, url string, ro *RequestOptions) (*http.Response, error) {
if ro == nil {
ro = &RequestOptions{}
}
// Create our own HTTP client
httpClient := buildHTTPClient(ro)
// Build our URL
var err error
if ro.Params != nil {
url, err = buildURLParams(url, ro.Params)
if err != nil {
return nil, err
}
}
// Build the request
req, err := buildHTTPRequest(httpMethod, url, ro)
if err != nil {
return nil, err
}
// Do we need to add any HTTP headers or Basic Auth?
addHTTPHeaders(ro, req)
addCookies(ro, req)
return httpClient.Do(req)
}
func buildHTTPRequest(httpMethod, userURL string, ro *RequestOptions) (*http.Request, error) {
if ro.JSON != nil {
return createBasicJSONRequest(httpMethod, userURL, ro)
}
if ro.XML != nil {
return createBasicXMLRequest(httpMethod, userURL, ro)
}
if ro.File != nil {
return createFileUploadRequest(httpMethod, userURL, ro)
}
if ro.Data != nil {
return createBasicRequest(httpMethod, userURL, ro)
}
return http.NewRequest(httpMethod, userURL, nil)
}
func createFileUploadRequest(httpMethod, userURL string, ro *RequestOptions) (*http.Request, error) {
if httpMethod == "POST" {
return createMultiPartPostRequest(httpMethod, userURL, ro)
}
// This may be a PUT or PATCH request so we will just put the raw io.ReadCloser in the request body
req, err := http.NewRequest(httpMethod, userURL, ro.File.FileContents)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", mime.TypeByExtension(ro.File.FileName))
return req, nil
}
func createBasicXMLRequest(httpMethod, userURL string, ro *RequestOptions) (*http.Request, error) {
tempBuffer := &bytes.Buffer{}
xmlEncoder := xml.NewEncoder(tempBuffer)
xmlEncoder.Encode(ro.XML)
req, err := http.NewRequest(httpMethod, userURL, tempBuffer)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/xml")
return req, nil
}
func createMultiPartPostRequest(httpMethod, userURL string, ro *RequestOptions) (*http.Request, error) {
requestBody := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(requestBody)
writer, err := multipartWriter.CreateFormFile("file", ro.File.FileName)
if err != nil {
return nil, err
}
if ro.File.FileContents == nil {
return nil, errors.New("pointer FileContents cannot be nil")
}
if _, err = io.Copy(writer, ro.File.FileContents); err != nil && err != io.EOF {
return nil, err
}
defer ro.File.FileContents.Close()
// Populate the other parts of the form (if there are any)
for key, value := range ro.Data {
multipartWriter.WriteField(key, value)
}
if err = multipartWriter.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest(httpMethod, userURL, requestBody)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", multipartWriter.FormDataContentType())
return req, err
}
func createBasicJSONRequest(httpMethod, userURL string, ro *RequestOptions) (*http.Request, error) {
tempBuffer := &bytes.Buffer{}
jsonEncoder := json.NewEncoder(tempBuffer)
jsonEncoder.Encode(ro.JSON)
req, err := http.NewRequest(httpMethod, userURL, tempBuffer)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
func createBasicRequest(httpMethod, userURL string, ro *RequestOptions) (*http.Request, error) {
req, err := http.NewRequest(httpMethod, userURL, strings.NewReader(encodePostValues(ro.Data)))
if err != nil {
return nil, err
}
// The content type must be set to a regular form
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}
func encodePostValues(postValues map[string]string) string {
urlValues := &url.Values{}
for key, value := range postValues {
urlValues.Set(key, value)
}
return urlValues.Encode() // This will sort all of the string values
}
func buildHTTPClient(ro *RequestOptions) *http.Client {
// Does the user want to change the defaults?
if ro.InsecureSkipVerify != true && ro.DisableCompression != true {
return http.DefaultClient
}
return &http.Client{
Transport: &http.Transport{
// These are borrowed from the default transporter
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
// He comes the user settings
TLSClientConfig: &tls.Config{InsecureSkipVerify: ro.InsecureSkipVerify == true},
DisableCompression: ro.DisableCompression,
},
}
}
// buildURLParams returns a URL with all of the params
// Note: This function will override current URL params if they contradict what is provided in the map
// That is what the "magic" is on the last line
func buildURLParams(userURL string, params map[string]string) (string, error) {
parsedURL, err := url.Parse(userURL)
if err != nil {
return "", err
}
parsedQuery, err := url.ParseQuery(parsedURL.RawQuery)
for key, value := range params {
parsedQuery.Set(key, value)
}
return strings.Join(
[]string{strings.Replace(parsedURL.String(),
"?"+parsedURL.RawQuery, "", -1),
parsedQuery.Encode()},
"?"), nil
}
// addHTTPHeaders adds any additional HTTP headers that need to be added are added here including:
// 1. Custom User agent
// 2. Authorization Headers
// 3. Any other header requested
func addHTTPHeaders(ro *RequestOptions, req *http.Request) {
for key, value := range ro.Headers {
req.Header.Set(key, value)
}
if ro.UserAgent != "" {
req.Header.Set("User-Agent", ro.UserAgent)
} else {
req.Header.Set("User-Agent", localUserAgent)
}
if ro.Auth != nil {
req.SetBasicAuth(ro.Auth[0], ro.Auth[1])
}
if ro.IsAjax == true {
req.Header.Set("X-Requested-With", "XMLHttpRequest")
}
}
func addCookies(ro *RequestOptions, req *http.Request) {
for _, c := range ro.Cookies {
req.AddCookie(&c)
}
}