Skip to content

Commit

Permalink
Improve mime type parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Oct 3, 2023
1 parent 1f54200 commit 3232f2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
28 changes: 14 additions & 14 deletions formats/dsd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func DumpToHTTPResponse(w http.ResponseWriter, r *http.Request, t interface{}) e
// MimeLoad loads the given data into the interface based on the given mime type.
func MimeLoad(data []byte, mimeType string, t interface{}) (format uint8, err error) {
// Find format.
mimeType = cleanMimeType(mimeType)
format = MimeTypeToFormat[mimeType]
format = FormatFromMime(mimeType)
if format == 0 {
return 0, ErrIncompatibleFormat
}
Expand All @@ -112,9 +111,9 @@ func MimeLoad(data []byte, mimeType string, t interface{}) (format uint8, err er
// MimeDump dumps the given interface based on the given mime type accept header.
func MimeDump(t any, accept string) (data []byte, mimeType string, format uint8, err error) {
// Find format.
accept = cleanMimeType(accept)
accept = extractMimeType(accept)
switch accept {
case "", "*", "*/*":
case "", "*":
format = DefaultSerializationFormat
default:
format = MimeTypeToFormat[accept]
Expand All @@ -132,17 +131,20 @@ func MimeDump(t any, accept string) (data []byte, mimeType string, format uint8,
// FormatFromMime returns the format for the given mime type.
// Will return AUTO format for unsupported or unrecognized mime types.
func FormatFromMime(mimeType string) (format uint8) {
return MimeTypeToFormat[cleanMimeType(mimeType)]
return MimeTypeToFormat[extractMimeType(mimeType)]
}

func cleanMimeType(mimeType string) string {
func extractMimeType(mimeType string) string {
if strings.Contains(mimeType, ",") {
mimeType, _, _ = strings.Cut(mimeType, ",")
}
if strings.Contains(mimeType, ";") {
mimeType, _, _ = strings.Cut(mimeType, ";")
}
return mimeType
if strings.Contains(mimeType, "/") {
_, mimeType, _ = strings.Cut(mimeType, "/")
}
return strings.ToLower(mimeType)
}

// Format and MimeType mappings.
Expand All @@ -154,12 +156,10 @@ var (
YAML: "application/yaml",
}
MimeTypeToFormat = map[string]uint8{
"application/cbor": CBOR,
"application/json": JSON,
"application/msgpack": MsgPack,
"application/yaml": YAML,
"text/json": JSON,
"text/yaml": YAML,
"text/yml": YAML,
"cbor": CBOR,
"json": JSON,
"msgpack": MsgPack,
"yaml": YAML,
"yml": YAML,
}
)
9 changes: 5 additions & 4 deletions formats/dsd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ func TestMimeTypes(t *testing.T) {

// Test assumptions.
for mimeType, mimeTypeCleaned := range map[string]string{
"application/xml, image/webp": "application/xml",
"application/xml;q=0.9, image/webp": "application/xml",
"application/xml, image/webp": "xml",
"application/xml;q=0.9, image/webp": "xml",
"*": "*",
"*/*": "*/*",
"*/*": "*",
"text/yAMl": "yaml",
} {
cleaned := cleanMimeType(mimeType)
cleaned := extractMimeType(mimeType)
assert.Equal(t, mimeTypeCleaned, cleaned, "assumption for %q should hold", mimeType)
}
}

0 comments on commit 3232f2d

Please sign in to comment.