1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

fix: route containing escaped colon should be matchable but is not matched to request path (fixes #2046)

This commit is contained in:
toimtoimtoim 2021-12-16 22:58:40 +02:00 committed by Martti T
parent 7bde9aea06
commit 6b5e62b27e
2 changed files with 17 additions and 2 deletions

View File

@ -99,6 +99,9 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
for i, lcpIndex := 0, len(path); i < lcpIndex; i++ { for i, lcpIndex := 0, len(path); i < lcpIndex; i++ {
if path[i] == ':' { if path[i] == ':' {
if i > 0 && path[i-1] == '\\' { if i > 0 && path[i-1] == '\\' {
path = path[:i-1] + path[i:]
i--
lcpIndex--
continue continue
} }
j := i + 1 j := i + 1

View File

@ -1124,6 +1124,8 @@ func TestRouterParam_escapeColon(t *testing.T) {
e := New() e := New()
e.POST("/files/a/long/file\\:undelete", handlerFunc) e.POST("/files/a/long/file\\:undelete", handlerFunc)
e.POST("/multilevel\\:undelete/second\\:something", handlerFunc)
e.POST("/mixed/:id/second\\:something", handlerFunc)
e.POST("/v1/some/resource/name:customVerb", handlerFunc) e.POST("/v1/some/resource/name:customVerb", handlerFunc)
var testCases = []struct { var testCases = []struct {
@ -1133,12 +1135,22 @@ func TestRouterParam_escapeColon(t *testing.T) {
expectError string expectError string
}{ }{
{ {
whenURL: "/files/a/long/file\\:undelete", whenURL: "/files/a/long/file:undelete",
expectRoute: "/files/a/long/file\\:undelete", expectRoute: "/files/a/long/file\\:undelete",
expectParam: map[string]string{}, expectParam: map[string]string{},
}, },
{ {
whenURL: "/files/a/long/file\\:notMatching", whenURL: "/multilevel:undelete/second:something",
expectRoute: "/multilevel\\:undelete/second\\:something",
expectParam: map[string]string{},
},
{
whenURL: "/mixed/123/second:something",
expectRoute: "/mixed/:id/second\\:something",
expectParam: map[string]string{"id": "123"},
},
{
whenURL: "/files/a/long/file:notMatching",
expectRoute: nil, expectRoute: nil,
expectError: "code=404, message=Not Found", expectError: "code=404, message=Not Found",
expectParam: nil, expectParam: nil,