-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
53 lines (46 loc) · 1.05 KB
/
util.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
package scrapegoat
import (
"code.google.com/p/go-charset/charset"
_ "code.google.com/p/go-charset/data"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
)
var debug bool = true
func debugf(format string, args ...interface{}) {
if debug {
log.Printf("DEBUG "+format, args)
}
}
func readResponse(resp *http.Response) string {
reader := charsetReader(resp)
r, err := ioutil.ReadAll(reader)
if err != nil {
panic(err)
}
return string(r)
}
// Returns encoding name found in HTTP Content-Type header
func getCharset(header http.Header) (string, error) {
char := header["Content-Type"][0]
idx := strings.Index(char, "charset")
if idx >= 0 {
return char[idx+8 : len(char)], nil
}
return "", errors.New("Charset header not found in " + char)
}
// Wraps the reader in charset.Reader if charset header found in resp
func charsetReader(resp *http.Response) io.Reader {
cs, err := getCharset(resp.Header)
if err != nil {
return resp.Body
}
r, err := charset.NewReader(cs, resp.Body)
if err != nil {
panic("Charset error " + cs)
}
return r
}