Skip to content

Commit

Permalink
mime: let FormatMediaType format slash-less media types, to mirror Pa…
Browse files Browse the repository at this point in the history
…rseMediaType.

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>
  • Loading branch information
bradfitz committed Dec 1, 2015
1 parent a3f99dc commit 5ff309f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/mime/mediatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/mime/mediatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 5ff309f

Please sign in to comment.