mirror of
https://github.com/labstack/echo.git
synced 2024-12-18 16:20:53 +02:00
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type (
|
|
// Skipper defines a function to skip middleware. Returning true skips processing
|
|
// the middleware.
|
|
Skipper func(echo.Context) bool
|
|
|
|
// BeforeFunc defines a function which is executed just before the middleware.
|
|
BeforeFunc func(echo.Context)
|
|
)
|
|
|
|
func captureTokens(pattern *regexp.Regexp, input string) *strings.Replacer {
|
|
groups := pattern.FindAllStringSubmatch(input, -1)
|
|
if groups == nil {
|
|
return nil
|
|
}
|
|
values := groups[0][1:]
|
|
replace := make([]string, 2*len(values))
|
|
for i, v := range values {
|
|
j := 2 * i
|
|
replace[j] = "$" + strconv.Itoa(i+1)
|
|
replace[j+1] = v
|
|
}
|
|
return strings.NewReplacer(replace...)
|
|
}
|
|
|
|
//rewritePath sets request url path and raw path
|
|
func rewritePath(replacer *strings.Replacer, target string, req *http.Request) error {
|
|
replacerRawPath := replacer.Replace(target)
|
|
replacerPath, err := url.PathUnescape(replacerRawPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.URL.Path, req.URL.RawPath = replacerPath, replacerRawPath
|
|
return nil
|
|
}
|
|
|
|
// DefaultSkipper returns false which processes the middleware.
|
|
func DefaultSkipper(echo.Context) bool {
|
|
return false
|
|
}
|