-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
373 lines (294 loc) · 7.53 KB
/
net.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package rest
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
)
var readVerbs = [3]string{http.MethodGet, http.MethodHead, http.MethodOptions}
var contentVerbs = [3]string{http.MethodPost, http.MethodPut, http.MethodPatch}
var defaultCheckRedirectFunc func(req *http.Request, via []*http.Request) error
var maxAge = regexp.MustCompile(`(?:max-age|s-maxage)=(\d+)`)
const httpDateFormat string = "Mon, 01 Jan 2006 15:04:05 GMT"
func (rb *RequestBuilder) doRequest(verb string, reqURL string, reqBody interface{}) (result *Response) {
var cacheURL string
var cacheResp *Response
result = new(Response)
reqURL = rb.BaseURL + reqURL
//If Cache enable && operation is read: Cache GET
if !rb.DisableCache && matchVerbs(verb, readVerbs) {
if cacheResp = resourceCache.get(reqURL); cacheResp != nil {
cacheResp.cacheHit.Store(true)
if !cacheResp.revalidate {
return cacheResp
}
}
}
func(verb string, reqURL string, reqBody interface{}) {
//Marshal request to JSON or XML
body, err := rb.marshalReqBody(reqBody)
if err != nil {
result.Err = err
return
}
// Change URL to point to Mockup server
reqURL, cacheURL, err = checkMockup(reqURL)
if err != nil {
result.Err = err
return
}
//Get Client (client + transport)
client := rb.getClient()
request, err := http.NewRequest(verb, reqURL, bytes.NewBuffer(body))
if err != nil {
result.Err = err
return
}
// Set extra parameters
rb.setParams(request, cacheResp, cacheURL)
// Make the request
httpResp, err := client.Do(request)
if err != nil {
result.Err = err
return
}
// Read response
defer httpResp.Body.Close()
respBody, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
result.Err = err
return
}
// If we get a 304, return response from cache
if httpResp.StatusCode == http.StatusNotModified {
result = cacheResp
return
}
result.Response = httpResp
result.byteBody = respBody
ttl := setTTL(result)
lastModified := setLastModified(result)
etag := setETag(result)
if !ttl && (lastModified || etag) {
result.revalidate = true
}
//If Cache enable: Cache SETNX
if !rb.DisableCache && matchVerbs(verb, readVerbs) && (ttl || lastModified || etag) {
resourceCache.setNX(cacheURL, result)
}
return
}(verb, reqURL, reqBody)
return
}
func checkMockup(reqURL string) (string, string, error) {
cacheURL := reqURL
if mockUpEnv {
rURL, err := url.Parse(reqURL)
if err != nil {
return reqURL, cacheURL, err
}
rURL.Scheme = mockServerURL.Scheme
rURL.Host = mockServerURL.Host
return rURL.String(), cacheURL, nil
}
return reqURL, cacheURL, nil
}
func (rb *RequestBuilder) marshalReqBody(body interface{}) (b []byte, err error) {
if body != nil {
switch rb.ContentType {
case JSON:
b, err = json.Marshal(body)
case XML:
b, err = xml.Marshal(body)
case BYTES:
var ok bool
b, ok = body.([]byte)
if !ok {
err = fmt.Errorf("bytes: body is %T(%v) not a byte slice", body, body)
}
}
}
return
}
func (rb *RequestBuilder) getClient() *http.Client {
// This will be executed only once
// per request builder
rb.clientMtxOnce.Do(func() {
dTransportMtxOnce.Do(func() {
if defaultTransport == nil {
defaultTransport = &http.Transport{
MaxIdleConnsPerHost: DefaultMaxIdleConnsPerHost,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{Timeout: rb.getConnectionTimeout()}).DialContext,
ResponseHeaderTimeout: rb.getRequestTimeout(),
}
}
defaultCheckRedirectFunc = http.Client{}.CheckRedirect
})
tr := defaultTransport
if cp := rb.CustomPool; cp != nil {
if cp.Transport == nil {
tr = &http.Transport{
MaxIdleConnsPerHost: rb.CustomPool.MaxIdleConnsPerHost,
DialContext: (&net.Dialer{Timeout: rb.getConnectionTimeout()}).DialContext,
ResponseHeaderTimeout: rb.getRequestTimeout(),
}
//Set Proxy
if cp.Proxy != "" {
if proxy, err := url.Parse(cp.Proxy); err == nil {
tr.(*http.Transport).Proxy = http.ProxyURL(proxy)
}
}
cp.Transport = tr
} else {
ctr, ok := cp.Transport.(*http.Transport)
if ok {
ctr.DialContext = (&net.Dialer{Timeout: rb.getConnectionTimeout()}).DialContext
ctr.ResponseHeaderTimeout = rb.getRequestTimeout()
tr = ctr
} else {
// If custom transport is not http.Transport, timeouts will not be overwritten.
tr = cp.Transport
}
}
}
rb.Client = &http.Client{Transport: tr}
})
if !rb.FollowRedirect {
rb.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return errors.New("Avoided redirect attempt")
}
} else {
rb.Client.CheckRedirect = defaultCheckRedirectFunc
}
return rb.Client
}
func (rb *RequestBuilder) getRequestTimeout() time.Duration {
switch {
case rb.DisableTimeout:
return 0
case rb.Timeout > 0:
return rb.Timeout
default:
return DefaultTimeout
}
}
func (rb *RequestBuilder) getConnectionTimeout() time.Duration {
switch {
case rb.DisableTimeout:
return 0
case rb.ConnectTimeout > 0:
return rb.ConnectTimeout
default:
return DefaultConnectTimeout
}
}
func (rb *RequestBuilder) setParams(req *http.Request, cacheResp *Response, cacheURL string) {
//Custom Headers
if rb.Headers != nil {
for key, values := range map[string][]string(rb.Headers) {
for _, value := range values {
req.Header.Add(key, value)
}
}
}
//Default headers
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Cache-Control", "no-cache")
//If mockup
if mockUpEnv {
req.Header.Set("X-Original-URL", cacheURL)
}
// Basic Auth
if rb.BasicAuth != nil {
req.SetBasicAuth(rb.BasicAuth.UserName, rb.BasicAuth.Password)
}
// User Agent
req.Header.Set("User-Agent", func() string {
if rb.UserAgent != "" {
return rb.UserAgent
}
return "github.com/go-loco/restful"
}())
//Encoding
var cType string
switch rb.ContentType {
case JSON:
cType = "json"
case XML:
cType = "xml"
}
if cType != "" {
req.Header.Set("Accept", "application/"+cType)
if matchVerbs(req.Method, contentVerbs) {
req.Header.Set("Content-Type", "application/"+cType)
}
}
if cacheResp != nil && cacheResp.revalidate {
switch {
case cacheResp.etag != "":
req.Header.Set("If-None-Match", cacheResp.etag)
case cacheResp.lastModified != nil:
req.Header.Set("If-Modified-Since", cacheResp.lastModified.Format(httpDateFormat))
}
}
}
func matchVerbs(s string, sarray [3]string) bool {
for i := 0; i < len(sarray); i++ {
if sarray[i] == s {
return true
}
}
return false
}
func setTTL(resp *Response) (set bool) {
now := time.Now()
//Cache-Control Header
cacheControl := maxAge.FindStringSubmatch(resp.Header.Get("Cache-Control"))
if len(cacheControl) > 1 {
ttl, err := strconv.Atoi(cacheControl[1])
if err != nil {
return
}
if ttl > 0 {
t := now.Add(time.Duration(ttl) * time.Second)
resp.ttl = &t
set = true
}
return
}
//Expires Header
//Date format from RFC-2616, Section 14.21
expires, err := time.Parse(httpDateFormat, resp.Header.Get("Expires"))
if err != nil {
return
}
if expires.Sub(now) > 0 {
resp.ttl = &expires
set = true
}
return
}
func setLastModified(resp *Response) bool {
lastModified, err := time.Parse(httpDateFormat, resp.Header.Get("Last-Modified"))
if err != nil {
return false
}
resp.lastModified = &lastModified
return true
}
func setETag(resp *Response) bool {
resp.etag = resp.Header.Get("ETag")
if resp.etag != "" {
return true
}
return false
}