-
Notifications
You must be signed in to change notification settings - Fork 17
/
filter_group.go
58 lines (48 loc) · 1.31 KB
/
filter_group.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
package mps
import "net/http"
type FilterGroup interface {
Handle()
}
// ReqFilterGroup ReqCondition is a request filter group
type ReqFilterGroup struct {
ctx *Context
filters []Filter
}
func (cond *ReqFilterGroup) DoFunc(fn func(req *http.Request, ctx *Context) (*http.Request, *http.Response)) {
cond.Do(RequestHandleFunc(fn))
}
func (cond *ReqFilterGroup) Do(h RequestHandle) {
cond.ctx.UseFunc(func(req *http.Request, ctx *Context) (*http.Response, error) {
total := len(cond.filters)
for i := 0; i < total; i++ {
if !cond.filters[i].Match(req) {
return ctx.Next(req)
}
}
req, resp := h.HandleRequest(req, ctx)
if resp != nil {
return resp, nil
}
return ctx.Next(req)
})
}
// RespFilterGroup ReqCondition is a response filter group
type RespFilterGroup struct {
ctx *Context
filters []Filter
}
func (cond *RespFilterGroup) DoFunc(fn func(resp *http.Response, err error, ctx *Context) (*http.Response, error)) {
cond.Do(ResponseHandleFunc(fn))
}
func (cond *RespFilterGroup) Do(h ResponseHandle) {
cond.ctx.UseFunc(func(req *http.Request, ctx *Context) (*http.Response, error) {
total := len(cond.filters)
for i := 0; i < total; i++ {
if !cond.filters[i].Match(req) {
return ctx.Next(req)
}
}
resp, err := ctx.Next(req)
return h.HandleResponse(resp, err, ctx)
})
}