-
Notifications
You must be signed in to change notification settings - Fork 259
/
surf.go
151 lines (133 loc) · 3.49 KB
/
surf.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
// Copyright 2015 andeya Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package surfer
import (
"compress/flate"
"compress/gzip"
"compress/zlib"
"crypto/tls"
"io"
"math/rand"
"net"
"net/http"
"net/http/cookiejar"
"strings"
"time"
)
// Surf is the default Download implementation.
type Surf struct {
cookieJar *cookiejar.Jar
}
// New 创建一个Surf下载器
func New() Surfer {
s := new(Surf)
s.cookieJar, _ = cookiejar.New(nil)
return s
}
// Download 实现surfer下载器接口
func (surf *Surf) Download(param *Request) (*http.Response, error) {
err := param.prepare()
if err != nil {
return nil, err
}
param.client = surf.buildClient(param)
resp, err := surf.httpRequest(param)
if err == nil {
switch resp.Header.Get("Content-Encoding") {
case "gzip":
var gzipReader *gzip.Reader
gzipReader, err = gzip.NewReader(resp.Body)
if err == nil {
resp.Body = gzipReader
}
case "deflate":
resp.Body = flate.NewReader(resp.Body)
case "zlib":
var readCloser io.ReadCloser
readCloser, err = zlib.NewReader(resp.Body)
if err == nil {
resp.Body = readCloser
}
}
}
return param.writeback(resp), err
}
// buildClient creates, configures, and returns a *http.Client type.
func (surf *Surf) buildClient(req *Request) *http.Client {
client := &http.Client{
CheckRedirect: req.checkRedirect,
}
if req.EnableCookie {
client.Jar = surf.cookieJar
}
transport := &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
c, err := net.DialTimeout(network, addr, req.DialTimeout)
if err != nil {
return nil, err
}
if req.ConnTimeout > 0 {
c.SetDeadline(time.Now().Add(req.ConnTimeout))
}
return c, nil
},
}
if req.proxy != nil {
transport.Proxy = http.ProxyURL(req.proxy)
}
if strings.ToLower(req.url.Scheme) == "https" {
transport.TLSClientConfig = &tls.Config{RootCAs: nil, InsecureSkipVerify: true}
transport.DisableCompression = true
}
client.Transport = transport
return client
}
// send uses the given *http.Request to make an HTTP request.
func (surf *Surf) httpRequest(param *Request) (resp *http.Response, err error) {
req, err := http.NewRequest(param.Method, param.Url, param.body)
if err != nil {
return nil, err
}
req.Header = param.Header
if param.TryTimes <= 0 {
for {
resp, err = param.client.Do(req)
if err != nil {
if !param.EnableCookie {
l := len(UserAgents["common"])
r := rand.New(rand.NewSource(time.Now().UnixNano()))
req.Header.Set("User-Agent", UserAgents["common"][r.Intn(l)])
}
time.Sleep(param.RetryPause)
continue
}
break
}
} else {
for i := 0; i < param.TryTimes; i++ {
resp, err = param.client.Do(req)
if err != nil {
if !param.EnableCookie {
l := len(UserAgents["common"])
r := rand.New(rand.NewSource(time.Now().UnixNano()))
req.Header.Set("User-Agent", UserAgents["common"][r.Intn(l)])
}
time.Sleep(param.RetryPause)
continue
}
break
}
}
return resp, err
}