diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-01-25 21:54:22 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-01-25 21:54:22 +0000 |
commit | af92e385667da3fc91ac7f9f0867a56c111110b8 (patch) | |
tree | c8e8990a2197e33f6fe50a28a16714aafe982102 /libgo/go/mime | |
parent | df1304ee03f41aed179545d1e8b4684cfd22bbdf (diff) | |
download | gcc-af92e385667da3fc91ac7f9f0867a56c111110b8.zip gcc-af92e385667da3fc91ac7f9f0867a56c111110b8.tar.gz gcc-af92e385667da3fc91ac7f9f0867a56c111110b8.tar.bz2 |
libgo: Update to weekly.2012-01-20.
From-SVN: r183540
Diffstat (limited to 'libgo/go/mime')
-rw-r--r-- | libgo/go/mime/mediatype.go | 17 | ||||
-rw-r--r-- | libgo/go/mime/mediatype_test.go | 21 | ||||
-rw-r--r-- | libgo/go/mime/type.go | 15 |
3 files changed, 36 insertions, 17 deletions
diff --git a/libgo/go/mime/mediatype.go b/libgo/go/mime/mediatype.go index 2bf7978..41844c2 100644 --- a/libgo/go/mime/mediatype.go +++ b/libgo/go/mime/mediatype.go @@ -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)) diff --git a/libgo/go/mime/mediatype_test.go b/libgo/go/mime/mediatype_test.go index c06f167..64ab291 100644 --- a/libgo/go/mime/mediatype_test.go +++ b/libgo/go/mime/mediatype_test.go @@ -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) + } + } +} diff --git a/libgo/go/mime/type.go b/libgo/go/mime/type.go index e3d968f..00cff26 100644 --- a/libgo/go/mime/type.go +++ b/libgo/go/mime/type.go @@ -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() |