-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.go
195 lines (177 loc) · 4.23 KB
/
response.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
185
186
187
188
189
190
191
192
193
194
195
package crawler
import (
"golang.org/x/net/html"
"io"
"net/url"
"strings"
)
// Response has the details from crawling a single URL
type Response struct {
URL string `json:"url"`
RedirectTo string `json:"redirect_to,omitempty"`
Links []Link `json:"links"`
Assets []Asset `json:"assets"`
request *Request
}
// Link contains the informaiton from a single `a` tag
type Link struct {
// URL contains the href attribute of the link. e.g: <a href="{href}">...</a>
URL string `json:"url"`
}
// Asset represents linked assets such as link, script and img tags
type Asset struct {
// Tag used to link the asset
Tag string `json:"tag"`
// URL of the asset
URL string `json:"url"`
// Rel contains the text of the rel attribute
Rel string `json:"rel,omitempty"`
// Type contains the text of the type attribute
Type string `json:"type,omitempty"`
}
// ReadResponse extracts links and assets from the HTML read form the given io
// reader and fills it in the response
func ReadResponse(base *url.URL, r io.Reader, res *Response) error {
res.URL = base.String()
res.Links = nil
res.Assets = nil
node, err := html.Parse(r)
if err != nil {
return err
}
var dfWalk func(*html.Node)
dfWalk = func(n *html.Node) {
if link := extractLink(base, n); link != nil {
res.Links = append(res.Links, *link)
} else if assets := extractAssets(base, n); len(assets) > 0 {
res.Assets = append(res.Assets, assets...)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
dfWalk(c)
}
}
dfWalk(node)
return nil
}
func extractLink(base *url.URL, n *html.Node) *Link {
if n.Type != html.ElementNode {
return nil
}
if n.Data != "a" {
return nil
}
var link Link
for _, attr := range n.Attr {
if attr.Key != "href" {
continue
}
v, err := url.Parse(strings.TrimSpace(attr.Val))
if err != nil {
continue
}
if v.Scheme != "" && v.Scheme != "http" && v.Scheme != "https" {
continue
}
v = base.ResolveReference(v)
v.Fragment = ""
link.URL = v.String()
}
if link.URL == "" {
return nil
}
return &link
}
func extractAssets(base *url.URL, n *html.Node) []Asset {
if n.Type != html.ElementNode {
return nil
}
fn, ok := extractTag[n.Data]
if !ok {
return nil
}
return fn(base, n)
}
func extractSimpleAsset(base *url.URL, n *html.Node) []Asset {
var asset Asset
asset.Tag = n.Data
var srcset string
for _, attr := range n.Attr {
switch attr.Key {
case "src", "href":
v, err := url.Parse(strings.TrimSpace(attr.Val))
if err != nil {
continue
}
asset.URL = base.ResolveReference(v).String()
case "srcset":
srcset = attr.Val
case "rel":
asset.Rel = attr.Val
case "type":
asset.Type = attr.Val
}
}
if srcset == "" && asset.URL == "" {
return nil
}
if srcset == "" {
return []Asset{asset}
}
return assetSrcset(base, asset, srcset)
}
func assetSrcset(base *url.URL, asset Asset, srcset string) []Asset {
srcs := strings.Split(srcset, ",")
assets := make([]Asset, 0, len(srcs))
for _, src := range srcs {
src = strings.TrimSpace(src)
parts := strings.Split(src, " ")
u := parts[0]
v, err := url.Parse(strings.TrimSpace(u))
if err != nil {
continue
}
asset.URL = base.ResolveReference(v).String()
if asset.URL == "" {
continue
}
assets = append(assets, asset)
}
return assets
}
func extractSourceAsset(base *url.URL, n *html.Node) []Asset {
if n.Parent == nil {
return nil
}
if n.Parent.Data != "picture" && n.Parent.Data != "video" {
return nil
}
assets := extractSimpleAsset(base, n)
for i := range assets {
assets[i].Tag = n.Parent.Data + ">" + n.Data
}
return assets
}
func extractImgAsset(base *url.URL, n *html.Node) []Asset {
if n.Parent != nil && n.Parent.Data == "picture" {
return extractSourceAsset(base, n)
}
return extractSimpleAsset(base, n)
}
func extractLinkAsset(base *url.URL, n *html.Node) []Asset {
assets := extractSimpleAsset(base, n)
res := assets[0:0]
for _, asset := range assets {
if asset.Rel == "stylesheet" {
res = append(res, asset)
}
}
return res
}
type extractAssetFunc func(*url.URL, *html.Node) []Asset
var extractTag = map[string]extractAssetFunc{
"link": extractLinkAsset,
"source": extractSourceAsset,
"img": extractImgAsset,
"script": extractSimpleAsset,
"video": extractSimpleAsset,
}