-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
154 lines (137 loc) · 3.4 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
package requests
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"golang.org/x/net/html"
"io/ioutil"
"net/http"
"regexp"
"sort"
"strings"
"time"
)
type Response struct {
HttpResp *http.Response
content []byte
hasRead bool
text string
Req *Request
header string
title string
Delay time.Duration
}
func (resp *Response) getRespText() (respText string) {
httpResp := resp.HttpResp
respText += fmt.Sprintf("%s %s\r\n", httpResp.Proto, httpResp.Status)
headers := []string{}
for k, v := range httpResp.Header {
headers = append(headers, fmt.Sprintf("%s: %s", k, strings.Join(v, ",")))
}
sort.Strings(headers)
respText += fmt.Sprintf("%s\r\n\r\n%s", strings.Join(headers, "\r\n"), resp.Text())
return
}
func (resp *Response) Text() string {
if !resp.hasRead {
resp.Content()
}
return resp.text
}
func (resp *Response) Cookie(key string) (value []string) {
for _, cookie := range resp.Cookies() {
if cookie.Name == key {
value = append(value, cookie.Value)
}
}
return
}
func (resp *Response) HeaderString() string {
if resp.header == "" {
for k, header := range resp.HttpResp.Header {
resp.header = resp.header + fmt.Sprintf("%s: %s\n", k, strings.Join(header, ","))
}
}
return resp.header
}
func (resp *Response) Content() []byte {
if resp.hasRead {
return resp.content
}
var Body = resp.HttpResp.Body
if resp.HttpResp.Header.Get("Content-Encoding") == "gzip" && resp.Req.header.Get("Accept-Encoding") != "" {
// fmt.Println("gzip")
reader, err := gzip.NewReader(Body)
if err != nil {
return nil
}
Body = reader
}
eof := make(chan bool)
go func() {
resp.content, _ = ioutil.ReadAll(Body)
eof <- true
}()
select {
case <-time.After(resp.Req.Client.Timeout):
case <-eof:
}
resp.hasRead = true
resp.text = string(decodeBody(resp.content))
return resp.content
}
func (resp *Response) Title() string {
if resp.title == "" {
find := titleReg.FindSubmatch([]byte(resp.Text()))
if len(find) > 1 {
resp.title = string(find[1])
resp.title = html.UnescapeString(resp.title)
resp.title = strings.ReplaceAll(resp.title, "\t", "")
resp.title = strings.ReplaceAll(resp.title, "\n", "")
resp.title = strings.ReplaceAll(resp.title, "\r", "")
resp.title = strings.TrimSpace(resp.title)
}
}
return resp.title
}
func (resp *Response) BodyContains(arg interface{}) (result bool) {
switch a := arg.(type) {
case string:
result = strings.Contains(strings.ToLower(resp.Text()), strings.ToLower(a))
case []byte:
result = bytes.Contains(bytes.ToLower(resp.Content()), bytes.ToLower(a))
}
return
}
func (resp *Response) HeaderContains(arg string) (result bool) {
result = strings.Contains(strings.ToLower(resp.HeaderString()), strings.ToLower(arg))
return
}
func (resp *Response) Search(reg *regexp.Regexp) map[string]string {
match := reg.FindStringSubmatch(resp.Text())
groupNames := reg.SubexpNames()
result := make(map[string]string)
if len(match) < len(groupNames) {
return result
}
for i, name := range groupNames {
if i != 0 && name != "" {
result[name] = match[i]
}
}
return result
}
func (resp *Response) Json(v interface{}) error {
if resp.content == nil {
resp.Content()
}
return json.Unmarshal(resp.content, v)
}
func (resp *Response) Cookies() (cookies []*http.Cookie) {
//HttpReq := resp.Req.HttpReq
//client := resp.Req.Client
//
//cookies = client.Jar.Cookies(HttpReq.URL)
return resp.HttpResp.Cookies()
}