Ignore invalid content type

This commit is contained in:
Frédéric Guillot 2019-09-18 22:12:15 -07:00 committed by Frédéric Guillot
parent 7d50cc40de
commit ca48f7612a
2 changed files with 4 additions and 9 deletions

View file

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"io" "io"
"io/ioutil" "io/ioutil"
"mime"
"regexp" "regexp"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -74,17 +73,12 @@ func (r *Response) IsModified(etag, lastModified string) bool {
// - Feeds with wrong encoding defined and already in UTF-8 // - Feeds with wrong encoding defined and already in UTF-8
func (r *Response) EnsureUnicodeBody() (err error) { func (r *Response) EnsureUnicodeBody() (err error) {
if r.ContentType != "" { if r.ContentType != "" {
mediaType, _, mediaErr := mime.ParseMediaType(r.ContentType)
if mediaErr != nil {
return mediaErr
}
// JSON feeds are always in UTF-8. // JSON feeds are always in UTF-8.
if strings.Contains(mediaType, "json") { if strings.Contains(r.ContentType, "json") {
return return
} }
if strings.Contains(mediaType, "xml") { if strings.Contains(r.ContentType, "xml") {
buffer, _ := ioutil.ReadAll(r.Body) buffer, _ := ioutil.ReadAll(r.Body)
r.Body = bytes.NewReader(buffer) r.Body = bytes.NewReader(buffer)

View file

@ -121,6 +121,7 @@ func TestEnsureUnicodeWithHTMLDocuments(t *testing.T) {
{"urdu.xml", "text/xml; charset=utf-8", true}, {"urdu.xml", "text/xml; charset=utf-8", true},
{"content-type-only-win-8859-1.xml", "application/xml; charset=ISO-8859-1", true}, {"content-type-only-win-8859-1.xml", "application/xml; charset=ISO-8859-1", true},
{"rdf_utf8.xml", "application/rss+xml; charset=utf-8", true}, {"rdf_utf8.xml", "application/rss+xml; charset=utf-8", true},
{"rdf_utf8.xml", "application/rss+xml; charset: utf-8", true}, // Invalid Content-Type
{"charset-content-type-xml-iso88591.xml", "application/rss+xml; charset=ISO-8859-1", false}, {"charset-content-type-xml-iso88591.xml", "application/rss+xml; charset=ISO-8859-1", false},
{"windows_1251.xml", "text/xml", false}, {"windows_1251.xml", "text/xml", false},
{"smallfile.xml", "text/xml; charset=utf-8", true}, {"smallfile.xml", "text/xml; charset=utf-8", true},
@ -136,7 +137,7 @@ func TestEnsureUnicodeWithHTMLDocuments(t *testing.T) {
r := &Response{Body: bytes.NewReader(content), ContentType: tc.contentType} r := &Response{Body: bytes.NewReader(content), ContentType: tc.contentType}
parseErr := r.EnsureUnicodeBody() parseErr := r.EnsureUnicodeBody()
if parseErr != nil { if parseErr != nil {
t.Fatalf(`Unicode conversion error for %q - %q: %v`, tc.filename, tc.contentType, err) t.Fatalf(`Unicode conversion error for %q - %q: %v`, tc.filename, tc.contentType, parseErr)
} }
isUnicode := utf8.ValidString(r.String()) isUnicode := utf8.ValidString(r.String())