forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_filterer.go
49 lines (38 loc) · 945 Bytes
/
link_filterer.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
package main
import (
"net/url"
"regexp"
)
var validSchemes = map[string]struct{}{
"": {},
"http": {},
"https": {},
}
type linkFilterer struct {
excludedPatterns []*regexp.Regexp
includedPatterns []*regexp.Regexp
}
func newLinkFilterer(es []*regexp.Regexp, is []*regexp.Regexp) linkFilterer {
return linkFilterer{excludedPatterns: es, includedPatterns: is}
}
func (f linkFilterer) IsValid(u *url.URL) bool {
s := u.String()
if _, ok := validSchemes[u.Scheme]; !ok {
return false
}
return !f.isLinkExcluded(s) && f.isLinkIncluded(s)
}
func (f linkFilterer) isLinkExcluded(u string) bool {
return f.matches(u, f.excludedPatterns)
}
func (f linkFilterer) isLinkIncluded(u string) bool {
return len(f.includedPatterns) == 0 || f.matches(u, f.includedPatterns)
}
func (f linkFilterer) matches(u string, rs []*regexp.Regexp) bool {
for _, r := range rs {
if r.MatchString(u) {
return true
}
}
return false
}