2016-08-27 22:03:40 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-08-27 22:03:40 +02:00
|
|
|
)
|
|
|
|
|
2018-01-30 05:54:02 +02:00
|
|
|
// RedirectConfig defines the config for Redirect middleware.
|
|
|
|
type RedirectConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper
|
|
|
|
|
|
|
|
// Status code to be used when redirecting the request.
|
|
|
|
// Optional. Default value http.StatusMovedPermanently.
|
|
|
|
Code int `yaml:"code"`
|
|
|
|
}
|
2016-09-01 05:10:14 +02:00
|
|
|
|
2018-02-02 17:31:07 +02:00
|
|
|
// redirectLogic represents a function that given a scheme, host and uri
|
2018-01-30 05:54:02 +02:00
|
|
|
// can both: 1) determine if redirect is needed (will set ok accordingly) and
|
|
|
|
// 2) return the appropriate redirect url.
|
2018-02-02 17:31:07 +02:00
|
|
|
type redirectLogic func(scheme, host, uri string) (ok bool, url string)
|
2016-10-02 05:41:50 +02:00
|
|
|
|
2018-01-30 05:54:02 +02:00
|
|
|
const www = "www"
|
|
|
|
|
|
|
|
// DefaultRedirectConfig is the default Redirect middleware config.
|
|
|
|
var DefaultRedirectConfig = RedirectConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
Code: http.StatusMovedPermanently,
|
|
|
|
}
|
2016-09-01 05:10:14 +02:00
|
|
|
|
2016-10-02 05:41:50 +02:00
|
|
|
// HTTPSRedirect redirects http requests to https.
|
2016-08-27 22:03:40 +02:00
|
|
|
// For example, http://labstack.com will be redirect to https://labstack.com.
|
2016-09-01 05:10:14 +02:00
|
|
|
//
|
|
|
|
// Usage `Echo#Pre(HTTPSRedirect())`
|
2016-08-27 22:03:40 +02:00
|
|
|
func HTTPSRedirect() echo.MiddlewareFunc {
|
2016-09-01 05:10:14 +02:00
|
|
|
return HTTPSRedirectWithConfig(DefaultRedirectConfig)
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
// HTTPSRedirectWithConfig returns an HTTPSRedirect middleware with config.
|
2016-09-01 05:10:14 +02:00
|
|
|
// See `HTTPSRedirect()`.
|
|
|
|
func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
2018-02-02 17:31:07 +02:00
|
|
|
return redirect(config, func(scheme, host, uri string) (ok bool, url string) {
|
|
|
|
if ok = scheme != "https"; ok {
|
2018-01-30 05:54:02 +02:00
|
|
|
url = "https://" + host + uri
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
2018-01-30 05:54:02 +02:00
|
|
|
return
|
|
|
|
})
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 05:41:50 +02:00
|
|
|
// HTTPSWWWRedirect redirects http requests to https www.
|
2016-08-27 22:03:40 +02:00
|
|
|
// For example, http://labstack.com will be redirect to https://www.labstack.com.
|
2016-09-01 05:10:14 +02:00
|
|
|
//
|
|
|
|
// Usage `Echo#Pre(HTTPSWWWRedirect())`
|
2016-08-27 22:03:40 +02:00
|
|
|
func HTTPSWWWRedirect() echo.MiddlewareFunc {
|
2016-09-01 05:10:14 +02:00
|
|
|
return HTTPSWWWRedirectWithConfig(DefaultRedirectConfig)
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
// HTTPSWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
|
2016-09-01 05:10:14 +02:00
|
|
|
// See `HTTPSWWWRedirect()`.
|
|
|
|
func HTTPSWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
2018-02-02 17:31:07 +02:00
|
|
|
return redirect(config, func(scheme, host, uri string) (ok bool, url string) {
|
|
|
|
if ok = scheme != "https" && host[:3] != www; ok {
|
2018-01-30 05:54:02 +02:00
|
|
|
url = "https://www." + host + uri
|
2016-09-28 19:26:02 +02:00
|
|
|
}
|
2018-01-30 05:54:02 +02:00
|
|
|
return
|
|
|
|
})
|
2016-09-28 19:26:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 05:41:50 +02:00
|
|
|
// HTTPSNonWWWRedirect redirects http requests to https non www.
|
2016-09-28 19:26:02 +02:00
|
|
|
// 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 {
|
2018-02-02 17:31:07 +02:00
|
|
|
return redirect(config, func(scheme, host, uri string) (ok bool, url string) {
|
|
|
|
if ok = scheme != "https"; ok {
|
2018-01-30 05:54:02 +02:00
|
|
|
if host[:3] == www {
|
|
|
|
host = host[4:]
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
2018-01-30 05:54:02 +02:00
|
|
|
url = "https://" + host + uri
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
2018-01-30 05:54:02 +02:00
|
|
|
return
|
|
|
|
})
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 05:41:50 +02:00
|
|
|
// WWWRedirect redirects non www requests to www.
|
2016-08-27 22:03:40 +02:00
|
|
|
// For example, http://labstack.com will be redirect to http://www.labstack.com.
|
2016-09-01 05:10:14 +02:00
|
|
|
//
|
|
|
|
// Usage `Echo#Pre(WWWRedirect())`
|
2016-08-27 22:03:40 +02:00
|
|
|
func WWWRedirect() echo.MiddlewareFunc {
|
2016-09-01 05:10:14 +02:00
|
|
|
return WWWRedirectWithConfig(DefaultRedirectConfig)
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
// WWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
|
2016-09-01 05:10:14 +02:00
|
|
|
// See `WWWRedirect()`.
|
|
|
|
func WWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
2018-02-02 17:31:07 +02:00
|
|
|
return redirect(config, func(scheme, host, uri string) (ok bool, url string) {
|
2018-01-30 05:54:02 +02:00
|
|
|
if ok = host[:3] != www; ok {
|
|
|
|
url = scheme + "://www." + host + uri
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
2018-01-30 05:54:02 +02:00
|
|
|
return
|
|
|
|
})
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 05:41:50 +02:00
|
|
|
// NonWWWRedirect redirects www requests to non www.
|
2016-08-27 22:03:40 +02:00
|
|
|
// For example, http://www.labstack.com will be redirect to http://labstack.com.
|
2016-09-01 05:10:14 +02:00
|
|
|
//
|
|
|
|
// Usage `Echo#Pre(NonWWWRedirect())`
|
2016-08-27 22:03:40 +02:00
|
|
|
func NonWWWRedirect() echo.MiddlewareFunc {
|
2016-09-01 05:10:14 +02:00
|
|
|
return NonWWWRedirectWithConfig(DefaultRedirectConfig)
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
// NonWWWRedirectWithConfig returns an HTTPSRedirect middleware with config.
|
2016-09-01 05:10:14 +02:00
|
|
|
// See `NonWWWRedirect()`.
|
|
|
|
func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
2018-02-02 17:31:07 +02:00
|
|
|
return redirect(config, func(scheme, host, uri string) (ok bool, url string) {
|
2018-01-30 05:54:02 +02:00
|
|
|
if ok = host[:3] == www; ok {
|
|
|
|
url = scheme + "://" + host[4:] + uri
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func redirect(config RedirectConfig, cb redirectLogic) echo.MiddlewareFunc {
|
2016-09-01 05:10:14 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultTrailingSlashConfig.Skipper
|
|
|
|
}
|
|
|
|
if config.Code == 0 {
|
|
|
|
config.Code = DefaultRedirectConfig.Code
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:03:40 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-09-01 05:10:14 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:54:02 +02:00
|
|
|
req, scheme := c.Request(), c.Scheme()
|
2016-09-23 07:53:44 +02:00
|
|
|
host := req.Host
|
2018-02-02 17:31:07 +02:00
|
|
|
if ok, url := cb(scheme, host, req.RequestURI); ok {
|
2018-01-30 05:54:02 +02:00
|
|
|
return c.Redirect(config.Code, url)
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
2018-01-30 05:54:02 +02:00
|
|
|
|
2016-08-27 22:03:40 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|