forked from gocolly/colly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
72 lines (66 loc) · 1.8 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
package colly
import (
"bytes"
"fmt"
"io/ioutil"
"mime"
"net/http"
"strings"
"github.com/saintfish/chardet"
"golang.org/x/net/html/charset"
)
// Response is the representation of a HTTP response made by a Collector
type Response struct {
// StatusCode is the status code of the Response
StatusCode int
// Body is the content of the Response
Body []byte
// Ctx is a context between a Request and a Response
Ctx *Context
// Request is the Request object of the response
Request *Request
// Headers contains the Response's HTTP headers
Headers *http.Header
}
// Save writes response body to disk
func (r *Response) Save(fileName string) error {
return ioutil.WriteFile(fileName, r.Body, 0644)
}
// FileName returns the sanitized file name parsed from "Content-Disposition"
// header or from URL
func (r *Response) FileName() string {
_, params, err := mime.ParseMediaType(r.Headers.Get("Content-Disposition"))
if fName, ok := params["filename"]; ok && err == nil {
return SanitizeFileName(fName)
}
if r.Request.URL.RawQuery != "" {
return SanitizeFileName(fmt.Sprintf("%s_%s", r.Request.URL.Path, r.Request.URL.RawQuery))
}
return SanitizeFileName(r.Request.URL.Path[1:])
}
func (r *Response) fixCharset(detectCharset bool) {
contentType := strings.ToLower(r.Headers.Get("Content-Type"))
if !strings.Contains(contentType, "charset") {
if !detectCharset {
return
}
d := chardet.NewTextDetector()
r, err := d.DetectBest(r.Body)
if err != nil {
return
}
contentType = r.Charset
}
if strings.Contains(contentType, "utf-8") || strings.Contains(contentType, "utf8") {
return
}
encodedBodyReader, err := charset.NewReader(bytes.NewReader(r.Body), contentType)
if err != nil {
return
}
tmpBody, err := ioutil.ReadAll(encodedBodyReader)
if err != nil {
return
}
r.Body = tmpBody
}