-
Notifications
You must be signed in to change notification settings - Fork 9
/
resp.go
165 lines (149 loc) · 3.53 KB
/
resp.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
package req
import (
"bytes"
"encoding/json"
"encoding/xml"
"github.com/PuerkitoBio/goquery"
"github.com/saintfish/chardet"
"github.com/tidwall/gjson"
"golang.org/x/net/html/charset"
"gopkg.in/xmlpath.v2"
"io/ioutil"
"net/http"
"strings"
)
// Response is a object of HTTP response
type Response struct {
*http.Response
// Body is the content of the Response
Body []byte
NoDecodeBody []byte
// Text is the content of the Response parsed as string
Text string
// Request is the Req object from goribot of the response.Tip: there is another Request attr come from *http.Response
Req *Request
Err error
}
func (s *Response) Resp() (*Response, error) {
return s, s.Err
}
func (s *Response) Txt() (string, error) {
return s.Text, s.Err
}
func (s *Response) HTML() (*goquery.Document, error) {
if s.Err != nil {
return nil, s.Err
}
return goquery.NewDocumentFromReader(bytes.NewReader(s.Body))
}
func (s *Response) XML() (*xmlpath.Node, error) {
if s.Err != nil {
return nil, s.Err
}
return xmlpath.Parse(bytes.NewReader(s.Body))
}
func (s *Response) JSON() (gjson.Result, error) {
return gjson.Parse(s.Text), s.Err
}
func (s *Response) Error() error {
return s.Err
}
// DecodeAndParas decodes the body to text and try to parse it to html or json.
func (s *Response) DecodeAndParse() error {
if s.Err != nil {
return s.Err
}
if len(s.Body) == 0 {
return nil
}
s.NoDecodeBody = s.Body
contentType := strings.ToLower(s.Header.Get("Content-Type"))
if strings.Contains(contentType, "text/") ||
strings.Contains(contentType, "/json") {
if !strings.Contains(contentType, "charset") {
if s.Req.RespEncode != "" {
contentType += "; charset=" + s.Req.RespEncode
} else {
r, err := chardet.NewTextDetector().DetectBest(s.Body)
if err != nil {
return err
}
contentType += "; charset=" + r.Charset
}
}
if strings.Contains(contentType, "utf-8") || strings.Contains(contentType, "utf8") {
s.Text = string(s.Body)
} else {
tmpBody, err := encodeBytes(s.Body, contentType)
if err != nil {
return err
}
s.Body = tmpBody
s.Text = string(s.Body)
}
}
return nil
}
func (s *Response) IsHTML() bool {
contentType := strings.ToLower(s.Header.Get("Content-Type"))
return strings.Contains(contentType, "/html")
}
func (s *Response) IsJSON() bool {
contentType := strings.ToLower(s.Header.Get("Content-Type"))
return strings.Contains(contentType, "/json")
}
func (s *Response) BindJSON(i interface{}) error {
if s.Err != nil {
return s.Err
}
return json.Unmarshal(s.Body, i)
}
func (s *Response) BindXML(i interface{}) error {
if s.Err != nil {
return s.Err
}
return xml.Unmarshal(s.Body, i)
}
// TODO Bind xml,json
//func (s *Response) Format(f fmt.State, c rune) {
// if s == nil {
// fmt.Print(nil)
// return
// }
// if s.Response == nil {
// fmt.Print(nil, s.Err)
// return
// }
// if s.Err != nil {
// fmt.Println("response error", s.Err)
// return
// }
//
// if f.Flag('+') {
// fmt.Println(s.Proto, s.Status)
// for k, v := range s.Header {
// for _, a := range v {
// fmt.Println(k+":", a)
// }
// }
// fmt.Println("")
// if s.Text != "" {
// fmt.Println(s.Text)
// } else {
// fmt.Println(s.Body)
// }
// } else {
// if s.Text != "" {
// fmt.Print(s.Proto, " ", s.Status, " ", s.Text)
// } else {
// fmt.Print(s.Proto, " ", s.Status, " ", s.Body)
// }
// }
//}
func encodeBytes(b []byte, contentType string) ([]byte, error) {
r, err := charset.NewReader(bytes.NewReader(b), contentType)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}