-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcontenttype.go
48 lines (41 loc) · 1.55 KB
/
contenttype.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2021-2024 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package restful
import (
"net/http"
"strings"
)
// ContentType strings
const (
AcceptHeader = "Accept"
ContentTypeHeader = "Content-type"
ContentTypeAny = "*/*"
ContentTypeApplicationAny = "application/*"
ContentTypeApplicationJSON = "application/json"
ContentTypeProblemJSON = "application/problem+json" // RFC 7807
ContentTypePatchJSON = "application/json-patch+json" // RFC 6902
ContentTypeMergeJSON = "application/merge-patch+json" // RFC 7386
ContentTypeForm = "application/x-www-form-urlencoded"
ContentTypeMsgPack = "application/msgpack"
ContentTypeMultipartForm = "multipart/form-data"
)
// BaseContentType returns the MIME type of the Content-Type header as lower-case string
// E.g.: "application/JSON; charset=ISO-8859-1" --> "application/json"
func BaseContentType(contentType string) string {
return strings.ToLower(strings.TrimSpace(strings.Split(contentType, ";")[0]))
}
// GetBaseContentType returns base content type from HTTP header.
// E.g.: "Content-Type: application/JSON; charset=ISO-8859-1" --> "application/json"
func GetBaseContentType(headers http.Header) string {
return BaseContentType(headers.Get(ContentTypeHeader))
}
func isMsgPackContentType(ct string) bool {
if len(ct) < len(ContentTypeMsgPack) {
return false
}
return ct[11:19] == "/msgpack"
}
func isJSONContentType(baseCT string) bool {
return strings.HasSuffix(baseCT, "json")
}