fix: use EscapedPath as header value when RawPath is empty
This commit is contained in:
parent
25e12aee14
commit
fe32a7e584
4 changed files with 28 additions and 13 deletions
|
@ -41,12 +41,12 @@ func (r *replacePath) GetTracingInformation() (string, ext.SpanKindEnum) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *replacePath) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
func (r *replacePath) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
if req.URL.RawPath == "" {
|
currentPath := req.URL.RawPath
|
||||||
req.Header.Add(ReplacedPathHeader, req.URL.Path)
|
if currentPath == "" {
|
||||||
} else {
|
currentPath = req.URL.EscapedPath()
|
||||||
req.Header.Add(ReplacedPathHeader, req.URL.RawPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Header.Add(ReplacedPathHeader, currentPath)
|
||||||
req.URL.RawPath = r.path
|
req.URL.RawPath = r.path
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -60,6 +60,16 @@ func TestReplacePath(t *testing.T) {
|
||||||
expectedRawPath: "/foo%2Fbar",
|
expectedRawPath: "/foo%2Fbar",
|
||||||
expectedHeader: "/path",
|
expectedHeader: "/path",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "replacement with percent encoded backspace char",
|
||||||
|
path: "/path/%08bar",
|
||||||
|
config: dynamic.ReplacePath{
|
||||||
|
Path: "/path/%08bar",
|
||||||
|
},
|
||||||
|
expectedPath: "/path/\bbar",
|
||||||
|
expectedRawPath: "/path/%08bar",
|
||||||
|
expectedHeader: "/path/%08bar",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
|
|
|
@ -16,9 +16,7 @@ import (
|
||||||
"github.com/traefik/traefik/v2/pkg/tracing"
|
"github.com/traefik/traefik/v2/pkg/tracing"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const typeName = "ReplacePathRegex"
|
||||||
typeName = "ReplacePathRegex"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ReplacePathRegex is a middleware used to replace the path of a URL request with a regular expression.
|
// ReplacePathRegex is a middleware used to replace the path of a URL request with a regular expression.
|
||||||
type replacePathRegex struct {
|
type replacePathRegex struct {
|
||||||
|
@ -50,16 +48,13 @@ func (rp *replacePathRegex) GetTracingInformation() (string, ext.SpanKindEnum) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *replacePathRegex) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
func (rp *replacePathRegex) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
var currentPath string
|
currentPath := req.URL.RawPath
|
||||||
if req.URL.RawPath == "" {
|
if currentPath == "" {
|
||||||
currentPath = req.URL.Path
|
currentPath = req.URL.EscapedPath()
|
||||||
} else {
|
|
||||||
currentPath = req.URL.RawPath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if rp.regexp != nil && len(rp.replacement) > 0 && rp.regexp.MatchString(currentPath) {
|
if rp.regexp != nil && len(rp.replacement) > 0 && rp.regexp.MatchString(currentPath) {
|
||||||
req.Header.Add(replacepath.ReplacedPathHeader, currentPath)
|
req.Header.Add(replacepath.ReplacedPathHeader, currentPath)
|
||||||
|
|
||||||
req.URL.RawPath = rp.regexp.ReplaceAllString(currentPath, rp.replacement)
|
req.URL.RawPath = rp.regexp.ReplaceAllString(currentPath, rp.replacement)
|
||||||
|
|
||||||
// as replacement can introduce escaped characters
|
// as replacement can introduce escaped characters
|
||||||
|
|
|
@ -106,6 +106,16 @@ func TestReplacePathRegex(t *testing.T) {
|
||||||
expectedPath: "/aaa/bbb",
|
expectedPath: "/aaa/bbb",
|
||||||
expectedRawPath: "/aaa%2Fbbb",
|
expectedRawPath: "/aaa%2Fbbb",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "path with percent encoded backspace char",
|
||||||
|
path: "/foo/%08bar",
|
||||||
|
config: dynamic.ReplacePathRegex{
|
||||||
|
Replacement: "/$1",
|
||||||
|
Regex: `^/foo/(.*)`,
|
||||||
|
},
|
||||||
|
expectedPath: "/\bbar",
|
||||||
|
expectedRawPath: "/%08bar",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
|
|
Loading…
Reference in a new issue