Skip to content

Commit 677a036

Browse files
committed
fix(filters): fix suffix filters and perf optimizations
1 parent 9b5ce75 commit 677a036

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

filters.go

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,22 @@ func Ignore(filter Filter) Filter {
1919

2020
// Method
2121
func AcceptMethod(methods ...string) Filter {
22-
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
23-
reqMethod := strings.ToLower(r.Method)
24-
25-
for _, method := range methods {
26-
if strings.ToLower(method) == reqMethod {
27-
return true
28-
}
29-
}
22+
for i := range methods {
23+
methods[i] = strings.ToLower(methods[i])
24+
}
3025

31-
return false
26+
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
27+
return slices.Contains(methods, strings.ToLower(r.Method))
3228
}
3329
}
3430

3531
func IgnoreMethod(methods ...string) Filter {
36-
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
37-
reqMethod := strings.ToLower(r.Method)
38-
39-
for _, method := range methods {
40-
if strings.ToLower(method) == reqMethod {
41-
return false
42-
}
43-
}
32+
for i := range methods {
33+
methods[i] = strings.ToLower(methods[i])
34+
}
4435

45-
return true
36+
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
37+
return !slices.Contains(methods, strings.ToLower(r.Method))
4638
}
4739
}
4840

@@ -160,10 +152,10 @@ func IgnorePathPrefix(prefixs ...string) Filter {
160152
}
161153
}
162154

163-
func AcceptPathSuffix(prefixs ...string) Filter {
155+
func AcceptPathSuffix(suffixs ...string) Filter {
164156
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
165-
for _, prefix := range prefixs {
166-
if strings.HasPrefix(r.URL.Path, prefix) {
157+
for _, suffix := range suffixs {
158+
if strings.HasSuffix(r.URL.Path, suffix) {
167159
return true
168160
}
169161
}
@@ -187,7 +179,7 @@ func IgnorePathSuffix(suffixs ...string) Filter {
187179
func AcceptPathMatch(regs ...regexp.Regexp) Filter {
188180
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
189181
for _, reg := range regs {
190-
if reg.Match([]byte(r.URL.Path)) {
182+
if reg.MatchString(r.URL.Path) {
191183
return true
192184
}
193185
}
@@ -199,7 +191,7 @@ func AcceptPathMatch(regs ...regexp.Regexp) Filter {
199191
func IgnorePathMatch(regs ...regexp.Regexp) Filter {
200192
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
201193
for _, reg := range regs {
202-
if reg.Match([]byte(r.URL.Path)) {
194+
if reg.MatchString(r.URL.Path) {
203195
return false
204196
}
205197
}
@@ -269,10 +261,10 @@ func IgnoreHostPrefix(prefixs ...string) Filter {
269261
}
270262
}
271263

272-
func AcceptHostSuffix(prefixs ...string) Filter {
264+
func AcceptHostSuffix(suffixs ...string) Filter {
273265
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
274-
for _, prefix := range prefixs {
275-
if strings.HasPrefix(r.URL.Host, prefix) {
266+
for _, suffix := range suffixs {
267+
if strings.HasSuffix(r.URL.Host, suffix) {
276268
return true
277269
}
278270
}
@@ -296,7 +288,7 @@ func IgnoreHostSuffix(suffixs ...string) Filter {
296288
func AcceptHostMatch(regs ...regexp.Regexp) Filter {
297289
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
298290
for _, reg := range regs {
299-
if reg.Match([]byte(r.URL.Host)) {
291+
if reg.MatchString(r.URL.Host) {
300292
return true
301293
}
302294
}
@@ -308,7 +300,7 @@ func AcceptHostMatch(regs ...regexp.Regexp) Filter {
308300
func IgnoreHostMatch(regs ...regexp.Regexp) Filter {
309301
return func(ww middleware.WrapResponseWriter, r *http.Request) bool {
310302
for _, reg := range regs {
311-
if reg.Match([]byte(r.URL.Host)) {
303+
if reg.MatchString(r.URL.Host) {
312304
return false
313305
}
314306
}

0 commit comments

Comments
 (0)