encoding/xml: case-insensitive encoding recognition

From the XML spec: "XML processors should match character encoding
names in a case-insensitive way"

Fixes #12417.

Change-Id: I678c50152a49c14364be62b3f21ab9b9b009b24b
Reviewed-on: https://go-review.googlesource.com/14084
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Giulio Iotti 2015-08-31 19:08:49 +03:00 committed by Russ Cox
parent 97c859f8da
commit 0b55be1ba2
2 changed files with 33 additions and 1 deletions

View File

@ -583,7 +583,7 @@ func (d *Decoder) rawToken() (Token, error) {
return nil, d.err
}
enc := procInst("encoding", content)
if enc != "" && enc != "utf-8" && enc != "UTF-8" {
if enc != "" && enc != "utf-8" && enc != "UTF-8" && !strings.EqualFold(enc, "utf-8") {
if d.CharsetReader == nil {
d.err = fmt.Errorf("xml: encoding %q declared but Decoder.CharsetReader is nil", enc)
return nil, d.err

View File

@ -771,3 +771,35 @@ func TestIssue11405(t *testing.T) {
}
}
}
func TestIssue12417(t *testing.T) {
testCases := []struct {
s string
ok bool
}{
{`<?xml encoding="UtF-8" version="1.0"?><root/>`, true},
{`<?xml encoding="UTF-8" version="1.0"?><root/>`, true},
{`<?xml encoding="utf-8" version="1.0"?><root/>`, true},
{`<?xml encoding="uuu-9" version="1.0"?><root/>`, false},
}
for _, tc := range testCases {
d := NewDecoder(strings.NewReader(tc.s))
var err error
for {
_, err = d.Token()
if err != nil {
if err == io.EOF {
err = nil
}
break
}
}
if err != nil && tc.ok {
t.Errorf("%q: Encoding charset: expected no error, got %s", tc.s, err)
continue
}
if err == nil && !tc.ok {
t.Errorf("%q: Encoding charset: expected error, got nil", tc.s)
}
}
}