1
0
mirror of https://github.com/golang/go synced 2024-09-30 00:04:28 -06:00

mime: let FormatMediaType format slash-less media types, to mirror ParseMediaType.

A Content-Type always has a slash (type/subtype)
A Content-Disposition does not (e.g. "attachment" or "line").
A "media type" is either one of those, plus optional parameters afterwards.

Our ParseMediaType and FormatMediaType weren't consistent in whether
they permitted Content-Dispositions. Now they both do.

Fixes #11289

Change-Id: Ia75723c9d7adb7f4de0f65482780f823cdadb5bd
Reviewed-on: https://go-review.googlesource.com/17135
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-11-30 19:33:49 +00:00
parent a3f99dc4e9
commit 5ff309f48d
2 changed files with 15 additions and 12 deletions

View File

@ -19,18 +19,21 @@ import (
// When any of the arguments result in a standard violation then
// FormatMediaType returns the empty string.
func FormatMediaType(t string, param map[string]string) string {
slash := strings.Index(t, "/")
if slash == -1 {
return ""
}
major, sub := t[:slash], t[slash+1:]
if !isToken(major) || !isToken(sub) {
return ""
}
var b bytes.Buffer
b.WriteString(strings.ToLower(major))
b.WriteByte('/')
b.WriteString(strings.ToLower(sub))
if slash := strings.Index(t, "/"); slash == -1 {
if !isToken(t) {
return ""
}
b.WriteString(strings.ToLower(t))
} else {
major, sub := t[:slash], t[slash+1:]
if !isToken(major) || !isToken(sub) {
return ""
}
b.WriteString(strings.ToLower(major))
b.WriteByte('/')
b.WriteString(strings.ToLower(sub))
}
attrs := make([]string, 0, len(param))
for a := range param {

View File

@ -281,7 +281,7 @@ type formatTest struct {
}
var formatTests = []formatTest{
{"noslash", nil, ""},
{"noslash", map[string]string{"X": "Y"}, "noslash; x=Y"}, // e.g. Content-Disposition values (RFC 2183); issue 11289
{"foo bar/baz", nil, ""},
{"foo/bar baz", nil, ""},
{"foo/BAR", nil, "foo/bar"},