Add 405 Support

pull/496/head
Fenny 2020-06-21 10:47:03 +02:00
parent 3187472744
commit 09116ae22b
2 changed files with 16 additions and 9 deletions

2
app.go
View File

@ -222,7 +222,7 @@ func New(settings ...*Settings) *App {
// Create a new app
app := &App{
// Create router stack
stack: make([][]*Route, len(methodINT)+1),
stack: make([][]*Route, len(methodINT)),
// Create Ctx pool
pool: sync.Pool{
New: func() interface{} {

View File

@ -18,30 +18,37 @@ import (
// Scan stack if other methods match
func setMethodNotAllowed(ctx *Ctx) {
original := getString(ctx.Fasthttp.Request.Header.Method())
for m := range methodINT {
var match bool
for k, v := range methodINT {
// Skip original method
if m == original {
if k == original {
continue
}
// Reset stack index
ctx.indexRoute = -1
// Set new method
ctx.method = m
ctx.method = k
// Get stack length
lenr := len(ctx.app.stack[9]) - 1
// Loop over the route stack starting from previous index
lenr := len(ctx.app.stack[v]) - 1
//Loop over the route stack starting from previous index
for ctx.indexRoute < lenr {
// Increment route index
ctx.indexRoute++
// Get *Route
route := ctx.app.stack[9][ctx.indexRoute]
route := ctx.app.stack[v][ctx.indexRoute]
// Check if it matches the request path
match, _ := route.match(ctx.path, ctx.pathOriginal)
match, _ = route.match(ctx.path, ctx.pathOriginal)
// No match, next route
if match {
ctx.SendStatus(StatusMethodNotAllowed)
ctx.Vary(HeaderAllow, m)
ctx.Append(HeaderAllow, k)
break
}
}
if match {
match = false
continue
}
}
}