-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresponse.go
212 lines (189 loc) · 3.99 KB
/
response.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
package crawler
import (
"bytes"
"compress/flate"
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"
"golang.org/x/text/encoding"
"github.com/fanyang01/crawler/cache"
)
const (
BodyStatusHeadOnly = iota
BodyStatusReading
BodyStatusEOF
BodyStatusError
)
// Response contains a http response and some metadata.
type Response struct {
*http.Response
URL *url.URL
NewURL *url.URL
Timestamp time.Time
CacheControl *cache.Control
ContentLocation *url.URL
ContentType string
CertainType bool
Refresh struct {
Seconds int
URL *url.URL
}
bodyCloser io.ReadCloser
Body io.Reader
BodyStatus int
Charset string
CertainCharset bool
Encoding encoding.Encoding
ctx *Context
links []*url.URL
}
var (
// respFreeList is a global free list for Response object.
respFreeList = sync.Pool{
New: func() interface{} { return new(Response) },
}
emptyResponse = Response{}
)
func NewResponse() *Response {
r := respFreeList.Get().(*Response)
return r
}
func (r *Response) free() {
links := r.links
if len(links) > perPage {
links = nil
} else {
links = links[:0]
}
// Let GC collect child objects.
*r = emptyResponse
r.links = links
respFreeList.Put(r)
}
func (r *Response) Context() *Context { return r.ctx }
type bodyReader struct {
err error
rc *io.ReadCloser
status *int
}
func (br *bodyReader) Read(p []byte) (n int, err error) {
switch *br.status {
case BodyStatusHeadOnly:
*br.status = BodyStatusReading
fallthrough
case BodyStatusReading:
n, err = (*br.rc).Read(p)
switch {
case err == io.EOF:
*br.status = BodyStatusEOF
(*br.rc).Close()
case err != nil:
*br.status = BodyStatusError
err = RetryableError{Err: err}
br.err = err
(*br.rc).Close()
}
return
case BodyStatusEOF:
return 0, io.EOF
default:
return 0, br.err
}
}
type bodyReadCloser struct {
err error
body, rc io.ReadCloser
closed bool
}
func (rc *bodyReadCloser) Read(p []byte) (int, error) {
if rc.closed {
return 0, errors.New("read on closed reader")
} else if rc.err != nil {
return 0, rc.err
} else if rc.rc != nil {
return rc.rc.Read(p)
}
return rc.body.Read(p)
}
func (rc *bodyReadCloser) Close() error {
if rc.closed {
return nil
}
if rc.rc != nil {
rc.rc.Close()
}
rc.body.Close()
rc.closed = true
return nil
}
// InitBody initializes Response.Body using given body, which is ensured
// to be closed at some point. If body is nil, embedded Response.Body is used.
func (resp *Response) InitBody(body io.ReadCloser) {
if body == nil {
body = resp.Response.Body
}
brc := &bodyReadCloser{
body: body,
}
br := &bodyReader{
rc: &resp.bodyCloser,
status: &resp.BodyStatus,
}
defer func() {
resp.bodyCloser = brc
resp.Body = br
}()
// Uncompress http compression
// We prefer Content-Encoding than Tranfer-Encoding
var encoding string
if encoding = resp.Header.Get("Content-Encoding"); encoding == "" {
if len(resp.TransferEncoding) == 0 {
encoding = "identity"
} else if len(resp.TransferEncoding) == 1 {
encoding = resp.TransferEncoding[0]
} else {
brc.err = fmt.Errorf("too many encodings: %v", resp.TransferEncoding)
return
}
}
switch encoding {
case "identity", "chunked":
// Do nothing
case "gzip":
// TODO: Normally gzip encoding is auto-decoded by http package,
// so this case may be needless.
r, err := gzip.NewReader(body)
if err != nil {
brc.err = err
return
}
brc.rc = ioutil.NopCloser(r)
case "deflate":
brc.rc = flate.NewReader(body)
default:
brc.err = fmt.Errorf("unsupported content encoding: %s", encoding)
}
return
}
func (resp *Response) preview(size int) ([]byte, error) {
r := resp.Body
preview := make([]byte, size)
n, err := io.ReadFull(r, preview)
switch {
case err == io.ErrUnexpectedEOF:
preview = preview[:n]
r = bytes.NewReader(preview)
case err != nil:
return nil, err
default:
r = io.MultiReader(bytes.NewReader(preview), r)
}
resp.Body = r
return preview, nil
}