2018-11-14 09:18:03 +00:00
|
|
|
package replacepathregex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-03-03 15:20:05 +00:00
|
|
|
"net/url"
|
2018-11-14 09:18:03 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares"
|
2024-03-12 08:48:04 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares/observability"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares/replacepath"
|
2024-01-08 08:10:06 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
2021-10-08 09:32:08 +00:00
|
|
|
const typeName = "ReplacePathRegex"
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
// ReplacePathRegex is a middleware used to replace the path of a URL request with a regular expression.
|
|
|
|
type replacePathRegex struct {
|
|
|
|
next http.Handler
|
|
|
|
regexp *regexp.Regexp
|
|
|
|
replacement string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new replace path regex middleware.
|
2019-07-10 07:26:04 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, config dynamic.ReplacePathRegex, name string) (http.Handler, error) {
|
2022-11-21 17:36:05 +00:00
|
|
|
middlewares.GetLogger(ctx, name, typeName).Debug().Msg("Creating middleware")
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
exp, err := regexp.Compile(strings.TrimSpace(config.Regex))
|
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return nil, fmt.Errorf("error compiling regular expression %s: %w", config.Regex, err)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &replacePathRegex{
|
|
|
|
regexp: exp,
|
|
|
|
replacement: strings.TrimSpace(config.Replacement),
|
|
|
|
next: next,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
func (rp *replacePathRegex) GetTracingInformation() (string, string, trace.SpanKind) {
|
|
|
|
return rp.name, typeName, trace.SpanKindInternal
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *replacePathRegex) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2021-10-08 09:32:08 +00:00
|
|
|
currentPath := req.URL.RawPath
|
|
|
|
if currentPath == "" {
|
|
|
|
currentPath = req.URL.EscapedPath()
|
2020-03-03 15:20:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 12:28:04 +00:00
|
|
|
if rp.regexp != nil && rp.regexp.MatchString(currentPath) {
|
2020-03-03 15:20:05 +00:00
|
|
|
req.Header.Add(replacepath.ReplacedPathHeader, currentPath)
|
|
|
|
req.URL.RawPath = rp.regexp.ReplaceAllString(currentPath, rp.replacement)
|
|
|
|
|
|
|
|
// as replacement can introduce escaped characters
|
|
|
|
// Path must remain an unescaped version of RawPath
|
|
|
|
// Doesn't handle multiple times encoded replacement (`/` => `%2F` => `%252F` => ...)
|
|
|
|
var err error
|
|
|
|
req.URL.Path, err = url.PathUnescape(req.URL.RawPath)
|
|
|
|
if err != nil {
|
2024-08-28 14:11:38 +00:00
|
|
|
middlewares.GetLogger(context.Background(), rp.name, typeName).Error().Msgf("Unable to unescape url raw path %q: %v", req.URL.RawPath, err)
|
|
|
|
observability.SetStatusErrorf(req.Context(), "Unable to unescape url raw path %q: %v", req.URL.RawPath, err)
|
2020-03-03 15:20:05 +00:00
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
req.RequestURI = req.URL.RequestURI()
|
|
|
|
}
|
2020-03-03 15:20:05 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
rp.next.ServeHTTP(rw, req)
|
|
|
|
}
|