-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
125 lines (107 loc) · 3.19 KB
/
filter.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
// Copyright 2014 beego Author. All Rights Reserved.
//
package web
import (
"strings"
"github.com/W3-Engineers-Ltd/Radiant/server/web/context"
)
// FilterChain is different from pure FilterFunc
// when you use this, you must invoke next(ctx) inside the FilterFunc which is returned
// And all those FilterChain will be invoked before other FilterFunc
type FilterChain func(next FilterFunc) FilterFunc
// FilterFunc defines a filter function which is invoked before the controller handler is executed.
// It's a alias of HandleFunc
// In fact, the HandleFunc is the last Filter. This is the truth
type FilterFunc = HandleFunc
// FilterRouter defines a filter operation which is invoked before the controller handler is executed.
// It can match the URL against a pattern, and execute a filter function
// when a request with a matching URL arrives.
type FilterRouter struct {
filterFunc FilterFunc
next *FilterRouter
tree *Tree
pattern string
returnOnOutput bool
resetParams bool
}
// params is for:
// 1. setting the returnOnOutput value (false allows multiple filters to execute)
// 2. determining whether or not params need to be reset.
func newFilterRouter(pattern string, filter FilterFunc, opts ...FilterOpt) *FilterRouter {
mr := &FilterRouter{
tree: NewTree(),
pattern: pattern,
filterFunc: filter,
}
fos := &filterOpts{
returnOnOutput: true,
}
for _, o := range opts {
o(fos)
}
if !fos.routerCaseSensitive {
mr.pattern = strings.ToLower(pattern)
}
mr.returnOnOutput = fos.returnOnOutput
mr.resetParams = fos.resetParams
mr.tree.AddRouter(pattern, true)
return mr
}
// filter will check whether we need to execute the filter logic
// return (started, done)
func (f *FilterRouter) filter(ctx *context.Context, urlPath string, preFilterParams map[string]string) (bool, bool) {
if f.returnOnOutput && ctx.ResponseWriter.Started {
return true, true
}
if f.resetParams {
preFilterParams = ctx.Input.Params()
}
if ok := f.ValidRouter(urlPath, ctx); ok {
f.filterFunc(ctx)
if f.resetParams {
ctx.Input.ResetParams()
for k, v := range preFilterParams {
ctx.Input.SetParam(k, v)
}
}
} else if f.next != nil {
return f.next.filter(ctx, urlPath, preFilterParams)
}
if f.returnOnOutput && ctx.ResponseWriter.Started {
return true, true
}
return false, false
}
// ValidRouter checks if the current request is matched by this filter.
// If the request is matched, the values of the URL parameters defined
// by the filter pattern are also returned.
func (f *FilterRouter) ValidRouter(url string, ctx *context.Context) bool {
isOk := f.tree.Match(url, ctx)
if isOk != nil {
if b, ok := isOk.(bool); ok {
return b
}
}
return false
}
type filterOpts struct {
returnOnOutput bool
resetParams bool
routerCaseSensitive bool
}
type FilterOpt func(opts *filterOpts)
func WithReturnOnOutput(ret bool) FilterOpt {
return func(opts *filterOpts) {
opts.returnOnOutput = ret
}
}
func WithResetParams(reset bool) FilterOpt {
return func(opts *filterOpts) {
opts.resetParams = reset
}
}
func WithCaseSensitive(sensitive bool) FilterOpt {
return func(opts *filterOpts) {
opts.routerCaseSensitive = sensitive
}
}