-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagematcher.go
143 lines (132 loc) · 2.75 KB
/
pagematcher.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package golem
import (
"net/url"
"strconv"
"strings"
)
type PageMatcher struct {
Scheme []string
User []string
Domain []string
Path []map[string]string
Query []map[string]string
Fragment []string
}
// Normalizes the fields
// Currently scheme and domain a forced to lower case
func (matcher *PageMatcher) Normalize() {
for i, v := range matcher.Scheme {
matcher.Scheme[i] = strings.ToLower(v)
}
for i, v := range matcher.Domain {
matcher.Domain[i] = strings.ToLower(v)
}
}
// Check whether or not the link matches
func (matcher *PageMatcher) Check(link *url.URL) (bool, error) {
if link == nil {
return true, nil
}
matcher.Normalize()
// Schema
if matcher.Scheme != nil && len(matcher.Scheme) > 0 && !contains(matcher.Scheme, strings.ToLower(link.Scheme)) {
return false, nil
}
// User
if matcher.User != nil && len(matcher.User) > 0 && !contains(matcher.User, link.User.String()) {
return false, nil
}
// Domain
if matcher.Domain != nil && len(matcher.Domain) > 0 && !contains(matcher.Domain, link.Host) {
return false, nil
}
var check = false
// Path
if matcher.Path != nil && len(matcher.Path) > 0 {
path := link.Path
if path[0] == 47 {
path = path[1:]
}
var segments = strings.Split(path, "/")
for _, pathmatcher := range matcher.Path {
// Check max
smax, ok := pathmatcher["max"]
var max int
if ok {
max, err := strconv.Atoi(smax)
if err != nil {
return false, err
}
if max < len(segments) {
continue
}
} else {
max = len(segments) - 1
}
// Check min
smin, ok := pathmatcher["min"]
var min int
if ok {
min, err := strconv.Atoi(smin)
if err != nil {
return false, err
}
if min > len(segments) {
continue
}
} else {
min = 0
}
// Check segment
var innerCheck = true
for num, segment := range pathmatcher {
if n, err := strconv.Atoi(num); err == nil {
if n < min || n > max || segments[n] != segment {
innerCheck = false
}
}
}
if !innerCheck {
check = true
break
}
}
if !check {
return false, nil
}
}
// Query
if matcher.Query != nil && len(matcher.Query) > 0 {
check = false
for _, query := range matcher.Query {
var all = true
q, err := url.ParseQuery(link.RawQuery)
if err != nil {
return false, err
}
for k, v := range query {
if !contains(q[k], v) {
all = false
break
}
}
if all {
check = true
break
}
}
if !check {
return false, nil
}
}
// Fragment
return matcher.Fragment == nil || len(matcher.Fragment) == 0 || contains(matcher.Fragment, link.Fragment), nil
}
func contains(arr []string, contain string) bool {
for _, v := range arr {
if v == contain {
return true
}
}
return false
}