-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_type.go
299 lines (248 loc) · 6.72 KB
/
media_type.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2020 Guoyao Wu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package negotiator
import (
"math"
"sort"
"strconv"
"strings"
"github.com/dlclark/regexp2"
)
var simpleMediaTypeRegExp = regexp2.MustCompile("^\\s*([^\\s\\/;]+)\\/([^;\\s]+)\\s*(?:;(.*))?$", regexp2.None)
type acceptMediaType struct {
mainType string
subtype string
params map[string]string
q float64
i int
}
type acceptMediaTypes []acceptMediaType
func (acs acceptMediaTypes) filter(f func(ac acceptMediaType) bool) acceptMediaTypes {
result := make(acceptMediaTypes, 0, len(acs))
for _, ac := range acs {
if f(ac) {
result = append(result, ac)
}
}
return result
}
func (acs acceptMediaTypes) toMediaTypes() []string {
result := make([]string, len(acs), len(acs))
for i, ac := range acs {
result[i] = ac.mainType + "/" + ac.subtype
}
return result
}
type acceptMediaTypeBy func(ac1, ac2 *acceptMediaType) bool
func (by acceptMediaTypeBy) sort(acs acceptMediaTypes) {
as := &acceptMediaTypeSorter{acs, by}
sort.Sort(as)
}
type acceptMediaTypeSorter struct {
acs acceptMediaTypes
by func(ac1, ac2 *acceptMediaType) bool
}
func (s *acceptMediaTypeSorter) Len() int {
return len(s.acs)
}
func (s *acceptMediaTypeSorter) Swap(i, j int) {
s.acs[i], s.acs[j] = s.acs[j], s.acs[i]
}
func (s *acceptMediaTypeSorter) Less(i, j int) bool {
return s.by(&s.acs[i], &s.acs[j])
}
// PreferredMediaTypes gets the preferred media types from an Accept header.
// RFC 2616 sec 14.2: no header = */*, so you should pass */* if no Accept field in header.
func PreferredMediaTypes(accept string, provided ...string) []string {
acs := parseAcceptMediaType(accept)
if len(provided) == 0 {
// sorted list of all media types
filteredAcs := acs.filter(isAcceptMediaTypeQuality)
acceptMediaTypeBy(func(ac1, ac2 *acceptMediaType) bool {
if ac1.q != ac2.q {
return ac1.q > ac2.q
}
return ac1.i < ac2.i
}).sort(filteredAcs)
return filteredAcs.toMediaTypes()
}
priorities := getMediaTypeSpecificities(provided, acs)
filteredPriorities := priorities.filter(isSpecificityQuality)
specificityBy(compareSpecs).sort(filteredPriorities)
results := make([]string, 0, len(filteredPriorities))
for _, v := range filteredPriorities {
i := priorities.indexOf(v)
if i >= 0 {
results = append(results, provided[i])
}
}
return results
}
// Parses the Accept header to slice with type acceptMediaType.
func parseAcceptMediaType(accept string) acceptMediaTypes {
accepts := splitMediaTypes(accept)
length := len(accepts)
results := make(acceptMediaTypes, 0, length)
for i := 0; i < length; i++ {
mediaType := parseMediaType(strings.Trim(accepts[i], " "), i)
if mediaType != nil {
results = append(results, *mediaType)
}
}
return results
}
// Parse a media type from the Accept header.
func parseMediaType(s string, i int) *acceptMediaType {
match, err := simpleMediaTypeRegExp.FindStringMatch(s)
if match == nil || match.GroupCount() == 0 || err != nil {
return nil
}
params := make(map[string]string)
mainType, subType, q := match.Groups()[1].String(), match.Groups()[2].String(), 1.0
if match.Groups()[3].String() != "" {
kvps := splitParameters(match.Groups()[3].String())
arr := make([][]string, len(kvps), len(kvps))
for i, v := range kvps {
arr[i] = splitKeyValuePair(v)
}
for j := 0; j < len(arr); j++ {
pair := arr[j]
key, val := strings.ToLower(pair[0]), pair[1]
if val != "" && val[0] == '"' && val[len(val)-1] == '"' {
val = val[1:int(math.Max(float64(len(val)-1), 1))]
}
if key == "q" {
q1, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil
}
q = q1
break
}
params[key] = val
}
}
return &acceptMediaType{mainType, subType, params, q, i}
}
// Get the priority of a media type.
func getMediaTypePriority(mediaType string, acs acceptMediaTypes, index int) specificity {
priority := specificity{o: -1, q: 0, s: 0}
for i := 0; i < len(acs); i++ {
spec := mediaTypeSpecify(mediaType, acs[i], index)
if spec != nil {
s, q, o := priority.s-spec.s, priority.q-spec.q, priority.o-spec.o
if s < 0 || q < 0 || o < 0 {
priority = *spec
}
}
}
return priority
}
// Get the specificity of the media type.
func mediaTypeSpecify(mediaType string, ac acceptMediaType, index int) *specificity {
p := parseMediaType(mediaType, index)
if p == nil {
return nil
}
s := 0
if strings.ToLower(ac.mainType) == strings.ToLower(p.mainType) {
s |= 4
} else if ac.mainType != "*" {
return nil
}
if strings.ToLower(ac.subtype) == strings.ToLower(p.subtype) {
s |= 2
} else if ac.subtype != "*" {
return nil
}
keys := getMapKeys(ac.params)
if len(keys) > 0 {
if every(keys, func(k string) bool {
return ac.params[k] == "*" || strings.ToLower(ac.params[k]) == strings.ToLower(p.params[k])
}) {
s |= 1
} else {
return nil
}
}
return &specificity{index, ac.i, ac.q, s}
}
func isAcceptMediaTypeQuality(ac acceptMediaType) bool {
return ac.q > 0
}
func getMediaTypeSpecificities(types []string, acs acceptMediaTypes) specificities {
result := make(specificities, len(types), len(types))
for i, v := range types {
result[i] = getMediaTypePriority(v, acs, i)
}
return result
}
// Count the number of quotes in a string.
func quoteCount(s string) int {
return strings.Count(s, "\"")
}
// Split a key value pair.
func splitKeyValuePair(s string) []string {
key, val, index := "", "", strings.Index(s, "=")
if index == -1 {
key = s
} else {
key, val = s[0:index], s[index+1:]
}
return []string{key, val}
}
// Split an Accept header into media types.
func splitMediaTypes(accept string) []string {
accepts := strings.Split(accept, ",")
length := len(accepts)
i, j := 1, 0
for ; i < length; i++ {
if quoteCount(accepts[j])%2 == 0 {
j++
accepts[j] = accepts[i]
} else {
accepts[j] += "," + accepts[i]
}
}
accepts = accepts[0 : j+1]
return accepts
}
// Split a string of parameters.
func splitParameters(str string) []string {
parameters := strings.Split(str, ";")
length := len(parameters)
i, j := 1, 0
for ; i < length; i++ {
if quoteCount(parameters[j])%2 == 0 {
j++
parameters[j] = parameters[i]
} else {
parameters[j] += ";" + parameters[i]
}
}
// trim parameters
parameters = parameters[0 : j+1]
length = len(parameters)
for i = 0; i < length; i++ {
parameters[i] = strings.Trim(parameters[i], " ")
}
return parameters
}
func getMapKeys(m map[string]string) []string {
i, length := 0, len(m)
keys := make([]string, length, length)
for k := range m {
keys[i] = k
i++
}
return keys
}
func every(arr []string, f func(s string) bool) bool {
for _, v := range arr {
if !f(v) {
return false
}
}
return true
}