WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit dfc8268

Browse files
committed
cleanups
1 parent 236ccdd commit dfc8268

File tree

7 files changed

+85
-48
lines changed

7 files changed

+85
-48
lines changed

pkg/camo/proxy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
253253

254254
func (p *Proxy) checkURL(reqURL *url.URL) error {
255255
// reject localhost urls
256-
uHostname := strings.ToLower(reqURL.Hostname())
257-
if uHostname == "" || localhostDomainProxyFilter.CheckHostname(uHostname) {
256+
// lower case for matching is done by CheckHostname, so no need to
257+
// ToLower here also
258+
uHostname := reqURL.Hostname()
259+
if uHostname == "" || localsFilter.CheckHostname(uHostname) {
258260
return errors.New("Bad url host")
259261
}
260262

pkg/camo/vars.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ var rejectIPv6Networks = mustParseNetmasks(
7777
},
7878
)
7979

80-
// match for localhost
81-
var localhostDomainProxyFilter = htrie.MustNewURLMatcherWithRules(
80+
// match for localhost, localdomain
81+
var localsFilter = htrie.MustNewURLMatcherWithRules(
8282
[]string{
8383
"|s|localhost||",
8484
"|s|localdomain||",

pkg/htrie/glob_path_chk.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,7 @@ func (gpc *GlobPathChecker) AddRule(rule string) error {
132132
// Note: CheckPathString requires that the url path component is already escaped,
133133
// in a similar way to `(*url.URL).EscapePath()`, as well as TrimSpace'd.
134134
func (gpc *GlobPathChecker) CheckPath(url string) bool {
135-
return gpc.CheckPathPtr(&url)
136-
}
137-
138-
// CheckPathPtr operates like CheckPath, but takes a *string
139-
// (to optionally avoid extra copies)
140-
func (gpc *GlobPathChecker) CheckPathPtr(url *string) bool {
141-
ulen := len(*url)
135+
ulen := len(url)
142136

143137
// if we have a case sensitive checker, check that one first
144138
if gpc.csNode != nil && gpc.csNode.checkPath(url, 0, ulen) {

pkg/htrie/glob_path_chk_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,54 @@ func TestGlobPathCheckerPathsMisc(t *testing.T) {
111111
assert.False(t, gpc.CheckPath(u), fmt.Sprintf("should NOT have matched: %s", u))
112112
}
113113
}
114+
115+
func BenchmarkGlobPathChecker(b *testing.B) {
116+
rules := []string{
117+
"|i|*/test.png",
118+
"||/hodor/test.png",
119+
"||/hodor/test.png.longer",
120+
"||/hodor/bar*",
121+
"||/hodor/ütest.png",
122+
"||/no*/to/s*/here",
123+
"||/i/can/s*/it*",
124+
"||/play/*/ball/img.png",
125+
"||/yalp*llab/img.png",
126+
}
127+
128+
testMatch := []string{
129+
"http://bar.example.com/foo/TEST.png",
130+
"http://example.org/foo/test.png",
131+
"http://example.org/hodor/test.png",
132+
"http://example.org/hodor/test.png.longer",
133+
"http://example.org/hodor/bartholemew",
134+
"http://example.org/hodor/bart/homer.png",
135+
"http://example.net/nothing/to/see/here",
136+
"http://example.net/i/can/see/it/in/the/clouds/file.png",
137+
"http://example.org/play/base/ball/img.png",
138+
"http://example.org/yalp/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/base/llab/img.png",
139+
"http://example.org/yalpllab/img.png",
140+
}
141+
142+
gpc := NewGlobPathChecker()
143+
for _, rule := range rules {
144+
err := gpc.AddRule(rule)
145+
assert.Nil(b, err)
146+
}
147+
148+
var (
149+
testIters = 10000
150+
)
151+
152+
// avoid inlining optimization
153+
var x bool
154+
b.ResetTimer()
155+
156+
for _, u := range testMatch {
157+
u, _ := url.Parse(u)
158+
z := u.EscapedPath()
159+
for i := 0; i < testIters; i++ {
160+
x = gpc.CheckPath(z)
161+
}
162+
}
163+
_ = x
164+
}

pkg/htrie/glob_path_node.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (gpn *globPathNode) addPath(s string) error {
8181
return nil
8282
}
8383

84-
func (gpn *globPathNode) globConsume(s *string, index, mlen int) bool {
84+
func (gpn *globPathNode) globConsume(s string, index, mlen int) bool {
8585
// we have a glob and no follow-on chars, so we can consume
8686
// till the end and then match. early return
8787
if gpn.canMatch {
@@ -101,7 +101,7 @@ func (gpn *globPathNode) globConsume(s *string, index, mlen int) bool {
101101
curnode := gpn
102102
// don't need to iter runes since we have ascii
103103
for i := index; i < mlen; i++ {
104-
part := (*s)[i]
104+
part := s[i]
105105
// if icase, use lowercase letters for comparisons
106106
if gpn.icase && 'A' <= part && part <= 'Z' {
107107
part = part + 32
@@ -151,11 +151,11 @@ func (gpn *globPathNode) globConsume(s *string, index, mlen int) bool {
151151
return false
152152
}
153153

154-
func (gpn *globPathNode) checkPath(s *string, index, mlen int) bool {
154+
func (gpn *globPathNode) checkPath(s string, index, mlen int) bool {
155155
curnode := gpn
156156
// don't need to iter runes since we have ascii
157157
for i := index; i < mlen; i++ {
158-
part := (*s)[i]
158+
part := s[i]
159159

160160
// if icase, use lowercase letters for comparisons
161161
if gpn.icase && 'A' <= part && part <= 'Z' {
@@ -230,16 +230,12 @@ func newGlobPathNode(icase bool) *globPathNode {
230230
// 0x0061...0x007A 97-122
231231
// 0x007E 126
232232
// so a total possible of 85 chars, but spread out over 94 slots
233-
// since there are quite a few slots, let's use a map for now...
233+
// since there are quite a few possible slots, let's use a map for now...
234234
// web searches say a map is faster in go above a certain size. benchmark later...
235235

236-
// for now, just use the top bounds minus low bound for array size: 126-32=94
237-
// plus one for our globbing
238-
239-
// NOTE: since realloc cost is paid at creation, and we want to reduce size
240-
// and we only care about lookup costs, just start with 0 and let it grow
241-
// as needed.
242-
// return &globPathNode{subtrees: make(map[int]*globPathNode, 95)}
236+
// for now, since realloc cost is paid at creation, and we want to RSS size
237+
// and since we only /really/ care about lookup costs, just start with 0 initial
238+
// map size and let it grow as needed
243239
return &globPathNode{
244240
subtrees: make(map[byte]*globPathNode, 0),
245241
icase: icase,

pkg/htrie/htrie.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ func (dt *URLMatcher) AddRule(rule string) error {
220220

221221
func (dt *URLMatcher) walkFind(s string) []*URLMatcher {
222222
// hostname should already be lowercase. avoid work by not doing it.
223-
// hostname := strings.ToLower(s)
224223
matches := *getURLMatcherSlice()
225224
labels := reverse(strings.Split(s, "."))
226225
plen := len(labels)
@@ -265,7 +264,8 @@ func (dt *URLMatcher) walkFind(s string) []*URLMatcher {
265264
// If the url matches (a "hit"), it returns true.
266265
// If the url does not match (a "miss"), it return false.
267266
func (dt *URLMatcher) CheckURL(u *url.URL) bool {
268-
hostname := u.Hostname()
267+
// alas, (*url.URL).Hostname() does not ToLower
268+
hostname := strings.ToLower(u.Hostname())
269269
matches := dt.walkFind(hostname)
270270
defer putURLMatcherSlice(&matches)
271271

@@ -291,10 +291,14 @@ func (dt *URLMatcher) CheckURL(u *url.URL) bool {
291291
}
292292

293293
// CheckHostname checks the supplied hostname (as a string).
294-
// Note: CheckHostnameString requires that the hostname is already escaped,
294+
// Note: CheckHostname requires that the hostname is already escaped,
295295
// sanitized, space trimmed, and lowercased...
296-
// Basically sanitized in a way similar to `(*url.URL).Hostname()`
296+
// Basically sanitized in a way similar to:
297+
//
298+
// strings.ToLower((*url.URL).Hostname())
299+
//
297300
func (dt *URLMatcher) CheckHostname(hostname string) bool {
301+
hostname = strings.ToLower(hostname)
298302
matches := dt.walkFind(hostname)
299303
defer putURLMatcherSlice(&matches)
300304
return len(matches) > 0

pkg/htrie/htrie_test.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,17 @@ func BenchmarkHTrieMatch(b *testing.B) {
156156
"|s|example.org|i|*/test.png",
157157
}
158158

159-
testURLin := []string{
159+
testURLs := []string{
160160
"http://example.com/foo/test.png",
161161
"http://bar.example.com/foo/test.png",
162162
"http://bar.example.com/foo/testx.png",
163163
"http://bar.example.com/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/test.png",
164164
"http://bar.example.com/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/testx.png",
165+
// this one kills the regex pretty bad.
166+
"bar.example.com/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/testx.png",
165167
}
166168

167-
var (
168-
testIters = 10
169-
testURLSize = 10000
170-
)
171-
testURLs := make([]string, 0)
172-
for i := 0; i < testURLSize; i++ {
173-
testURLs = append(testURLs, testURLin[i%3])
174-
}
169+
testIters := 10000
175170

176171
dt := NewURLMatcher()
177172
for _, rule := range rules {
@@ -183,7 +178,6 @@ func BenchmarkHTrieMatch(b *testing.B) {
183178
for _, u := range testURLs {
184179
u, _ := url.Parse(u)
185180
parsed = append(parsed, u)
186-
187181
}
188182

189183
// avoid inlining optimization
@@ -200,30 +194,26 @@ func BenchmarkHTrieMatch(b *testing.B) {
200194

201195
func BenchmarkRegexMatch(b *testing.B) {
202196
rules := []string{
203-
`^foo.example.net/test.png`,
197+
// giving regex lots of help here, putting this rule first
198+
`^.*\.example.com/.*/test.png`,
204199
`^bar.example.net/test.png`,
200+
`^foo.example.net/test.png`,
205201
`^.*\.bar.example.net/test.png`,
206202
`^.*\.hodor.example.net/.*/test.png`,
207-
`^.*\.example.com/.*/test.png`,
208203
`^(.*\.)?example.org/(?:i.*/test.png)`,
209204
}
210205

211-
testURLin := []string{
206+
testURLs := []string{
212207
"example.com/foo/test.png",
213208
"bar.example.com/foo/test.png",
214209
"bar.example.com/foo/testx.png",
215210
"bar.example.com/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/test.png",
216211
"bar.example.com/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/testx.png",
212+
// this one kills the regex pretty bad. :(
213+
//"bar.example.com/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/foo/testx.png",
217214
}
218215

219-
var (
220-
testIters = 10
221-
testURLSize = 10000
222-
)
223-
testURLs := make([]string, 0)
224-
for i := 0; i < testURLSize; i++ {
225-
testURLs = append(testURLs, testURLin[i%3])
226-
}
216+
testIters := 10000
227217

228218
rexes := make([]*regexp.Regexp, 0)
229219
for _, r := range rules {

0 commit comments

Comments
 (0)