@@ -58,6 +58,9 @@ type RequestOptions struct {
58
58
59
59
// Cookies is an array of `http.Cookie` that allows you to attach cookies to your request
60
60
Cookies []http.Cookie
61
+
62
+ // Proxies is a map in the following format *protocol* => proxy address e.g http => http://127.0.0.1:8080
63
+ Proxies map [string ]* url.URL
61
64
}
62
65
63
66
func doRequest (requestVerb , url string , ro * RequestOptions ) (* Response , error ) {
@@ -133,9 +136,13 @@ func createFileUploadRequest(httpMethod, userURL string, ro *RequestOptions) (*h
133
136
return createMultiPartPostRequest (httpMethod , userURL , ro )
134
137
}
135
138
136
- // This may be a PUT or PATCH request so we will just put the raw io.ReadCloser in the request body
139
+ // This may be a PUT or PATCH request so we will just put the raw
140
+ // io.ReadCloser in the request body
137
141
// and guess the MIME type from the file name
138
142
143
+ // At the moment, we will only support 1 file upload as a time
144
+ // when uploading using PUT or PATCH
145
+
139
146
req , err := http .NewRequest (httpMethod , userURL , ro .File .FileContents )
140
147
141
148
if err != nil {
@@ -246,16 +253,46 @@ func encodePostValues(postValues map[string]string) string {
246
253
return urlValues .Encode () // This will sort all of the string values
247
254
}
248
255
256
+ // proxySettings will default to the default proxy settings if none are provided
257
+ // if settings are provided – they will override the environment variables
258
+ func (ro RequestOptions ) proxySettings (req * http.Request ) (* url.URL , error ) {
259
+ // No proxies – lets use the default
260
+ if len (ro .Proxies ) == 0 {
261
+ return http .ProxyFromEnvironment (req )
262
+ }
263
+
264
+ // There was a proxy specified – do we support the protocol?
265
+ if _ , ok := ro .Proxies [req .URL .Scheme ]; ok {
266
+ return ro .Proxies [req .URL .Scheme ], nil
267
+ }
268
+
269
+ // Proxies were specified but not for any protocol that we use
270
+ return http .ProxyFromEnvironment (req )
271
+
272
+ }
273
+
274
+ // useDefaultClient will tell the "client creator" if a custom client is needed
275
+ // it checks the following items (and will create a custom client of these are)
276
+ // true
277
+ // 1. Do we want to accept invalid SSL certificates?
278
+ // 2. Do we want to disable compression?
279
+ // 3. Do we want a custom proxy?
280
+ func (ro RequestOptions ) dontUseDefaultClient () bool {
281
+ return ro .InsecureSkipVerify == true ||
282
+ ro .DisableCompression == true ||
283
+ len (ro .Proxies ) != 0
284
+ }
285
+
249
286
func buildHTTPClient (ro * RequestOptions ) * http.Client {
250
287
// Does the user want to change the defaults?
251
- if ro . InsecureSkipVerify != true && ro .DisableCompression != true {
288
+ if ! ro .dontUseDefaultClient () {
252
289
return http .DefaultClient
253
290
}
254
291
255
292
return & http.Client {
256
293
Transport : & http.Transport {
257
294
// These are borrowed from the default transporter
258
- Proxy : http . ProxyFromEnvironment ,
295
+ Proxy : ro . proxySettings ,
259
296
Dial : (& net.Dialer {
260
297
Timeout : 30 * time .Second ,
261
298
KeepAlive : 30 * time .Second ,
0 commit comments