-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccept_header.go
168 lines (130 loc) · 3.94 KB
/
accept_header.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package mimeheader
import "sort"
type MimeHeader struct {
MimeType
Quality float32
}
type AcceptHeader struct {
MHeaders []MimeHeader
}
func NewAcceptHeaderPlain(mheaders []MimeHeader) AcceptHeader {
return AcceptHeader{MHeaders: mheaders}
}
func NewAcceptHeader(mheaders []MimeHeader) AcceptHeader {
ah := AcceptHeader{MHeaders: mheaders}
ah.sort()
return ah
}
// Len function for sort.Interface interface.
func (ah AcceptHeader) Len() int {
return len(ah.MHeaders)
}
// Less function for sort.Interface interface.
func (ah AcceptHeader) Less(i, j int) bool {
// Sort accepts by quality value. If quality is not equal, then return is i less than j
if ah.MHeaders[i].Quality != ah.MHeaders[j].Quality {
return ah.MHeaders[i].Quality < ah.MHeaders[j].Quality
}
less, done := ah.lessWildcard(i, j)
if done {
return less
}
return ah.lessParams(i, j)
}
func (ah AcceptHeader) lessParams(i, j int) bool {
li := len(ah.MHeaders[i].Params)
_, ok := ah.MHeaders[i].Params["q"]
if ok {
li--
}
lj := len(ah.MHeaders[j].Params)
_, ok = ah.MHeaders[j].Params["q"]
if ok {
lj--
}
return li < lj
}
func (ah AcceptHeader) lessWildcard(i, j int) (less, done bool) {
// '*' value has less priority than a specific type
// If i contains '*' and j has specific type, then i less than j
if ah.MHeaders[i].Type == MimeAny && ah.MHeaders[j].Type != MimeAny {
return true, true
}
// If i contains a specific type and j contains '*' then i greater than j
if ah.MHeaders[i].Type != MimeAny && ah.MHeaders[j].Type == MimeAny {
return false, true
}
// '*' value has less priority than a specific type
// If i contains '*' and j has specific type, then i less than j
if ah.MHeaders[i].Subtype == MimeAny && ah.MHeaders[j].Subtype != MimeAny {
return true, true
}
// If i contains a specific type and j contains '*' then i greater than j
if ah.MHeaders[i].Subtype != MimeAny && ah.MHeaders[j].Subtype == MimeAny {
return false, true
}
return false, false
}
// Swap function for sort.Interface interface.
func (ah *AcceptHeader) Swap(i, j int) {
ah.MHeaders[i], ah.MHeaders[j] = ah.MHeaders[j], ah.MHeaders[i]
}
// Add mime header to accept header.
// MimeHeader will be validated and added ONLY if valid.
// AcceptHeader will be sorted.
// For performance reasons better to use Set, instead of Add.
func (ah *AcceptHeader) Add(mh MimeHeader) {
if !mh.Valid() {
return
}
ah.MHeaders = append(ah.MHeaders, mh)
ah.sort()
}
// Set all valid headers to AcceprHeader (override old ones).
// Sorting will be applied.
func (ah *AcceptHeader) Set(mhs []MimeHeader) {
mheaders := make([]MimeHeader, 0, len(mhs))
for _, mh := range mhs {
if mh.Valid() {
mheaders = append(mheaders, mh)
}
}
ah.MHeaders = mheaders
ah.sort()
}
// Negotiate return appropriate type fot current accept list from supported (common) mime types.
// First parameter returns matched value from accept header.
// Second parameter returns matched common type.
// Third parameter returns matched common type or default type applied.
func (ah AcceptHeader) Negotiate(ctypes []string, dtype string) (accept MimeHeader, mimeType string, matched bool) {
if len(ctypes) == 0 || len(ah.MHeaders) == 0 {
return MimeHeader{}, dtype, false
}
var parsedCType MimeType
mhid := -1
for _, ctype := range ctypes {
for hid, header := range ah.MHeaders {
mtype, err := ParseMediaType(ctype)
if err != nil {
continue
}
if header.Match(mtype) && (mhid > hid || mhid < 0) {
parsedCType = mtype
mhid = hid
}
}
}
if mhid >= 0 {
return ah.MHeaders[mhid], parsedCType.String(), true
}
return MimeHeader{}, dtype, false
}
// Match is the same function as AcceptHeader.Negotiate.
// It implements simplified interface to match only one type and return only matched or not information.
func (ah AcceptHeader) Match(mtype string) bool {
_, _, matched := ah.Negotiate([]string{mtype}, "")
return matched
}
func (ah *AcceptHeader) sort() {
sort.Sort(sort.Reverse(ah))
}