[beego]beego 1.6 版本中路由":splat"的bug

2024-01-30 745 views
4

访问路径是“http://127.0.0.1:8080/1/files/M03/C3/3D/oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12

在路由中的配置是 beego.Router("/1/files/*", &controllers.FileController{}, "post:Upload;delete:Delete")

在controller中想通过 url := fc.Ctx.Input.Param(":splat") 获取路由中“*”号的内容,但发现得到的url的变量值为:“1/files/M03/C3/3D/oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12”

于是调试beego中的源码: `// Param returns router param by a given key.

func (input *BeegoInput) Param(key string) string {

for i, v := range input.pnames {

    fmt.Println("get bee go param key:"+v )

    fmt.Println("get bee go param val:"+input.pvalues[i] )

}

for i, v := range input.pnames {

    if v == key && i <= len(input.pvalues) {

        return input.pvalues[i]

    }

}

return ""

}`

发现输出结果为: get bee go param key::splat get bee go param val:1/files/M03/C3/3D/oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12 get bee go param key::splat get bee go param val:M03/C3/3D/oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12 get bee go param key:0 get bee go param val:1 get bee go param key:1 get bee go param val:files get bee go param key:2 get bee go param val:M03 get bee go param key:3 get bee go param val:C3 get bee go param key:4 get bee go param val:3D get bee go param key:5 get bee go param val:oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12

从测试结果可知, 有两个 ":splat"的key,只返回了第一个错误的结果,正确的应该是第二个":splat"的值。

回答

9

hey @jeffbmob , I has test in mac , it work well. the param :splat = M03/C3/3D/oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12 my test code

func init() {
    beego.Router("/1/files/*", &RC{}, "post:Upload;delete:Delete")
}
type RC struct {
    beego.Controller
}
func (r *RC) Upload() {
    url := r.Ctx.Input.Param(":splat")
    beego.Debug(":splat =", url)
    r.Ctx.WriteString("OK")
}

run result :

bee   :1.4.1
beego :1.6.1
Go    :go version go1.5beta1 darwin/amd64

2016/03/19 21:51:46 [INFO] Uses 'issue' as 'appname'
2016/03/19 21:51:46 [INFO] Initializing watcher...
2016/03/19 21:51:46 [TRAC] Directory(/Users/laoyu/Desktop/mygo/src/github.com/ysqi/issue/controllers)
2016/03/19 21:51:46 [TRAC] Directory(/Users/laoyu/Desktop/mygo/src/github.com/ysqi/issue)
2016/03/19 21:51:46 [TRAC] Directory(/Users/laoyu/Desktop/mygo/src/github.com/ysqi/issue/routers)
2016/03/19 21:51:46 [TRAC] Directory(/Users/laoyu/Desktop/mygo/src/github.com/ysqi/issue/tests)
2016/03/19 21:51:46 [INFO] Start building...
2016/03/19 21:51:48 [SUCC] Build was successful
2016/03/19 21:51:48 [INFO] Restarting issue ...
2016/03/19 21:51:48 [INFO] ./issue is running...
2016/03/19 21:51:48 [asm_amd64.s:1696][I] http server Running on :8080
2016/03/19 21:51:49 [router.go:23][D] :splat = M03/C3/3D/oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12
2016/03/19 21:51:49 [router.go:829][D] | POST       | /1/files/M03/C3/3D/oYYBAFbVNqGAF-KTAAAA9D1jMPQ965.p12 | 852.838µs        | match      | /1/files/*                               |