-
Notifications
You must be signed in to change notification settings - Fork 0
/
charset.go
254 lines (208 loc) · 5.5 KB
/
charset.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
// 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 (
"sort"
"strconv"
"strings"
"github.com/dlclark/regexp2"
)
var simpleCharsetRegExp = regexp2.MustCompile("^\\s*([^\\s;]+)\\s*(?:;(.*))?$", regexp2.None)
type acceptCharset struct {
charset string
q float64
i int
}
type acceptCharsets []acceptCharset
func (acs acceptCharsets) filter(f func(ac acceptCharset) bool) acceptCharsets {
result := make(acceptCharsets, 0, len(acs))
for _, ac := range acs {
if f(ac) {
result = append(result, ac)
}
}
return result
}
func (acs acceptCharsets) toCharsets() []string {
result := make([]string, len(acs), len(acs))
for i, ac := range acs {
result[i] = ac.charset
}
return result
}
type acceptCharsetBy func(ac1, ac2 *acceptCharset) bool
func (by acceptCharsetBy) sort(acs acceptCharsets) {
as := &acceptCharsetSorter{acs, by}
sort.Sort(as)
}
type acceptCharsetSorter struct {
acs acceptCharsets
by func(ac1, ac2 *acceptCharset) bool
}
func (s *acceptCharsetSorter) Len() int {
return len(s.acs)
}
func (s *acceptCharsetSorter) Swap(i, j int) {
s.acs[i], s.acs[j] = s.acs[j], s.acs[i]
}
func (s *acceptCharsetSorter) Less(i, j int) bool {
return s.by(&s.acs[i], &s.acs[j])
}
type specificity struct {
i int
o int
q float64
s int
}
type specificities []specificity
func (ss specificities) filter(f func(s specificity) bool) specificities {
result := make(specificities, 0, len(ss))
for _, s := range ss {
if f(s) {
result = append(result, s)
}
}
return result
}
func (ss specificities) indexOf(s specificity) int {
index := -1
for i, v := range ss {
if v == s {
index = i
break
}
}
return index
}
type specificityBy func(s1, s2 *specificity) bool
func (by specificityBy) sort(specs specificities) {
ss := &specificitySorter{specs, by}
sort.Sort(ss)
}
type specificitySorter struct {
ss specificities
by func(s1, s2 *specificity) bool
}
func (s *specificitySorter) Len() int {
return len(s.ss)
}
func (s *specificitySorter) Swap(i, j int) {
s.ss[i], s.ss[j] = s.ss[j], s.ss[i]
}
func (s *specificitySorter) Less(i, j int) bool {
return s.by(&s.ss[i], &s.ss[j])
}
// PreferredCharsets gets the preferred charsets from an Accept-Charset header.
// RFC 2616 sec 14.2: no header = *, so you should pass * if no Accept-Charset field in header.
func PreferredCharsets(accept string, provided ...string) []string {
acs := parseAcceptCharset(accept)
if len(provided) == 0 {
// sorted list of all charsets
filteredAcs := acs.filter(isAcceptCharsetQuality)
acceptCharsetBy(func(ac1, ac2 *acceptCharset) bool {
if ac1.q != ac2.q {
return ac1.q > ac2.q
}
return ac1.i < ac2.i
}).sort(filteredAcs)
return filteredAcs.toCharsets()
}
// sorted list of accepted charsets
priorities := getCharsetSpecificities(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-Charset header to slice with type acceptCharset.
func parseAcceptCharset(accept string) acceptCharsets {
accepts := strings.Split(accept, ",")
length := len(accepts)
results := make(acceptCharsets, 0, length)
for i := 0; i < length; i++ {
charset := parseCharset(strings.Trim(accepts[i], " "), i)
if charset != nil {
results = append(results, *charset)
}
}
return results
}
// Parse a charset from the Accept-Charset header.
func parseCharset(s string, i int) *acceptCharset {
match, err := simpleCharsetRegExp.FindStringMatch(s)
if match == nil || match.GroupCount() == 0 || err != nil {
return nil
}
charset, q := match.Groups()[1].String(), 1.0
if match.Groups()[2].String() != "" {
params := strings.Split(match.Groups()[2].String(), ";")
for j := 0; j < len(params); j++ {
p := strings.Split(strings.Trim(params[j], " "), "=")
if p[0] == "q" {
q1, err := strconv.ParseFloat(p[1], 64)
if err != nil {
return nil
}
q = q1
break
}
}
}
return &acceptCharset{charset, q, i}
}
// Get the priority of a charset.
func getCharsetPriority(charset string, acs acceptCharsets, index int) specificity {
priority := specificity{o: -1, q: 0, s: 0}
for i := 0; i < len(acs); i++ {
spec := charsetSpecify(charset, 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 charset.
func charsetSpecify(charset string, ac acceptCharset, index int) *specificity {
s := 0
if strings.ToLower(ac.charset) == strings.ToLower(charset) {
s |= 1
} else if ac.charset != "*" {
return nil
}
return &specificity{index, ac.i, ac.q, s}
}
func compareSpecs(s1, s2 *specificity) bool {
if s1.q != s2.q {
return s1.q > s2.q
}
if s1.s != s2.s {
return s1.s > s2.s
}
if s1.o != s2.o {
return s1.o < s2.o
}
return s1.i < s2.i
}
func isAcceptCharsetQuality(ac acceptCharset) bool {
return ac.q > 0
}
func isSpecificityQuality(s specificity) bool {
return s.q > 0
}
func getCharsetSpecificities(types []string, acs acceptCharsets) specificities {
result := make(specificities, len(types), len(types))
for i, v := range types {
result[i] = getCharsetPriority(v, acs, i)
}
return result
}