forked from thecodeteam/goisilon
/
api_ordered_values.go
347 lines (321 loc) · 7.54 KB
/
api_ordered_values.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package api
import (
"bytes"
"io"
"net/url"
"strings"
)
var chrs = []byte(`,=&+%`)
const (
chComa = iota
chEqul
chAmps
chPlus
chPrct
)
// OrderedValues maps a string key to a list of values and preserves insertion
// order of the keys. It is typically used for query parameters and form values.
// Unlike in the http.Header map, the keys in a Values map are case-sensitive.
type OrderedValues [][][]byte
// NewOrderedValues returns a new OrderedValues object.
func NewOrderedValues(vals [][]string) OrderedValues {
if len(vals) == 0 {
return nil
}
var nov OrderedValues
for i := range vals {
var a [][]byte
for j := range vals[i] {
a = append(a, []byte(vals[i][j]))
}
nov = append(nov, a)
}
return nov
}
// StringAdd adds the value to key. It appends to any existing values
// associated with key.
func (v *OrderedValues) StringAdd(key, val string) {
if len(key) == 0 {
return
}
if len(val) == 0 {
v.Add([]byte(key), chrs[0:0])
} else {
v.Add([]byte(key), []byte(val))
}
}
// Add adds the value to key. It appends to any existing values associated with
// key.
func (v *OrderedValues) Add(key, val []byte) {
for i, j := range *v {
if len(j) > 0 && bytes.Equal(j[0], key) {
if len(val) > 0 {
(*v)[i] = append((*v)[i], val)
}
return
}
}
if len(val) == 0 {
*v = append(*v, [][]byte{key})
} else {
*v = append(*v, [][]byte{key, val})
}
}
// StringSet sets the key to value. It replaces any existing values.
func (v *OrderedValues) StringSet(key, val string) {
if len(key) == 0 {
return
}
if len(val) == 0 {
v.Set([]byte(key), chrs[0:0])
} else {
v.Set([]byte(key), []byte(val))
}
}
// Set sets the key to value. It replaces any existing values.
func (v *OrderedValues) Set(key, val []byte) {
for i, j := range *v {
if len(j) > 0 && bytes.Equal(j[0], key) {
if len(val) == 0 {
(*v)[i] = [][]byte{j[0]}
} else {
(*v)[i] = [][]byte{j[0], val}
}
return
}
}
if len(val) == 0 {
*v = append(*v, [][]byte{key})
} else {
*v = append(*v, [][]byte{key, val})
}
}
// StringGet gets the first value associated with the given key. If there are no
// values associated with the key, Get returns the empty string. To access
// multiple values, use the array directly.
func (v *OrderedValues) StringGet(key string) string {
if len(key) == 0 {
return ""
}
return string(v.Get([]byte(key)))
}
// Get gets the first value associated with the given key. If there are no
// values associated with the key, Get returns the empty string. To access
// multiple values, use the array directly.
func (v *OrderedValues) Get(key []byte) []byte {
for _, j := range *v {
if len(j) > 0 && bytes.Equal(j[0], key) {
if len(j) > 1 {
return j[1]
}
return nil
}
}
return nil
}
// StringGetOk returns a flag indicating whether or not the key exists. The
// StringGet method can return an empty value for keys that do not have values,
// so it cannot be trusted to indicate the existence of a key.
func (v *OrderedValues) StringGetOk(key string) (string, bool) {
if len(key) == 0 {
return "", false
}
val, ok := v.GetOk([]byte(key))
if !ok {
return "", false
}
return string(val), true
}
// GetOk returns a flag indicating whether or not the key exists. The Get
// method can return an empty value for keys that do not have values, so it
// cannot be trusted to indicate the existence of a key.
func (v *OrderedValues) GetOk(key []byte) ([]byte, bool) {
for _, j := range *v {
if len(j) > 0 && bytes.Equal(j[0], key) {
if len(j) > 1 {
return j[1], true
}
return nil, true
}
}
return nil, false
}
// StringDel deletes the values associated with key.
func (v *OrderedValues) StringDel(key string) {
v.Del([]byte(key))
}
// Del deletes the values associated with key.
func (v *OrderedValues) Del(key []byte) {
var (
i int
ok bool
j [][]byte
)
for i, j = range *v {
if len(j) > 0 && bytes.Equal(j[0], key) {
ok = true
break
}
}
if !ok {
return
}
copy((*v)[i:], (*v)[i+1:])
(*v)[len(*v)-1] = nil
*v = (*v)[:len(*v)-1]
}
// Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux")
// using insertion order.
func (v *OrderedValues) Encode() string {
buf := &bytes.Buffer{}
v.EncodeTo(buf)
return buf.String()
}
// EncodeTo encodes the values into “URL encoded” form ("bar=baz&foo=quux")
// using insertion order.
func (v *OrderedValues) EncodeTo(w io.Writer) error {
first := true
for _, j := range *v {
if len(j) == 0 {
continue
}
if !first {
if _, err := w.Write(chrs[chAmps : chAmps+1]); err != nil {
return err
}
} else {
first = false
}
if _, err := w.Write(j[0]); err != nil {
return err
}
if len(j) == 1 {
continue
}
if _, err := w.Write(chrs[chEqul : chEqul+1]); err != nil {
return err
}
for e := 1; e < len(j); e++ {
if e > 1 {
if _, err := w.Write(chrs[chComa : chComa+1]); err != nil {
return err
}
}
if err := escapeTo(w, j[e]); err != nil {
return err
}
}
}
return nil
}
func (v *OrderedValues) String() string {
return v.Encode()
}
// ParseQuery parses the URL-encoded query string and returns an OrderedValues
// object. ParseQuery may return nil if no valid query parameters are found. The
// return error object is set to the first encountered decoding error, if any.
func ParseQuery(query string) (OrderedValues, error) {
ov := OrderedValues{}
for query != "" {
if err := parseQuery(&ov, &query); err != nil {
return nil, err
}
}
return ov, nil
}
func parseQuery(m *OrderedValues, query *string) error {
var (
err error
value string
key = *query
)
if i := strings.IndexAny(key, "&;"); i >= 0 {
key, *query = key[:i], key[i+1:]
} else {
*query = ""
}
if key == "" {
return nil
}
if i := strings.Index(key, "="); i >= 0 {
key, value = key[:i], key[i+1:]
}
if key, err = url.QueryUnescape(key); err != nil {
return err
}
if value, err = url.QueryUnescape(value); err != nil {
return err
}
(*m).StringAdd(key, value)
return nil
}
var hexChars = []byte{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
}
func escapeTo(w io.Writer, s []byte) error {
var (
hexCount = 0
spaceCount = 0
)
for i := 0; i < len(s); i++ {
c := s[i]
if shouldEscape(c) {
if c == ' ' {
spaceCount++
} else {
hexCount++
}
}
}
if spaceCount == 0 && hexCount == 0 {
if _, err := w.Write(s); err != nil {
return err
}
return nil
}
for i := 0; i < len(s); i++ {
switch c := s[i]; {
case c == ' ':
if _, err := w.Write(chrs[chPlus : chPlus+1]); err != nil {
return err
}
case shouldEscape(c):
if _, err := w.Write(chrs[chPrct : chPrct+1]); err != nil {
return err
}
c4, c15 := c>>4, c&15
if _, err := w.Write(hexChars[c4 : c4+1]); err != nil {
return err
}
if _, err := w.Write(hexChars[c15 : c15+1]); err != nil {
return err
}
default:
if _, err := w.Write(s[i : i+1]); err != nil {
return err
}
}
}
return nil
}
// shouldEscape returns true if the specified character should be escaped when
// appearing in a URL string, according to RFC 3986.
//
// Please be informed that for now shouldEscape does not check all
// reserved characters correctly. See golang.org/issue/5684.
func shouldEscape(c byte) bool {
// §2.3 Unreserved characters (alphanum)
if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' {
return false
}
switch c {
case '-', '_', '.', '~':
// §2.3 Unreserved characters (mark)
return false
case '$', '&', '+', ',', '/', ':', ';', '=', '?', '@':
return true
}
// Everything else must be escaped.
return true
}