1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Enhanced redirect middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-09-28 10:26:02 -07:00
parent 152be1aaff
commit ef584add8c
2 changed files with 60 additions and 8 deletions

View File

@ -34,7 +34,7 @@ func HTTPSRedirect() echo.MiddlewareFunc {
return HTTPSRedirectWithConfig(DefaultRedirectConfig)
}
// HTTPSRedirectWithConfig returns a HTTPSRedirect middleware with config.
// HTTPSRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `HTTPSRedirect()`.
func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults
@ -63,7 +63,7 @@ func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
}
}
// HTTPSWWWRedirect redirects HTTP requests to WWW HTTPS.
// HTTPSWWWRedirect redirects HTTP requests to HTTPS WWW.
// For example, http://labstack.com will be redirect to https://www.labstack.com.
//
// Usage `Echo#Pre(HTTPSWWWRedirect())`
@ -71,7 +71,7 @@ func HTTPSWWWRedirect() echo.MiddlewareFunc {
return HTTPSWWWRedirectWithConfig(DefaultRedirectConfig)
}
// HTTPSWWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
// HTTPSWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `HTTPSWWWRedirect()`.
func HTTPSWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults
@ -92,7 +92,46 @@ func HTTPSWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
host := req.Host
uri := req.RequestURI
if !c.IsTLS() && host[:3] != "www" {
return c.Redirect(http.StatusMovedPermanently, "https://www."+host+uri)
return c.Redirect(config.Code, "https://www."+host+uri)
}
return next(c)
}
}
}
// HTTPSNonWWWRedirect redirects HTTP requests to HTTPS non WWW.
// For example, http://www.labstack.com will be redirect to https://labstack.com.
//
// Usage `Echo#Pre(HTTPSNonWWWRedirect())`
func HTTPSNonWWWRedirect() echo.MiddlewareFunc {
return HTTPSNonWWWRedirectWithConfig(DefaultRedirectConfig)
}
// HTTPSNonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `HTTPSNonWWWRedirect()`.
func HTTPSNonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults
if config.Skipper == nil {
config.Skipper = DefaultTrailingSlashConfig.Skipper
}
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() {
if host[:3] == "www" {
return c.Redirect(config.Code, "https://"+host[4:]+uri)
}
return c.Redirect(config.Code, "https://"+host+uri)
}
return next(c)
}
@ -107,7 +146,7 @@ func WWWRedirect() echo.MiddlewareFunc {
return WWWRedirectWithConfig(DefaultRedirectConfig)
}
// WWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
// WWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `WWWRedirect()`.
func WWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
// Defaults
@ -129,7 +168,7 @@ func WWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
host := req.Host
if host[:3] != "www" {
uri := req.RequestURI
return c.Redirect(http.StatusMovedPermanently, scheme+"://www."+host+uri)
return c.Redirect(config.Code, scheme+"://www."+host+uri)
}
return next(c)
}
@ -144,7 +183,7 @@ func NonWWWRedirect() echo.MiddlewareFunc {
return NonWWWRedirectWithConfig(DefaultRedirectConfig)
}
// NonWWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
// NonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
// See `NonWWWRedirect()`.
func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
if config.Skipper == nil {
@ -165,7 +204,7 @@ func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
host := req.Host
if host[:3] == "www" {
uri := req.RequestURI
return c.Redirect(http.StatusMovedPermanently, scheme+"://"+host[4:]+uri)
return c.Redirect(config.Code, scheme+"://"+host[4:]+uri)
}
return next(c)
}

View File

@ -35,6 +35,19 @@ func TestRedirectHTTPSWWWRedirect(t *testing.T) {
assert.Equal(t, "https://www.labstack.com", res.Header().Get(echo.HeaderLocation))
}
func TestRedirectHTTPSNonWWWRedirect(t *testing.T) {
e := echo.New()
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req, _ := http.NewRequest(echo.GET, "http://www.labstack.com", nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
HTTPSNonWWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Code)
assert.Equal(t, "https://labstack.com", res.Header().Get(echo.HeaderLocation))
}
func TestRedirectWWWRedirect(t *testing.T) {
e := echo.New()
next := func(c echo.Context) (err error) {