-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
168 lines (151 loc) · 3.52 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
/**
* Author: Tony.Shao
* Email: [email protected]
* Github: github.com/xiocode
* File: client.go
* Description: httplib client
*/
package httplib
import (
"io"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"sync"
"time"
)
type status int
type Client struct {
sync.Mutex
Conn *http.Client
session http.Header
}
func NewClient() *Client {
return &Client{
Conn: &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
deadline := time.Now().Add(5 * time.Second)
conn, err := net.DialTimeout(network, addr, 5*time.Second)
if err != nil {
return nil, err
}
conn.SetDeadline(deadline)
return conn, nil
},
ResponseHeaderTimeout: 5 * time.Second,
},
},
}
}
func NewProxyClient(proxy string) *Client {
proxyURL, _ := url.Parse(proxy)
return &Client{
Conn: &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
deadline := time.Now().Add(5 * time.Second)
conn, err := net.DialTimeout(network, addr, 5*time.Second)
if err != nil {
return nil, err
}
conn.SetDeadline(deadline)
return conn, nil
},
Proxy: http.ProxyURL(proxyURL),
ResponseHeaderTimeout: 5 * time.Second,
},
},
}
}
func NewSession() *Client {
jar, _ := cookiejar.New(nil)
return &Client{
Conn: &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
deadline := time.Now().Add(5 * time.Second)
conn, err := net.DialTimeout(network, addr, 5*time.Second)
if err != nil {
return nil, err
}
conn.SetDeadline(deadline)
return conn, nil
},
ResponseHeaderTimeout: 5 * time.Second,
},
Jar: jar,
},
session: make(http.Header),
}
}
func NewProxySession(proxy string) *Client {
proxyURL, _ := url.Parse(proxy)
jar, _ := cookiejar.New(nil)
return &Client{
Conn: &http.Client{
Jar: jar,
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
deadline := time.Now().Add(60 * time.Second)
conn, err := net.DialTimeout(network, addr, 60*time.Second)
if err != nil {
return nil, err
}
conn.SetDeadline(deadline)
return conn, nil
},
Proxy: http.ProxyURL(proxyURL),
ResponseHeaderTimeout: 60 * time.Second,
},
},
session: make(http.Header),
}
}
func (c *Client) Do(method, url string, headers map[string][]string, body io.Reader) (*Response, error) {
req, err := http.NewRequest(method, url, body)
if checkError(err) {
return nil, err
}
c.Lock()
defer c.Unlock()
if c.session != nil {
if headers != nil {
for key, v := range headers {
for _, val := range v {
c.session.Set(key, val)
}
}
}
for key, v := range c.session {
for _, val := range v {
req.Header.Set(key, val)
}
}
}
if headers != nil {
for key, v := range headers {
for _, val := range v {
req.Header.Set(key, val)
}
}
}
resp, err := c.Conn.Do(req)
if checkError(err) {
return nil, err
}
return &Response{resp}, nil
}
func (c *Client) GET(url string, headers map[string][]string) (*Response, error) {
return c.Do("GET", url, headers, nil)
}
func (c *Client) POST(url string, headers map[string][]string, body io.Reader) (*Response, error) {
if headers == nil {
headers = make(map[string][]string)
}
if _, ok := headers["Content-Type"]; !ok {
headers["Content-Type"] = []string{"application/x-www-form-urlencoded"}
}
return c.Do("POST", url, headers, body)
}