1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00
echo/middleware/method_override.go

40 lines
889 B
Go
Raw Normal View History

2016-04-24 15:07:55 +02:00
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)
}
}
}