1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-15 01:34:53 +02:00

middleware: add method override

This commit is contained in:
haoxin
2016-04-24 21:07:55 +08:00
parent c830734fd5
commit 592790bb43
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package middleware
import (
"github.com/labstack/echo"
)
const (
HttpMethodOverrideHeader = "X-HTTP-Method-Override"
)
func OverrideMethod() echo.MiddlewareFunc {
return Override()
}
// Override checks for the X-HTTP-Method-Override header
// or the body for parameter, `_method`
// and uses the http method instead of Request.Method.
// It isn't secure to override e.g a GET to a POST,
// so only Request.Method which are POSTs are considered.
func Override() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
originalMethod := c.Request().Method()
if originalMethod == "POST" {
m := c.FormValue("_method")
if m != "" {
c.Request().SetMethod(m)
}
m = c.Request().Header().Get(HttpMethodOverrideHeader)
if m != "" {
c.Request().SetMethod(m)
}
}
return next(c)
}
}
}