3a5de83e by astaxie

beego: support router case sensitive

1 parent f5f33955
...@@ -81,6 +81,7 @@ var ( ...@@ -81,6 +81,7 @@ var (
81 FlashSeperator string // used to seperate flash key:value 81 FlashSeperator string // used to seperate flash key:value
82 AppConfigProvider string // config provider 82 AppConfigProvider string // config provider
83 EnableDocs bool // enable generate docs & server docs API Swagger 83 EnableDocs bool // enable generate docs & server docs API Swagger
84 RouterCaseSensitive bool // router case sensitive default is true
84 ) 85 )
85 86
86 func init() { 87 func init() {
...@@ -164,6 +165,8 @@ func init() { ...@@ -164,6 +165,8 @@ func init() {
164 FlashName = "BEEGO_FLASH" 165 FlashName = "BEEGO_FLASH"
165 FlashSeperator = "BEEGOFLASH" 166 FlashSeperator = "BEEGOFLASH"
166 167
168 RouterCaseSensitive = true
169
167 runtime.GOMAXPROCS(runtime.NumCPU()) 170 runtime.GOMAXPROCS(runtime.NumCPU())
168 171
169 // init BeeLogger 172 // init BeeLogger
...@@ -375,6 +378,10 @@ func ParseConfig() (err error) { ...@@ -375,6 +378,10 @@ func ParseConfig() (err error) {
375 if enabledocs, err := GetConfig("bool", "EnableDocs"); err == nil { 378 if enabledocs, err := GetConfig("bool", "EnableDocs"); err == nil {
376 EnableDocs = enabledocs.(bool) 379 EnableDocs = enabledocs.(bool)
377 } 380 }
381
382 if casesensitive, err := GetConfig("bool", "RouterCaseSensitive"); err == nil {
383 RouterCaseSensitive = casesensitive.(bool)
384 }
378 } 385 }
379 return nil 386 return nil
380 } 387 }
......
...@@ -163,6 +163,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM ...@@ -163,6 +163,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
163 } 163 }
164 164
165 func (p *ControllerRegistor) addToRouter(method, pattern string, r *controllerInfo) { 165 func (p *ControllerRegistor) addToRouter(method, pattern string, r *controllerInfo) {
166 if !RouterCaseSensitive {
167 pattern = strings.ToLower(pattern)
168 }
166 if t, ok := p.routers[method]; ok { 169 if t, ok := p.routers[method]; ok {
167 t.AddRouter(pattern, r) 170 t.AddRouter(pattern, r)
168 } else { 171 } else {
...@@ -381,6 +384,9 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter ...@@ -381,6 +384,9 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter
381 mr.tree = NewTree() 384 mr.tree = NewTree()
382 mr.pattern = pattern 385 mr.pattern = pattern
383 mr.filterFunc = filter 386 mr.filterFunc = filter
387 if !RouterCaseSensitive {
388 pattern = strings.ToLower(pattern)
389 }
384 mr.tree.AddRouter(pattern, true) 390 mr.tree.AddRouter(pattern, true)
385 return p.insertFilterRouter(pos, mr) 391 return p.insertFilterRouter(pos, mr)
386 } 392 }
...@@ -565,12 +571,18 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -565,12 +571,18 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
565 context.Output.Context = context 571 context.Output.Context = context
566 context.Output.EnableGzip = EnableGzip 572 context.Output.EnableGzip = EnableGzip
567 573
574 var urlPath string
575 if !RouterCaseSensitive {
576 urlPath = strings.ToLower(r.URL.Path)
577 } else {
578 urlPath = r.URL.Path
579 }
568 // defined filter function 580 // defined filter function
569 do_filter := func(pos int) (started bool) { 581 do_filter := func(pos int) (started bool) {
570 if p.enableFilter { 582 if p.enableFilter {
571 if l, ok := p.filters[pos]; ok { 583 if l, ok := p.filters[pos]; ok {
572 for _, filterR := range l { 584 for _, filterR := range l {
573 if ok, p := filterR.ValidRouter(r.URL.Path); ok { 585 if ok, p := filterR.ValidRouter(urlPath); ok {
574 context.Input.Params = p 586 context.Input.Params = p
575 filterR.filterFunc(context) 587 filterR.filterFunc(context)
576 if w.started { 588 if w.started {
...@@ -628,7 +640,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -628,7 +640,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
628 640
629 if !findrouter { 641 if !findrouter {
630 if t, ok := p.routers[r.Method]; ok { 642 if t, ok := p.routers[r.Method]; ok {
631 runObject, p := t.Match(r.URL.Path) 643 runObject, p := t.Match(urlPath)
632 if r, ok := runObject.(*controllerInfo); ok { 644 if r, ok := runObject.(*controllerInfo); ok {
633 routerInfo = r 645 routerInfo = r
634 findrouter = true 646 findrouter = true
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!