-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
84 lines (73 loc) · 2.19 KB
/
Copy pathquery.go
File metadata and controls
84 lines (73 loc) · 2.19 KB
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
package routeit
import (
"net/url"
"strings"
)
type queryParameters map[string][]string
type QueryParams struct {
q queryParameters
}
func newQueryParams() *QueryParams {
return &QueryParams{q: queryParameters{}}
}
// Access a query parameter if present. This returns a slice that may contain
// multiple elements, some of which may be empty (e.g. if the client sends
// `?foo=`).
func (q *QueryParams) All(key string) ([]string, bool) {
val, found := q.q[key]
return val, found
}
// Access a query parameter, asserting that it is only present exactly once in
// the request URI. This will return false if the query parameter is not
// present, and a 400: Bad Request error if the query parameter is present more
// than once.
func (q *QueryParams) Only(key string) (string, bool, error) {
val, found := q.All(key)
if !found {
return "", false, nil
}
if len(val) != 1 {
return "", true, ErrBadRequest().WithMessagef("Query parameter %#q should only be present once", key)
}
return val[0], true, nil
}
// Access the first query parameter for the given key, if present
func (q *QueryParams) First(key string) (string, bool) {
val, found := q.All(key)
if !found || len(val) == 0 {
return "", false
}
return val[0], true
}
// Access the last query parameter for the given key, if present
func (q *QueryParams) Last(key string) (string, bool) {
val, found := q.All(key)
length := len(val)
if !found || length == 0 {
return "", false
}
return val[length-1], true
}
func parseQueryParams(rawQuery string, params *QueryParams) *HttpError {
if strings.Contains(rawQuery, "?") {
// There should only be 1 `?`, which we have stripped off. Any `?` that
// feature as part of the query string should be URL encoded.
return ErrBadRequest()
}
queryParams := params.q
for query := range strings.SplitSeq(rawQuery, "&") {
// Most servers interpret the query component "?foo=" or "?foo" to mean
// that the value of "foo" is "".
key, rest, _ := strings.Cut(query, "=")
key, err := url.QueryUnescape(key)
if err != nil {
return ErrBadRequest()
}
val, err := url.QueryUnescape(rest)
if err != nil {
return ErrBadRequest()
}
queryParams[key] = append(queryParams[key], val)
}
return nil
}