Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions internal/httputil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ func ContentType(subtype string) string {
return baseContentType + "/" + subtype
}

// ContentSubtype returns the content-subtype for the given content-type. The
// given content-type must be a valid content-type that starts with
// but no content-subtype will be returned.
// according rfc7231.
// contentType is assumed to be lowercase already.
// ContentSubtype extracts and returns the content subtype from a given Content-Type string.
// The input is expected to be lowercase, following the conventions of RFC 7231.
// It handles formats like "type/subtype" or "type/subtype; key=value".
// If the input is not well-formed, an empty string is returned.
func ContentSubtype(contentType string) string {
left := strings.Index(contentType, "/")
switch contentType {
case "":
return ""
case "application/json":
return "json"
}

left := strings.IndexByte(contentType, '/')
if left == -1 {
return ""
}
right := strings.Index(contentType, ";")
right := strings.IndexByte(contentType, ';')
if right == -1 {
right = len(contentType)
}
Expand Down
Loading