diff options
Diffstat (limited to 'vendor/github.com/labstack/echo/v4/middleware')
5 files changed, 62 insertions, 49 deletions
diff --git a/vendor/github.com/labstack/echo/v4/middleware/middleware.go b/vendor/github.com/labstack/echo/v4/middleware/middleware.go index 6bdb0eb7..a7ad73a5 100644 --- a/vendor/github.com/labstack/echo/v4/middleware/middleware.go +++ b/vendor/github.com/labstack/echo/v4/middleware/middleware.go @@ -2,7 +2,6 @@ package middleware import ( "net/http" - "net/url" "regexp" "strconv" "strings" @@ -49,30 +48,39 @@ func rewriteRulesRegex(rewrite map[string]string) map[*regexp.Regexp]string { return rulesRegex } -func rewritePath(rewriteRegex map[*regexp.Regexp]string, req *http.Request) { - for k, v := range rewriteRegex { - rawPath := req.URL.RawPath - if rawPath != "" { - // RawPath is only set when there has been escaping done. In that case Path must be deduced from rewritten RawPath - // because encoded Path could match rules that RawPath did not - if replacer := captureTokens(k, rawPath); replacer != nil { - rawPath = replacer.Replace(v) - - req.URL.RawPath = rawPath - req.URL.Path, _ = url.PathUnescape(rawPath) - - return // rewrite only once - } +func rewriteURL(rewriteRegex map[*regexp.Regexp]string, req *http.Request) error { + if len(rewriteRegex) == 0 { + return nil + } - continue + // 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) + } + } - if replacer := captureTokens(k, req.URL.Path); replacer != nil { - req.URL.Path = replacer.Replace(v) + 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 // rewrite only once + return nil // rewrite only once } } + return nil } // DefaultSkipper returns false which processes the middleware. diff --git a/vendor/github.com/labstack/echo/v4/middleware/proxy.go b/vendor/github.com/labstack/echo/v4/middleware/proxy.go index 63eec5a2..6f01f3a7 100644 --- a/vendor/github.com/labstack/echo/v4/middleware/proxy.go +++ b/vendor/github.com/labstack/echo/v4/middleware/proxy.go @@ -231,8 +231,9 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc { tgt := config.Balancer.Next(c) c.Set(config.ContextKey, tgt) - // Set rewrite path and raw path - rewritePath(config.RegexRewrite, req) + if err := rewriteURL(config.RegexRewrite, req); err != nil { + return err + } // Fix header // Basically it's not good practice to unconditionally pass incoming x-real-ip header to upstream. diff --git a/vendor/github.com/labstack/echo/v4/middleware/redirect.go b/vendor/github.com/labstack/echo/v4/middleware/redirect.go index 813e5b85..13877db3 100644 --- a/vendor/github.com/labstack/echo/v4/middleware/redirect.go +++ b/vendor/github.com/labstack/echo/v4/middleware/redirect.go @@ -2,6 +2,7 @@ package middleware import ( "net/http" + "strings" "github.com/labstack/echo/v4" ) @@ -40,11 +41,11 @@ func HTTPSRedirect() echo.MiddlewareFunc { // HTTPSRedirectWithConfig returns an HTTPSRedirect middleware with config. // See `HTTPSRedirect()`. func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { - return redirect(config, func(scheme, host, uri string) (ok bool, url string) { - if ok = scheme != "https"; ok { - url = "https://" + host + uri + return redirect(config, func(scheme, host, uri string) (bool, string) { + if scheme != "https" { + return true, "https://" + host + uri } - return + return false, "" }) } @@ -59,11 +60,11 @@ func HTTPSWWWRedirect() echo.MiddlewareFunc { // HTTPSWWWRedirectWithConfig returns an HTTPSRedirect middleware with config. // See `HTTPSWWWRedirect()`. func HTTPSWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { - return redirect(config, func(scheme, host, uri string) (ok bool, url string) { - if ok = scheme != "https" && host[:4] != www; ok { - url = "https://www." + host + uri + return redirect(config, func(scheme, host, uri string) (bool, string) { + if scheme != "https" && !strings.HasPrefix(host, www) { + return true, "https://www." + host + uri } - return + return false, "" }) } @@ -79,13 +80,11 @@ func HTTPSNonWWWRedirect() echo.MiddlewareFunc { // See `HTTPSNonWWWRedirect()`. func HTTPSNonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { return redirect(config, func(scheme, host, uri string) (ok bool, url string) { - if ok = scheme != "https"; ok { - if host[:4] == www { - host = host[4:] - } - url = "https://" + host + uri + if scheme != "https" { + host = strings.TrimPrefix(host, www) + return true, "https://" + host + uri } - return + return false, "" }) } @@ -100,11 +99,11 @@ func WWWRedirect() echo.MiddlewareFunc { // WWWRedirectWithConfig returns an HTTPSRedirect middleware with config. // See `WWWRedirect()`. func WWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { - return redirect(config, func(scheme, host, uri string) (ok bool, url string) { - if ok = host[:4] != www; ok { - url = scheme + "://www." + host + uri + return redirect(config, func(scheme, host, uri string) (bool, string) { + if !strings.HasPrefix(host, www) { + return true, scheme + "://www." + host + uri } - return + return false, "" }) } @@ -119,17 +118,17 @@ func NonWWWRedirect() echo.MiddlewareFunc { // NonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config. // See `NonWWWRedirect()`. func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { - return redirect(config, func(scheme, host, uri string) (ok bool, url string) { - if ok = host[:4] == www; ok { - url = scheme + "://" + host[4:] + uri + return redirect(config, func(scheme, host, uri string) (bool, string) { + if strings.HasPrefix(host, www) { + return true, scheme + "://" + host[4:] + uri } - return + return false, "" }) } func redirect(config RedirectConfig, cb redirectLogic) echo.MiddlewareFunc { if config.Skipper == nil { - config.Skipper = DefaultTrailingSlashConfig.Skipper + config.Skipper = DefaultRedirectConfig.Skipper } if config.Code == 0 { config.Code = DefaultRedirectConfig.Code diff --git a/vendor/github.com/labstack/echo/v4/middleware/rewrite.go b/vendor/github.com/labstack/echo/v4/middleware/rewrite.go index c05d5d84..e5b0a6b5 100644 --- a/vendor/github.com/labstack/echo/v4/middleware/rewrite.go +++ b/vendor/github.com/labstack/echo/v4/middleware/rewrite.go @@ -72,9 +72,9 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc { return next(c) } - req := c.Request() - // Set rewrite path and raw path - rewritePath(config.RegexRules, req) + if err := rewriteURL(config.RegexRules, c.Request()); err != nil { + return err + } return next(c) } } diff --git a/vendor/github.com/labstack/echo/v4/middleware/timeout.go b/vendor/github.com/labstack/echo/v4/middleware/timeout.go index 68f464e4..5d23ff45 100644 --- a/vendor/github.com/labstack/echo/v4/middleware/timeout.go +++ b/vendor/github.com/labstack/echo/v4/middleware/timeout.go @@ -42,8 +42,8 @@ var ( } ) -// Timeout returns a middleware which recovers from panics anywhere in the chain -// and handles the control to the centralized HTTPErrorHandler. +// Timeout returns a middleware which returns error (503 Service Unavailable error) to client immediately when handler +// call runs for longer than its time limit. NB: timeout does not stop handler execution. func Timeout() echo.MiddlewareFunc { return TimeoutWithConfig(DefaultTimeoutConfig) } @@ -106,6 +106,11 @@ func (t echoHandlerFuncWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Reques // so on timeout writer stays what http.TimeoutHandler uses and prevents writing headers/body t.ctx.Response().Writer = originalWriter if err != nil { + // call global error handler to write error to the client. This is needed or `http.TimeoutHandler` will send status code by itself + // and after that our tries to write status code will not work anymore + t.ctx.Error(err) + // we pass error from handler to middlewares up in handler chain to act on it if needed. But this means that + // global error handler is probably be called twice as `t.ctx.Error` already does that. t.errChan <- err } } |