Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: route containing escaped colon is not actually matched to the request path #2047

Merged
merged 1 commit into from Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions router.go
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions router_test.go
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down