diff --git a/src/mime/mediatype.go b/src/mime/mediatype.go index ad63f9bb98e9a4..00076048a1578b 100644 --- a/src/mime/mediatype.go +++ b/src/mime/mediatype.go @@ -237,24 +237,23 @@ func consumeToken(v string) (token, rest string) { // quoted-string) and the rest of the string. On failure, returns // ("", v). func consumeValue(v string) (value, rest string) { - if !strings.HasPrefix(v, `"`) && !strings.HasPrefix(v, `'`) { + if v == "" { + return + } + if v[0] != '"' { return consumeToken(v) } - leadQuote := rune(v[0]) - // parse a quoted-string rest = v[1:] // consume the leading quote buffer := new(bytes.Buffer) - var idx int - var r rune var nextIsLiteral bool - for idx, r = range rest { + for idx, r := range rest { switch { case nextIsLiteral: buffer.WriteRune(r) nextIsLiteral = false - case r == leadQuote: + case r == '"': return buffer.String(), rest[idx+1:] case r == '\\': nextIsLiteral = true diff --git a/src/mime/mediatype_test.go b/src/mime/mediatype_test.go index 026bfa4d734681..e72f95f0a0338f 100644 --- a/src/mime/mediatype_test.go +++ b/src/mime/mediatype_test.go @@ -159,7 +159,7 @@ func TestParseMediaType(t *testing.T) { m("filename", "foo.html")}, {`attachment; filename='foo.html'`, "attachment", - m("filename", "foo.html")}, + m("filename", "'foo.html'")}, {`attachment; filename="foo-%41.html"`, "attachment", m("filename", "foo-%41.html")}, @@ -294,6 +294,7 @@ var formatTests = []formatTest{ {"foo/BAR", map[string]string{"bad attribute": "baz"}, ""}, {"foo/BAR", map[string]string{"nonascii": "not an ascii character: รค"}, ""}, {"foo/bar", map[string]string{"a": "av", "b": "bv", "c": "cv"}, "foo/bar; a=av; b=bv; c=cv"}, + {"foo/bar", map[string]string{"0": "'", "9": "'"}, "foo/bar; 0='; 9='"}, } func TestFormatMediaType(t *testing.T) {