/
filter.go
67 lines (55 loc) · 1.36 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
package httpx
import (
"regexp"
"strings"
)
// Filter defines a generic filter interface to apply to responses
type Filter interface {
Filter(response *Response) (bool, error)
}
// FilterString defines a filter of type string
type FilterString struct {
Keywords []string
}
// Filter a response with strings filtering
func (f FilterString) Filter(response *Response) (bool, error) {
for _, keyword := range f.Keywords {
if strings.Contains(response.Raw, keyword) {
return true, nil
}
}
return false, nil
}
// FilterRegex defines a filter of type regex
type FilterRegex struct {
Regexs []string
}
// Filter a response with regexes
func (f FilterRegex) Filter(response *Response) (bool, error) {
for _, regex := range f.Regexs {
matched, err := regexp.MatchString(regex, response.Raw)
if err != nil {
return false, err
}
if matched {
return true, nil
}
}
return false, nil
}
// CustomCallback used in custom filters
type CustomCallback func(response *Response) (bool, error)
// FilterCustom defines a filter with callback functions applied
type FilterCustom struct {
CallBacks []CustomCallback
}
// Filter a response with custom callbacks
func (f FilterCustom) Filter(response *Response) (bool, error) {
for _, callback := range f.CallBacks {
ok, err := callback(response)
if ok && err == nil {
return true, err
}
}
return false, nil
}