feat: add escaped colon support in route paths

This commit is contained in:
yuxialuozi 2026-01-09 21:35:39 +08:00
parent 9914178584
commit 4f89cd5dbd
2 changed files with 38 additions and 0 deletions

12
tree.go
View File

@ -272,7 +272,19 @@ func findWildcard(path string) (wildcard string, i int, valid bool) {
// Find end and check for invalid characters
valid = true
escapeNextColon := false
for end, c := range []byte(path[start+1:]) {
if escapeNextColon {
escapeNextColon = false
if c == ':' {
continue
}
panic("invalid escape string in path '" + path + "'")
}
if c == '\\' {
escapeNextColon = true
continue
}
switch c {
case '/':
return path[start : start+1+end], start, valid

View File

@ -1018,3 +1018,29 @@ func TestWildcardInvalidSlash(t *testing.T) {
}
}
}
func TestTreeEscapedColon(t *testing.T) {
routes := map[string]bool{
"/files/:id\\:undelete": true,
"/api/:resource\\:method": true,
"/escape/\\:static": true,
"/invalid/\\x": false,
}
for route, valid := range routes {
tree := &node{}
recv := catchPanic(func() {
tree.addRoute(route, fakeHandler(route))
})
if recv == nil != valid {
t.Fatalf("%s should be %t but got %v", route, valid, recv)
}
if recv != nil {
if rs, ok := recv.(string); !ok || !strings.Contains(rs, "invalid escape") {
t.Fatalf(`Expected panic with "invalid escape" for route '%s', got "%v"`, route, recv)
}
}
}
}