修复路由变量匹配到'/'字符的问题

This commit is contained in:
john
2018-10-26 11:39:17 +08:00
parent 77b317f182
commit 8315ada9db
2 changed files with 13 additions and 11 deletions

View File

@ -275,11 +275,11 @@ func (s *Server) patternToRegRule(rule string) (regrule string, names []string)
switch v[0] {
case ':':
if len(v) > 1 {
regrule += `/(.+)`
regrule += `/([^/]+)`
names = append(names, v[1:])
break
} else {
regrule += `/.+`
regrule += `/[^/]+`
break
}
fallthrough
@ -302,7 +302,7 @@ func (s *Server) patternToRegRule(rule string) (regrule string, names []string)
})
s, _ := gregex.ReplaceStringFunc(`\{[\w\.\-]+\}`, v, func(s string) string {
names = append(names, s[1 : len(s) - 1])
return `(.+)`
return `([^/]+)`
})
if strings.EqualFold(s, v) {
regrule += "/" + v

View File

@ -1,16 +1,18 @@
package main
import (
"gitee.com/johng/gf/g/os/gcron"
"gitee.com/johng/gf/g/os/glog"
"gitee.com/johng/gf/g/os/gtime"
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/net/ghttp"
)
func main() {
start := gtime.Second()
gcron.Add("*/5 * * * * ?", func() {
glog.Println(gtime.Second() - start)
s := g.Server()
s.BindHandler("/{class}-{course}/:name/*act", func(r *ghttp.Request){
r.Response.Writeln(r.Get("class"))
r.Response.Writeln(r.Get("course"))
r.Response.Writeln(r.Get("name"))
r.Response.Writeln(r.Get("act"))
})
select{}
s.SetPort(8199)
s.Run()
}