/
respproc_stage.go
184 lines (156 loc) · 3.56 KB
/
respproc_stage.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
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"net/http"
"net/url"
"path"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
const extractorCount = 10
/*
func responseProcessor(id int) {
defer wg.Done()
for i := 1; i <= extractorCount; i++ {
wg.Add(1)
go linkExtractor()
}
// TODO: can we remove all this?
/*
loop:
for {
select {
case <-exiting:
debug("processor exiting")
break loop
case <-time.After(time.Second * 500):
//ping()
continue loop // TODO: here goes the fan out
}
}
return
}
*/
func linkExtractor() {
defer wg.Done()
loop:
for {
select {
case <-exiting:
debug("link extractor exiting")
break loop
case resp := <-downloadOutputQ:
ping()
parseHTML(resp)
//regexResponse(resp)
}
}
return
}
func parseHTML(resp *http.Response) {
debug("extracting links")
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
debug("error creating goquery document")
return
}
defaultScheme := resp.Request.URL.Scheme
defaultAuthority := resp.Request.URL.Host
defaultPath := path.Dir(resp.Request.URL.EscapedPath()) + "/"
// use CSS selector found with the browser inspector
// for each, use index and item
doc.Find("body a").Each(func(index int, item *goquery.Selection) {
linkTag := item
link, _ := linkTag.Attr("href")
link = getFullLink(link, defaultScheme, defaultAuthority, defaultPath)
//debug(link)
if isScoped(link) {
req, err := http.NewRequest("GET", link, nil)
if err == nil {
select {
case reqFilterInputQ <- req:
//default:
}
}
}
})
}
func isScoped(link string) bool {
//debug("scope", scope)
//debug("link", link)
u, err := url.Parse(link)
if err != nil {
return false
}
if u.Host != scope && !strings.HasSuffix(u.Host, "."+scope) {
return false
}
return true
}
var fullLink = regexp.MustCompile("(?i)^https?://.*$")
var inheritScheme = regexp.MustCompile("^//.*$")
var absoluteLink = regexp.MustCompile("^/[^/].*$")
var relativeLink = regexp.MustCompile("^[^/].*$")
func getFullLink(href string, defaultScheme string, defaultAuthority string, defaultPath string) string {
switch {
case fullLink.MatchString(href):
return href
case inheritScheme.MatchString(href):
return defaultScheme + "://" + href
case absoluteLink.MatchString(href):
return defaultScheme + "://" + defaultAuthority + href
case relativeLink.MatchString(href):
return defaultScheme + "://" + defaultAuthority + defaultPath + href
}
return href
}
/*
z := html.NewTokenizer(resp.Body)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
// End of the document, we're done
return
case tt == html.StartTagToken:
t := z.Token()
// Check if the token is an <a> tag
isAnchor := t.Data == "a"
if !isAnchor {
continue
}
// Extract the href value, if there is one
ok, link := getHref(t)
if !ok {
continue
}
// Make sure the url begines in http**
hasProto := strings.Index(link, "http") == 0
if hasProto {
req, err := http.NewRequest("GET", link, nil)
if err == nil {
select {
case reqFilterInputQ <- req:
default:
//case <-time.After(time.Millisecond * 0.5):
//debug("link lost")
}
}
}
}
}
}
// Helper function to pull the href attribute from a Token
func getHref(t html.Token) (ok bool, href string) {
// Iterate over all of the Token's attributes until we find an "href"
for _, a := range t.Attr {
if a.Key == "href" {
href = string(a.Val)
ok = true
}
}
// "bare" return will return the variables (ok, href) as defined in
// the function definition
return
}
*/