From 085c362ffb2e6f9613455af4bff06f8ca16c3bcf Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 18 Jun 2014 23:32:47 +0800 Subject: [PATCH] beego:fix router expge --- router.go | 10 ++++++++-- router_test.go | 7 +++++++ tree.go | 7 +++++++ tree_test.go | 1 + utils/captcha/captcha.go | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/router.go b/router.go index 51b7eaa0..00c7f237 100644 --- a/router.go +++ b/router.go @@ -428,7 +428,9 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin if c.routerType == routerTypeBeego && c.controllerType.Name() == controllName { find := false if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok { - if m, ok := c.methods[strings.ToUpper(methodName)]; ok && m == strings.ToUpper(methodName) { + if len(c.methods) == 0 { + find = true + } else if m, ok := c.methods[strings.ToUpper(methodName)]; ok && m == strings.ToUpper(methodName) { find = true } else if m, ok = c.methods["*"]; ok && m == methodName { find = true @@ -504,7 +506,11 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin } } if l.regexps.MatchString(regurl) { - return true, url + "/" + regurl + tourl(params) + if url == "/" { + return true, url + regurl + tourl(params) + } else { + return true, url + "/" + regurl + tourl(params) + } } } } diff --git a/router_test.go b/router_test.go index 60a16248..83dc5dca 100644 --- a/router_test.go +++ b/router_test.go @@ -99,6 +99,7 @@ func TestUrlFor2(t *testing.T) { handler := NewControllerRegister() handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List") handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param") + handler.Add("/:year:int/:month:int/:title/:entid", &TestController{}) if handler.UrlFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") != "/v1/za/cms_12_123.html" { Info(handler.UrlFor("TestController.List")) @@ -109,6 +110,12 @@ func TestUrlFor2(t *testing.T) { Info(handler.UrlFor("TestController.Param")) t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html") } + if handler.UrlFor("TestController.Get", ":year", "1111", ":month", "11", + ":title", "aaaa", ":entid", "aaaa") != + "/1111/11/aaaa/aaaa" { + Info(handler.UrlFor("TestController.Get")) + t.Errorf("TestController.Get must equal to /1111/11/aaaa/aaaa") + } } func TestUserFunc(t *testing.T) { diff --git a/tree.go b/tree.go index 26591a8c..10ebb428 100644 --- a/tree.go +++ b/tree.go @@ -160,6 +160,13 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, } else { regexpStr = "/" + regexpStr } + } else if reg != "" { + for _, w := range params { + if w == "." || w == ":" { + continue + } + regexpStr = "/([^/]+)" + regexpStr + } } t.wildcard.addseg(segments[1:], route, append(wildcards, params...), reg+regexpStr) } else { diff --git a/tree_test.go b/tree_test.go index 09f768a1..991611f7 100644 --- a/tree_test.go +++ b/tree_test.go @@ -24,6 +24,7 @@ func init() { routers = append(routers, testinfo{"/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) routers = append(routers, testinfo{"/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) routers = append(routers, testinfo{"/v1/shop/:id:int", "/v1/shop/123", map[string]string{":id": "123"}}) + routers = append(routers, testinfo{"/:year:int/:month:int/:id/:endid", "/1111/111/aaa/aaa", map[string]string{":year": "1111", ":month": "111", ":id": "aaa", ":endid": "aaa"}}) routers = append(routers, testinfo{"/v1/shop/:id/:name", "/v1/shop/123/nike", map[string]string{":id": "123", ":name": "nike"}}) routers = append(routers, testinfo{"/v1/shop/:id/account", "/v1/shop/123/account", map[string]string{":id": "123"}}) routers = append(routers, testinfo{"/v1/shop/:name:string", "/v1/shop/nike", map[string]string{":name": "nike"}}) diff --git a/utils/captcha/captcha.go b/utils/captcha/captcha.go index f12634d5..16d85c37 100644 --- a/utils/captcha/captcha.go +++ b/utils/captcha/captcha.go @@ -248,7 +248,7 @@ func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha { cpt := NewCaptcha(urlPrefix, store) // create filter for serve captcha image - beego.AddFilter(cpt.URLPrefix+":", "BeforeRouter", cpt.Handler) + beego.InsertFilter(cpt.URLPrefix+":", beego.BeforeRouter, cpt.Handler) // add to template func map beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHtml)