Commit 5ff309f4 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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/17135Reviewed-by: default avatarRuss Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a3f99dc4
...@@ -19,18 +19,21 @@ import ( ...@@ -19,18 +19,21 @@ import (
// When any of the arguments result in a standard violation then // When any of the arguments result in a standard violation then
// FormatMediaType returns the empty string. // FormatMediaType returns the empty string.
func FormatMediaType(t string, param map[string]string) 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 var b bytes.Buffer
b.WriteString(strings.ToLower(major)) if slash := strings.Index(t, "/"); slash == -1 {
b.WriteByte('/') if !isToken(t) {
b.WriteString(strings.ToLower(sub)) 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)) attrs := make([]string, 0, len(param))
for a := range param { for a := range param {
......
...@@ -281,7 +281,7 @@ type formatTest struct { ...@@ -281,7 +281,7 @@ type formatTest struct {
} }
var formatTests = []formatTest{ 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 baz", nil, ""}, {"foo/bar baz", nil, ""},
{"foo/BAR", nil, "foo/bar"}, {"foo/BAR", nil, "foo/bar"},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment