2016-08-27 22:03:40 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
type (
|
|
|
|
// RedirectConfig defines the config for Redirect middleware.
|
|
|
|
RedirectConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
|
|
|
// Status code to be used when redirecting the request.
|
|
|
|
// Optional. Default value http.StatusMovedPermanently.
|
2017-12-28 21:24:34 +02:00
|
|
|
Code int `yaml:"code"`
|
2016-09-01 05:10:14 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-10-02 05:41:50 +02:00
|
|
|
const (
|
|
|
|
www = "www"
|
|
|
|
)
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
var (
|
|
|
|
// DefaultRedirectConfig is the default Redirect middleware config.
|
|
|
|
DefaultRedirectConfig = RedirectConfig{
|
2017-01-28 21:43:56 +02:00
|
|
|
Skipper: DefaultSkipper,
|
2016-09-01 05:10:14 +02:00
|
|
|
Code: http.StatusMovedPermanently,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
// Defaults
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:03:40 +02:00
|
|
|
req := c.Request()
|
2016-09-23 07:53:44 +02:00
|
|
|
host := req.Host
|
|
|
|
uri := req.RequestURI
|
|
|
|
if !c.IsTLS() {
|
2016-09-01 05:10:14 +02:00
|
|
|
return c.Redirect(config.Code, "https://"+host+uri)
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// Defaults
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:03:40 +02:00
|
|
|
req := c.Request()
|
2016-09-23 07:53:44 +02:00
|
|
|
host := req.Host
|
|
|
|
uri := req.RequestURI
|
2016-10-02 05:41:50 +02:00
|
|
|
if !c.IsTLS() && host[:3] != www {
|
2016-09-28 19:26:02 +02:00
|
|
|
return c.Redirect(config.Code, "https://www."+host+uri)
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// 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() {
|
2016-10-02 05:41:50 +02:00
|
|
|
if host[:3] == www {
|
2016-09-28 19:26:02 +02:00
|
|
|
return c.Redirect(config.Code, "https://"+host[4:]+uri)
|
|
|
|
}
|
|
|
|
return c.Redirect(config.Code, "https://"+host+uri)
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// Defaults
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:03:40 +02:00
|
|
|
req := c.Request()
|
2016-09-23 07:53:44 +02:00
|
|
|
scheme := c.Scheme()
|
|
|
|
host := req.Host
|
2016-10-02 05:41:50 +02:00
|
|
|
if host[:3] != www {
|
2016-09-23 07:53:44 +02:00
|
|
|
uri := req.RequestURI
|
2016-09-28 19:26:02 +02:00
|
|
|
return c.Redirect(config.Code, scheme+"://www."+host+uri)
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:03:40 +02:00
|
|
|
req := c.Request()
|
2016-09-23 07:53:44 +02:00
|
|
|
scheme := c.Scheme()
|
|
|
|
host := req.Host
|
2016-10-02 05:41:50 +02:00
|
|
|
if host[:3] == www {
|
2016-09-23 07:53:44 +02:00
|
|
|
uri := req.RequestURI
|
2016-09-28 19:26:02 +02:00
|
|
|
return c.Redirect(config.Code, scheme+"://"+host[4:]+uri)
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|