forked from maciekmm/sitemap-generator
/
validator.go
171 lines (157 loc) · 4.31 KB
/
validator.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package sitemapgen
import (
"log"
"net/url"
"strings"
"sync"
"github.com/eapache/channels"
"github.com/maciekmm/sitemap-generator/config"
"github.com/temoto/robotstxt-go"
)
//Validator manages address flow by pushing them to certain creation proccesses and makes sure no links are parsed twice.
type Validator struct {
sites map[string]struct{}
workerQueue *channels.InfiniteChannel
Input chan *url.URL
config config.Config
url *url.URL
waitGroup *sync.WaitGroup
robots *robotstxt.RobotsData
generator chan string
}
//NewValidator creates a new validator instance
func NewValidator(config config.Config, workerQueue *channels.InfiniteChannel, waitGroup *sync.WaitGroup, robots *robotstxt.RobotsData, generator chan string) *Validator {
parsedURL, err := url.Parse(config.URL)
parsedURL.Host = StripWWW(parsedURL.Host)
if err != nil {
return nil
}
return &Validator{
sites: make(map[string]struct{}),
workerQueue: workerQueue,
Input: make(chan *url.URL, 256),
config: config,
url: parsedURL,
waitGroup: waitGroup,
robots: robots,
generator: generator,
}
}
func (v *Validator) start() {
for {
select {
case url, ok := <-v.Input:
//Stop worker if channel is closed
if !ok {
log.Println("Validator: channel closed -> stopping")
return
}
ReorderAndCrop(v.config.Parsing, url)
stringURL := url.String()
URLProtStripped := stringURL
if v.config.Parsing.CutProtocol {
URLProtStripped = StripProtocol(stringURL)
}
//log.Println(stringurl)
if _, ok := v.sites[URLProtStripped]; ok {
//log.Println("Validator: Skipping ", stringurl, " - already looked up")
v.waitGroup.Done()
continue
}
//Mark as already processed
v.sites[URLProtStripped] = struct{}{}
//Check for robots
if v.config.Parsing.RespectRobots && v.robots != nil {
if !v.robots.TestAgent(url.Path, v.config.Parsing.UserAgent) {
log.Println("Validator: Skipping ", stringURL, " - denied by robots")
v.waitGroup.Done()
continue
}
}
if !strings.HasSuffix(strings.ToLower(url.Host), strings.ToLower(v.url.Host)) {
log.Println("Validator: Skipping ", stringURL, " - invalid host: ", url.Host, " expected: ", v.url.Host)
v.waitGroup.Done()
continue
}
//Push to workers
if !ShallParse(v.config.Parsing, stringURL) {
//log.Println("Validator: Skipping ", stringurl, " - excluded from parsing")
//Excluding from parsing does not exclude from adding to sitemap files
v.waitGroup.Add(1)
v.generator <- url.String()
v.waitGroup.Done()
continue
}
v.workerQueue.In() <- url
}
}
}
//ReorderAndCrop removes the anchor (#sth) Fragment,
//sorts, removes and encodes query string parameters
//and lowercases Host
func ReorderAndCrop(conf *config.ParsingConfig, url *url.URL) {
url.Path = strings.TrimSuffix(strings.TrimSpace(url.Path), "/")
if conf.StripQueryString {
url.RawQuery = ""
} else {
stringURL := url.String()
query := url.Query()
for _, filter := range conf.Params {
if filter.Regex.MatchString(stringURL) {
//If only specified params are relevent
if filter.Include {
for key := range query {
//Check if param is allowed
found := false
for _, param := range filter.Params {
if param == key {
found = true
}
}
//If not remove
if !found {
query.Del(key)
}
}
//If params are irrelevant
} else {
for _, param := range filter.Params {
query.Del(param)
}
}
}
}
url.RawQuery = query.Encode()
}
url.Fragment = ""
url.Host = strings.ToLower(url.Host)
if conf.StripWWW {
url.Host = StripWWW(url.Host)
}
}
//StripWWW strips a www. prefix/subdomain from URL represented in string
func StripWWW(host string) string {
if strings.HasPrefix(host, "www.") {
return host[4:]
}
return host
}
//StripProtocol strips a protocol from URL represented in string
func StripProtocol(url string) string {
if url[6] == '/' && strings.HasPrefix(url, "http") {
if url[4] == ':' {
return url[7:]
}
return url[8:]
}
return url
}
//ShallParse checks whether site's source should be parsed
func ShallParse(conf *config.ParsingConfig, url string) bool {
for _, excl := range conf.ParseExclusions {
if excl.MatchString(url) {
return false
}
}
return true
}