-
Notifications
You must be signed in to change notification settings - Fork 0
/
supervisor.go
163 lines (130 loc) · 2.94 KB
/
supervisor.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
package main
import (
"fmt"
"log"
"net/url"
"github.com/xperimental/linky/html"
)
type supervisor struct {
baseURL *url.URL
workers chan location
updates chan update
queue []location
visited map[location]bool
results []update
done chan struct{}
verbose bool
quiet bool
ignoreReferrer bool
}
func newSupervisor(baseURL string, verbose bool, quiet bool, ignoreReferrer bool) (*supervisor, error) {
base, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
if !base.IsAbs() {
return nil, fmt.Errorf("not an absolute URL: %s", baseURL)
}
start := location{
URL: baseURL,
Referrer: "",
}
s := &supervisor{
baseURL: base,
workers: make(chan location),
updates: make(chan update),
queue: []location{start},
visited: make(map[location]bool),
results: []update{},
done: make(chan struct{}),
verbose: verbose,
quiet: quiet,
ignoreReferrer: ignoreReferrer,
}
go s.loop()
return s, nil
}
func (s *supervisor) UpdateChan() chan<- update {
return s.updates
}
func (s *supervisor) WorkerChan() <-chan location {
return s.workers
}
func (s *supervisor) Done() <-chan struct{} {
return s.done
}
func (s *supervisor) Results() []update {
return s.results
}
func (s *supervisor) loop() {
for {
if len(s.queue) == 0 {
break
}
next := s.queue[0]
s.queue = s.queue[1:]
if s.checkVisited(next) {
continue
}
result := s.checkAndVisit(next)
s.markVisit(result.Location)
s.results = append(s.results, result)
if s.verbose || (!result.IsOK() && !result.Skipped) || (result.IsOK() && !s.quiet) {
fmt.Println(result)
}
unvisited := s.filterLinks(result.Location.URL, result.Links)
s.queue = append(s.queue, unvisited...)
}
s.done <- struct{}{}
}
func (s *supervisor) checkAndVisit(loc location) update {
parsed, err := url.Parse(loc.URL)
if err != nil {
return update{
Location: loc,
Error: err,
}
}
if parsed.Host != s.baseURL.Host {
return update{
Location: loc,
Skipped: true,
}
}
s.workers <- loc
return <-s.updates
}
func (s *supervisor) filterLinks(referrer string, links []string) []location {
refererURL, err := url.Parse(referrer)
if err != nil {
refererURL = s.baseURL
}
unvisited := []location{}
for _, u := range links {
canonical, err := html.CanonicalizeURL(refererURL, u)
if err != nil {
log.Printf("[s] Error parsing link %s: %s", u, err)
}
canonicalLocation := location{
URL: canonical,
Referrer: referrer,
}
if s.checkVisited(canonicalLocation) {
continue
}
unvisited = append(unvisited, canonicalLocation)
}
return unvisited
}
func (s *supervisor) markVisit(loc location) {
if s.ignoreReferrer {
loc.Referrer = ""
}
s.visited[loc] = true
}
func (s *supervisor) checkVisited(loc location) bool {
if s.ignoreReferrer {
loc.Referrer = ""
}
return s.visited[loc]
}