-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia_type.go
More file actions
80 lines (70 loc) · 2.13 KB
/
Copy pathmedia_type.go
File metadata and controls
80 lines (70 loc) · 2.13 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package mediaType
import (
"errors"
"regexp"
"strings"
)
/**
* RegExp to match type in RFC 6838
*
* type-name = restricted-name
* subtype-name = restricted-name
* restricted-name = restricted-name-first *126restricted-name-chars
* restricted-name-first = ALPHA / DIGIT
* restricted-name-chars = ALPHA / DIGIT / "!" / "#" /
* "$" / "&" / "-" / "^" / "_"
* restricted-name-chars =/ "." ; Characters before first dot always
* ; specify a facet name
* restricted-name-chars =/ "+" ; Characters after last plus always
* ; specify a structured syntax suffix
* ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
* DIGIT = %x30-39 ; 0-9
*/
var (
subtypeNameRegexp = regexp.MustCompile(`[A-Za-z0-9][A-Za-z0-9!#$&^_.-]{0,126}$`)
typeNameRegexp = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126}$`)
nameRegexp = regexp.MustCompile(`^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})\/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$`)
)
// MediaType mediaType object.
type MediaType struct {
Type string
SubType string
Suffix string
Parameters map[string]string
}
// Format format object to media type.
func (mt *MediaType) Format() (string, error) {
if mt.Type == "" || !typeNameRegexp.MatchString(mt.Type) {
return "", errors.New("invalid type")
}
if mt.SubType == "" || !subtypeNameRegexp.MatchString(mt.SubType) {
return "", errors.New("invalid subtype")
}
str := mt.Type + "/" + mt.SubType
if mt.Suffix != "" {
if !typeNameRegexp.MatchString(mt.Suffix) {
return "", errors.New("invliad suffix")
}
str += "+" + mt.Suffix
}
return str, nil
}
// Parse parse media type to object.
func Parse(str string) (*MediaType, error) {
if str == "" {
return nil, errors.New("invliad parse string")
}
match := nameRegexp.FindStringSubmatch(str)
if len(match) == 0 || len(match) < 3 {
return nil, errors.New("invlaid media type")
}
mt := &MediaType{}
mt.Type = match[1]
mt.SubType = match[2]
index := strings.LastIndex(mt.SubType, "+")
if index != -1 {
mt.Suffix = mt.SubType[index+1:]
mt.SubType = mt.SubType[0:index]
}
return mt, nil
}