mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
6ef5f77bf2
WIP: make default logger implemented custom writer for jsonlike logs WIP: improve examples WIP: defaultErrorHandler use errors.As to unwrap errors. Update readme WIP: default logger logs json, restore e.Start method WIP: clean router.Match a bit WIP: func types/fields have echo.Context has first element WIP: remove yaml tags as functions etc can not be serialized anyway WIP: change BindPathParams,BindQueryParams,BindHeaders from methods to functions and reverse arguments to be like DefaultBinder.Bind is WIP: improved comments, logger now extracts status from error WIP: go mod tidy WIP: rebase with 4.5.0 WIP: * removed todos. * removed StartAutoTLS and StartH2CServer methods from `StartConfig` * KeyAuth middleware errorhandler can swallow the error and resume next middleware WIP: add RouterConfig.UseEscapedPathForMatching to use escaped path for matching request against routes WIP: FIXMEs WIP: upgrade golang-jwt/jwt to `v4` WIP: refactor http methods to return RouteInfo WIP: refactor static not creating multiple routes WIP: refactor route and middleware adding functions not to return error directly WIP: Use 401 for problematic/missing headers for key auth and JWT middleware (#1552, #1402). > In summary, a 401 Unauthorized response should be used for missing or bad authentication WIP: replace `HTTPError.SetInternal` with `HTTPError.WithInternal` so we could not mutate global error variables WIP: add RouteInfo and RouteMatchType into Context what we could know from in middleware what route was matched and/or type of that match (200/404/405) WIP: make notFoundHandler and methodNotAllowedHandler private. encourage that all errors be handled in Echo.HTTPErrorHandler WIP: server cleanup ideas WIP: routable.ForGroup WIP: note about logger middleware WIP: bind should not default values on second try. use crypto rand for better randomness WIP: router add route as interface and returns info as interface WIP: improve flaky test (remains still flaky) WIP: add notes about bind default values WIP: every route can have their own path params names WIP: routerCreator and different tests WIP: different things WIP: remove route implementation WIP: support custom method types WIP: extractor tests WIP: v5.0.x proposal over v4.4.0
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// Skipper defines a function to skip middleware. Returning true skips processing the middleware.
|
|
type Skipper func(c echo.Context) bool
|
|
|
|
// BeforeFunc defines a function which is executed just before the middleware.
|
|
type BeforeFunc func(c 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...)
|
|
}
|
|
|
|
func rewriteRulesRegex(rewrite map[string]string) map[*regexp.Regexp]string {
|
|
// Initialize
|
|
rulesRegex := map[*regexp.Regexp]string{}
|
|
for k, v := range rewrite {
|
|
k = regexp.QuoteMeta(k)
|
|
k = strings.Replace(k, `\*`, "(.*?)", -1)
|
|
if strings.HasPrefix(k, `\^`) {
|
|
k = strings.Replace(k, `\^`, "^", -1)
|
|
}
|
|
k = k + "$"
|
|
rulesRegex[regexp.MustCompile(k)] = v
|
|
}
|
|
return rulesRegex
|
|
}
|
|
|
|
func rewriteURL(rewriteRegex map[*regexp.Regexp]string, req *http.Request) error {
|
|
if len(rewriteRegex) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// Depending how HTTP request is sent RequestURI could contain Scheme://Host/path or be just /path.
|
|
// We only want to use path part for rewriting and therefore trim prefix if it exists
|
|
rawURI := req.RequestURI
|
|
if rawURI != "" && rawURI[0] != '/' {
|
|
prefix := ""
|
|
if req.URL.Scheme != "" {
|
|
prefix = req.URL.Scheme + "://"
|
|
}
|
|
if req.URL.Host != "" {
|
|
prefix += req.URL.Host // host or host:port
|
|
}
|
|
if prefix != "" {
|
|
rawURI = strings.TrimPrefix(rawURI, prefix)
|
|
}
|
|
}
|
|
|
|
for k, v := range rewriteRegex {
|
|
if replacer := captureTokens(k, rawURI); replacer != nil {
|
|
url, err := req.URL.Parse(replacer.Replace(v))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.URL = url
|
|
|
|
return nil // rewrite only once
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DefaultSkipper returns false which processes the middleware.
|
|
func DefaultSkipper(echo.Context) bool {
|
|
return false
|
|
}
|
|
|
|
func toMiddlewareOrPanic(config echo.MiddlewareConfigurator) echo.MiddlewareFunc {
|
|
mw, err := config.ToMiddleware()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return mw
|
|
}
|