2018-01-20 07:42:55 +01:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package encoding // import "miniflux.app/reader/encoding"
|
2018-01-20 07:42:55 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2018-10-30 07:00:03 +01:00
|
|
|
"io/ioutil"
|
2018-01-20 07:42:55 +01:00
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"golang.org/x/net/html/charset"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CharsetReader is used when the XML encoding is specified for the input document.
|
|
|
|
//
|
|
|
|
// The document is converted in UTF-8 only if a different encoding is specified
|
|
|
|
// and the document is not already UTF-8.
|
|
|
|
//
|
|
|
|
// Several edge cases could exists:
|
|
|
|
//
|
2018-10-30 07:00:03 +01:00
|
|
|
// - Feeds with encoding specified only in Content-Type header and not in XML document
|
|
|
|
// - Feeds with encoding specified in both places
|
|
|
|
// - Feeds with encoding specified only in XML document and not in HTTP header
|
|
|
|
// - Feeds with wrong encoding defined and already in UTF-8
|
2018-01-20 07:42:55 +01:00
|
|
|
func CharsetReader(label string, input io.Reader) (io.Reader, error) {
|
2018-10-30 07:00:03 +01:00
|
|
|
buffer, _ := ioutil.ReadAll(input)
|
|
|
|
r := bytes.NewReader(buffer)
|
2018-01-20 07:42:55 +01:00
|
|
|
|
2018-10-30 07:00:03 +01:00
|
|
|
// The document is already UTF-8, do not do anything (avoid double-encoding).
|
|
|
|
// That means the specified encoding in XML prolog is wrong.
|
|
|
|
if utf8.Valid(buffer) {
|
|
|
|
return r, nil
|
2018-01-20 07:42:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 07:00:03 +01:00
|
|
|
// Transform document to UTF-8 from the specified encoding in XML prolog.
|
|
|
|
return charset.NewReaderLabel(label, r)
|
2018-01-20 07:42:55 +01:00
|
|
|
}
|