/
decode.go
37 lines (33 loc) · 913 Bytes
/
decode.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
// Package httputil is simple http decode request wrapper
package httputil
import (
"bufio"
"io"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding/htmlindex"
)
func detectContentCharset(body io.Reader) (string, io.Reader) {
r := bufio.NewReader(body)
if data, err := r.Peek(1024); err == nil {
_, name, _ := charset.DetermineEncoding(data, "")
return name, r
}
return "utf-8", r
}
// DecodeHTMLBody returns an decoding reader of the html Body for the specified `charset`
// If `charset` is empty, DecodeHTMLBody tries to guess the encoding from the content
func DecodeHTMLBody(body io.Reader) (io.Reader, error) {
// if charset == "" {
charset, r := detectContentCharset(body)
// }
e, err := htmlindex.Get(charset)
if err != nil {
return nil, err
}
if name, _ := htmlindex.Name(e); name != "utf-8" {
body = e.NewDecoder().Reader(r)
} else {
body = r
}
return body, nil
}