-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
39 lines (31 loc) · 891 Bytes
/
parser.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
package mimeheader
import (
"mime"
"strings"
)
// Error messages.
const (
MimeParseErrMsg = "error in a parse media type"
MimeTypePartsErrMsg = "wrong number of mime type parts"
MimeTypeWildcardErrMsg = "mimetype cannot be as */plain"
)
// ParseMediaType parses media type to MimeType structure.
func ParseMediaType(mtype string) (MimeType, error) {
mtype, params, err := mime.ParseMediaType(mtype)
if err != nil {
return MimeType{}, MimeParseErr{Err: err, Msg: MimeParseErrMsg}
}
mtypes := strings.SplitN(mtype, MimeSeparator, MimeParts)
if len(mtypes) != MimeParts {
return MimeType{}, MimeTypePartsErr{Msg: MimeTypePartsErrMsg}
}
if mtypes[0] == MimeAny && mtypes[1] != MimeAny {
return MimeType{}, MimeTypeWildcardErr{Msg: MimeTypeWildcardErrMsg}
}
mt := MimeType{
Type: mtypes[0],
Subtype: mtypes[1],
Params: params,
}
return mt, nil
}