Skip to content

Commit

Permalink
Add escaped 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 c1585c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
23 changes: 23 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
)

const defaultMultipartMemory = 32 << 20 // 32 MB
const escapedColon = "\\:"
const colon = ":"
const backslash = "\\"

var (
default404Body = []byte("404 page not found")
Expand Down Expand Up @@ -345,6 +348,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 +398,25 @@ func (engine *Engine) parseTrustedProxies() error {
return err
}

// updateRouteTree do update to the route tree recursively
func updateRouteTree(n *node) {
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
5 changes: 5 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

0 comments on commit c1585c2

Please sign in to comment.