]> Cypherpunks repositories - gostls13.git/commitdiff
mime: make FormatMediaType take full type for consistency
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 17 Jan 2012 19:57:42 +0000 (11:57 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 17 Jan 2012 19:57:42 +0000 (11:57 -0800)
Fixes #2405

R=rsc
CC=golang-dev
https://golang.org/cl/5539048

src/pkg/mime/mediatype.go
src/pkg/mime/mediatype_test.go
src/pkg/mime/type.go

index 2bf79788c74972e4fe6fa1938aa87e2d26803bdd..41844c25f2af595f8e5b419939089734517c95e9 100644 (file)
@@ -12,17 +12,22 @@ import (
        "unicode"
 )
 
-// FormatMediaType serializes type t, subtype sub and the paramaters
-// param as a media type conform RFC 2045 and RFC 2616.
-// The type, subtype, and parameter names are written in lower-case.
+// FormatMediaType serializes mediatype t and the parameters
+// param as a media type conforming to RFC 2045 and RFC 2616.
+// The type and parameter names are written in lower-case.
 // When any of the arguments result in a standard violation then
 // FormatMediaType returns the empty string.
-func FormatMediaType(t, sub string, param map[string]string) string {
-       if !(IsToken(t) && IsToken(sub)) {
+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(t))
+       b.WriteString(strings.ToLower(major))
        b.WriteByte('/')
        b.WriteString(strings.ToLower(sub))
 
index c06f167ddc93cc2814405a6e65603f19e15de018..64ab29134157217699ffd1a2a217045e04c1803c 100644 (file)
@@ -253,3 +253,24 @@ func TestParseMediaTypeBogus(t *testing.T) {
                t.Errorf("expected invalid media parameter; got error %q", err)
        }
 }
+
+type formatTest struct {
+       typ    string
+       params map[string]string
+       want   string
+}
+
+var formatTests = []formatTest{
+       {"noslash", nil, ""},
+       {"foo/BAR", nil, "foo/bar"},
+       {"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"},
+}
+
+func TestFormatMediaType(t *testing.T) {
+       for i, tt := range formatTests {
+               got := FormatMediaType(tt.typ, tt.params)
+               if got != tt.want {
+                       t.Errorf("%d. FormatMediaType(%q, %v) = %q; want %q", i, tt.typ, tt.params, got, tt.want)
+               }
+       }
+}
index e3d968fb81a15b882c1b1dcbd182488595686e41..00cff263bad95646190c43d735584526d6e610d7 100644 (file)
@@ -62,21 +62,14 @@ func AddExtensionType(ext, typ string) error {
 }
 
 func setExtensionType(extension, mimeType string) error {
-       full, param, err := ParseMediaType(mimeType)
+       _, param, err := ParseMediaType(mimeType)
        if err != nil {
                return err
        }
-       if split := strings.Index(full, "/"); split < 0 {
-               return fmt.Errorf(`mime: malformed MIME type "%s"`, mimeType)
-       } else {
-               main := full[:split]
-               sub := full[split+1:]
-               if main == "text" && param["charset"] == "" {
-                       param["charset"] = "utf-8"
-               }
-               mimeType = FormatMediaType(main, sub, param)
+       if strings.HasPrefix(mimeType, "text/") && param["charset"] == "" {
+               param["charset"] = "utf-8"
+               mimeType = FormatMediaType(mimeType, param)
        }
-
        mimeLock.Lock()
        mimeTypes[extension] = mimeType
        mimeLock.Unlock()