1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-26 13:25:56 +00:00
This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-09-18 01:52:30 +03:00
parent 9e8a58bf3b
commit 3d99983d96
8 changed files with 111 additions and 49 deletions

View File

@@ -9,9 +9,13 @@ import (
const (
// ParamStart the character in string representation where the underline router starts its dynamic named parameter.
ParamStart = ":"
// paramStartCharacter is the character as rune of ParamStart.
paramStartCharacter = ':'
// WildcardParamStart the character in string representation where the underline router starts its dynamic wildcard
// path parameter.
WildcardParamStart = "*"
// wildcardParamStartCharacter is the character as rune of WildcardParamStart.
wildcardParamStartCharacter = '*'
)
// An iris-specific identical version of the https://github.com/kataras/muxie version 1.0.0 released at 15 Oct 2018
@@ -112,6 +116,9 @@ func slowPathSplit(path string) []string {
func (tr *trie) insert(path string, route context.RouteReadOnly, handlers context.Handlers) {
input := slowPathSplit(path)
if len(input) == 0 {
return
}
n := tr.root
if path == pathSep {
@@ -121,9 +128,13 @@ func (tr *trie) insert(path string, route context.RouteReadOnly, handlers contex
var paramKeys []string
for _, s := range input {
if len(s) == 0 {
continue
}
c := s[0]
if isParam, isWildcard := c == ParamStart[0], c == WildcardParamStart[0]; isParam || isWildcard {
if isParam, isWildcard := c == paramStartCharacter, c == wildcardParamStartCharacter; isParam || isWildcard {
n.hasDynamicChild = true
paramKeys = append(paramKeys, s[1:]) // without : or *.
@@ -166,7 +177,6 @@ func (tr *trie) insert(path string, route context.RouteReadOnly, handlers contex
}
n.staticKey = path[:i]
// fmt.Printf("trie.insert: (whole path=%v) Path: %s, Route name: %s, Handlers len: %d\n", n.end, n.key, route.Name(), len(handlers))
}
@@ -192,15 +202,26 @@ func (tr *trie) search(q string, params *context.RequestParams) *trieNode {
for {
if i == end || q[i] == pathSepB {
if child := n.getChild(q[start:i]); child != nil {
segment := q[start:i]
if child := n.getChild(segment); child != nil {
n = child
// Possible reserved param character, should catch it as
// dynamic node instead of static-path based.
if segment == ParamStart { // len(n.paramKeys) > 0 && (segment == ParamStart || segment == WildcardParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = segment
} else {
paramValues = append(paramValues, segment)
}
}
} else if n.childNamedParameter {
n = n.getChild(ParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = q[start:i]
paramValues[ln] = segment
} else {
paramValues = append(paramValues, q[start:i])
paramValues = append(paramValues, segment)
}
} else if n.childWildcardParameter {
n = n.getChild(WildcardParamStart)
@@ -213,7 +234,7 @@ func (tr *trie) search(q string, params *context.RequestParams) *trieNode {
break
} else {
n = n.findClosestParentWildcardNode()
if n != nil {
if n != nil && len(n.paramKeys) > 0 {
// means that it has :param/static and *wildcard, we go trhough the :param
// but the next path segment is not the /static, so go back to *wildcard
// instead of not found.
@@ -248,7 +269,7 @@ func (tr *trie) search(q string, params *context.RequestParams) *trieNode {
if n == nil || !n.end {
if n != nil { // we need it on both places, on last segment (below) or on the first unnknown (above).
if n = n.findClosestParentWildcardNode(); n != nil {
if n = n.findClosestParentWildcardNode(); n != nil && len(n.paramKeys) > 0 {
params.Set(n.paramKeys[0], q[len(n.staticKey):])
return n
}
@@ -263,6 +284,10 @@ func (tr *trie) search(q string, params *context.RequestParams) *trieNode {
// the /other2/*myparam and not the root wildcard, which is what we want.
//
n = tr.root.getChild(WildcardParamStart)
if len(n.paramKeys) == 0 { // fix crashes on /*/*/*.
return nil
}
params.Set(n.paramKeys[0], q[1:])
return n
}