mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 07:20:55 +00:00
beego:fix the some regexp routes to different func
This commit is contained in:
parent
3b807845f2
commit
117904be73
@ -256,11 +256,12 @@ func addPrefix(t *Tree, prefix string) {
|
|||||||
if t.wildcard != nil {
|
if t.wildcard != nil {
|
||||||
addPrefix(t.wildcard, prefix)
|
addPrefix(t.wildcard, prefix)
|
||||||
}
|
}
|
||||||
if t.leaf != nil {
|
for _, l := range t.leaves {
|
||||||
if c, ok := t.leaf.runObject.(*controllerInfo); ok {
|
if c, ok := l.runObject.(*controllerInfo); ok {
|
||||||
c.pattern = prefix + c.pattern
|
c.pattern = prefix + c.pattern
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Namespace Condition
|
// Namespace Condition
|
||||||
|
38
router.go
38
router.go
@ -423,8 +423,8 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
|
|||||||
return ok, u
|
return ok, u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t.leaf != nil {
|
for _, l := range t.leaves {
|
||||||
if c, ok := t.leaf.runObject.(*controllerInfo); ok {
|
if c, ok := l.runObject.(*controllerInfo); ok {
|
||||||
if c.routerType == routerTypeBeego && c.controllerType.Name() == controllName {
|
if c.routerType == routerTypeBeego && c.controllerType.Name() == controllName {
|
||||||
find := false
|
find := false
|
||||||
if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok {
|
if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok {
|
||||||
@ -442,20 +442,20 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if find {
|
if find {
|
||||||
if t.leaf.regexps == nil {
|
if l.regexps == nil {
|
||||||
if len(t.leaf.wildcards) == 0 {
|
if len(l.wildcards) == 0 {
|
||||||
return true, url
|
return true, url
|
||||||
}
|
}
|
||||||
if len(t.leaf.wildcards) == 1 {
|
if len(l.wildcards) == 1 {
|
||||||
if v, ok := params[t.leaf.wildcards[0]]; ok {
|
if v, ok := params[l.wildcards[0]]; ok {
|
||||||
delete(params, t.leaf.wildcards[0])
|
delete(params, l.wildcards[0])
|
||||||
return true, url + "/" + v + tourl(params)
|
return true, url + "/" + v + tourl(params)
|
||||||
}
|
}
|
||||||
if t.leaf.wildcards[0] == ":splat" {
|
if l.wildcards[0] == ":splat" {
|
||||||
return true, url + tourl(params)
|
return true, url + tourl(params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(t.leaf.wildcards) == 3 && t.leaf.wildcards[0] == "." {
|
if len(l.wildcards) == 3 && l.wildcards[0] == "." {
|
||||||
if p, ok := params[":path"]; ok {
|
if p, ok := params[":path"]; ok {
|
||||||
if e, isok := params[":ext"]; isok {
|
if e, isok := params[":ext"]; isok {
|
||||||
delete(params, ":path")
|
delete(params, ":path")
|
||||||
@ -465,7 +465,7 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
canskip := false
|
canskip := false
|
||||||
for _, v := range t.leaf.wildcards {
|
for _, v := range l.wildcards {
|
||||||
if v == ":" {
|
if v == ":" {
|
||||||
canskip = true
|
canskip = true
|
||||||
continue
|
continue
|
||||||
@ -485,33 +485,33 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
|
|||||||
} else {
|
} else {
|
||||||
var i int
|
var i int
|
||||||
var startreg bool
|
var startreg bool
|
||||||
url = url + "/"
|
regurl := ""
|
||||||
for _, v := range t.leaf.regexps.String() {
|
for _, v := range strings.Trim(l.regexps.String(), "^$") {
|
||||||
if v == '(' {
|
if v == '(' {
|
||||||
startreg = true
|
startreg = true
|
||||||
continue
|
continue
|
||||||
} else if v == ')' {
|
} else if v == ')' {
|
||||||
startreg = false
|
startreg = false
|
||||||
if v, ok := params[t.leaf.wildcards[i]]; ok {
|
if v, ok := params[l.wildcards[i]]; ok {
|
||||||
url = url + v
|
delete(params, l.wildcards[i])
|
||||||
|
regurl = regurl + v
|
||||||
i++
|
i++
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else if !startreg {
|
} else if !startreg {
|
||||||
url = string(append([]rune(url), v))
|
regurl = string(append([]rune(regurl), v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t.leaf.regexps.MatchString(url) {
|
if l.regexps.MatchString(regurl) {
|
||||||
return true, url
|
return true, url + "/" + regurl + tourl(params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return false, ""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,6 +95,22 @@ func TestUrlFor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
if handler.UrlFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") !=
|
||||||
|
"/v1/za/cms_12_123.html" {
|
||||||
|
Info(handler.UrlFor("TestController.List"))
|
||||||
|
t.Errorf("TestController.List must equal to /v1/za/cms_12_123.html")
|
||||||
|
}
|
||||||
|
if handler.UrlFor("TestController.Param", ":v", "za", ":id", "12", ":page", "123") !=
|
||||||
|
"/v1/za_cms/ttt_12_123.html" {
|
||||||
|
Info(handler.UrlFor("TestController.Param"))
|
||||||
|
t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUserFunc(t *testing.T) {
|
func TestUserFunc(t *testing.T) {
|
||||||
r, _ := http.NewRequest("GET", "/api/list", nil)
|
r, _ := http.NewRequest("GET", "/api/list", nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
46
tree.go
46
tree.go
@ -16,7 +16,7 @@ type Tree struct {
|
|||||||
wildcard *Tree
|
wildcard *Tree
|
||||||
|
|
||||||
//if set, failure to match wildcard search
|
//if set, failure to match wildcard search
|
||||||
leaf *leafInfo
|
leaves []*leafInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTree() *Tree {
|
func NewTree() *Tree {
|
||||||
@ -86,35 +86,35 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) {
|
|||||||
if t.wildcard != nil {
|
if t.wildcard != nil {
|
||||||
filterTreeWithPrefix(t.wildcard, wildcards, reg)
|
filterTreeWithPrefix(t.wildcard, wildcards, reg)
|
||||||
}
|
}
|
||||||
if t.leaf != nil {
|
for _, l := range t.leaves {
|
||||||
t.leaf.wildcards = append(wildcards, t.leaf.wildcards...)
|
l.wildcards = append(wildcards, l.wildcards...)
|
||||||
if reg != "" {
|
if reg != "" {
|
||||||
filterCards := []string{}
|
filterCards := []string{}
|
||||||
for _, v := range t.leaf.wildcards {
|
for _, v := range l.wildcards {
|
||||||
if v == ":" || v == "." {
|
if v == ":" || v == "." {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
filterCards = append(filterCards, v)
|
filterCards = append(filterCards, v)
|
||||||
}
|
}
|
||||||
t.leaf.wildcards = filterCards
|
l.wildcards = filterCards
|
||||||
t.leaf.regexps = regexp.MustCompile("^" + reg + strings.Trim(t.leaf.regexps.String(), "^$") + "$")
|
l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$")
|
||||||
} else {
|
} else {
|
||||||
if t.leaf.regexps != nil {
|
if l.regexps != nil {
|
||||||
filterCards := []string{}
|
filterCards := []string{}
|
||||||
for _, v := range t.leaf.wildcards {
|
for _, v := range l.wildcards {
|
||||||
if v == ":" || v == "." {
|
if v == ":" || v == "." {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
filterCards = append(filterCards, v)
|
filterCards = append(filterCards, v)
|
||||||
}
|
}
|
||||||
t.leaf.wildcards = filterCards
|
l.wildcards = filterCards
|
||||||
for _, w := range wildcards {
|
for _, w := range wildcards {
|
||||||
if w == "." || w == ":" {
|
if w == "." || w == ":" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
reg = "([^/]+)/" + reg
|
reg = "([^/]+)/" + reg
|
||||||
}
|
}
|
||||||
t.leaf.regexps = regexp.MustCompile("^" + reg + strings.Trim(t.leaf.regexps.String(), "^$") + "$")
|
l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,9 +137,9 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string,
|
|||||||
}
|
}
|
||||||
filterCards = append(filterCards, v)
|
filterCards = append(filterCards, v)
|
||||||
}
|
}
|
||||||
t.leaf = &leafInfo{runObject: route, wildcards: wildcards, regexps: regexp.MustCompile("^" + reg + "$")}
|
t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards, regexps: regexp.MustCompile("^" + reg + "$")})
|
||||||
} else {
|
} else {
|
||||||
t.leaf = &leafInfo{runObject: route, wildcards: wildcards}
|
t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards})
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -185,15 +185,18 @@ func (t *Tree) Match(pattern string) (runObject interface{}, params map[string]s
|
|||||||
func (t *Tree) match(segments []string, wildcardValues []string) (runObject interface{}, params map[string]string) {
|
func (t *Tree) match(segments []string, wildcardValues []string) (runObject interface{}, params map[string]string) {
|
||||||
// Handle leaf nodes:
|
// Handle leaf nodes:
|
||||||
if len(segments) == 0 {
|
if len(segments) == 0 {
|
||||||
if t.leaf != nil {
|
for _, l := range t.leaves {
|
||||||
if ok, pa := t.leaf.match(wildcardValues); ok {
|
if ok, pa := l.match(wildcardValues); ok {
|
||||||
return t.leaf.runObject, pa
|
return l.runObject, pa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t.wildcard != nil && t.wildcard.leaf != nil {
|
if t.wildcard != nil {
|
||||||
if ok, pa := t.wildcard.leaf.match(wildcardValues); ok {
|
for _, l := range t.wildcard.leaves {
|
||||||
return t.wildcard.leaf.runObject, pa
|
if ok, pa := l.match(wildcardValues); ok {
|
||||||
|
return l.runObject, pa
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@ -222,13 +225,12 @@ func (t *Tree) match(segments []string, wildcardValues []string) (runObject inte
|
|||||||
runObject, params = t.wildcard.match(segs, append(wildcardValues, seg))
|
runObject, params = t.wildcard.match(segs, append(wildcardValues, seg))
|
||||||
}
|
}
|
||||||
if runObject == nil {
|
if runObject == nil {
|
||||||
if t.leaf != nil {
|
for _, l := range t.leaves {
|
||||||
if ok, pa := t.leaf.match(append(wildcardValues, segments...)); ok {
|
if ok, pa := l.match(append(wildcardValues, segments...)); ok {
|
||||||
return t.leaf.runObject, pa
|
return l.runObject, pa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return runObject, params
|
return runObject, params
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user