2024-05-23 23:44:31 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2017-02-18 23:00:46 +01:00
|
|
|
package middleware
|
|
|
|
|
2019-01-31 17:06:36 +01:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
2017-02-18 23:00:46 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// MethodOverrideConfig defines the config for MethodOverride middleware.
|
|
|
|
type MethodOverrideConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
2017-02-18 23:00:46 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// Getter is a function that gets overridden method from the request.
|
|
|
|
// Optional. Default values MethodFromHeader(echo.HeaderXHTTPMethodOverride).
|
|
|
|
Getter MethodOverrideGetter
|
|
|
|
}
|
2017-02-18 23:00:46 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// MethodOverrideGetter is a function that gets overridden method from the request
|
|
|
|
type MethodOverrideGetter func(echo.Context) string
|
2017-02-18 23:00:46 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// DefaultMethodOverrideConfig is the default MethodOverride middleware config.
|
|
|
|
var DefaultMethodOverrideConfig = MethodOverrideConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
Getter: MethodFromHeader(echo.HeaderXHTTPMethodOverride),
|
|
|
|
}
|
2017-02-18 23:00:46 +01:00
|
|
|
|
|
|
|
// MethodOverride returns a MethodOverride middleware.
|
|
|
|
// MethodOverride middleware checks for the overridden method from the request and
|
|
|
|
// uses it instead of the original method.
|
|
|
|
//
|
|
|
|
// For security reasons, only `POST` method can be overridden.
|
|
|
|
func MethodOverride() echo.MiddlewareFunc {
|
|
|
|
return MethodOverrideWithConfig(DefaultMethodOverrideConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MethodOverrideWithConfig returns a MethodOverride middleware with config.
|
|
|
|
// See: `MethodOverride()`.
|
|
|
|
func MethodOverrideWithConfig(config MethodOverrideConfig) echo.MiddlewareFunc {
|
|
|
|
// Defaults
|
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultMethodOverrideConfig.Skipper
|
|
|
|
}
|
|
|
|
if config.Getter == nil {
|
|
|
|
config.Getter = DefaultMethodOverrideConfig.Getter
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := c.Request()
|
2019-01-31 17:06:36 +01:00
|
|
|
if req.Method == http.MethodPost {
|
2017-02-18 23:00:46 +01:00
|
|
|
m := config.Getter(c)
|
|
|
|
if m != "" {
|
|
|
|
req.Method = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|