Add HTTPUrlRewrite Filter in Gateway API
This commit is contained in:
parent
3ca667a3d4
commit
a696f7c654
15 changed files with 754 additions and 110 deletions
68
pkg/middlewares/gatewayapi/urlrewrite/url_rewrite.go
Normal file
68
pkg/middlewares/gatewayapi/urlrewrite/url_rewrite.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package urlrewrite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
||||
"github.com/traefik/traefik/v3/pkg/middlewares"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
const (
|
||||
typeName = "URLRewrite"
|
||||
)
|
||||
|
||||
type urlRewrite struct {
|
||||
name string
|
||||
next http.Handler
|
||||
|
||||
hostname *string
|
||||
path *string
|
||||
pathPrefix *string
|
||||
}
|
||||
|
||||
// NewURLRewrite creates a URL rewrite middleware.
|
||||
func NewURLRewrite(ctx context.Context, next http.Handler, conf dynamic.URLRewrite, name string) http.Handler {
|
||||
logger := middlewares.GetLogger(ctx, name, typeName)
|
||||
logger.Debug().Msg("Creating middleware")
|
||||
|
||||
return urlRewrite{
|
||||
name: name,
|
||||
next: next,
|
||||
hostname: conf.Hostname,
|
||||
path: conf.Path,
|
||||
pathPrefix: conf.PathPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (u urlRewrite) GetTracingInformation() (string, string, trace.SpanKind) {
|
||||
return u.name, typeName, trace.SpanKindInternal
|
||||
}
|
||||
|
||||
func (u urlRewrite) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
newPath := req.URL.Path
|
||||
if u.path != nil && u.pathPrefix == nil {
|
||||
newPath = *u.path
|
||||
}
|
||||
if u.path != nil && u.pathPrefix != nil {
|
||||
newPath = path.Join(*u.path, strings.TrimPrefix(req.URL.Path, *u.pathPrefix))
|
||||
|
||||
// add the trailing slash if needed, as path.Join removes trailing slashes.
|
||||
if strings.HasSuffix(req.URL.Path, "/") && !strings.HasSuffix(newPath, "/") {
|
||||
newPath += "/"
|
||||
}
|
||||
}
|
||||
|
||||
req.URL.Path = newPath
|
||||
req.URL.RawPath = req.URL.EscapedPath()
|
||||
req.RequestURI = req.URL.RequestURI()
|
||||
|
||||
if u.hostname != nil {
|
||||
req.Host = *u.hostname
|
||||
}
|
||||
|
||||
u.next.ServeHTTP(rw, req)
|
||||
}
|
126
pkg/middlewares/gatewayapi/urlrewrite/url_rewrite_test.go
Normal file
126
pkg/middlewares/gatewayapi/urlrewrite/url_rewrite_test.go
Normal file
|
@ -0,0 +1,126 @@
|
|||
package urlrewrite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
||||
"k8s.io/utils/ptr"
|
||||
)
|
||||
|
||||
func TestURLRewriteHandler(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
config dynamic.URLRewrite
|
||||
url string
|
||||
wantURL string
|
||||
wantHost string
|
||||
}{
|
||||
{
|
||||
desc: "replace path",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace path without trailing slash",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar/",
|
||||
wantURL: "http://foo.com/baz",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace path with trailing slash",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz/"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "only host",
|
||||
config: dynamic.URLRewrite{
|
||||
Hostname: ptr.To("bar.com"),
|
||||
},
|
||||
url: "http://foo.com/foo/",
|
||||
wantURL: "http://foo.com/foo/",
|
||||
wantHost: "bar.com",
|
||||
},
|
||||
{
|
||||
desc: "host and path",
|
||||
config: dynamic.URLRewrite{
|
||||
Hostname: ptr.To("bar.com"),
|
||||
Path: ptr.To("/baz/"),
|
||||
},
|
||||
url: "http://foo.com/foo/",
|
||||
wantURL: "http://foo.com/baz/",
|
||||
wantHost: "bar.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
PathPrefix: ptr.To("/foo"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/bar",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path with trailing slash",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
PathPrefix: ptr.To("/foo"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar/",
|
||||
wantURL: "http://foo.com/baz/bar/",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path without slash prefix",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("baz"),
|
||||
PathPrefix: ptr.To("/foo"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/bar",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path without slash prefix",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
PathPrefix: ptr.To("/foo/"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/bar",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
||||
|
||||
handler := NewURLRewrite(context.Background(), next, test.config, "traefikTest")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, test.url, nil)
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, test.wantURL, req.URL.String())
|
||||
assert.Equal(t, test.wantHost, req.Host)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue