Skip to content

Commit

Permalink
Add literal colon support (#1432)
Browse files Browse the repository at this point in the history
  • Loading branch information
wssccc committed Sep 6, 2021
1 parent a550c56 commit ed0a30c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
23 changes: 23 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func (engine *Engine) Run(addr ...string) (err error) {
if err != nil {
return err
}
engine.updateRouteTrees()

address := resolveAddress(addr)
debugPrint("Listening and serving HTTP on %s\n", address)
Expand Down Expand Up @@ -394,6 +395,28 @@ func (engine *Engine) parseTrustedProxies() error {
return err
}

// updateRouteTree do update to the route tree recursively
func updateRouteTree(n *node) {
const escapedColon = "\\:"
const colon = ":"
const backslash = "\\"
n.path = strings.ReplaceAll(n.path, escapedColon, colon)
n.fullPath = strings.ReplaceAll(n.fullPath, escapedColon, colon)
n.indices = strings.ReplaceAll(n.indices, backslash, colon)
if n.children != nil {
for _, child := range n.children {
updateRouteTree(child)
}
}
}

// updateRouteTrees do update to the route trees
func (engine *Engine) updateRouteTrees() {
for _, tree := range engine.trees {
updateRouteTree(tree.root)
}
}

// parseIP parse a string representation of an IP and returns a net.IP with the
// minimum byte representation or nil if input is invalid.
func parseIP(ip string) net.IP {
Expand Down
17 changes: 16 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,20 @@ walk:
// Returns -1 as index, if no wildcard was found.
func findWildcard(path string) (wildcard string, i int, valid bool) {
// Find start
escapeColon := false
for start, c := range []byte(path) {
if escapeColon {
if c == ':' {
escapeColon = false
continue
} else {
panic("invalid escaped char in " + path)
}
}
if c == '\\' {
escapeColon = true
continue
}
// A wildcard starts with ':' (param) or '*' (catch-all)
if c != ':' && c != '*' {
continue
Expand Down Expand Up @@ -402,6 +415,7 @@ func (n *node) getValue(path string, params *Params, unescape bool) (value nodeV
var (
skippedPath string
latestNode = n // Caching the latest node
latestPath = path
)

walk: // Outer loop for walking the tree
Expand All @@ -427,6 +441,7 @@ walk: // Outer loop for walking the tree
handlers: n.handlers,
fullPath: n.fullPath,
}
latestPath = path
}

n = n.children[i]
Expand Down Expand Up @@ -457,7 +472,7 @@ walk: // Outer loop for walking the tree
// fix truncate the parameter
// tree_test.go line: 204
if matched {
path = prefix + path
path = latestPath
// The saved path is used after the prefix route is intercepted by matching
if n.indices == "/" {
path = skippedPath[1:]
Expand Down
24 changes: 24 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func TestTreeWildcard(t *testing.T) {
"/get/abc/123abg/:param",
"/get/abc/123abf/:param",
"/get/abc/123abfff/:param",
"/get/abc/escaped_colon/test\\:param",
}
for _, route := range routes {
tree.addRoute(route, fakeHandler(route))
Expand Down Expand Up @@ -305,6 +306,7 @@ func TestTreeWildcard(t *testing.T) {
{"/get/abc/123abg/test", false, "/get/abc/123abg/:param", Params{Param{Key: "param", Value: "test"}}},
{"/get/abc/123abf/testss", false, "/get/abc/123abf/:param", Params{Param{Key: "param", Value: "testss"}}},
{"/get/abc/123abfff/te", false, "/get/abc/123abfff/:param", Params{Param{Key: "param", Value: "te"}}},
{"/get/abc/escaped_colon/test\\:param", false, "/get/abc/escaped_colon/test\\:param", nil},
})

checkPriorities(t, tree)
Expand Down Expand Up @@ -407,6 +409,9 @@ func TestTreeWildcardConflict(t *testing.T) {
{"/user_:name", false},
{"/id:id", false},
{"/id/:id", false},
{"/escape/test\\:d1", false},
{"/escape/test\\:d2", false},
{"/escape/test:param", false},
}
testRoutes(t, routes)
}
Expand Down Expand Up @@ -886,3 +891,22 @@ func TestTreeWildcardConflictEx(t *testing.T) {
}
}
}

func TestTreeInvalidEscape(t *testing.T) {
routes := map[string]bool{
"/r1/r": true,
"/r2/:r": true,
"/r3/\\:r": true,
"/r4/\\\\:r": false,
"/r5/\\~:r": false,
}
tree := &node{}
for route, valid := range routes {
recv := catchPanic(func() {
tree.addRoute(route, fakeHandler(route))
})
if recv == nil != valid {
t.Fatalf("%s should be %t but got %v", route, valid, recv)
}
}
}

0 comments on commit ed0a30c

Please sign in to comment.