1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-17 01:43:02 +02:00

Applied a little DRY to the redirect middleware (#1053)

This commit is contained in:
Alexandru Ungur
2018-01-30 05:54:02 +02:00
committed by Vishal Rana
parent 40cae83772
commit 2769113edf

View File

@ -6,29 +6,28 @@ import (
"github.com/labstack/echo" "github.com/labstack/echo"
) )
type ( // RedirectConfig defines the config for Redirect middleware.
// RedirectConfig defines the config for Redirect middleware. type RedirectConfig struct {
RedirectConfig struct { // Skipper defines a function to skip middleware.
// Skipper defines a function to skip middleware. Skipper
Skipper Skipper
// Status code to be used when redirecting the request. // Status code to be used when redirecting the request.
// Optional. Default value http.StatusMovedPermanently. // Optional. Default value http.StatusMovedPermanently.
Code int `yaml:"code"` Code int `yaml:"code"`
} }
)
const ( // redirectLogic represents a function that given a tls flag, host and uri
www = "www" // can both: 1) determine if redirect is needed (will set ok accordingly) and
) // 2) return the appropriate redirect url.
type redirectLogic func(tls bool, scheme, host, uri string) (ok bool, url string)
var ( const www = "www"
// DefaultRedirectConfig is the default Redirect middleware config.
DefaultRedirectConfig = RedirectConfig{ // DefaultRedirectConfig is the default Redirect middleware config.
Skipper: DefaultSkipper, var DefaultRedirectConfig = RedirectConfig{
Code: http.StatusMovedPermanently, Skipper: DefaultSkipper,
} Code: http.StatusMovedPermanently,
) }
// HTTPSRedirect redirects http requests to https. // HTTPSRedirect redirects http requests to https.
// For example, http://labstack.com will be redirect to https://labstack.com. // For example, http://labstack.com will be redirect to https://labstack.com.
@ -41,29 +40,12 @@ func HTTPSRedirect() echo.MiddlewareFunc {
// HTTPSRedirectWithConfig returns an HTTPSRedirect middleware with config. // HTTPSRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `HTTPSRedirect()`. // See `HTTPSRedirect()`.
func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults return redirect(config, func(isTLS bool, _, host, uri string) (ok bool, url string) {
if config.Skipper == nil { if ok = !isTLS; ok {
config.Skipper = DefaultTrailingSlashConfig.Skipper url = "https://" + host + uri
}
if config.Code == 0 {
config.Code = DefaultRedirectConfig.Code
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
req := c.Request()
host := req.Host
uri := req.RequestURI
if !c.IsTLS() {
return c.Redirect(config.Code, "https://"+host+uri)
}
return next(c)
} }
} return
})
} }
// HTTPSWWWRedirect redirects http requests to https www. // HTTPSWWWRedirect redirects http requests to https www.
@ -77,29 +59,12 @@ func HTTPSWWWRedirect() echo.MiddlewareFunc {
// HTTPSWWWRedirectWithConfig returns an HTTPSRedirect middleware with config. // HTTPSWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `HTTPSWWWRedirect()`. // See `HTTPSWWWRedirect()`.
func HTTPSWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { func HTTPSWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults return redirect(config, func(isTLS bool, _, host, uri string) (ok bool, url string) {
if config.Skipper == nil { if ok = !isTLS && host[:3] != www; ok {
config.Skipper = DefaultTrailingSlashConfig.Skipper url = "https://www." + host + uri
}
if config.Code == 0 {
config.Code = DefaultRedirectConfig.Code
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
req := c.Request()
host := req.Host
uri := req.RequestURI
if !c.IsTLS() && host[:3] != www {
return c.Redirect(config.Code, "https://www."+host+uri)
}
return next(c)
} }
} return
})
} }
// HTTPSNonWWWRedirect redirects http requests to https non www. // HTTPSNonWWWRedirect redirects http requests to https non www.
@ -113,32 +78,15 @@ func HTTPSNonWWWRedirect() echo.MiddlewareFunc {
// HTTPSNonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config. // HTTPSNonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `HTTPSNonWWWRedirect()`. // See `HTTPSNonWWWRedirect()`.
func HTTPSNonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { func HTTPSNonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults return redirect(config, func(isTLS bool, _, host, uri string) (ok bool, url string) {
if config.Skipper == nil { if ok = !isTLS; ok {
config.Skipper = DefaultTrailingSlashConfig.Skipper if host[:3] == www {
} host = host[4:]
if config.Code == 0 {
config.Code = DefaultRedirectConfig.Code
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
} }
url = "https://" + host + uri
req := c.Request()
host := req.Host
uri := req.RequestURI
if !c.IsTLS() {
if host[:3] == www {
return c.Redirect(config.Code, "https://"+host[4:]+uri)
}
return c.Redirect(config.Code, "https://"+host+uri)
}
return next(c)
} }
} return
})
} }
// WWWRedirect redirects non www requests to www. // WWWRedirect redirects non www requests to www.
@ -152,30 +100,12 @@ func WWWRedirect() echo.MiddlewareFunc {
// WWWRedirectWithConfig returns an HTTPSRedirect middleware with config. // WWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `WWWRedirect()`. // See `WWWRedirect()`.
func WWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { func WWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults return redirect(config, func(_ bool, scheme, host, uri string) (ok bool, url string) {
if config.Skipper == nil { if ok = host[:3] != www; ok {
config.Skipper = DefaultTrailingSlashConfig.Skipper url = scheme + "://www." + host + uri
}
if config.Code == 0 {
config.Code = DefaultRedirectConfig.Code
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
req := c.Request()
scheme := c.Scheme()
host := req.Host
if host[:3] != www {
uri := req.RequestURI
return c.Redirect(config.Code, scheme+"://www."+host+uri)
}
return next(c)
} }
} return
})
} }
// NonWWWRedirect redirects www requests to non www. // NonWWWRedirect redirects www requests to non www.
@ -189,6 +119,15 @@ func NonWWWRedirect() echo.MiddlewareFunc {
// NonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config. // NonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `NonWWWRedirect()`. // See `NonWWWRedirect()`.
func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc { func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
return redirect(config, func(isTLS bool, scheme, host, uri string) (ok bool, url string) {
if ok = host[:3] == www; ok {
url = scheme + "://" + host[4:] + uri
}
return
})
}
func redirect(config RedirectConfig, cb redirectLogic) echo.MiddlewareFunc {
if config.Skipper == nil { if config.Skipper == nil {
config.Skipper = DefaultTrailingSlashConfig.Skipper config.Skipper = DefaultTrailingSlashConfig.Skipper
} }
@ -202,13 +141,12 @@ func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
return next(c) return next(c)
} }
req := c.Request() req, scheme := c.Request(), c.Scheme()
scheme := c.Scheme()
host := req.Host host := req.Host
if host[:3] == www { if ok, url := cb(c.IsTLS(), scheme, host, req.RequestURI); ok {
uri := req.RequestURI return c.Redirect(config.Code, url)
return c.Redirect(config.Code, scheme+"://"+host[4:]+uri)
} }
return next(c) return next(c)
} }
} }