-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
198 lines (177 loc) · 4.36 KB
/
parser.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
196
197
198
//Copyright 2024 qingyu31
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package robotstxt
import (
"bufio"
"io"
"strings"
"unicode"
)
// Parse parses robots.txt and returns a Matcher.
func Parse(robotsBody io.Reader, options ...ParseOption) Matcher {
config := new(ParseConfig)
config.applyDefault()
for _, option := range options {
option.Apply(config)
}
handler := config.Handler
scanner := bufio.NewScanner(robotsBody)
handler.HandleStart()
lineNum := 0
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
if lineNum == 1 {
strings.TrimPrefix(line, "\ufeff")
}
if line == "" {
continue
}
key, value, ok := getKeyAndValueFrom(line)
if !ok {
continue
}
switch parseKey(key) {
case keyTypeUserAgent:
handler.HandleUserAgent(lineNum, value)
case keyTypeAllow:
handler.HandleAllow(lineNum, maybeEscapePattern(value))
case keyTypeDisallow:
handler.HandleDisallow(lineNum, maybeEscapePattern(value))
case keyTypeSiteMap:
handler.HandleSitemap(lineNum, value)
default:
handler.HandleUnknownAction(lineNum, key, maybeEscapePattern(value))
}
}
handler.HandleEnd()
m := new(defaultMatcher)
m.groups = handler.(*DefaultParseHandler).ruleGroup
m.matchStrategy = config.MatchStrategy
return m
}
type ParseOption interface {
Apply(*ParseConfig)
}
type ParseConfig struct {
Handler ParseHandler
MatchStrategy MatchStrategy
}
func (c *ParseConfig) applyDefault() {
if c.Handler == nil {
c.Handler = new(DefaultParseHandler)
}
if c.MatchStrategy == nil {
c.MatchStrategy = new(DefaultMatchStrategy)
}
}
var kHexDigits = []byte("0123456789ABCDEF")
// maybeEscapePattern 规范化允许/拒绝的路径
func maybeEscapePattern(src string) string {
numToEscape := 0
needCapitalize := false
for _, char := range src {
if char == '%' && isHexDigit(src[1]) && isHexDigit(src[2]) {
if isLower(src[1]) || isLower(src[2]) {
needCapitalize = true
}
src = src[3:]
} else if char&0x80 != 0 {
numToEscape++
}
}
if numToEscape == 0 && !needCapitalize {
return src
}
dst := make([]byte, len(src)+numToEscape*2)
j := 0
for i := 0; i < len(src); i++ {
if src[i] == '%' && isHexDigit(src[i+1]) && isHexDigit(src[i+2]) {
dst[j] = src[i]
dst[j+1] = toUpper(src[i+1])
dst[j+2] = toUpper(src[i+2])
i += 2
} else if src[i]&0x80 != 0 {
dst[j] = '%'
dst[j+1] = kHexDigits[(src[i]>>4)&0xf]
dst[j+2] = kHexDigits[src[i]&0xf]
} else {
dst[j] = src[i]
}
j++
}
return string(dst)
}
func isHexDigit(ch byte) bool {
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F') || ('a' <= ch && ch <= 'f')
}
func isLower(ch byte) bool {
return 'a' <= ch && ch <= 'z'
}
func toUpper(ch byte) byte {
if 'a' <= ch && ch <= 'z' {
return ch - 'a' + 'A'
}
return ch
}
func getKeyAndValueFrom(line string) (key, value string, ok bool) {
idx := strings.IndexFunc(line, func(char rune) bool {
return char == ':' || unicode.IsSpace(char)
})
if idx == -1 {
return "", "", false
}
key = strings.TrimSpace(line[:idx])
value = strings.TrimSpace(line[idx+1:])
return key, value, true
}
var disallowedTypos = []string{
"dissallow", "dissalow", "disalow", "diasllow", "disallaw",
}
const allowFrequentTypos = true
type keyType int
const (
keyTypeUserAgent keyType = iota
keyTypeSiteMap
keyTypeAllow
keyTypeDisallow
keyTypeUnknown = 128
)
func parseKey(key string) keyType {
lowerKey := strings.ToLower(key)
switch lowerKey {
case "user-agent":
return keyTypeUserAgent
case "allow":
return keyTypeAllow
case "disallow":
return keyTypeDisallow
case "sitemap":
return keyTypeSiteMap
case "site-map":
return keyTypeSiteMap
}
if !allowFrequentTypos {
return keyTypeUnknown
}
if lowerKey == "useragent" || lowerKey == "user agent" {
return keyTypeUserAgent
}
for _, typo := range disallowedTypos {
if lowerKey == typo {
return keyTypeDisallow
}
}
return keyTypeUnknown
}