]> Cypherpunks repositories - gostls13.git/commitdiff
mime: let FormatMediaType format slash-less media types, to mirror ParseMediaType.
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 30 Nov 2015 19:33:49 +0000 (19:33 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 1 Dec 2015 16:29:28 +0000 (16:29 +0000)
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>

src/mime/mediatype.go
src/mime/mediatype_test.go

index 00076048a1578b90bffeae6564e3f6dfdd730a86..6d4560a351b0375b843618366d69f247dd891576 100644 (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 {
index e72f95f0a0338fc2afccf9ae5a0e58b7880e6f08..d018adef2f3d29a3eac88e9c2b9779bad1a09363 100644 (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"},