diff --git a/pkg/middlewares/redirect/redirect.go b/pkg/middlewares/redirect/redirect.go index d2783d1bc..e09611e37 100644 --- a/pkg/middlewares/redirect/redirect.go +++ b/pkg/middlewares/redirect/redirect.go @@ -15,6 +15,8 @@ const ( schemeHTTPS = "https" ) +var uriRegexp = regexp.MustCompile(`^(https?):\/\/(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$`) + type redirect struct { next http.Handler regex *regexp.Regexp diff --git a/pkg/middlewares/redirect/redirect_regex.go b/pkg/middlewares/redirect/redirect_regex.go index f26b5e3d4..bc4ae3843 100644 --- a/pkg/middlewares/redirect/redirect_regex.go +++ b/pkg/middlewares/redirect/redirect_regex.go @@ -3,7 +3,6 @@ package redirect import ( "context" "net/http" - "regexp" "strings" "github.com/traefik/traefik/v2/pkg/config/dynamic" @@ -11,9 +10,7 @@ import ( "github.com/traefik/traefik/v2/pkg/middlewares" ) -const ( - typeRegexName = "RedirectRegex" -) +const typeRegexName = "RedirectRegex" // NewRedirectRegex creates a redirect middleware. func NewRedirectRegex(ctx context.Context, next http.Handler, conf dynamic.RedirectRegex, name string) (http.Handler, error) { @@ -30,10 +27,7 @@ func rawURL(req *http.Request) string { port := "" uri := req.RequestURI - schemeRegex := `^(https?):\/\/(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$` - re, _ := regexp.Compile(schemeRegex) - if re.Match([]byte(req.RequestURI)) { - match := re.FindStringSubmatch(req.RequestURI) + if match := uriRegexp.FindStringSubmatch(req.RequestURI); len(match) > 0 { scheme = match[1] if len(match[2]) > 0 { diff --git a/pkg/middlewares/redirect/redirect_scheme.go b/pkg/middlewares/redirect/redirect_scheme.go index 36bcb25fd..00e30b8b4 100644 --- a/pkg/middlewares/redirect/redirect_scheme.go +++ b/pkg/middlewares/redirect/redirect_scheme.go @@ -5,7 +5,6 @@ import ( "errors" "net" "net/http" - "regexp" "strings" "github.com/traefik/traefik/v2/pkg/config/dynamic" @@ -14,8 +13,8 @@ import ( ) const ( - typeSchemeName = "RedirectScheme" - schemeRedirectRegex = `^(https?:\/\/)?(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$` + typeSchemeName = "RedirectScheme" + uriPattern = `^(https?:\/\/)?(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$` ) // NewRedirectScheme creates a new RedirectScheme middleware. @@ -33,7 +32,7 @@ func NewRedirectScheme(ctx context.Context, next http.Handler, conf dynamic.Redi port = ":" + conf.Port } - return newRedirect(next, schemeRedirectRegex, conf.Scheme+"://${2}"+port+"${4}", conf.Permanent, rawURLScheme, name) + return newRedirect(next, uriPattern, conf.Scheme+"://${2}"+port+"${4}", conf.Permanent, rawURLScheme, name) } func rawURLScheme(req *http.Request) string { @@ -46,10 +45,7 @@ func rawURLScheme(req *http.Request) string { } uri := req.RequestURI - schemeRegex := `^(https?):\/\/(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$` - re, _ := regexp.Compile(schemeRegex) - if re.Match([]byte(req.RequestURI)) { - match := re.FindStringSubmatch(req.RequestURI) + if match := uriRegexp.FindStringSubmatch(req.RequestURI); len(match) > 0 { scheme = match[1] if len(match[2]) > 0 {