-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhttp.go
307 lines (277 loc) · 8.28 KB
/
xhttp.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package GoLib
import (
"bytes"
"crypto/md5"
"crypto/tls"
"encoding/hex"
"fmt"
"golang.org/x/net/publicsuffix"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/transform"
"html"
"io"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/textproto"
"net/url"
"os"
"regexp"
"strings"
"time"
)
//初始化结构体
type Http struct {
Proxy string
Header map[string]string
TimeOut int
Client *http.Client
}
//返回结果
type HttpResponse struct {
BaseResponse *http.Response
Url string
Status string
ResponseHeader map[string][]string
ResponseBody []byte
Title string
RequestPackage string
ResponsePackage string
}
//JSON请求
func (h *Http) JsonRequest(url, body string) *HttpResponse {
return h.httpRequest("POST", url, body, "json")
}
//xml请求
func (h *Http) XMLRequest(url, body string) *HttpResponse {
return h.httpRequest("POST", url, body, "xml")
}
//GET请求
func (h *Http) GET(url string) *HttpResponse {
return h.httpRequest("GET", url, "", "")
}
//POST请求
func (h *Http) POST(url string, body string) *HttpResponse {
return h.httpRequest("POST", url, body, "")
}
//PUT请求
func (h *Http) PUT(url string, body string) *HttpResponse {
return h.httpRequest("POST", url, body, "")
}
//HEAD请求
func (h *Http) HEAD(url string) *HttpResponse {
return h.httpRequest("HEAD", url, "", "")
}
//判断url是否为文件
func (h *Http) ISFile(url string) (bool, int) {
response := h.HEAD(url)
if response != nil {
headerKeys := h.getHeaderKeys(response.ResponseHeader)
if StrInList("ETag", headerKeys, true) && StrInList("Last-Modified", headerKeys, true) {
return true, Str2Int(strings.Join(response.ResponseHeader["Content-Length"], ""))
}
}
return false, 0
}
//下载文件到本地
func (h *Http) DownloadFile(url, fileFullPath string) bool {
httpresponse := h.GET(url)
if httpresponse.BaseResponse != nil {
file, _ := os.Create(fileFullPath)
file.Write(httpresponse.ResponseBody)
defer file.Close()
if FileExists(fileFullPath) {
return true
}
return false
}
return false
}
//获取header的keys
func (h *Http) getHeaderKeys(headers map[string][]string) (result []string) {
for k := range headers {
result = append(result, k)
}
return
}
//正常请求
func (h *Http) httpRequest(method, url, body, tp string) *HttpResponse {
if h.Client == nil {
h.init()
}
I := bytes.NewReader([]byte(body))
O := transform.NewReader(I, traditionalchinese.Big5.NewEncoder())
requests, _ := http.NewRequest(method, url, O)
if requests == nil {
return nil
}
for k, v := range h.Header {
requests.Header.Add(k, v)
}
//json格式传输
if tp == "json" {
requests.Header.Add("Content-Type", "application/json")
} else if tp == "xml" {
//xml格式传输
requests.Header.Add("Content-Type", "text/xml")
} else {
//普通格式传输
requests.Header.Add("Content-Type", "application/x- www-form-urlencoded")
}
response, err := h.Client.Do(requests)
//手动清理前面的变量
requests = nil
I = nil
O = nil
if err != nil {
return nil
}
responseBody := getBody(response)
return &HttpResponse{BaseResponse: response, Url: response.Request.URL.RequestURI(), Status: response.Status, ResponseHeader: response.Header, ResponseBody: responseBody, Title: getTitle(response, string(responseBody)), RequestPackage: getRequestPackage(response, body), ResponsePackage: getResponsePackage(response) + string(responseBody)}
}
//文件上传
func (h *Http) FileUpload(fieldName, fileName, url, contentType string, fileContent []byte, params map[string]string) *HttpResponse {
/*
根据文件上传的底层代码重写
req, err := NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
*/
if h.Client == nil {
h.init()
}
body := new(bytes.Buffer)
mulWriter := multipart.NewWriter(body)
//添加参数
for k, v := range params {
err := mulWriter.WriteField(k, v)
if err != nil {
return nil
}
}
file, _ := createFormFile(fieldName, fileName, contentType, mulWriter)
//file, err := mulWriter.CreateFormFile(fieldName, fileName)
_, err := file.Write(fileContent)
if err != nil {
return nil
}
contentType2 := mulWriter.FormDataContentType()
err = mulWriter.Close()
if err != nil {
return nil
}
requestBody := body.Bytes()
requests, _ := http.NewRequest("POST", url, body)
for k, v := range h.Header {
requests.Header.Add(k, v)
}
requests.Header.Add("Content-Type", contentType2)
response, _ := h.Client.Do(requests)
//手动清理前面不使用变量
body = nil
requests = nil
contentType2 = ""
file = nil
mulWriter = nil
responseBody := getBody(response)
return &HttpResponse{BaseResponse: response, Url: response.Request.URL.RequestURI(), Status: response.Status, ResponseHeader: response.Header, ResponseBody: responseBody, Title: getTitle(response, string(responseBody)), RequestPackage: getRequestPackage(response, string(requestBody)), ResponsePackage: getResponsePackage(response) + string(responseBody)}
}
//初始化client
func (h *Http) init() {
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if h.Proxy != "" {
proxy, _ := url.Parse(h.Proxy)
h.Client = &http.Client{Jar: cookieJar, Timeout: time.Duration(h.TimeOut) * time.Second, Transport: &http.Transport{Proxy: http.ProxyURL(proxy), TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
} else {
h.Client = &http.Client{Jar: cookieJar, Timeout: time.Duration(h.TimeOut) * time.Second, Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
}
}
//CreateFormFile附属函数
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
//重写multipart.Writer.CreateFormFile()函数实现可以控制Content-Type内容
func createFormFile(fieldname, filename, contentType string, w *multipart.Writer) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldname), escapeQuotes(filename)))
h.Set("Content-Type", contentType)
return w.CreatePart(h)
}
//获取html-content
func getBody(response *http.Response) []byte {
defer response.Body.Close()
//默认 3MB 可以改成你自己想要的
all, err := io.ReadAll(io.LimitReader(response.Body, int64(4<<20)))
if err != nil {
return nil
}
return all
}
func trimTitleTags(title string) string {
// trim <title>*</title>
titleBegin := strings.Index(title, ">")
titleEnd := strings.Index(title, "</")
return title[titleBegin+1 : titleEnd]
}
//获取title
func getTitle(response *http.Response, body string) (title string) {
var re = regexp.MustCompile(`(?im)<\s*title.*>(.*?)<\s*/\s*title>`)
for _, match := range re.FindAllString(string(body), -1) {
title = html.UnescapeString(trimTitleTags(match))
break
}
if contentTypes, ok := response.Header["Content-Type"]; ok {
contentType := strings.ToLower(strings.Join(contentTypes, ";"))
if strings.Contains(contentType, "charset=gb2312") || strings.Contains(contentType, "charset=gbk") {
titleUtf8, err := Decodegbk([]byte(title))
if err != nil {
return
}
return string(titleUtf8)
}
}
re = nil
return
}
//strng形式获取headers
func getHeaders(response *http.Response) (header string) {
for k, v := range response.Header {
header = header + k + ": " + strings.Join(v, ";") + "\r\n"
}
return header
}
//string形式获取请求包
func getRequestPackage(response *http.Response, body string) (result string) {
request := response.Request
result += request.Method + " " + request.URL.Path + " " + request.Proto + "\r\n"
result += "Host: " + response.Request.Host + "\r\n"
for k, v := range request.Header {
result += k + ": " + strings.Join(v, ";") + "\r\n"
}
if body != "" {
result += "\r\n" + body
}
request = nil
return result
}
//获取响应包
func getResponsePackage(response *http.Response) (result string) {
result = response.Proto + " " + response.Status + "\r\n"
result += getHeaders(response) + "\r\n"
return
}
//获取文件md5
func GetMd5(response *HttpResponse) string {
if response.BaseResponse.StatusCode == 200 && string(response.ResponseBody) != "" {
h := md5.New()
h.Write(response.ResponseBody)
return hex.EncodeToString(h.Sum(nil))
}
return ""
}