-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
216 lines (196 loc) · 4.69 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
package crawler
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
"github.com/fanyang01/crawler/cache"
)
// Client defines how requests are made.
type Client interface {
Do(*Request) (*Response, error)
}
var (
DefaultHTTPTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
// DefaultHTTPClient uses DefaultHTTPTransport to make HTTP request,
// and enables the cookie jar.
DefaultHTTPClient *http.Client
// DefaultClient is the default client used by crawler to fetch static
// resources.
DefaultClient *StdClient
)
func init() {
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
DefaultHTTPClient = &http.Client{
Transport: DefaultHTTPTransport,
Jar: jar,
Timeout: 10 * time.Second,
}
DefaultClient = &StdClient{client: DefaultHTTPClient}
}
// StdClient is intended to be used for fetching static resources.
type StdClient struct {
client *http.Client
cache *cache.Pool
}
// NewStdClient returns a new standard client that uses the provided HTTP
// client to do HTTP requests. cacheSize specifies the maxmium size(in
// bytes) of the HTTP cache pool. If cacheSize <= 0, HTTP caching will be
// disabled.
func NewStdClient(client *http.Client, cacheSize int) *StdClient {
if client == nil {
client = DefaultHTTPClient
}
c := &StdClient{client: client}
if cacheSize > 0 {
c.cache = cache.NewPool(cacheSize)
}
return c
}
// ResponseStatusError represents unexpected response status.
type ResponseStatusError int
func (e ResponseStatusError) Error() string {
if e == 0 {
return ""
}
return fmt.Sprintf("unexpected response status: %d %s", int(e), http.StatusText(int(e)))
}
// Do implements the Client interface.
func (c *StdClient) Do(req *Request) (r *Response, err error) {
defer func() {
if err != nil && r != nil {
r.free()
}
}()
var (
hr *http.Response
cc *cache.Control
body []byte
now time.Time
ok bool
modified bool = true
)
if req.Method == "GET" && c.cache != nil {
if hr, body, cc, ok = c.cache.Get(req.URL); ok {
if cc.NeedValidate() {
if hr, cc, modified, err = c.revalidate(
req.URL, hr, body, cc,
); err != nil {
return
}
} else {
modified = false
hr.Body = ioutil.NopCloser(bytes.NewReader(body))
}
now = time.Now()
goto INIT
}
}
if hr, err = c.client.Do(req.Request); err != nil {
return nil, RetryableError{Err: err}
}
now = time.Now()
// Only status code 2xx is OK.
switch {
case 200 <= hr.StatusCode && hr.StatusCode < 300:
// 5xx and 4xx but 404 are retryable.
case hr.StatusCode >= 500:
fallthrough
case hr.StatusCode >= 400 && hr.StatusCode != 404:
hr.Body.Close()
err = RetryableError{
Err: ResponseStatusError(hr.StatusCode),
}
return
default:
hr.Body.Close()
err = ResponseStatusError(hr.StatusCode)
return
}
if c.cache != nil {
cc = cache.Parse(hr, now)
}
INIT:
r = NewResponse()
r.init(req.URL, hr, now, cc)
if c.cache != nil && cc != nil && cc.IsCacheable() {
if !modified { // Just update cached header
if ok := c.cache.Update(req.URL, r.CacheControl, r.Header); ok {
return
}
}
r.Body = c.cache.NewReader(r.NewURL, r.CacheControl, r.Response, r.Body)
}
return
}
func (c *StdClient) revalidate(
u *url.URL, r *http.Response, body []byte, cc *cache.Control,
) (
rr *http.Response, rcc *cache.Control, modified bool, err error,
) {
modified = true
req, _ := http.NewRequest("GET", u.String(), nil)
if cc.ETag != "" {
req.Header.Add("If-None-Match", cc.ETag)
}
var t time.Time
if t = cc.LastModified; t.IsZero() {
t = cc.Date
}
req.Header.Add("If-Modified-Since", t.Format(http.TimeFormat))
if rr, err = c.client.Do(req); err != nil {
return
}
switch {
case rr.StatusCode == 304:
rr.Body.Close()
rr = cache.Construct(r, rr, body)
if rr.Request.URL.String() == u.String() {
modified = false
}
fallthrough
case 200 <= rr.StatusCode && rr.StatusCode < 300:
rcc = cache.Parse(rr, time.Now())
if rcc == nil || !rcc.IsCacheable() {
c.cache.Remove(u)
}
return
// 5xx and 4xx but 404 are retryable.
case rr.StatusCode >= 500:
fallthrough
case rr.StatusCode >= 400 && rr.StatusCode != 404:
rr.Body.Close()
err = RetryableError{
Err: ResponseStatusError(rr.StatusCode),
}
return
default:
rr.Body.Close()
err = ResponseStatusError(rr.StatusCode)
return
}
}
func (r *Response) init(u *url.URL, hr *http.Response,
t time.Time, cc *cache.Control) *Response {
r.URL = u
r.NewURL = hr.Request.URL
r.Timestamp = t
r.Response = hr
r.CacheControl = cc
r.InitBody(hr.Body)
return r
}