Skip to content

Commit 9b1fa09

Browse files
committed
Fix catch-all and segment root conflict check
closes #3
1 parent cafe406 commit 9b1fa09

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

tree.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (n *node) insertChild(method, path string, handle Handle) {
153153

154154
// find prefix until first wildcard (beginning with ':'' or '*'')
155155
for i, j := 0, len(path); i < j; i++ {
156-
if b := path[i]; b == ':' || b == '*' {
156+
if c := path[i]; c == ':' || c == '*' {
157157
// Check if this Node existing children which would be
158158
// unreachable if we insert the wildcard here
159159
if len(n.children) > 0 {
@@ -170,7 +170,7 @@ func (n *node) insertChild(method, path string, handle Handle) {
170170
panic("wildcards must be named with a non-empty name")
171171
}
172172

173-
if b == ':' { // param
173+
if c == ':' { // param
174174
// split path at the beginning of the wildcard
175175
if i > 0 {
176176
n.path = path[offset:i]
@@ -199,13 +199,17 @@ func (n *node) insertChild(method, path string, handle Handle) {
199199

200200
} else { // catchAll
201201
if len(path) != k {
202-
panic("catchAlls are only allowed at the end of the path")
202+
panic("catch-all routes are only allowed at the end of the path")
203+
}
204+
205+
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
206+
panic("catch-all conflicts with existing handle for the path segment root")
203207
}
204208

205209
// currently fixed width 1 for '/'
206210
i--
207211
if path[i] != '/' {
208-
panic("no / before catchAll")
212+
panic("no / before catch-all")
209213
}
210214

211215
n.path = path[offset:i]

tree_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func printChildren(n *node, prefix string) {
15-
fmt.Printf(" %02d %s%s[%d] %v %t \r\n", n.priority, prefix, n.path, len(n.children), n.handle, n.wildChild)
15+
fmt.Printf(" %02d %s%s[%d] %v %t %d \r\n", n.priority, prefix, n.path, len(n.children), n.handle, n.wildChild, n.nType)
1616
for l := len(n.path); l > 0; l-- {
1717
prefix += " "
1818
}
@@ -290,6 +290,14 @@ func TestTreeCatchAllConflict(t *testing.T) {
290290
testRoutes(t, routes)
291291
}
292292

293+
func TestTreeCatchAllConflictRoot(t *testing.T) {
294+
routes := []testRoute{
295+
{"/", false},
296+
{"/*filepath", true},
297+
}
298+
testRoutes(t, routes)
299+
}
300+
293301
func TestTreeMultipleHandlers(t *testing.T) {
294302
tree := &node{}
295303

0 commit comments

Comments
 (0)