mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-07 10:51:26 +08:00
feat: add escaped colon support in route paths
This commit is contained in:
parent
9914178584
commit
4f89cd5dbd
12
tree.go
12
tree.go
@ -272,7 +272,19 @@ func findWildcard(path string) (wildcard string, i int, valid bool) {
|
|||||||
|
|
||||||
// Find end and check for invalid characters
|
// Find end and check for invalid characters
|
||||||
valid = true
|
valid = true
|
||||||
|
escapeNextColon := false
|
||||||
for end, c := range []byte(path[start+1:]) {
|
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 {
|
switch c {
|
||||||
case '/':
|
case '/':
|
||||||
return path[start : start+1+end], start, valid
|
return path[start : start+1+end], start, valid
|
||||||
|
|||||||
26
tree_test.go
26
tree_test.go
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user