diff options
author | 2020-10-30 22:46:43 -0700 | |
---|---|---|
committer | 2020-10-30 23:00:05 -0700 | |
commit | 2f3708d40c5007062470b0c89b293353ee4f6aa8 (patch) | |
tree | 26975b860ecfb645ebcd09edce8d4de9f615e3a9 /http/client/response.go | |
parent | 46c13b51851374b445ed0458c321808bd83e8869 (diff) | |
download | v2-2f3708d40c5007062470b0c89b293353ee4f6aa8.tar.gz v2-2f3708d40c5007062470b0c89b293353ee4f6aa8.tar.zst v2-2f3708d40c5007062470b0c89b293353ee4f6aa8.zip |
Do not use charset.NewReader if the body is a valid UTF-8 document
Diffstat (limited to 'http/client/response.go')
-rw-r--r-- | http/client/response.go | 42 |
1 files changed, 18 insertions, 24 deletions
diff --git a/http/client/response.go b/http/client/response.go index 122c40c3..c9c124a5 100644 --- a/http/client/response.go +++ b/http/client/response.go @@ -87,32 +87,26 @@ func (r *Response) IsModified(etag, lastModified string) bool { // - Feeds with encoding specified only in XML document and not in HTTP header // - Feeds with wrong encoding defined and already in UTF-8 func (r *Response) EnsureUnicodeBody() (err error) { - if r.ContentType != "" { - // JSON feeds are always in UTF-8. - if strings.Contains(r.ContentType, "json") { - return + buffer, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + + r.Body = bytes.NewReader(buffer) + if utf8.Valid(buffer) { + return nil + } + + if strings.Contains(r.ContentType, "xml") { + // We ignore documents with encoding specified in XML prolog. + // This is going to be handled by the XML parser. + length := 1024 + if len(buffer) < 1024 { + length = len(buffer) } - if strings.Contains(r.ContentType, "xml") { - buffer, _ := ioutil.ReadAll(r.Body) - r.Body = bytes.NewReader(buffer) - - // We ignore documents with encoding specified in XML prolog. - // This is going to be handled by the XML parser. - length := 1024 - if len(buffer) < 1024 { - length = len(buffer) - } - - if xmlEncodingRegex.Match(buffer[0:length]) { - return - } - - // If no encoding is specified in the XML prolog and - // the document is valid UTF-8, nothing needs to be done. - if utf8.Valid(buffer) { - return - } + if xmlEncodingRegex.Match(buffer[0:length]) { + return nil } } |