2016-04-24 15:07:55 +02:00
|
|
|
package middleware
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
import "github.com/labstack/echo"
|
2016-04-24 15:07:55 +02:00
|
|
|
|
2016-04-28 06:08:06 +02:00
|
|
|
type (
|
2016-07-27 18:34:44 +02:00
|
|
|
// MethodOverrideConfig defines the config for MethodOverride middleware.
|
2016-04-28 06:08:06 +02:00
|
|
|
MethodOverrideConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-04-28 07:03:54 +02:00
|
|
|
// Getter is a function that gets overridden method from the request.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default values MethodFromHeader(echo.HeaderXHTTPMethodOverride).
|
2016-04-28 06:08:06 +02:00
|
|
|
Getter MethodOverrideGetter
|
|
|
|
}
|
|
|
|
|
|
|
|
// MethodOverrideGetter is a function that gets overridden method from the request
|
|
|
|
MethodOverrideGetter func(echo.Context) string
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-07-27 18:34:44 +02:00
|
|
|
// DefaultMethodOverrideConfig is the default MethodOverride middleware config.
|
2016-04-28 06:08:06 +02:00
|
|
|
DefaultMethodOverrideConfig = MethodOverrideConfig{
|
2016-07-27 18:34:44 +02:00
|
|
|
Skipper: defaultSkipper,
|
|
|
|
Getter: MethodFromHeader(echo.HeaderXHTTPMethodOverride),
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
2016-04-24 15:07:55 +02:00
|
|
|
)
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
// MethodOverride returns a MethodOverride middleware.
|
2016-05-05 08:33:05 +02:00
|
|
|
// MethodOverride middleware checks for the overridden method from the request and
|
2016-04-28 06:08:06 +02:00
|
|
|
// uses it instead of the original method.
|
|
|
|
//
|
2016-04-28 07:03:54 +02:00
|
|
|
// For security reasons, only `POST` method can be overridden.
|
2016-04-28 06:08:06 +02:00
|
|
|
func MethodOverride() echo.MiddlewareFunc {
|
|
|
|
return MethodOverrideWithConfig(DefaultMethodOverrideConfig)
|
2016-04-24 15:07:55 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
// MethodOverrideWithConfig returns a MethodOverride middleware from config.
|
2016-05-13 02:45:00 +02:00
|
|
|
// See: `MethodOverride()`.
|
2016-04-28 06:08:06 +02:00
|
|
|
func MethodOverrideWithConfig(config MethodOverrideConfig) echo.MiddlewareFunc {
|
2016-05-03 17:32:28 +02:00
|
|
|
// Defaults
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultMethodOverrideConfig.Skipper
|
|
|
|
}
|
2016-05-03 17:32:28 +02:00
|
|
|
if config.Getter == nil {
|
|
|
|
config.Getter = DefaultMethodOverrideConfig.Getter
|
|
|
|
}
|
|
|
|
|
2016-04-24 15:07:55 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2016-04-28 06:08:06 +02:00
|
|
|
req := c.Request()
|
|
|
|
if req.Method() == echo.POST {
|
|
|
|
m := config.Getter(c)
|
2016-04-24 15:07:55 +02:00
|
|
|
if m != "" {
|
2016-05-03 17:32:28 +02:00
|
|
|
req.SetMethod(m)
|
2016-04-24 15:07:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 06:08:06 +02:00
|
|
|
|
|
|
|
// MethodFromHeader is a `MethodOverrideGetter` that gets overridden method from
|
|
|
|
// the request header.
|
|
|
|
func MethodFromHeader(header string) MethodOverrideGetter {
|
|
|
|
return func(c echo.Context) string {
|
|
|
|
return c.Request().Header().Get(header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MethodFromForm is a `MethodOverrideGetter` that gets overridden method from the
|
|
|
|
// form parameter.
|
|
|
|
func MethodFromForm(param string) MethodOverrideGetter {
|
|
|
|
return func(c echo.Context) string {
|
|
|
|
return c.FormValue(param)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MethodFromQuery is a `MethodOverrideGetter` that gets overridden method from
|
|
|
|
// the query parameter.
|
|
|
|
func MethodFromQuery(param string) MethodOverrideGetter {
|
|
|
|
return func(c echo.Context) string {
|
|
|
|
return c.QueryParam(param)
|
|
|
|
}
|
|
|
|
}
|