Skip to content

Commit 6b5e62b

Browse files
committed
fix: route containing escaped colon should be matchable but is not matched to request path (fixes #2046)
1 parent 7bde9ae commit 6b5e62b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

router.go

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
9999
for i, lcpIndex := 0, len(path); i < lcpIndex; i++ {
100100
if path[i] == ':' {
101101
if i > 0 && path[i-1] == '\\' {
102+
path = path[:i-1] + path[i:]
103+
i--
104+
lcpIndex--
102105
continue
103106
}
104107
j := i + 1

router_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,8 @@ func TestRouterParam_escapeColon(t *testing.T) {
11241124
e := New()
11251125

11261126
e.POST("/files/a/long/file\\:undelete", handlerFunc)
1127+
e.POST("/multilevel\\:undelete/second\\:something", handlerFunc)
1128+
e.POST("/mixed/:id/second\\:something", handlerFunc)
11271129
e.POST("/v1/some/resource/name:customVerb", handlerFunc)
11281130

11291131
var testCases = []struct {
@@ -1133,12 +1135,22 @@ func TestRouterParam_escapeColon(t *testing.T) {
11331135
expectError string
11341136
}{
11351137
{
1136-
whenURL: "/files/a/long/file\\:undelete",
1138+
whenURL: "/files/a/long/file:undelete",
11371139
expectRoute: "/files/a/long/file\\:undelete",
11381140
expectParam: map[string]string{},
11391141
},
11401142
{
1141-
whenURL: "/files/a/long/file\\:notMatching",
1143+
whenURL: "/multilevel:undelete/second:something",
1144+
expectRoute: "/multilevel\\:undelete/second\\:something",
1145+
expectParam: map[string]string{},
1146+
},
1147+
{
1148+
whenURL: "/mixed/123/second:something",
1149+
expectRoute: "/mixed/:id/second\\:something",
1150+
expectParam: map[string]string{"id": "123"},
1151+
},
1152+
{
1153+
whenURL: "/files/a/long/file:notMatching",
11421154
expectRoute: nil,
11431155
expectError: "code=404, message=Not Found",
11441156
expectParam: nil,

0 commit comments

Comments
 (0)