-
Notifications
You must be signed in to change notification settings - Fork 14
/
matchers.go
280 lines (230 loc) · 6.3 KB
/
matchers.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
// Copyright 2012 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package reverse
import (
"net/http"
"net/url"
"strings"
)
// Result stores the results from a match.
type Result struct {
Handler http.Handler
Values url.Values
}
// Matcher matches a request.
type Matcher interface {
Match(*http.Request) bool
}
// Extractor extracts variables from a request.
type Extractor interface {
Extract(*Result, *http.Request)
}
// Builder builds a URL based on positional and/or named variables.
type Builder interface {
Build(*url.URL, url.Values) error
}
// Func -----------------------------------------------------------------------
// Func is a function signature for custom matchers.
type Func func(*http.Request) bool
func (m Func) Match(r *http.Request) bool {
return m(r)
}
// Header ---------------------------------------------------------------------
// NewHeader returns a header matcher, converting keys to the canonical form.
func NewHeader(m map[string]string) Header {
for k, v := range m {
delete(m, k)
m[http.CanonicalHeaderKey(k)] = v
}
return Header(m)
}
// Header matches request headers. All values, if non-empty, must match.
// Empty values only check if the header is present.
type Header map[string]string
func (m Header) Match(r *http.Request) bool {
src := r.Header
loop:
for k, v := range m {
if values, ok := src[k]; !ok {
return false
} else if v != "" {
for _, value := range values {
if v == value {
continue loop
}
}
return false
}
}
return true
}
// Host -----------------------------------------------------------------------
// NewHost returns a static URL host matcher.
func NewHost(host string) Host {
return Host(host)
}
// Host matches a static URL host.
type Host string
func (m Host) Match(r *http.Request) bool {
return getHost(r) == string(m)
}
// Method ---------------------------------------------------------------------
// NewMethod retuns a request method matcher, converting values to upper-case.
func NewMethod(m []string) Method {
for k, v := range m {
m[k] = strings.ToUpper(v)
}
return Method(m)
}
// Method matches the request method. One of the values must match.
type Method []string
func (m Method) Match(r *http.Request) bool {
for _, v := range m {
if v == r.Method {
return true
}
}
return false
}
// None -----------------------------------------------------------------------
// NewNone returns a matcher that never matches.
func NewNone() *None {
return nil
}
// None never matches.
type None bool
func (m *None) Match(r *http.Request) bool {
return false
}
// Path -----------------------------------------------------------------------
// NewPath returns a static URL path matcher.
func NewPath(path string) Path {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return Path(path)
}
// Path matches a static URL path.
type Path string
func (m Path) Match(r *http.Request) bool {
return r.URL.Path == string(m)
}
// PathRedirect ---------------------------------------------------------------
// NewPathRedirect returns a static URL path matcher that redirects if the
// trailing slash differs.
func NewPathRedirect(path string) PathRedirect {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return PathRedirect(path)
}
// PathRedirect matches a static URL path and redirects to the trailing-slash
// or non-trailing-slash version if it differs.
type PathRedirect string
func (m PathRedirect) Match(r *http.Request) bool {
return strings.TrimRight(r.URL.Path, "/") == strings.TrimRight(string(m), "/")
}
func (m PathRedirect) Extract(result *Result, r *http.Request) {
if result.Handler == nil {
result.Handler = redirectPath(string(m), r)
}
}
// PathPrefix -----------------------------------------------------------------
// NewPathPrefix returns a static URL path prefix matcher.
func NewPathPrefix(prefix string) PathPrefix {
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
return PathPrefix(prefix)
}
// PathPrefix matches a static URL path prefix.
type PathPrefix string
func (m PathPrefix) Match(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, string(m))
}
// Query ----------------------------------------------------------------------
// NewQuery returns a URL query matcher.
func NewQuery(m map[string]string) Query {
return Query(m)
}
// Query matches URL queries. All values, if non-empty, must match.
// Empty values only check if the query is present.
type Query map[string]string
func (m Query) Match(r *http.Request) bool {
src := r.URL.Query()
loop:
for k, v := range m {
if values, ok := src[k]; !ok {
return false
} else if v != "" {
for _, value := range values {
if v == value {
continue loop
}
}
return false
}
}
return true
}
// Scheme ---------------------------------------------------------------------
// NewScheme retuns a URL scheme matcher, converting values to lower-case.
func NewScheme(m []string) Scheme {
for k, v := range m {
m[k] = strings.ToLower(v)
}
return Scheme(m)
}
// Scheme matches the URL scheme. One of the values must match.
type Scheme []string
func (m Scheme) Match(r *http.Request) bool {
for _, v := range m {
if v == r.URL.Scheme {
return true
}
}
return false
}
// Helpers --------------------------------------------------------------------
// getHost tries its best to return the request host.
func getHost(r *http.Request) string {
if r.URL.IsAbs() {
host := r.Host
// Slice off any port information.
if i := strings.Index(host, ":"); i != -1 {
host = host[:i]
}
return host
}
return r.URL.Host
}
// mergeValues returns the result of merging two url.Values.
func mergeValues(u1, u2 url.Values) url.Values {
if u1 == nil {
return u2
}
if u2 == nil {
return u1
}
for k, v := range u2 {
u1[k] = append(u1[k], v...)
}
return u1
}
// redirectPath returns a handler that redirects if the path trailing slash
// differs from the request URL path.
func redirectPath(path string, r *http.Request) http.Handler {
t1 := strings.HasSuffix(path, "/")
t2 := strings.HasSuffix(r.URL.Path, "/")
if t1 != t2 {
u, _ := url.Parse(r.URL.String())
if t1 {
u.Path += "/"
} else {
u.Path = u.Path[:len(u.Path)-1]
}
return http.RedirectHandler(u.String(), 301)
}
return nil
}