1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 18:30:56 +00:00

Merge pull request #2950 from huhaocc/change_httpmethod_type

Change the type of HTTPMETHOD
This commit is contained in:
astaxie 2017-12-06 14:42:35 +08:00 committed by GitHub
commit b068a676dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 28 deletions

2
app.go
View File

@ -254,7 +254,7 @@ func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *A
func UnregisterFixedRoute(fixedRoute string, method string) *App { func UnregisterFixedRoute(fixedRoute string, method string) *App {
subPaths := splitPath(fixedRoute) subPaths := splitPath(fixedRoute)
if method == "" || method == "*" { if method == "" || method == "*" {
for _, m := range HTTPMETHOD { for m := range HTTPMETHOD {
if _, ok := BeeApp.Handlers.routers[m]; !ok { if _, ok := BeeApp.Handlers.routers[m]; !ok {
continue continue
} }

View File

@ -50,23 +50,23 @@ const (
var ( var (
// HTTPMETHOD list the supported http methods. // HTTPMETHOD list the supported http methods.
HTTPMETHOD = map[string]string{ HTTPMETHOD = map[string]bool{
"GET": "GET", "GET": true,
"POST": "POST", "POST": true,
"PUT": "PUT", "PUT": true,
"DELETE": "DELETE", "DELETE": true,
"PATCH": "PATCH", "PATCH": true,
"OPTIONS": "OPTIONS", "OPTIONS": true,
"HEAD": "HEAD", "HEAD": true,
"TRACE": "TRACE", "TRACE": true,
"CONNECT": "CONNECT", "CONNECT": true,
"MKCOL": "MKCOL", "MKCOL": true,
"COPY": "COPY", "COPY": true,
"MOVE": "MOVE", "MOVE": true,
"PROPFIND": "PROPFIND", "PROPFIND": true,
"PROPPATCH": "PROPPATCH", "PROPPATCH": true,
"LOCK": "LOCK", "LOCK": true,
"UNLOCK": "UNLOCK", "UNLOCK": true,
} }
// these beego.Controller's methods shouldn't reflect to AutoRouter // these beego.Controller's methods shouldn't reflect to AutoRouter
exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString", exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString",
@ -170,7 +170,7 @@ func (p *ControllerRegister) addWithMethodParams(pattern string, c ControllerInt
} }
comma := strings.Split(colon[0], ",") comma := strings.Split(colon[0], ",")
for _, m := range comma { for _, m := range comma {
if _, ok := HTTPMETHOD[strings.ToUpper(m)]; m == "*" || ok { if m == "*" || HTTPMETHOD[strings.ToUpper(m)] {
if val := reflectVal.MethodByName(colon[1]); val.IsValid() { if val := reflectVal.MethodByName(colon[1]); val.IsValid() {
methods[strings.ToUpper(m)] = colon[1] methods[strings.ToUpper(m)] = colon[1]
} else { } else {
@ -211,13 +211,13 @@ func (p *ControllerRegister) addWithMethodParams(pattern string, c ControllerInt
route.methodParams = methodParams route.methodParams = methodParams
if len(methods) == 0 { if len(methods) == 0 {
for _, m := range HTTPMETHOD { for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route) p.addToRouter(m, pattern, route)
} }
} else { } else {
for k := range methods { for k := range methods {
if k == "*" { if k == "*" {
for _, m := range HTTPMETHOD { for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route) p.addToRouter(m, pattern, route)
} }
} else { } else {
@ -359,7 +359,7 @@ func (p *ControllerRegister) Any(pattern string, f FilterFunc) {
// }) // })
func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) { func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
method = strings.ToUpper(method) method = strings.ToUpper(method)
if _, ok := HTTPMETHOD[method]; method != "*" && !ok { if method != "*" && !HTTPMETHOD[method] {
panic("not support http method: " + method) panic("not support http method: " + method)
} }
route := &ControllerInfo{} route := &ControllerInfo{}
@ -368,7 +368,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
route.runFunction = f route.runFunction = f
methods := make(map[string]string) methods := make(map[string]string)
if method == "*" { if method == "*" {
for _, val := range HTTPMETHOD { for val := range HTTPMETHOD {
methods[val] = val methods[val] = val
} }
} else { } else {
@ -377,7 +377,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
route.methods = methods route.methods = methods
for k := range methods { for k := range methods {
if k == "*" { if k == "*" {
for _, m := range HTTPMETHOD { for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route) p.addToRouter(m, pattern, route)
} }
} else { } else {
@ -397,7 +397,7 @@ func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ...
pattern = path.Join(pattern, "?:all(.*)") pattern = path.Join(pattern, "?:all(.*)")
} }
} }
for _, m := range HTTPMETHOD { for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route) p.addToRouter(m, pattern, route)
} }
} }
@ -432,7 +432,7 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface)
patternFix := path.Join(prefix, strings.ToLower(controllerName), strings.ToLower(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) patternFixInit := path.Join(prefix, controllerName, rt.Method(i).Name)
route.pattern = pattern route.pattern = pattern
for _, m := range HTTPMETHOD { for m := range HTTPMETHOD {
p.addToRouter(m, pattern, route) p.addToRouter(m, pattern, route)
p.addToRouter(m, patternInit, route) p.addToRouter(m, patternInit, route)
p.addToRouter(m, patternFix, route) p.addToRouter(m, patternFix, route)
@ -533,7 +533,7 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
if c.routerType == routerTypeBeego && if c.routerType == routerTypeBeego &&
strings.HasSuffix(path.Join(c.controllerType.PkgPath(), c.controllerType.Name()), controllName) { strings.HasSuffix(path.Join(c.controllerType.PkgPath(), c.controllerType.Name()), controllName) {
find := false find := false
if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok { if HTTPMETHOD[strings.ToUpper(methodName)] {
if len(c.methods) == 0 { if len(c.methods) == 0 {
find = true find = true
} else if m, ok := c.methods[strings.ToUpper(methodName)]; ok && m == strings.ToUpper(methodName) { } else if m, ok := c.methods[strings.ToUpper(methodName)]; ok && m == strings.ToUpper(methodName) {
@ -681,7 +681,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
} }
// filter wrong http method // filter wrong http method
if _, ok := HTTPMETHOD[r.Method]; !ok { if !HTTPMETHOD[r.Method] {
http.Error(rw, "Method Not Allowed", 405) http.Error(rw, "Method Not Allowed", 405)
goto Admin goto Admin
} }