forked from suosi-inc/go-pkg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
173 lines (147 loc) · 4.03 KB
/
http.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
package spider
import (
"crypto/tls"
"errors"
"net"
"net/http"
"time"
"github.com/x-funs/go-fun"
)
const (
HttpDefaultTimeOut = 10000
HttpDefaultMaxContentLength = 10 * 1024 * 1024
HttpDefaultUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
HttpDefaultAcceptEncoding = "gzip, deflate"
)
var (
textContentTypes = []string{
"text/plain",
"text/html",
"text/xml",
"application/xml",
"application/xhtml+xml",
"application/json",
}
)
type HttpReq struct {
// 嵌入 fun.HttpReq
*fun.HttpReq
// 禁止自动探测字符集和转换字符集
DisableCharset bool
// 强制 ContentType 为文本类型
ForceTextContentType bool
}
type HttpResp struct {
*fun.HttpResp
// 字符集
Charset CharsetRes
}
// HttpDefaultTransport 默认全局使用的 http.Transport
var HttpDefaultTransport = &http.Transport{
DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
DisableKeepAlives: true,
IdleConnTimeout: 60 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// HttpGet 参数为请求地址 (HttpReq, 超时时间)
// HttpGet(url)、HttpGet(url, HttpReq)、HttpGet(url, timeout)、HttpGet(url, HttpReq, timeout)
// 返回 body, 错误信息
func HttpGet(urlStr string, args ...any) ([]byte, error) {
l := len(args)
switch l {
case 0:
return HttpGetDo(urlStr, nil, 0)
case 1:
switch v := args[0].(type) {
case int:
timeout := fun.ToInt(args[0])
return HttpGetDo(urlStr, nil, timeout)
case *HttpReq:
return HttpGetDo(urlStr, v, 0)
}
case 2:
timeout := fun.ToInt(args[1])
switch v := args[0].(type) {
case *HttpReq:
return HttpGetDo(urlStr, v, timeout)
}
}
return nil, errors.New("http get params error")
}
// HttpGetDo Http Get 请求, 参数为请求地址, HttpReq, 超时时间(毫秒)
// 返回 body, 错误信息
func HttpGetDo(urlStr string, r *HttpReq, timeout int) ([]byte, error) {
resp, err := HttpGetResp(urlStr, r, timeout)
if err != nil {
return nil, err
} else {
return resp.Body, nil
}
}
// HttpGetResp Http Get 请求, 参数为请求地址, HttpReq, 超时时间(毫秒)
// 返回 HttpResp, 错误信息
func HttpGetResp(urlStr string, r *HttpReq, timeout int) (*HttpResp, error) {
req, err := http.NewRequest(http.MethodGet, urlStr, nil)
if err != nil {
return nil, err
}
return HttpDoResp(req, r, timeout)
}
// HttpDo Http 请求, 参数为 http.Request, HttpReq, 超时时间(毫秒)
// 返回 body, 错误信息
func HttpDo(req *http.Request, r *HttpReq, timeout int) ([]byte, error) {
resp, err := HttpDoResp(req, r, timeout)
if err != nil {
return nil, err
} else {
return resp.Body, nil
}
}
// HttpDoResp Http 请求, 参数为 http.Request, HttpReq, 超时时间(毫秒)
// 返回 HttpResp, 错误信息
func HttpDoResp(req *http.Request, r *HttpReq, timeout int) (*HttpResp, error) {
// 处理 Transport
if r == nil {
r = &HttpReq{
HttpReq: &fun.HttpReq{
Transport: HttpDefaultTransport,
},
}
} else if r.HttpReq == nil {
r.HttpReq = &fun.HttpReq{
Transport: HttpDefaultTransport,
}
} else if r.Transport == nil {
r.Transport = HttpDefaultTransport
}
// 强制文本类型
if r != nil && r.ForceTextContentType {
r.AllowedContentTypes = textContentTypes
}
// HttpResp
var charset CharsetRes
httpResp := &HttpResp{
Charset: charset,
}
resp, err := fun.HttpDoResp(req, r.HttpReq, timeout)
httpResp.HttpResp = resp
if err != nil {
return httpResp, err
}
// 默认会自动进行探测编码和转码, 除非手动禁用
if r == nil || !r.DisableCharset {
charsetRes := Charset(httpResp.Body, httpResp.Headers)
httpResp.Charset = charsetRes
if charsetRes.Charset != "" && charsetRes.Charset != "UTF-8" {
utf8Body, e := fun.ToUtf8(httpResp.Body, charsetRes.Charset)
if e != nil {
return httpResp, errors.New("ErrorCharset")
} else {
httpResp.Body = utf8Body
}
}
}
return httpResp, nil
}