Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
a00de45b
Commit
a00de45b
authored
Jan 17, 2012
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mime: make FormatMediaType take full type for consistency
Fixes #2405 R=rsc CC=golang-dev
https://golang.org/cl/5539048
parent
45d73974
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
17 deletions
+36
-17
mediatype.go
src/pkg/mime/mediatype.go
+11
-6
mediatype_test.go
src/pkg/mime/mediatype_test.go
+21
-0
type.go
src/pkg/mime/type.go
+4
-11
No files found.
src/pkg/mime/mediatype.go
View file @
a00de45b
...
...
@@ -12,17 +12,22 @@ import (
"unicode"
)
// FormatMediaType serializes
type t, subtype sub and the parama
ters
// 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 parame
ters
// param as a media type conform
ing 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
))
...
...
src/pkg/mime/mediatype_test.go
View file @
a00de45b
...
...
@@ -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
)
}
}
}
src/pkg/mime/type.go
View file @
a00de45b
...
...
@@ -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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment