1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 17:10:54 +00:00
Beego/server/web/router.go

1017 lines
28 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
2020-07-22 14:50:08 +00:00
import (
"errors"
"fmt"
"net/http"
"path"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/astaxie/beego/core/logs"
"github.com/astaxie/beego/core/utils"
beecontext "github.com/astaxie/beego/server/web/context"
"github.com/astaxie/beego/server/web/context/param"
2020-07-22 14:50:08 +00:00
)
// default filter execution points
const (
BeforeStatic = iota
BeforeRouter
BeforeExec
AfterExec
FinishRouter
)
const (
routerTypeBeego = iota
routerTypeRESTFul
routerTypeHandler
)
var (
// HTTPMETHOD list the supported http methods.
HTTPMETHOD = map[string]bool{
"GET": true,
"POST": true,
"PUT": true,
"DELETE": true,
"PATCH": true,
"OPTIONS": true,
"HEAD": true,
"TRACE": true,
"CONNECT": true,
"MKCOL": true,
"COPY": true,
"MOVE": true,
"PROPFIND": true,
"PROPPATCH": true,
"LOCK": true,
"UNLOCK": true,
}
// these beego.Controller's methods shouldn't reflect to AutoRouter
exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString",
"RenderBytes", "Redirect", "Abort", "StopRun", "UrlFor", "ServeJSON", "ServeJSONP",
"ServeYAML", "ServeXML", "Input", "ParseForm", "GetString", "GetStrings", "GetInt", "GetBool",
"GetFloat", "GetFile", "SaveToFile", "StartSession", "SetSession", "GetSession",
"DelSession", "SessionRegenerateID", "DestroySession", "IsAjax", "GetSecureCookie",
"SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml",
"GetControllerAndAction", "ServeFormatted"}
urlPlaceholder = "{{placeholder}}"
// DefaultAccessLogFilter will skip the accesslog if return true
DefaultAccessLogFilter FilterHandler = &logFilter{}
)
// FilterHandler is an interface for
type FilterHandler interface {
Filter(*beecontext.Context) bool
}
// default log filter static file will not show
type logFilter struct {
}
func (l *logFilter) Filter(ctx *beecontext.Context) bool {
requestPath := path.Clean(ctx.Request.URL.Path)
if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
return true
}
for prefix := range BConfig.WebConfig.StaticDir {
if strings.HasPrefix(requestPath, prefix) {
return true
}
}
return false
}
// ExceptMethodAppend to append a slice's value into "exceptMethod", for controller's methods shouldn't reflect to AutoRouter
func ExceptMethodAppend(action string) {
exceptMethod = append(exceptMethod, action)
}
// ControllerInfo holds information about the controller.
type ControllerInfo struct {
pattern string
controllerType reflect.Type
methods map[string]string
handler http.Handler
runFunction FilterFunc
routerType int
initialize func() ControllerInterface
methodParams []*param.MethodParam
}
func (c *ControllerInfo) GetPattern() string {
return c.pattern
}
// ControllerRegister containers registered router rules, controller handlers and filters.
type ControllerRegister struct {
routers map[string]*Tree
enablePolicy bool
policies map[string]*Tree
enableFilter bool
filters [FinishRouter + 1][]*FilterRouter
pool sync.Pool
2020-07-31 13:43:11 +00:00
// the filter created by FilterChain
chainRoot *FilterRouter
2020-09-09 14:55:18 +00:00
cfg *Config
2020-07-22 14:50:08 +00:00
}
// NewControllerRegister returns a new ControllerRegister.
2020-09-09 14:55:18 +00:00
// Usually you should not use this method
// please use NewControllerRegisterWithCfg
2020-07-22 14:50:08 +00:00
func NewControllerRegister() *ControllerRegister {
2020-09-09 14:55:18 +00:00
return NewControllerRegisterWithCfg(BeeApp.Cfg)
}
func NewControllerRegisterWithCfg(cfg *Config) *ControllerRegister {
2020-07-31 13:43:11 +00:00
res := &ControllerRegister{
2020-07-22 14:50:08 +00:00
routers: make(map[string]*Tree),
policies: make(map[string]*Tree),
pool: sync.Pool{
New: func() interface{} {
return beecontext.NewContext()
},
},
2020-09-20 07:36:08 +00:00
cfg: cfg,
2020-07-22 14:50:08 +00:00
}
2020-09-02 14:44:31 +00:00
res.chainRoot = newFilterRouter("/*", res.serveHttp, WithCaseSensitive(false))
2020-07-31 13:43:11 +00:00
return res
2020-07-22 14:50:08 +00:00
}
// Add controller handler and pattern rules to ControllerRegister.
// usage:
// default methods is the same name as method
// Add("/user",&UserController{})
// Add("/api/list",&RestController{},"*:ListFood")
// Add("/api/create",&RestController{},"post:CreateFood")
// Add("/api/update",&RestController{},"put:UpdateFood")
// Add("/api/delete",&RestController{},"delete:DeleteFood")
// Add("/api",&RestController{},"get,post:ApiFunc"
// Add("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingMethods ...string) {
p.addWithMethodParams(pattern, c, nil, mappingMethods...)
}
func (p *ControllerRegister) addWithMethodParams(pattern string, c ControllerInterface, methodParams []*param.MethodParam, mappingMethods ...string) {
reflectVal := reflect.ValueOf(c)
t := reflect.Indirect(reflectVal).Type()
methods := make(map[string]string)
if len(mappingMethods) > 0 {
semi := strings.Split(mappingMethods[0], ";")
for _, v := range semi {
colon := strings.Split(v, ":")
if len(colon) != 2 {
panic("method mapping format is invalid")
}
comma := strings.Split(colon[0], ",")
for _, m := range comma {
if m == "*" || HTTPMETHOD[strings.ToUpper(m)] {
if val := reflectVal.MethodByName(colon[1]); val.IsValid() {
methods[strings.ToUpper(m)] = colon[1]
} else {
panic("'" + colon[1] + "' method doesn't exist in the controller " + t.Name())
}
} else {
panic(v + " is an invalid method mapping. Method doesn't exist " + m)
}
}
}
}
route := &ControllerInfo{}
route.pattern = pattern
route.methods = methods
route.routerType = routerTypeBeego
route.controllerType = t
route.initialize = func() ControllerInterface {
vc := reflect.New(route.controllerType)
execController, ok := vc.Interface().(ControllerInterface)
if !ok {
panic("controller is not ControllerInterface")
}
elemVal := reflect.ValueOf(c).Elem()
elemType := reflect.TypeOf(c).Elem()
execElem := reflect.ValueOf(execController).Elem()
numOfFields := elemVal.NumField()
for i := 0; i < numOfFields; i++ {
fieldType := elemType.Field(i)
elemField := execElem.FieldByName(fieldType.Name)
if elemField.CanSet() {
fieldVal := elemVal.Field(i)
elemField.Set(fieldVal)
}
}
return execController
}
route.methodParams = methodParams
if len(methods) == 0 {
for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route)
}
} else {
for k := range methods {
if k == "*" {
for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route)
}
} else {
p.addToRouter(k, pattern, route)
}
}
}
}
func (p *ControllerRegister) addToRouter(method, pattern string, r *ControllerInfo) {
2020-09-09 14:55:18 +00:00
if !p.cfg.RouterCaseSensitive {
2020-07-22 14:50:08 +00:00
pattern = strings.ToLower(pattern)
}
if t, ok := p.routers[method]; ok {
t.AddRouter(pattern, r)
} else {
t := NewTree()
t.AddRouter(pattern, r)
p.routers[method] = t
}
}
// Include only when the Runmode is dev will generate router file in the router/auto.go from the controller
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
func (p *ControllerRegister) Include(cList ...ControllerInterface) {
for _, c := range cList {
reflectVal := reflect.ValueOf(c)
t := reflect.Indirect(reflectVal).Type()
key := t.PkgPath() + ":" + t.Name()
if comm, ok := GlobalControllerRouter[key]; ok {
for _, a := range comm {
for _, f := range a.Filters {
2020-09-02 14:44:31 +00:00
p.InsertFilter(f.Pattern, f.Pos, f.Filter, WithReturnOnOutput(f.ReturnOnOutput), WithResetParams(f.ResetParams))
2020-07-22 14:50:08 +00:00
}
p.addWithMethodParams(a.Router, c, a.MethodParams, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
}
}
}
}
// GetContext returns a context from pool, so usually you should remember to call Reset function to clean the context
// And don't forget to give back context to pool
// example:
// ctx := p.GetContext()
// ctx.Reset(w, q)
// defer p.GiveBackContext(ctx)
func (p *ControllerRegister) GetContext() *beecontext.Context {
return p.pool.Get().(*beecontext.Context)
}
// GiveBackContext put the ctx into pool so that it could be reuse
func (p *ControllerRegister) GiveBackContext(ctx *beecontext.Context) {
p.pool.Put(ctx)
}
// Get add get method
// usage:
// Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Get(pattern string, f FilterFunc) {
p.AddMethod("get", pattern, f)
}
// Post add post method
// usage:
// Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Post(pattern string, f FilterFunc) {
p.AddMethod("post", pattern, f)
}
// Put add put method
// usage:
// Put("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Put(pattern string, f FilterFunc) {
p.AddMethod("put", pattern, f)
}
// Delete add delete method
// usage:
// Delete("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Delete(pattern string, f FilterFunc) {
p.AddMethod("delete", pattern, f)
}
// Head add head method
// usage:
// Head("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Head(pattern string, f FilterFunc) {
p.AddMethod("head", pattern, f)
}
// Patch add patch method
// usage:
// Patch("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Patch(pattern string, f FilterFunc) {
p.AddMethod("patch", pattern, f)
}
// Options add options method
// usage:
// Options("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Options(pattern string, f FilterFunc) {
p.AddMethod("options", pattern, f)
}
// Any add all method
// usage:
// Any("/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) Any(pattern string, f FilterFunc) {
p.AddMethod("*", pattern, f)
}
// AddMethod add http method router
// usage:
// AddMethod("get","/api/:id", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
method = strings.ToUpper(method)
if method != "*" && !HTTPMETHOD[method] {
panic("not support http method: " + method)
}
route := &ControllerInfo{}
route.pattern = pattern
route.routerType = routerTypeRESTFul
route.runFunction = f
methods := make(map[string]string)
if method == "*" {
for val := range HTTPMETHOD {
methods[val] = val
}
} else {
methods[method] = method
}
route.methods = methods
for k := range methods {
if k == "*" {
for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route)
}
} else {
p.addToRouter(k, pattern, route)
}
}
}
// Handler add user defined Handler
func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ...interface{}) {
route := &ControllerInfo{}
route.pattern = pattern
route.routerType = routerTypeHandler
route.handler = h
if len(options) > 0 {
if _, ok := options[0].(bool); ok {
pattern = path.Join(pattern, "?:all(.*)")
}
}
for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route)
}
}
// AddAuto router to ControllerRegister.
// example beego.AddAuto(&MainContorlller{}),
// MainController has method List and Page.
// visit the url /main/list to execute List function
// /main/page to execute Page function.
func (p *ControllerRegister) AddAuto(c ControllerInterface) {
p.AddAutoPrefix("/", c)
}
// AddAutoPrefix Add auto router to ControllerRegister with prefix.
// example beego.AddAutoPrefix("/admin",&MainContorlller{}),
// MainController has method List and Page.
// visit the url /admin/main/list to execute List function
// /admin/main/page to execute Page function.
func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface) {
reflectVal := reflect.ValueOf(c)
rt := reflectVal.Type()
ct := reflect.Indirect(reflectVal).Type()
controllerName := strings.TrimSuffix(ct.Name(), "Controller")
for i := 0; i < rt.NumMethod(); i++ {
if !utils.InSlice(rt.Method(i).Name, exceptMethod) {
route := &ControllerInfo{}
route.routerType = routerTypeBeego
route.methods = map[string]string{"*": rt.Method(i).Name}
route.controllerType = ct
pattern := path.Join(prefix, strings.ToLower(controllerName), strings.ToLower(rt.Method(i).Name), "*")
patternInit := path.Join(prefix, controllerName, rt.Method(i).Name, "*")
patternFix := path.Join(prefix, strings.ToLower(controllerName), strings.ToLower(rt.Method(i).Name))
patternFixInit := path.Join(prefix, controllerName, rt.Method(i).Name)
route.pattern = pattern
for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route)
p.addToRouter(m, patternInit, route)
p.addToRouter(m, patternFix, route)
p.addToRouter(m, patternFixInit, route)
}
}
}
}
// InsertFilter Add a FilterFunc with pattern rule and action constant.
// params is for:
// 1. setting the returnOnOutput value (false allows multiple filters to execute)
// 2. determining whether or not params need to be reset.
2020-09-02 14:44:31 +00:00
func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt) error {
2020-09-09 14:55:18 +00:00
opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive))
2020-09-02 14:44:31 +00:00
mr := newFilterRouter(pattern, filter, opts...)
2020-07-22 14:50:08 +00:00
return p.insertFilterRouter(pos, mr)
}
2020-07-31 13:43:11 +00:00
// InsertFilterChain is similar to InsertFilter,
// but it will using chainRoot.filterFunc as input to build a new filterFunc
// for example, assume that chainRoot is funcA
// and we add new FilterChain
// fc := func(next) {
// return func(ctx) {
// // do something
// next(ctx)
// // do something
// }
// }
2020-09-02 14:44:31 +00:00
func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) {
2020-07-31 13:43:11 +00:00
root := p.chainRoot
filterFunc := chain(root.filterFunc)
2020-09-09 14:55:18 +00:00
opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive))
2020-09-02 14:44:31 +00:00
p.chainRoot = newFilterRouter(pattern, filterFunc, opts...)
2020-08-10 15:04:57 +00:00
p.chainRoot.next = root
2020-07-31 13:43:11 +00:00
2020-08-10 15:04:57 +00:00
}
2020-07-31 13:43:11 +00:00
2020-07-22 14:50:08 +00:00
// add Filter into
func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) (err error) {
if pos < BeforeStatic || pos > FinishRouter {
return errors.New("can not find your filter position")
}
p.enableFilter = true
p.filters[pos] = append(p.filters[pos], mr)
return nil
}
// URLFor does another controller handler in this request function.
// it can access any controller method.
func (p *ControllerRegister) URLFor(endpoint string, values ...interface{}) string {
paths := strings.Split(endpoint, ".")
if len(paths) <= 1 {
logs.Warn("urlfor endpoint must like path.controller.method")
return ""
}
if len(values)%2 != 0 {
logs.Warn("urlfor params must key-value pair")
return ""
}
params := make(map[string]string)
if len(values) > 0 {
key := ""
for k, v := range values {
if k%2 == 0 {
key = fmt.Sprint(v)
} else {
params[key] = fmt.Sprint(v)
}
}
}
controllerName := strings.Join(paths[:len(paths)-1], "/")
methodName := paths[len(paths)-1]
for m, t := range p.routers {
ok, url := p.getURL(t, "/", controllerName, methodName, params, m)
if ok {
return url
}
}
return ""
}
func (p *ControllerRegister) getURL(t *Tree, url, controllerName, methodName string, params map[string]string, httpMethod string) (bool, string) {
for _, subtree := range t.fixrouters {
u := path.Join(url, subtree.prefix)
ok, u := p.getURL(subtree, u, controllerName, methodName, params, httpMethod)
if ok {
return ok, u
}
}
if t.wildcard != nil {
u := path.Join(url, urlPlaceholder)
ok, u := p.getURL(t.wildcard, u, controllerName, methodName, params, httpMethod)
if ok {
return ok, u
}
}
for _, l := range t.leaves {
if c, ok := l.runObject.(*ControllerInfo); ok {
if c.routerType == routerTypeBeego &&
strings.HasSuffix(path.Join(c.controllerType.PkgPath(), c.controllerType.Name()), controllerName) {
find := false
if HTTPMETHOD[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
}
}
if !find {
for m, md := range c.methods {
if (m == "*" || m == httpMethod) && md == methodName {
find = true
}
}
}
if find {
if l.regexps == nil {
if len(l.wildcards) == 0 {
return true, strings.Replace(url, "/"+urlPlaceholder, "", 1) + toURL(params)
}
if len(l.wildcards) == 1 {
if v, ok := params[l.wildcards[0]]; ok {
delete(params, l.wildcards[0])
return true, strings.Replace(url, urlPlaceholder, v, 1) + toURL(params)
}
return false, ""
}
if len(l.wildcards) == 3 && l.wildcards[0] == "." {
if p, ok := params[":path"]; ok {
if e, isok := params[":ext"]; isok {
delete(params, ":path")
delete(params, ":ext")
return true, strings.Replace(url, urlPlaceholder, p+"."+e, -1) + toURL(params)
}
}
}
canSkip := false
for _, v := range l.wildcards {
if v == ":" {
canSkip = true
continue
}
if u, ok := params[v]; ok {
delete(params, v)
url = strings.Replace(url, urlPlaceholder, u, 1)
} else {
if canSkip {
canSkip = false
continue
}
return false, ""
}
}
return true, url + toURL(params)
}
var i int
var startReg bool
regURL := ""
for _, v := range strings.Trim(l.regexps.String(), "^$") {
if v == '(' {
startReg = true
continue
} else if v == ')' {
startReg = false
if v, ok := params[l.wildcards[i]]; ok {
delete(params, l.wildcards[i])
regURL = regURL + v
i++
} else {
break
}
} else if !startReg {
regURL = string(append([]rune(regURL), v))
}
}
if l.regexps.MatchString(regURL) {
ps := strings.Split(regURL, "/")
for _, p := range ps {
url = strings.Replace(url, urlPlaceholder, p, 1)
}
return true, url + toURL(params)
}
}
}
}
}
return false, ""
}
func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, pos int) (started bool) {
var preFilterParams map[string]string
for _, filterR := range p.filters[pos] {
2020-07-31 13:43:11 +00:00
b, done := filterR.filter(context, urlPath, preFilterParams)
if done {
return b
2020-07-22 14:50:08 +00:00
}
}
return false
}
// Implement http.Handler interface.
func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
2020-07-31 13:43:11 +00:00
ctx := p.GetContext()
ctx.Reset(rw, r)
defer p.GiveBackContext(ctx)
var preFilterParams map[string]string
p.chainRoot.filter(ctx, p.getUrlPath(ctx), preFilterParams)
}
func (p *ControllerRegister) serveHttp(ctx *beecontext.Context) {
var err error
2020-07-22 14:50:08 +00:00
startTime := time.Now()
2020-07-31 13:43:11 +00:00
r := ctx.Request
rw := ctx.ResponseWriter.ResponseWriter
2020-07-22 14:50:08 +00:00
var (
runRouter reflect.Type
findRouter bool
runMethod string
methodParams []*param.MethodParam
routerInfo *ControllerInfo
isRunnable bool
)
2020-09-09 14:55:18 +00:00
if p.cfg.RecoverFunc != nil {
2020-09-20 07:36:08 +00:00
defer p.cfg.RecoverFunc(ctx, p.cfg)
2020-07-22 14:50:08 +00:00
}
2020-09-09 14:55:18 +00:00
ctx.Output.EnableGzip = p.cfg.EnableGzip
2020-07-22 14:50:08 +00:00
2020-09-09 14:55:18 +00:00
if p.cfg.RunMode == DEV {
ctx.Output.Header("Server", p.cfg.ServerName)
2020-07-22 14:50:08 +00:00
}
2020-07-31 13:43:11 +00:00
urlPath := p.getUrlPath(ctx)
2020-07-22 14:50:08 +00:00
// filter wrong http method
if !HTTPMETHOD[r.Method] {
2020-07-31 13:43:11 +00:00
exception("405", ctx)
2020-07-22 14:50:08 +00:00
goto Admin
}
// filter for static file
2020-07-31 13:43:11 +00:00
if len(p.filters[BeforeStatic]) > 0 && p.execFilter(ctx, urlPath, BeforeStatic) {
2020-07-22 14:50:08 +00:00
goto Admin
}
2020-07-31 13:43:11 +00:00
serverStaticRouter(ctx)
2020-07-22 14:50:08 +00:00
2020-07-31 13:43:11 +00:00
if ctx.ResponseWriter.Started {
2020-07-22 14:50:08 +00:00
findRouter = true
goto Admin
}
if r.Method != http.MethodGet && r.Method != http.MethodHead {
if ctx.Input.IsUpload() {
ctx.Input.Context.Request.Body = http.MaxBytesReader(ctx.Input.Context.ResponseWriter,
ctx.Input.Context.Request.Body,
p.cfg.MaxUploadSize)
} else if p.cfg.CopyRequestBody {
2020-07-22 14:50:08 +00:00
// connection will close if the incoming data are larger (RFC 7231, 6.5.11)
2020-09-09 14:55:18 +00:00
if r.ContentLength > p.cfg.MaxMemory {
2020-07-22 14:50:08 +00:00
logs.Error(errors.New("payload too large"))
2020-07-31 13:43:11 +00:00
exception("413", ctx)
2020-07-22 14:50:08 +00:00
goto Admin
}
2020-09-09 14:55:18 +00:00
ctx.Input.CopyBody(p.cfg.MaxMemory)
} else {
ctx.Input.Context.Request.Body = http.MaxBytesReader(ctx.Input.Context.ResponseWriter,
ctx.Input.Context.Request.Body,
p.cfg.MaxMemory)
2020-07-22 14:50:08 +00:00
}
err = ctx.Input.ParseFormOrMultiForm(p.cfg.MaxMemory)
if err != nil {
logs.Error(err)
2020-10-18 16:22:55 +00:00
if strings.Contains(err.Error(), `http: request body too large`) {
exception("413", ctx)
} else {
exception("500", ctx)
}
goto Admin
}
2020-07-22 14:50:08 +00:00
}
// session init
2020-09-09 14:55:18 +00:00
if p.cfg.WebConfig.Session.SessionOn {
2020-07-31 13:43:11 +00:00
ctx.Input.CruSession, err = GlobalSessions.SessionStart(rw, r)
2020-07-22 14:50:08 +00:00
if err != nil {
logs.Error(err)
2020-07-31 13:43:11 +00:00
exception("503", ctx)
2020-07-22 14:50:08 +00:00
goto Admin
}
defer func() {
2020-07-31 13:43:11 +00:00
if ctx.Input.CruSession != nil {
2020-08-30 15:39:07 +00:00
ctx.Input.CruSession.SessionRelease(nil, rw)
2020-07-22 14:50:08 +00:00
}
}()
}
2020-07-31 13:43:11 +00:00
if len(p.filters[BeforeRouter]) > 0 && p.execFilter(ctx, urlPath, BeforeRouter) {
2020-07-22 14:50:08 +00:00
goto Admin
}
// User can define RunController and RunMethod in filter
2020-07-31 13:43:11 +00:00
if ctx.Input.RunController != nil && ctx.Input.RunMethod != "" {
2020-07-22 14:50:08 +00:00
findRouter = true
2020-07-31 13:43:11 +00:00
runMethod = ctx.Input.RunMethod
runRouter = ctx.Input.RunController
2020-07-22 14:50:08 +00:00
} else {
2020-07-31 13:43:11 +00:00
routerInfo, findRouter = p.FindRouter(ctx)
2020-07-22 14:50:08 +00:00
}
// if no matches to url, throw a not found exception
if !findRouter {
2020-07-31 13:43:11 +00:00
exception("404", ctx)
2020-07-22 14:50:08 +00:00
goto Admin
}
2020-07-31 13:43:11 +00:00
if splat := ctx.Input.Param(":splat"); splat != "" {
2020-07-22 14:50:08 +00:00
for k, v := range strings.Split(splat, "/") {
2020-07-31 13:43:11 +00:00
ctx.Input.SetParam(strconv.Itoa(k), v)
2020-07-22 14:50:08 +00:00
}
}
if routerInfo != nil {
// store router pattern into context
2020-07-31 13:43:11 +00:00
ctx.Input.SetData("RouterPattern", routerInfo.pattern)
2020-07-22 14:50:08 +00:00
}
// execute middleware filters
2020-07-31 13:43:11 +00:00
if len(p.filters[BeforeExec]) > 0 && p.execFilter(ctx, urlPath, BeforeExec) {
2020-07-22 14:50:08 +00:00
goto Admin
}
// check policies
2020-07-31 13:43:11 +00:00
if p.execPolicy(ctx, urlPath) {
2020-07-22 14:50:08 +00:00
goto Admin
}
if routerInfo != nil {
if routerInfo.routerType == routerTypeRESTFul {
if _, ok := routerInfo.methods[r.Method]; ok {
isRunnable = true
2020-07-31 13:43:11 +00:00
routerInfo.runFunction(ctx)
2020-07-22 14:50:08 +00:00
} else {
2020-07-31 13:43:11 +00:00
exception("405", ctx)
2020-07-22 14:50:08 +00:00
goto Admin
}
} else if routerInfo.routerType == routerTypeHandler {
isRunnable = true
2020-07-31 13:43:11 +00:00
routerInfo.handler.ServeHTTP(ctx.ResponseWriter, ctx.Request)
2020-07-22 14:50:08 +00:00
} else {
runRouter = routerInfo.controllerType
methodParams = routerInfo.methodParams
method := r.Method
2020-07-31 13:43:11 +00:00
if r.Method == http.MethodPost && ctx.Input.Query("_method") == http.MethodPut {
2020-07-22 14:50:08 +00:00
method = http.MethodPut
}
2020-07-31 13:43:11 +00:00
if r.Method == http.MethodPost && ctx.Input.Query("_method") == http.MethodDelete {
2020-07-22 14:50:08 +00:00
method = http.MethodDelete
}
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
var execController ControllerInterface
if routerInfo != nil && routerInfo.initialize != nil {
execController = routerInfo.initialize()
} else {
vc := reflect.New(runRouter)
var ok bool
execController, ok = vc.Interface().(ControllerInterface)
if !ok {
panic("controller is not ControllerInterface")
}
}
// call the controller init function
2020-07-31 13:43:11 +00:00
execController.Init(ctx, runRouter.Name(), runMethod, execController)
2020-07-22 14:50:08 +00:00
// call prepare function
execController.Prepare()
// if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
2020-09-09 14:55:18 +00:00
if p.cfg.WebConfig.EnableXSRF {
2020-07-22 14:50:08 +00:00
execController.XSRFToken()
if r.Method == http.MethodPost || r.Method == http.MethodDelete || r.Method == http.MethodPut ||
2020-07-31 13:43:11 +00:00
(r.Method == http.MethodPost && (ctx.Input.Query("_method") == http.MethodDelete || ctx.Input.Query("_method") == http.MethodPut)) {
2020-07-22 14:50:08 +00:00
execController.CheckXSRFCookie()
}
}
execController.URLMapping()
2020-07-31 13:43:11 +00:00
if !ctx.ResponseWriter.Started {
2020-07-22 14:50:08 +00:00
// exec main logic
switch runMethod {
case http.MethodGet:
execController.Get()
case http.MethodPost:
execController.Post()
case http.MethodDelete:
execController.Delete()
case http.MethodPut:
execController.Put()
case http.MethodHead:
execController.Head()
case http.MethodPatch:
execController.Patch()
case http.MethodOptions:
execController.Options()
case http.MethodTrace:
execController.Trace()
default:
if !execController.HandlerFunc(runMethod) {
vc := reflect.ValueOf(execController)
method := vc.MethodByName(runMethod)
2020-07-31 13:43:11 +00:00
in := param.ConvertParams(methodParams, method.Type(), ctx)
2020-07-22 14:50:08 +00:00
out := method.Call(in)
// For backward compatibility we only handle response if we had incoming methodParams
if methodParams != nil {
2020-07-31 13:43:11 +00:00
p.handleParamResponse(ctx, execController, out)
2020-07-22 14:50:08 +00:00
}
}
}
// render template
2020-07-31 13:43:11 +00:00
if !ctx.ResponseWriter.Started && ctx.Output.Status == 0 {
2020-09-09 14:55:18 +00:00
if p.cfg.WebConfig.AutoRender {
2020-07-22 14:50:08 +00:00
if err := execController.Render(); err != nil {
logs.Error(err)
}
}
}
}
// finish all runRouter. release resource
execController.Finish()
}
// execute middleware filters
2020-07-31 13:43:11 +00:00
if len(p.filters[AfterExec]) > 0 && p.execFilter(ctx, urlPath, AfterExec) {
2020-07-22 14:50:08 +00:00
goto Admin
}
2020-07-31 13:43:11 +00:00
if len(p.filters[FinishRouter]) > 0 && p.execFilter(ctx, urlPath, FinishRouter) {
2020-07-22 14:50:08 +00:00
goto Admin
}
Admin:
// admin module record QPS
2020-07-31 13:43:11 +00:00
statusCode := ctx.ResponseWriter.Status
2020-07-22 14:50:08 +00:00
if statusCode == 0 {
statusCode = 200
}
2020-07-31 13:43:11 +00:00
LogAccess(ctx, &startTime, statusCode)
2020-07-22 14:50:08 +00:00
timeDur := time.Since(startTime)
2020-07-31 13:43:11 +00:00
ctx.ResponseWriter.Elapsed = timeDur
2020-09-09 14:55:18 +00:00
if p.cfg.Listen.EnableAdmin {
2020-07-22 14:50:08 +00:00
pattern := ""
if routerInfo != nil {
pattern = routerInfo.pattern
}
if FilterMonitorFunc(r.Method, r.URL.Path, timeDur, pattern, statusCode) {
routerName := ""
if runRouter != nil {
routerName = runRouter.Name()
}
go StatisticsMap.AddStatistics(r.Method, r.URL.Path, routerName, timeDur)
2020-07-22 14:50:08 +00:00
}
}
2020-09-09 14:55:18 +00:00
if p.cfg.RunMode == DEV && !p.cfg.Log.AccessLogs {
2020-07-22 14:50:08 +00:00
match := map[bool]string{true: "match", false: "nomatch"}
devInfo := fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s",
2020-07-31 13:43:11 +00:00
ctx.Input.IP(),
2020-07-22 14:50:08 +00:00
logs.ColorByStatus(statusCode), statusCode, logs.ResetColor(),
timeDur.String(),
match[findRouter],
logs.ColorByMethod(r.Method), r.Method, logs.ResetColor(),
r.URL.Path)
if routerInfo != nil {
devInfo += fmt.Sprintf(" r:%s", routerInfo.pattern)
}
logs.Debug(devInfo)
}
// Call WriteHeader if status code has been set changed
2020-07-31 13:43:11 +00:00
if ctx.Output.Status != 0 {
ctx.ResponseWriter.WriteHeader(ctx.Output.Status)
}
}
func (p *ControllerRegister) getUrlPath(ctx *beecontext.Context) string {
urlPath := ctx.Request.URL.Path
2020-09-09 14:55:18 +00:00
if !p.cfg.RouterCaseSensitive {
2020-07-31 13:43:11 +00:00
urlPath = strings.ToLower(urlPath)
2020-07-22 14:50:08 +00:00
}
2020-07-31 13:43:11 +00:00
return urlPath
2020-07-22 14:50:08 +00:00
}
func (p *ControllerRegister) handleParamResponse(context *beecontext.Context, execController ControllerInterface, results []reflect.Value) {
// looping in reverse order for the case when both error and value are returned and error sets the response status code
for i := len(results) - 1; i >= 0; i-- {
result := results[i]
if result.Kind() != reflect.Interface || !result.IsNil() {
resultValue := result.Interface()
context.RenderMethodResult(resultValue)
}
}
if !context.ResponseWriter.Started && len(results) > 0 && context.Output.Status == 0 {
context.Output.SetStatus(200)
}
}
// FindRouter Find Router info for URL
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *ControllerInfo, isFind bool) {
var urlPath = context.Input.URL()
2020-09-09 14:55:18 +00:00
if !p.cfg.RouterCaseSensitive {
2020-07-22 14:50:08 +00:00
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 toURL(params map[string]string) string {
if len(params) == 0 {
return ""
}
u := "?"
for k, v := range params {
u += k + "=" + v + "&"
}
return strings.TrimRight(u, "&")
}
// LogAccess logging info HTTP Access
func LogAccess(ctx *beecontext.Context, startTime *time.Time, statusCode int) {
2020-09-09 14:55:18 +00:00
BeeApp.LogAccess(ctx, startTime, statusCode)
2020-07-22 14:50:08 +00:00
}