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.
|
|
|
|
Code int `json:"code"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultRedirectConfig is the default Redirect middleware config.
|
|
|
|
DefaultRedirectConfig = RedirectConfig{
|
|
|
|
Skipper: defaultSkipper,
|
|
|
|
Code: http.StatusMovedPermanently,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-08-27 22:03:40 +02:00
|
|
|
// HTTPSRedirect redirects HTTP requests to HTTPS.
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPSRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
|
|
|
// 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
|
|
|
|
println(uri)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPSWWWRedirect redirects HTTP requests to WWW HTTPS.
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPSWWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
|
|
|
// 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
|
|
|
|
if !c.IsTLS() && host[:3] != "www" {
|
2016-08-27 22:03:40 +02:00
|
|
|
return c.Redirect(http.StatusMovedPermanently, "https://www."+host+uri)
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WWWRedirect redirects non WWW requests to WWW.
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
|
|
|
// 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-08-27 22:03:40 +02:00
|
|
|
if host[:3] != "www" {
|
2016-09-23 07:53:44 +02:00
|
|
|
uri := req.RequestURI
|
2016-08-27 22:03:40 +02:00
|
|
|
return c.Redirect(http.StatusMovedPermanently, scheme+"://www."+host+uri)
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 19:20:10 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NonWWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
|
|
|
// 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-08-27 22:03:40 +02:00
|
|
|
if host[:3] == "www" {
|
2016-09-23 07:53:44 +02:00
|
|
|
uri := req.RequestURI
|
2016-08-27 22:03:40 +02:00
|
|
|
return c.Redirect(http.StatusMovedPermanently, scheme+"://"+host[4:]+uri)
|
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|