1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-04 21:50:54 +00:00
Beego/filter.go

57 lines
1.6 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
2013-09-09 16:00:11 +00:00
package beego
import "regexp"
2013-09-09 16:00:11 +00:00
// FilterRouter defines filter operation before controller handler execution.
// it can match patterned url and do filter function when action arrives.
2013-09-09 16:00:11 +00:00
type FilterRouter struct {
2013-11-26 08:47:50 +00:00
pattern string
regex *regexp.Regexp
filterFunc FilterFunc
hasregex bool
params map[int]string
parseParams map[string]string
2013-09-09 16:00:11 +00:00
}
// ValidRouter check current request is valid for this filter.
// if matched, returns parsed params in this request by defined filter router pattern.
2013-11-26 08:47:50 +00:00
func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) {
2013-09-09 16:00:11 +00:00
if mr.pattern == "" {
2013-11-26 08:47:50 +00:00
return true, nil
2013-09-09 16:00:11 +00:00
}
2013-09-19 15:14:47 +00:00
if mr.pattern == "*" {
2013-11-26 08:47:50 +00:00
return true, nil
2013-09-19 15:14:47 +00:00
}
2013-09-09 16:00:11 +00:00
if router == mr.pattern {
2013-11-26 08:47:50 +00:00
return true, nil
2013-09-09 16:00:11 +00:00
}
//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
}
2013-09-09 16:00:11 +00:00
if mr.hasregex {
2013-11-26 08:47:50 +00:00
if !mr.regex.MatchString(router) {
return false, nil
2013-09-09 16:00:11 +00:00
}
matches := mr.regex.FindStringSubmatch(router)
2013-10-12 08:56:30 +00:00
if len(matches) > 0 {
if len(matches[0]) == len(router) {
2013-11-26 08:47:50 +00:00
params := make(map[string]string)
for i, match := range matches[1:] {
params[mr.params[i]] = match
}
return true, params
2013-10-12 08:56:30 +00:00
}
2013-09-09 16:00:11 +00:00
}
}
2013-11-26 08:47:50 +00:00
return false, nil
}