1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:01:28 +00:00

beego: support router case sensitive

This commit is contained in:
astaxie 2014-09-28 22:10:43 +08:00
parent f5f3395560
commit 3a5de83ec2
2 changed files with 21 additions and 2 deletions

View File

@ -81,6 +81,7 @@ var (
FlashSeperator string // used to seperate flash key:value FlashSeperator string // used to seperate flash key:value
AppConfigProvider string // config provider AppConfigProvider string // config provider
EnableDocs bool // enable generate docs & server docs API Swagger EnableDocs bool // enable generate docs & server docs API Swagger
RouterCaseSensitive bool // router case sensitive default is true
) )
func init() { func init() {
@ -164,6 +165,8 @@ func init() {
FlashName = "BEEGO_FLASH" FlashName = "BEEGO_FLASH"
FlashSeperator = "BEEGOFLASH" FlashSeperator = "BEEGOFLASH"
RouterCaseSensitive = true
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
// init BeeLogger // init BeeLogger
@ -375,6 +378,10 @@ func ParseConfig() (err error) {
if enabledocs, err := GetConfig("bool", "EnableDocs"); err == nil { if enabledocs, err := GetConfig("bool", "EnableDocs"); err == nil {
EnableDocs = enabledocs.(bool) EnableDocs = enabledocs.(bool)
} }
if casesensitive, err := GetConfig("bool", "RouterCaseSensitive"); err == nil {
RouterCaseSensitive = casesensitive.(bool)
}
} }
return nil return nil
} }

View File

@ -163,6 +163,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
} }
func (p *ControllerRegistor) addToRouter(method, pattern string, r *controllerInfo) { func (p *ControllerRegistor) addToRouter(method, pattern string, r *controllerInfo) {
if !RouterCaseSensitive {
pattern = strings.ToLower(pattern)
}
if t, ok := p.routers[method]; ok { if t, ok := p.routers[method]; ok {
t.AddRouter(pattern, r) t.AddRouter(pattern, r)
} else { } else {
@ -381,6 +384,9 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter
mr.tree = NewTree() mr.tree = NewTree()
mr.pattern = pattern mr.pattern = pattern
mr.filterFunc = filter mr.filterFunc = filter
if !RouterCaseSensitive {
pattern = strings.ToLower(pattern)
}
mr.tree.AddRouter(pattern, true) mr.tree.AddRouter(pattern, true)
return p.insertFilterRouter(pos, mr) return p.insertFilterRouter(pos, mr)
} }
@ -565,12 +571,18 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context.Output.Context = context context.Output.Context = context
context.Output.EnableGzip = EnableGzip context.Output.EnableGzip = EnableGzip
var urlPath string
if !RouterCaseSensitive {
urlPath = strings.ToLower(r.URL.Path)
} else {
urlPath = r.URL.Path
}
// defined filter function // defined filter function
do_filter := func(pos int) (started bool) { do_filter := func(pos int) (started bool) {
if p.enableFilter { if p.enableFilter {
if l, ok := p.filters[pos]; ok { if l, ok := p.filters[pos]; ok {
for _, filterR := range l { for _, filterR := range l {
if ok, p := filterR.ValidRouter(r.URL.Path); ok { if ok, p := filterR.ValidRouter(urlPath); ok {
context.Input.Params = p context.Input.Params = p
filterR.filterFunc(context) filterR.filterFunc(context)
if w.started { if w.started {
@ -628,7 +640,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if !findrouter { if !findrouter {
if t, ok := p.routers[r.Method]; ok { if t, ok := p.routers[r.Method]; ok {
runObject, p := t.Match(r.URL.Path) runObject, p := t.Match(urlPath)
if r, ok := runObject.(*controllerInfo); ok { if r, ok := runObject.(*controllerInfo); ok {
routerInfo = r routerInfo = r
findrouter = true findrouter = true