mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 10:11:29 +00:00
Merge pull request #1976 from ysqi/develop
Fxied bug and Optimized code
This commit is contained in:
commit
3ca68f9e30
@ -1,8 +1,8 @@
|
|||||||
package beego
|
package beego
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestList_01(t *testing.T) {
|
func TestList_01(t *testing.T) {
|
||||||
@ -11,8 +11,8 @@ func TestList_01(t *testing.T) {
|
|||||||
t.Log(m)
|
t.Log(m)
|
||||||
om := oldMap()
|
om := oldMap()
|
||||||
for k, v := range om {
|
for k, v := range om {
|
||||||
if fmt.Sprint(m[k])!= fmt.Sprint(v) {
|
if fmt.Sprint(m[k]) != fmt.Sprint(v) {
|
||||||
t.Log(k, "old-key",v,"new-key", m[k])
|
t.Log(k, "old-key", v, "new-key", m[k])
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
app.go
4
app.go
@ -148,7 +148,7 @@ func (app *App) Run() {
|
|||||||
BeeLogger.Info("Start https server error, confict with http.Please reset https port")
|
BeeLogger.Info("Start https server error, confict with http.Please reset https port")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logs.Info("https server Running on %s", app.Server.Addr)
|
logs.Info("https server Running on https://%s", app.Server.Addr)
|
||||||
if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
|
if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
|
||||||
logs.Critical("ListenAndServeTLS: ", err)
|
logs.Critical("ListenAndServeTLS: ", err)
|
||||||
time.Sleep(100 * time.Microsecond)
|
time.Sleep(100 * time.Microsecond)
|
||||||
@ -159,7 +159,7 @@ func (app *App) Run() {
|
|||||||
if BConfig.Listen.EnableHTTP {
|
if BConfig.Listen.EnableHTTP {
|
||||||
go func() {
|
go func() {
|
||||||
app.Server.Addr = addr
|
app.Server.Addr = addr
|
||||||
logs.Info("http server Running on %s", app.Server.Addr)
|
logs.Info("http server Running on http://%s", app.Server.Addr)
|
||||||
if BConfig.Listen.ListenTCP4 {
|
if BConfig.Listen.ListenTCP4 {
|
||||||
ln, err := net.Listen("tcp4", app.Server.Addr)
|
ln, err := net.Listen("tcp4", app.Server.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -86,11 +86,10 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
|
|||||||
if _, ok := err.(*os.PathError); ok {
|
if _, ok := err.(*os.PathError); ok {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
line = bytes.TrimSpace(line)
|
||||||
if bytes.Equal(line, bEmpty) {
|
if bytes.Equal(line, bEmpty) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
line = bytes.TrimSpace(line)
|
|
||||||
|
|
||||||
var bComment []byte
|
var bComment []byte
|
||||||
switch {
|
switch {
|
||||||
case bytes.HasPrefix(line, bNumComment):
|
case bytes.HasPrefix(line, bNumComment):
|
||||||
|
@ -117,8 +117,7 @@ func TestAssignConfig_03(t *testing.T) {
|
|||||||
ac.Set("StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
|
ac.Set("StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
|
||||||
assignConfig(ac)
|
assignConfig(ac)
|
||||||
|
|
||||||
|
t.Logf("%#v", BConfig)
|
||||||
t.Logf("%#v",BConfig)
|
|
||||||
|
|
||||||
if BConfig.AppName != "test_app" {
|
if BConfig.AppName != "test_app" {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
5
error.go
5
error.go
@ -380,6 +380,11 @@ func ErrorController(c ControllerInterface) *App {
|
|||||||
return BeeApp
|
return BeeApp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exception Write HttpStatus with errCode and Exec error handler if exist.
|
||||||
|
func Exception(errCode uint64, ctx *context.Context) {
|
||||||
|
exception(strconv.FormatUint(errCode, 10), ctx)
|
||||||
|
}
|
||||||
|
|
||||||
// show error string as simple text message.
|
// show error string as simple text message.
|
||||||
// if error string is empty, show 503 or 500 error as default.
|
// if error string is empty, show 503 or 500 error as default.
|
||||||
func exception(errCode string, ctx *context.Context) {
|
func exception(errCode string, ctx *context.Context) {
|
||||||
|
254
router.go
254
router.go
@ -603,6 +603,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
findRouter bool
|
findRouter bool
|
||||||
runMethod string
|
runMethod string
|
||||||
routerInfo *controllerInfo
|
routerInfo *controllerInfo
|
||||||
|
isRunnable bool
|
||||||
)
|
)
|
||||||
context := p.pool.Get().(*beecontext.Context)
|
context := p.pool.Get().(*beecontext.Context)
|
||||||
context.Reset(rw, r)
|
context.Reset(rw, r)
|
||||||
@ -666,136 +667,127 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
goto Admin
|
goto Admin
|
||||||
}
|
}
|
||||||
|
|
||||||
if !findRouter {
|
routerInfo, findRouter = p.FindRouter(context)
|
||||||
httpMethod := r.Method
|
|
||||||
if t, ok := p.routers[httpMethod]; ok {
|
|
||||||
runObject := t.Match(urlPath, context)
|
|
||||||
if r, ok := runObject.(*controllerInfo); ok {
|
|
||||||
routerInfo = r
|
|
||||||
findRouter = true
|
|
||||||
if splat := context.Input.Param(":splat"); splat != "" {
|
|
||||||
for k, v := range strings.Split(splat, "/") {
|
|
||||||
context.Input.SetParam(strconv.Itoa(k), v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//if no matches to url, throw a not found exception
|
//if no matches to url, throw a not found exception
|
||||||
if !findRouter {
|
if !findRouter {
|
||||||
exception("404", context)
|
exception("404", context)
|
||||||
goto Admin
|
goto Admin
|
||||||
}
|
}
|
||||||
|
if splat := context.Input.Param(":splat"); splat != "" {
|
||||||
if findRouter {
|
for k, v := range strings.Split(splat, "/") {
|
||||||
//execute middleware filters
|
context.Input.SetParam(strconv.Itoa(k), v)
|
||||||
if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) {
|
|
||||||
goto Admin
|
|
||||||
}
|
|
||||||
isRunnable := false
|
|
||||||
if routerInfo != nil {
|
|
||||||
if routerInfo.routerType == routerTypeRESTFul {
|
|
||||||
if _, ok := routerInfo.methods[r.Method]; ok {
|
|
||||||
isRunnable = true
|
|
||||||
routerInfo.runFunction(context)
|
|
||||||
} else {
|
|
||||||
exception("405", context)
|
|
||||||
goto Admin
|
|
||||||
}
|
|
||||||
} else if routerInfo.routerType == routerTypeHandler {
|
|
||||||
isRunnable = true
|
|
||||||
routerInfo.handler.ServeHTTP(rw, r)
|
|
||||||
} else {
|
|
||||||
runRouter = routerInfo.controllerType
|
|
||||||
method := r.Method
|
|
||||||
if r.Method == "POST" && context.Input.Query("_method") == "PUT" {
|
|
||||||
method = "PUT"
|
|
||||||
}
|
|
||||||
if r.Method == "POST" && context.Input.Query("_method") == "DELETE" {
|
|
||||||
method = "DELETE"
|
|
||||||
}
|
|
||||||
if m, ok := routerInfo.methods[method]; ok {
|
|
||||||
runMethod = m
|
|
||||||
} else if m, ok = routerInfo.methods["*"]; ok {
|
|
||||||
runMethod = m
|
|
||||||
} else {
|
|
||||||
runMethod = method
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// also defined runRouter & runMethod from filter
|
|
||||||
if !isRunnable {
|
|
||||||
//Invoke the request handler
|
|
||||||
vc := reflect.New(runRouter)
|
|
||||||
execController, ok := vc.Interface().(ControllerInterface)
|
|
||||||
if !ok {
|
|
||||||
panic("controller is not ControllerInterface")
|
|
||||||
}
|
|
||||||
|
|
||||||
//call the controller init function
|
|
||||||
execController.Init(context, runRouter.Name(), runMethod, vc.Interface())
|
|
||||||
|
|
||||||
//call prepare function
|
|
||||||
execController.Prepare()
|
|
||||||
|
|
||||||
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
|
||||||
if BConfig.WebConfig.EnableXSRF {
|
|
||||||
execController.XSRFToken()
|
|
||||||
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
|
||||||
(r.Method == "POST" && (context.Input.Query("_method") == "DELETE" || context.Input.Query("_method") == "PUT")) {
|
|
||||||
execController.CheckXSRFCookie()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
execController.URLMapping()
|
|
||||||
|
|
||||||
if !context.ResponseWriter.Started {
|
|
||||||
//exec main logic
|
|
||||||
switch runMethod {
|
|
||||||
case "GET":
|
|
||||||
execController.Get()
|
|
||||||
case "POST":
|
|
||||||
execController.Post()
|
|
||||||
case "DELETE":
|
|
||||||
execController.Delete()
|
|
||||||
case "PUT":
|
|
||||||
execController.Put()
|
|
||||||
case "HEAD":
|
|
||||||
execController.Head()
|
|
||||||
case "PATCH":
|
|
||||||
execController.Patch()
|
|
||||||
case "OPTIONS":
|
|
||||||
execController.Options()
|
|
||||||
default:
|
|
||||||
if !execController.HandlerFunc(runMethod) {
|
|
||||||
var in []reflect.Value
|
|
||||||
method := vc.MethodByName(runMethod)
|
|
||||||
method.Call(in)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//render template
|
|
||||||
if !context.ResponseWriter.Started && context.Output.Status == 0 {
|
|
||||||
if BConfig.WebConfig.AutoRender {
|
|
||||||
if err := execController.Render(); err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// finish all runRouter. release resource
|
|
||||||
execController.Finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
//execute middleware filters
|
|
||||||
if len(p.filters[AfterExec]) > 0 && p.execFilter(context, urlPath, AfterExec) {
|
|
||||||
goto Admin
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//store router pattern into context
|
||||||
|
context.Input.SetData("RouterPattern", routerInfo.pattern)
|
||||||
|
|
||||||
|
//execute middleware filters
|
||||||
|
if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) {
|
||||||
|
goto Admin
|
||||||
|
}
|
||||||
|
|
||||||
|
if routerInfo != nil {
|
||||||
|
if routerInfo.routerType == routerTypeRESTFul {
|
||||||
|
if _, ok := routerInfo.methods[r.Method]; ok {
|
||||||
|
isRunnable = true
|
||||||
|
routerInfo.runFunction(context)
|
||||||
|
} else {
|
||||||
|
exception("405", context)
|
||||||
|
goto Admin
|
||||||
|
}
|
||||||
|
} else if routerInfo.routerType == routerTypeHandler {
|
||||||
|
isRunnable = true
|
||||||
|
routerInfo.handler.ServeHTTP(rw, r)
|
||||||
|
} else {
|
||||||
|
runRouter = routerInfo.controllerType
|
||||||
|
method := r.Method
|
||||||
|
if r.Method == "POST" && context.Input.Query("_method") == "PUT" {
|
||||||
|
method = "PUT"
|
||||||
|
}
|
||||||
|
if r.Method == "POST" && context.Input.Query("_method") == "DELETE" {
|
||||||
|
method = "DELETE"
|
||||||
|
}
|
||||||
|
if m, ok := routerInfo.methods[method]; ok {
|
||||||
|
runMethod = m
|
||||||
|
} else if m, ok = routerInfo.methods["*"]; ok {
|
||||||
|
runMethod = m
|
||||||
|
} else {
|
||||||
|
runMethod = method
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// also defined runRouter & runMethod from filter
|
||||||
|
if !isRunnable {
|
||||||
|
//Invoke the request handler
|
||||||
|
vc := reflect.New(runRouter)
|
||||||
|
execController, ok := vc.Interface().(ControllerInterface)
|
||||||
|
if !ok {
|
||||||
|
panic("controller is not ControllerInterface")
|
||||||
|
}
|
||||||
|
|
||||||
|
//call the controller init function
|
||||||
|
execController.Init(context, runRouter.Name(), runMethod, vc.Interface())
|
||||||
|
|
||||||
|
//call prepare function
|
||||||
|
execController.Prepare()
|
||||||
|
|
||||||
|
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
|
||||||
|
if BConfig.WebConfig.EnableXSRF {
|
||||||
|
execController.XSRFToken()
|
||||||
|
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
|
||||||
|
(r.Method == "POST" && (context.Input.Query("_method") == "DELETE" || context.Input.Query("_method") == "PUT")) {
|
||||||
|
execController.CheckXSRFCookie()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
execController.URLMapping()
|
||||||
|
|
||||||
|
if !context.ResponseWriter.Started {
|
||||||
|
//exec main logic
|
||||||
|
switch runMethod {
|
||||||
|
case "GET":
|
||||||
|
execController.Get()
|
||||||
|
case "POST":
|
||||||
|
execController.Post()
|
||||||
|
case "DELETE":
|
||||||
|
execController.Delete()
|
||||||
|
case "PUT":
|
||||||
|
execController.Put()
|
||||||
|
case "HEAD":
|
||||||
|
execController.Head()
|
||||||
|
case "PATCH":
|
||||||
|
execController.Patch()
|
||||||
|
case "OPTIONS":
|
||||||
|
execController.Options()
|
||||||
|
default:
|
||||||
|
if !execController.HandlerFunc(runMethod) {
|
||||||
|
var in []reflect.Value
|
||||||
|
method := vc.MethodByName(runMethod)
|
||||||
|
method.Call(in)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//render template
|
||||||
|
if !context.ResponseWriter.Started && context.Output.Status == 0 {
|
||||||
|
if BConfig.WebConfig.AutoRender {
|
||||||
|
if err := execController.Render(); err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// finish all runRouter. release resource
|
||||||
|
execController.Finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
//execute middleware filters
|
||||||
|
if len(p.filters[AfterExec]) > 0 && p.execFilter(context, urlPath, AfterExec) {
|
||||||
|
goto Admin
|
||||||
|
}
|
||||||
|
|
||||||
if len(p.filters[FinishRouter]) > 0 && p.execFilter(context, urlPath, FinishRouter) {
|
if len(p.filters[FinishRouter]) > 0 && p.execFilter(context, urlPath, FinishRouter) {
|
||||||
goto Admin
|
goto Admin
|
||||||
}
|
}
|
||||||
@ -851,6 +843,22 @@ Admin:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindRouter Find Router info for URL
|
||||||
|
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *controllerInfo, isFind bool) {
|
||||||
|
var urlPath = context.Input.URL()
|
||||||
|
if !BConfig.RouterCaseSensitive {
|
||||||
|
urlPath = strings.ToLower(urlPath)
|
||||||
|
}
|
||||||
|
httpMethod := context.Input.Method()
|
||||||
|
if t, ok := p.routers[httpMethod]; ok {
|
||||||
|
runObject := t.Match(urlPath, context)
|
||||||
|
if r, ok := runObject.(*controllerInfo); ok {
|
||||||
|
return r, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (p *ControllerRegister) recoverPanic(context *beecontext.Context) {
|
func (p *ControllerRegister) recoverPanic(context *beecontext.Context) {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
if err == ErrAbort {
|
if err == ErrAbort {
|
||||||
|
Loading…
Reference in New Issue
Block a user