forked from gogf/gf
/
client.go
287 lines (260 loc) · 7.6 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
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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package client
import (
"context"
"crypto/rand"
"crypto/tls"
"fmt"
"github.com/gogf/gf"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/text/gstr"
"golang.org/x/net/proxy"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/gogf/gf/text/gregex"
)
// Client is the HTTP client for HTTP request management.
type Client struct {
http.Client // Underlying HTTP Client.
ctx context.Context // Context for each request.
dump bool // Mark this request will be dumped.
parent *Client // Parent http client, this is used for chaining operations.
header map[string]string // Custom header map.
cookies map[string]string // Custom cookie map.
prefix string // Prefix for request.
authUser string // HTTP basic authentication: user.
authPass string // HTTP basic authentication: pass.
retryCount int // Retry count when request fails.
retryInterval time.Duration // Retry interval when request fails.
middlewareHandler []HandlerFunc // Interceptor handlers
}
var (
defaultClientAgent = fmt.Sprintf(`GoFrameHTTPClient %s`, gf.VERSION)
)
// New creates and returns a new HTTP client object.
func New() *Client {
client := &Client{
Client: http.Client{
Transport: &http.Transport{
// No validation for https certification of the server in default.
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DisableKeepAlives: true,
},
},
header: make(map[string]string),
cookies: make(map[string]string),
}
client.header["User-Agent"] = defaultClientAgent
return client
}
// Clone deeply clones current client and returns a new one.
func (c *Client) Clone() *Client {
newClient := New()
*newClient = *c
newClient.header = make(map[string]string)
newClient.cookies = make(map[string]string)
for k, v := range c.header {
newClient.header[k] = v
}
for k, v := range c.cookies {
newClient.cookies[k] = v
}
return newClient
}
// SetBrowserMode enables browser mode of the client.
// When browser mode is enabled, it automatically saves and sends cookie content
// from and to server.
func (c *Client) SetBrowserMode(enabled bool) *Client {
if enabled {
jar, _ := cookiejar.New(nil)
c.Jar = jar
}
return c
}
// SetHeader sets a custom HTTP header pair for the client.
func (c *Client) SetHeader(key, value string) *Client {
c.header[key] = value
return c
}
// SetHeaderMap sets custom HTTP headers with map.
func (c *Client) SetHeaderMap(m map[string]string) *Client {
for k, v := range m {
c.header[k] = v
}
return c
}
// SetAgent sets the User-Agent header for client.
func (c *Client) SetAgent(agent string) *Client {
c.header["User-Agent"] = agent
return c
}
// SetContentType sets HTTP content type for the client.
func (c *Client) SetContentType(contentType string) *Client {
c.header["Content-Type"] = contentType
return c
}
// SetHeaderRaw sets custom HTTP header using raw string.
func (c *Client) SetHeaderRaw(headers string) *Client {
for _, line := range gstr.SplitAndTrim(headers, "\n") {
array, _ := gregex.MatchString(`^([\w\-]+):\s*(.+)`, line)
if len(array) >= 3 {
c.header[array[1]] = array[2]
}
}
return c
}
// SetCookie sets a cookie pair for the client.
func (c *Client) SetCookie(key, value string) *Client {
c.cookies[key] = value
return c
}
// SetDump enables/disables dump feature for this request.
func (c *Client) SetDump(dump bool) *Client {
c.dump = dump
return c
}
// SetCookieMap sets cookie items with map.
func (c *Client) SetCookieMap(m map[string]string) *Client {
for k, v := range m {
c.cookies[k] = v
}
return c
}
// SetPrefix sets the request server URL prefix.
func (c *Client) SetPrefix(prefix string) *Client {
c.prefix = prefix
return c
}
// SetTimeOut sets the request timeout for the client.
func (c *Client) SetTimeout(t time.Duration) *Client {
c.Client.Timeout = t
return c
}
// SetBasicAuth sets HTTP basic authentication information for the client.
func (c *Client) SetBasicAuth(user, pass string) *Client {
c.authUser = user
c.authPass = pass
return c
}
// SetCtx sets context for each request of this client.
func (c *Client) SetCtx(ctx context.Context) *Client {
c.ctx = ctx
return c
}
// SetRetry sets retry count and interval.
func (c *Client) SetRetry(retryCount int, retryInterval time.Duration) *Client {
c.retryCount = retryCount
c.retryInterval = retryInterval
return c
}
// SetRedirectLimit limit the number of jumps
func (c *Client) SetRedirectLimit(redirectLimit int) *Client {
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= redirectLimit {
return http.ErrUseLastResponse
}
return nil
}
return c
}
// SetProxy set proxy for the client.
// This func will do nothing when the parameter `proxyURL` is empty or in wrong pattern.
// The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.
// Only `http` and `socks5` proxies are supported currently.
func (c *Client) SetProxy(proxyURL string) {
if strings.TrimSpace(proxyURL) == "" {
return
}
_proxy, err := url.Parse(proxyURL)
if err != nil {
return
}
if _proxy.Scheme == "http" {
if v, ok := c.Transport.(*http.Transport); ok {
v.Proxy = http.ProxyURL(_proxy)
}
} else {
var auth = &proxy.Auth{}
user := _proxy.User.Username()
if user != "" {
auth.User = user
password, hasPassword := _proxy.User.Password()
if hasPassword && password != "" {
auth.Password = password
}
} else {
auth = nil
}
// refer to the source code, error is always nil
dialer, err := proxy.SOCKS5(
"tcp",
_proxy.Host,
auth,
&net.Dialer{
Timeout: c.Client.Timeout,
KeepAlive: c.Client.Timeout,
},
)
if err != nil {
return
}
if v, ok := c.Transport.(*http.Transport); ok {
v.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
return dialer.Dial(network, addr)
}
}
//c.SetTimeout(10*time.Second)
}
}
// SetTlsKeyCrt sets the certificate and key file for TLS configuration of client.
func (c *Client) SetTLSKeyCrt(crtFile, keyFile string) error {
tlsConfig, err := LoadKeyCrt(crtFile, keyFile)
if err != nil {
return err
}
if v, ok := c.Transport.(*http.Transport); ok {
tlsConfig.InsecureSkipVerify = true
v.TLSClientConfig = tlsConfig
return nil
}
return gerror.New(`cannot set TLSClientConfig for custom Transport of the client`)
}
// SetTlsConfig sets the TLS configuration of client.
func (c *Client) SetTLSConfig(tlsConfig *tls.Config) error {
if v, ok := c.Transport.(*http.Transport); ok {
v.TLSClientConfig = tlsConfig
return nil
}
return gerror.New(`cannot set TLSClientConfig for custom Transport of the client`)
}
// LoadKeyCrt creates and returns a TLS configuration object with given certificate and key files.
func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error) {
crtPath, err := gfile.Search(crtFile)
if err != nil {
return nil, err
}
keyPath, err := gfile.Search(keyFile)
if err != nil {
return nil, err
}
crt, err := tls.LoadX509KeyPair(crtPath, keyPath)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{}
tlsConfig.Certificates = []tls.Certificate{crt}
tlsConfig.Time = time.Now
tlsConfig.Rand = rand.Reader
return tlsConfig, nil
}