From ffef4c0b8d8cf16f1ba949db0d7423c319dbab82 Mon Sep 17 00:00:00 2001 From: toimtoimtoim Date: Thu, 16 Dec 2021 22:58:40 +0200 Subject: [PATCH] fix: route containing escaped colon should be matchable but is not matched to request path (fixes #2046) --- router.go | 3 +++ router_test.go | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/router.go b/router.go index a8277c8b8..dc93e29c8 100644 --- a/router.go +++ b/router.go @@ -99,6 +99,9 @@ func (r *Router) Add(method, path string, h HandlerFunc) { for i, lcpIndex := 0, len(path); i < lcpIndex; i++ { if path[i] == ':' { if i > 0 && path[i-1] == '\\' { + path = path[:i-1] + path[i:] + i-- + lcpIndex-- continue } j := i + 1 diff --git a/router_test.go b/router_test.go index 1cb36b447..57be74deb 100644 --- a/router_test.go +++ b/router_test.go @@ -1124,6 +1124,8 @@ func TestRouterParam_escapeColon(t *testing.T) { e := New() 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) var testCases = []struct { @@ -1133,12 +1135,22 @@ func TestRouterParam_escapeColon(t *testing.T) { expectError string }{ { - whenURL: "/files/a/long/file\\:undelete", + whenURL: "/files/a/long/file:undelete", expectRoute: "/files/a/long/file\\:undelete", 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, expectError: "code=404, message=Not Found", expectParam: nil,