-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
129 lines (103 loc) · 3.34 KB
/
utils.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 req
import (
"errors"
"io"
"net/http"
"runtime"
"time"
)
const (
localUserAgent = "req/0.10"
// Default value for net.Dialer Timeout
dialTimeout = 30 * time.Second
// Default value for net.Dialer KeepAlive
dialKeepAlive = 30 * time.Second
// Default value for http.Transport TLSHandshakeTimeout
tlsHandshakeTimeout = 10 * time.Second
// Default value for Request Timeout
requestTimeout = 90 * time.Second
)
const (
// MethodGet HTTP method
MethodGet = "GET"
// MethodPost HTTP method
MethodPost = "POST"
// MethodPut HTTP method
MethodPut = "PUT"
// MethodDelete HTTP method
MethodDelete = "DELETE"
// MethodPatch HTTP method
MethodPatch = "PATCH"
// MethodHead HTTP method
MethodHead = "HEAD"
// MethodOptions HTTP method
MethodOptions = "OPTIONS"
)
var (
// ErrRedirectLimitExceeded is the error returned when the request responded
// with too many redirects
ErrRedirectLimitExceeded = errors.New("grequests: Request exceeded redirect count")
// RedirectLimit is a tunable variable that specifies how many times we can
// redirect in response to a redirect. This is the global variable, if you
// wish to set this on a request by request basis, set it within the
// `RqOptions` structure
RedirectLimit = 30
// SensitiveHTTPHeaders is a map of sensitive HTTP headers that a user
// doesn't want passed on a redirect. This is the global variable, if you
// wish to set this on a request by request basis, set it within the
// `RqOptions` structure
SensitiveHTTPHeaders = map[string]struct{}{
"Www-Authenticate": {},
"Authorization": {},
"Proxy-Authorization": {},
}
)
// XMLCharDecoder is a helper type that takes a stream of bytes (not encoded in
// UTF-8) and returns a reader that encodes the bytes into UTF-8. This is done
// because Go's XML library only supports XML encoded in UTF-8
type XMLCharDecoder func(charset string, input io.Reader) (io.Reader, error)
func addRedirectFunctionality(client *http.Client, ro *RqOptions) {
if client.CheckRedirect != nil {
return
}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if ro.RedirectLimit < 0 {
return http.ErrUseLastResponse
}
if ro.RedirectLimit == 0 {
ro.RedirectLimit = RedirectLimit
}
if len(via) >= ro.RedirectLimit {
return ErrRedirectLimitExceeded
}
if ro.SensitiveHTTPHeaders == nil {
ro.SensitiveHTTPHeaders = SensitiveHTTPHeaders
}
for k, vv := range via[0].Header {
// Is this a sensitive header?
if _, found := ro.SensitiveHTTPHeaders[k]; found {
continue
}
for _, v := range vv {
req.Header.Add(k, v)
}
}
return nil
}
}
// EnsureTransporterFinalized will ensure that when the HTTP client is GCed
// the runtime will close the idle connections (so that they won't leak)
// this function was adopted from Hashicorp's go-cleanhttp package
func EnsureTransporterFinalized(httpTransport *http.Transport) {
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
(*transportInt).CloseIdleConnections()
})
}
// EnsureResponseFinalized will ensure that when the Response is GCed
// the request body is closed so we aren't leaking fds
// func EnsureResponseFinalized(httpResp *Response) {
// runtime.SetFinalizer(&httpResp, func(httpResponseInt **Response) {
// (*httpResponseInt).RawResponse.Body.Close()
// })
// }
// This will come back in 1.0