2024-03-09 11:21:24 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
package middleware
|
|
|
|
|
2017-12-28 01:14:44 +02:00
|
|
|
import (
|
2020-08-28 21:47:02 +02:00
|
|
|
"net/http"
|
2017-12-28 01:14:44 +02:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2017-12-28 01:14:44 +02:00
|
|
|
)
|
2016-07-27 18:34:44 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Skipper defines a function to skip middleware. Returning true skips processing
|
|
|
|
// the middleware.
|
|
|
|
type Skipper func(c echo.Context) bool
|
2018-06-29 03:13:02 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// BeforeFunc defines a function which is executed just before the middleware.
|
|
|
|
type BeforeFunc func(c echo.Context)
|
2016-07-27 18:34:44 +02:00
|
|
|
|
2017-12-28 01:14:44 +02:00
|
|
|
func captureTokens(pattern *regexp.Regexp, input string) *strings.Replacer {
|
|
|
|
groups := pattern.FindAllStringSubmatch(input, -1)
|
|
|
|
if groups == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
values := groups[0][1:]
|
|
|
|
replace := make([]string, 2*len(values))
|
|
|
|
for i, v := range values {
|
|
|
|
j := 2 * i
|
|
|
|
replace[j] = "$" + strconv.Itoa(i+1)
|
|
|
|
replace[j+1] = v
|
|
|
|
}
|
|
|
|
return strings.NewReplacer(replace...)
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:39:57 +02:00
|
|
|
func rewriteRulesRegex(rewrite map[string]string) map[*regexp.Regexp]string {
|
|
|
|
// Initialize
|
|
|
|
rulesRegex := map[*regexp.Regexp]string{}
|
|
|
|
for k, v := range rewrite {
|
|
|
|
k = regexp.QuoteMeta(k)
|
2023-03-28 10:42:55 +02:00
|
|
|
k = strings.ReplaceAll(k, `\*`, "(.*?)")
|
2020-09-03 09:39:57 +02:00
|
|
|
if strings.HasPrefix(k, `\^`) {
|
2023-03-28 10:42:55 +02:00
|
|
|
k = strings.ReplaceAll(k, `\^`, "^")
|
2020-09-03 09:39:57 +02:00
|
|
|
}
|
|
|
|
k = k + "$"
|
|
|
|
rulesRegex[regexp.MustCompile(k)] = v
|
|
|
|
}
|
|
|
|
return rulesRegex
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:22:11 +02:00
|
|
|
func rewriteURL(rewriteRegex map[*regexp.Regexp]string, req *http.Request) error {
|
|
|
|
if len(rewriteRegex) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-08 03:01:02 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Depending on how HTTP request is sent RequestURI could contain Scheme://Host/path or be just /path.
|
2021-03-09 14:22:11 +02:00
|
|
|
// We only want to use path part for rewriting and therefore trim prefix if it exists
|
|
|
|
rawURI := req.RequestURI
|
|
|
|
if rawURI != "" && rawURI[0] != '/' {
|
|
|
|
prefix := ""
|
|
|
|
if req.URL.Scheme != "" {
|
|
|
|
prefix = req.URL.Scheme + "://"
|
2021-03-08 03:01:02 +02:00
|
|
|
}
|
2021-03-09 14:22:11 +02:00
|
|
|
if req.URL.Host != "" {
|
|
|
|
prefix += req.URL.Host // host or host:port
|
|
|
|
}
|
|
|
|
if prefix != "" {
|
|
|
|
rawURI = strings.TrimPrefix(rawURI, prefix)
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 03:01:02 +02:00
|
|
|
|
2021-03-09 14:22:11 +02:00
|
|
|
for k, v := range rewriteRegex {
|
|
|
|
if replacer := captureTokens(k, rawURI); replacer != nil {
|
|
|
|
url, err := req.URL.Parse(replacer.Replace(v))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.URL = url
|
2021-03-08 03:01:02 +02:00
|
|
|
|
2021-03-09 14:22:11 +02:00
|
|
|
return nil // rewrite only once
|
2020-09-03 09:39:57 +02:00
|
|
|
}
|
2020-08-28 21:47:02 +02:00
|
|
|
}
|
2021-03-09 14:22:11 +02:00
|
|
|
return nil
|
2020-08-28 21:47:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-28 21:43:56 +02:00
|
|
|
// DefaultSkipper returns false which processes the middleware.
|
2017-02-19 23:02:10 +02:00
|
|
|
func DefaultSkipper(echo.Context) bool {
|
2016-07-27 18:34:44 +02:00
|
|
|
return false
|
|
|
|
}
|