-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobots.go
72 lines (63 loc) · 1.21 KB
/
robots.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
package robots
import (
"io"
"net/url"
"strings"
"github.com/0x51-dev/robots/abnf"
"github.com/0x51-dev/upeg/parser"
)
type File struct {
Groups []*Group
Sitemaps []*url.URL
Others []OtherField
}
// ParseRobotsFile parses a robots.txt file from the given reader.
func ParseRobotsFile(f io.Reader) (*File, error) {
raw, err := io.ReadAll(f)
if err != nil {
return nil, err
}
p, err := parser.New([]rune(string(raw)))
if err != nil {
return nil, err
}
n, err := p.ParseEOF(abnf.Robotstxt)
if err != nil {
return nil, err
}
return parseRobotsTXT(n)
}
func (f File) Allowed(pt ProductToken, path *url.URL) bool {
for i := range f.Groups {
g := f.Groups[len(f.Groups)-1-i]
allowed := g.Allowed(pt, path)
if allowed != nil {
return *allowed
}
}
// No rules applied.
return true
}
func (r File) Equal(o *File) bool {
if len(r.Groups) != len(o.Groups) {
return false
}
for i := range r.Groups {
if !r.Groups[i].Equal(o.Groups[i]) {
return false
}
}
return true
}
func (r File) String() string {
var b strings.Builder
for _, g := range r.Groups {
b.WriteString(g.String())
b.WriteString("\n")
}
return b.String()
}
type OtherField struct {
Key string
Value string
}