mirror of
https://github.com/astaxie/beego.git
synced 2025-07-13 16:31:01 +00:00
beego: change to tree
This commit is contained in:
49
filter.go
49
filter.go
@ -6,51 +6,24 @@
|
||||
|
||||
package beego
|
||||
|
||||
import "regexp"
|
||||
|
||||
// FilterRouter defines filter operation before controller handler execution.
|
||||
// it can match patterned url and do filter function when action arrives.
|
||||
type FilterRouter struct {
|
||||
pattern string
|
||||
regex *regexp.Regexp
|
||||
filterFunc FilterFunc
|
||||
hasregex bool
|
||||
params map[int]string
|
||||
parseParams map[string]string
|
||||
filterFunc FilterFunc
|
||||
tree *Tree
|
||||
pattern string
|
||||
}
|
||||
|
||||
// ValidRouter check current request is valid for this filter.
|
||||
// if matched, returns parsed params in this request by defined filter router pattern.
|
||||
func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) {
|
||||
if mr.pattern == "" {
|
||||
return true, nil
|
||||
func (f *FilterRouter) ValidRouter(router string) (bool, map[string]string) {
|
||||
isok, params := f.tree.Match(router)
|
||||
if isok == nil {
|
||||
return false, nil
|
||||
}
|
||||
if mr.pattern == "*" {
|
||||
return true, nil
|
||||
if isok, ok := isok.(bool); ok {
|
||||
return isok, params
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
if router == mr.pattern {
|
||||
return true, nil
|
||||
}
|
||||
//pattern /admin router /admin/ match
|
||||
//pattern /admin/ router /admin don't match, because url will 301 in router
|
||||
if n := len(router); n > 1 && router[n-1] == '/' && router[:n-2] == mr.pattern {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if mr.hasregex {
|
||||
if !mr.regex.MatchString(router) {
|
||||
return false, nil
|
||||
}
|
||||
matches := mr.regex.FindStringSubmatch(router)
|
||||
if len(matches) > 0 {
|
||||
if len(matches[0]) == len(router) {
|
||||
params := make(map[string]string)
|
||||
for i, match := range matches[1:] {
|
||||
params[mr.params[i]] = match
|
||||
}
|
||||
return true, params
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user