forked from franela/goreq
-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
197 lines (165 loc) · 4.51 KB
/
client.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
package goreq
import (
"crypto/tls"
"errors"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/imdario/mergo"
)
const (
defaultDialTimeout = 1 * time.Second
defaultDialKeepAlive = 30 * time.Second
defaultClientTimeout = 5 * time.Second
defaultClientMaxRedirect = 0
defaultClientTLS = false
)
//Options for create a client.
type Options struct {
Timeout time.Duration
Insecure bool
MaxRedirects int
CookieJar http.CookieJar
Proxy string
ProxyConnectHeaders http.Header
MaxIdleConnsPerHost int
}
//AddProxyConnectHeader add an Proxy connect header.
func (options Options) AddProxyConnectHeader(name string, value string) {
if options.ProxyConnectHeaders == nil {
options.ProxyConnectHeaders = make(http.Header)
}
options.ProxyConnectHeaders.Add(name, value)
}
//Client for do request in http.
type Client struct {
*http.Client
}
var (
defaultClientOptions = Options{
Timeout: defaultClientTimeout,
Insecure: defaultClientTLS,
MaxRedirects: defaultClientMaxRedirect,
}
)
// NewClient create a new client HTTP.
func NewClient(options Options) (client Client) {
mergo.Merge(&options, defaultClientOptions)
client = Client{newDefaultClient(options)}
if options.Proxy != "" {
client.setProxy(options.Proxy, options.ProxyConnectHeaders)
}
if options.MaxRedirects > 0 {
client.setLimitRedirect(options.MaxRedirects)
}
return client
}
func newDefaultClient(options Options) *http.Client {
dialer := &net.Dialer{
Timeout: defaultDialTimeout,
KeepAlive: defaultDialKeepAlive,
}
transport := &http.Transport{
DialContext: dialer.DialContext,
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: options.Insecure,
},
MaxIdleConnsPerHost: options.MaxIdleConnsPerHost,
}
return &http.Client{
Transport: transport,
Timeout: options.Timeout,
Jar: options.CookieJar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
func (client Client) setProxy(proxyURL string, proxyHeaders http.Header) *Error {
url, err := url.Parse(proxyURL)
if err != nil {
return &Error{Err: err}
}
proxy := http.ProxyURL(url)
if transport, ok := client.Transport.(*http.Transport); ok {
transport.Proxy = proxy
transport.ProxyConnectHeader = proxyHeaders
}
return nil
}
func (client Client) setLimitRedirect(maxRedirects int) {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) > maxRedirects {
return errors.New("GoReq: Error redirecting. MaxRedirects reached")
}
return nil
}
}
func (client Client) check() error {
if client.Timeout == time.Duration(0) {
return errors.New("GoReq: Client without timeout")
}
return nil
}
func (client Client) setConnectTimeout(timeout time.Duration) {
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: defaultDialKeepAlive,
}
if transport, ok := client.Transport.(*http.Transport); ok {
transport.DialContext = dialer.DialContext
}
}
//Do sends an HTTP request and returns an HTTP response,
//following policy (such as redirects, cookies, auth) as configured on the client.
func (client Client) Do(request Request) (*Response, error) {
if err := client.check(); err != nil {
return nil, err
}
req, err := request.NewRequest()
if err != nil {
return nil, &Error{Err: err}
}
if request.ShowDebug {
dump, err := httputil.DumpRequest(req, true)
if err != nil {
log.Println(err)
}
log.Println(string(dump))
}
if request.OnBeforeRequest != nil {
request.OnBeforeRequest(&request, req)
}
res, err := client.Client.Do(req)
if err != nil {
timeout := false
if t, ok := err.(itimeout); ok {
timeout = t.Timeout()
}
if ue, ok := err.(*url.Error); ok {
if t, ok := ue.Err.(itimeout); ok {
timeout = t.Timeout()
}
}
var body *Body
var URL string
if res != nil {
body = &Body{reader: res.Body}
URL = res.Request.URL.String()
}
return &Response{res, URL, body, req}, &Error{timeout: timeout, Err: err}
}
if request.Compression != nil && strings.Contains(res.Header.Get("Content-Encoding"), request.Compression.ContentEncoding) {
compressedReader, err := request.Compression.reader(res.Body)
if err != nil {
return nil, &Error{Err: err}
}
return &Response{res, res.Request.URL.String(), &Body{reader: res.Body, compressedReader: compressedReader}, req}, nil
}
return &Response{res, res.Request.URL.String(), &Body{reader: res.Body}, req}, nil
}