2024-03-09 11:21:24 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2016-04-24 15:07:55 +02:00
|
|
|
package middleware
|
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-10-14 17:16:58 +02:00
|
|
|
)
|
2016-04-24 15:07:55 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// MethodOverrideConfig defines the config for MethodOverride middleware.
|
|
|
|
type MethodOverrideConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
|
|
|
// Getter is a function that gets overridden method from the request.
|
|
|
|
// Optional. Default values MethodFromHeader(echo.HeaderXHTTPMethodOverride).
|
|
|
|
Getter MethodOverrideGetter
|
|
|
|
}
|
2016-04-28 06:08:06 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// MethodOverrideGetter is a function that gets overridden method from the request
|
|
|
|
type MethodOverrideGetter func(echo.Context) string
|
2016-04-28 06:08:06 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// DefaultMethodOverrideConfig is the default MethodOverride middleware config.
|
|
|
|
var DefaultMethodOverrideConfig = MethodOverrideConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
Getter: MethodFromHeader(echo.HeaderXHTTPMethodOverride),
|
|
|
|
}
|
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-09-01 05:10:14 +02:00
|
|
|
// MethodOverrideWithConfig returns a MethodOverride middleware with 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()
|
2018-10-14 17:16:58 +02:00
|
|
|
if req.Method == http.MethodPost {
|
2016-04-28 06:08:06 +02:00
|
|
|
m := config.Getter(c)
|
2016-04-24 15:07:55 +02:00
|
|
|
if m != "" {
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Method = 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 {
|
2016-09-23 07:53:44 +02:00
|
|
|
return c.Request().Header.Get(header)
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|