pick the fix 4459: /abc.html/aaa match /abc/aaa

This commit is contained in:
Deng Ming 2022-04-25 12:14:31 +08:00
parent ebb13b7941
commit e593efdb15

27
tree.go
View File

@ -32,13 +32,13 @@ var (
// wildcard stores params // wildcard stores params
// leaves store the endpoint information // leaves store the endpoint information
type Tree struct { type Tree struct {
//prefix set for static router // prefix set for static router
prefix string prefix string
//search fix route first // search fix route first
fixrouters []*Tree fixrouters []*Tree
//if set, failure to match fixrouters search then search wildcard // if set, failure to match fixrouters search then search wildcard
wildcard *Tree wildcard *Tree
//if set, failure to match wildcard search // if set, failure to match wildcard search
leaves []*leafInfo leaves []*leafInfo
} }
@ -68,13 +68,13 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st
filterTreeWithPrefix(tree, wildcards, reg) filterTreeWithPrefix(tree, wildcards, reg)
} }
} }
//Rule: /login/*/access match /login/2009/11/access // Rule: /login/*/access match /login/2009/11/access
//if already has *, and when loop the access, should as a regexpStr // if already has *, and when loop the access, should as a regexpStr
if !iswild && utils.InSlice(":splat", wildcards) { if !iswild && utils.InSlice(":splat", wildcards) {
iswild = true iswild = true
regexpStr = seg regexpStr = seg
} }
//Rule: /user/:id/* // Rule: /user/:id/*
if seg == "*" && len(wildcards) > 0 && reg == "" { if seg == "*" && len(wildcards) > 0 && reg == "" {
regexpStr = "(.+)" regexpStr = "(.+)"
} }
@ -221,13 +221,13 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string,
t.addseg(segments[1:], route, wildcards, reg) t.addseg(segments[1:], route, wildcards, reg)
params = params[1:] params = params[1:]
} }
//Rule: /login/*/access match /login/2009/11/access // Rule: /login/*/access match /login/2009/11/access
//if already has *, and when loop the access, should as a regexpStr // if already has *, and when loop the access, should as a regexpStr
if !iswild && utils.InSlice(":splat", wildcards) { if !iswild && utils.InSlice(":splat", wildcards) {
iswild = true iswild = true
regexpStr = seg regexpStr = seg
} }
//Rule: /user/:id/* // Rule: /user/:id/*
if seg == "*" && len(wildcards) > 0 && reg == "" { if seg == "*" && len(wildcards) > 0 && reg == "" {
regexpStr = "(.+)" regexpStr = "(.+)"
} }
@ -341,8 +341,9 @@ func (t *Tree) match(treePattern string, pattern string, wildcardValues []string
if runObject == nil && len(t.fixrouters) > 0 { if runObject == nil && len(t.fixrouters) > 0 {
// Filter the .json .xml .html extension // Filter the .json .xml .html extension
for _, str := range allowSuffixExt { for _, str := range allowSuffixExt {
if strings.HasSuffix(seg, str) { if strings.HasSuffix(seg, str) && strings.HasSuffix(treePattern, seg) {
for _, subTree := range t.fixrouters { for _, subTree := range t.fixrouters {
// strings.HasSuffix(treePattern, seg) avoid cases: /aaa.html/bbb could access /aaa/bbb
if subTree.prefix == seg[:len(seg)-len(str)] { if subTree.prefix == seg[:len(seg)-len(str)] {
runObject = subTree.match(treePattern, pattern, wildcardValues, ctx) runObject = subTree.match(treePattern, pattern, wildcardValues, ctx)
if runObject != nil { if runObject != nil {
@ -392,7 +393,7 @@ type leafInfo struct {
} }
func (leaf *leafInfo) match(treePattern string, wildcardValues []string, ctx *context.Context) (ok bool) { func (leaf *leafInfo) match(treePattern string, wildcardValues []string, ctx *context.Context) (ok bool) {
//fmt.Println("Leaf:", wildcardValues, leaf.wildcards, leaf.regexps) // fmt.Println("Leaf:", wildcardValues, leaf.wildcards, leaf.regexps)
if leaf.regexps == nil { if leaf.regexps == nil {
if len(wildcardValues) == 0 && len(leaf.wildcards) == 0 { // static path if len(wildcardValues) == 0 && len(leaf.wildcards) == 0 { // static path
return true return true
@ -499,7 +500,7 @@ func splitSegment(key string) (bool, []string, string) {
continue continue
} }
if start { if start {
//:id:int and :name:string // :id:int and :name:string
if v == ':' { if v == ':' {
if len(key) >= i+4 { if len(key) >= i+4 {
if key[i+1:i+4] == "int" { if key[i+1:i+4] == "int" {