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
|
package headers
import (
"fmt"
"sort"
"strconv"
"strings"
)
const (
AcceptKey = "Accept"
AcceptEncodingKey = "Accept-Encoding"
)
type AcceptableEncodings []AcceptableEncoding
type AcceptableEncoding struct {
Encoding ContentEncoding
Weight float32
pos int
}
func (as AcceptableEncodings) IsAcceptable(encoding ContentEncoding) bool {
for _, ae := range as {
if ae.Encoding == encoding {
return ae.Weight != 0
}
}
return false
}
func (as AcceptableEncodings) String() string {
if len(as) == 0 {
return ""
}
var b strings.Builder
for i, ae := range as {
b.WriteString(ae.Encoding.String())
if ae.Weight > 0.0 && ae.Weight < 1.0 {
b.WriteString(";q=" + strconv.FormatFloat(float64(ae.Weight), 'f', 1, 32))
}
if i < len(as)-1 {
b.WriteString(", ")
}
}
return b.String()
}
type AcceptableTypes []AcceptableType
func (as AcceptableTypes) IsAcceptable(contentType ContentType) bool {
for _, ae := range as {
if ae.Type == contentType {
return ae.Weight != 0
}
}
return false
}
type AcceptableType struct {
Type ContentType
Weight float32
Extension map[string]string
pos int
}
func (as AcceptableTypes) String() string {
if len(as) == 0 {
return ""
}
var b strings.Builder
for i, at := range as {
b.WriteString(at.Type.String())
if at.Weight > 0.0 && at.Weight < 1.0 {
b.WriteString(";q=" + strconv.FormatFloat(float64(at.Weight), 'f', 1, 32))
}
for k, v := range at.Extension {
b.WriteString(";" + k + "=" + v)
}
if i < len(as)-1 {
b.WriteString(", ")
}
}
return b.String()
}
// ParseAccept parses Accept HTTP header.
// It will sort acceptable types by weight, specificity and position.
// See: https://tools.ietf.org/html/rfc2616#section-14.1
func ParseAccept(headerValue string) (AcceptableTypes, error) {
if headerValue == "" {
return nil, nil
}
parsedValues, err := parseAcceptFamilyHeader(headerValue)
if err != nil {
return nil, err
}
ah := make(AcceptableTypes, 0, len(parsedValues))
for _, parsedValue := range parsedValues {
ah = append(ah, AcceptableType{
Type: ContentType(parsedValue.Value),
Weight: parsedValue.Weight,
Extension: parsedValue.Extension,
pos: parsedValue.pos,
})
}
sort.Slice(ah, func(i, j int) bool {
// sort by weight only
if ah[i].Weight != ah[j].Weight {
return ah[i].Weight > ah[j].Weight
}
// sort by most specific if types are equal
if ah[i].Type == ah[j].Type {
return len(ah[i].Extension) > len(ah[j].Extension)
}
// move counterpart up if one of types is ANY
if ah[i].Type == ContentTypeAny {
return false
}
if ah[j].Type == ContentTypeAny {
return true
}
// i type has j type as prefix
if strings.HasSuffix(string(ah[j].Type), "/*") &&
strings.HasPrefix(string(ah[i].Type), string(ah[j].Type)[:len(ah[j].Type)-1]) {
return true
}
// j type has i type as prefix
if strings.HasSuffix(string(ah[i].Type), "/*") &&
strings.HasPrefix(string(ah[j].Type), string(ah[i].Type)[:len(ah[i].Type)-1]) {
return false
}
// sort by position if nothing else left
return ah[i].pos < ah[j].pos
})
return ah, nil
}
// ParseAcceptEncoding parses Accept-Encoding HTTP header.
// It will sort acceptable encodings by weight and position.
// See: https://tools.ietf.org/html/rfc2616#section-14.3
func ParseAcceptEncoding(headerValue string) (AcceptableEncodings, error) {
if headerValue == "" {
return nil, nil
}
// e.g. gzip;q=1.0, compress, identity
parsedValues, err := parseAcceptFamilyHeader(headerValue)
if err != nil {
return nil, err
}
acceptableEncodings := make(AcceptableEncodings, 0, len(parsedValues))
for _, parsedValue := range parsedValues {
acceptableEncodings = append(acceptableEncodings, AcceptableEncoding{
Encoding: ContentEncoding(parsedValue.Value),
Weight: parsedValue.Weight,
pos: parsedValue.pos,
})
}
sort.Slice(acceptableEncodings, func(i, j int) bool {
// sort by weight only
if acceptableEncodings[i].Weight != acceptableEncodings[j].Weight {
return acceptableEncodings[i].Weight > acceptableEncodings[j].Weight
}
// move counterpart up if one of encodings is ANY
if acceptableEncodings[i].Encoding == EncodingAny {
return false
}
if acceptableEncodings[j].Encoding == EncodingAny {
return true
}
// sort by position if nothing else left
return acceptableEncodings[i].pos < acceptableEncodings[j].pos
})
return acceptableEncodings, nil
}
type acceptHeaderValue struct {
Value string
Weight float32
Extension map[string]string
pos int
}
// parseAcceptFamilyHeader parses family of Accept* HTTP headers
// See: https://tools.ietf.org/html/rfc2616#section-14.1
func parseAcceptFamilyHeader(header string) ([]acceptHeaderValue, error) {
headerValues := strings.Split(header, ",")
parsedValues := make([]acceptHeaderValue, 0, len(headerValues))
for i, headerValue := range headerValues {
valueParams := strings.Split(headerValue, ";")
parsedValue := acceptHeaderValue{
Value: strings.TrimSpace(valueParams[0]),
Weight: 1.0,
pos: i,
}
// parse quality factor and/or accept extension
if len(valueParams) > 1 {
for _, rawParam := range valueParams[1:] {
rawParam = strings.TrimSpace(rawParam)
params := strings.SplitN(rawParam, "=", 2)
key := strings.TrimSpace(params[0])
// quality factor
if key == "q" {
if len(params) != 2 {
return nil, fmt.Errorf("invalid quality factor format: %q", rawParam)
}
w, err := strconv.ParseFloat(params[1], 32)
if err != nil {
return nil, err
}
parsedValue.Weight = float32(w)
continue
}
// extension
if parsedValue.Extension == nil {
parsedValue.Extension = make(map[string]string)
}
var value string
if len(params) == 2 {
value = strings.TrimSpace(params[1])
}
parsedValue.Extension[key] = value
}
}
parsedValues = append(parsedValues, parsedValue)
}
return parsedValues, nil
}
|