-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add URL normalizePath() filter #3237
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,4 +370,5 @@ const ( | |
SetFastCgiFilenameName = "setFastCgiFilename" | ||
DisableRatelimitName = "disableRatelimit" | ||
UnknownRatelimitName = "unknownRatelimit" | ||
NormalizePath = "normalizePath" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NormalizePath -> NormalizePathName |
||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package normalizepath | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/zalando/skipper/filters" | ||
) | ||
|
||
const ( | ||
Name = filters.NormalizePath | ||
) | ||
|
||
type normalizePath struct{} | ||
|
||
func NewNormalizePath() filters.Spec { return normalizePath{} } | ||
|
||
func (spec normalizePath) Name() string { return "normalizePath" } | ||
|
||
func (spec normalizePath) CreateFilter(config []interface{}) (filters.Filter, error) { | ||
return normalizePath{}, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can
|
||
} | ||
|
||
func (f normalizePath) Request(ctx filters.FilterContext) { | ||
req := ctx.Request() | ||
|
||
segments := strings.Split(req.URL.Path, "/") | ||
var filteredSegments []string | ||
for _, seg := range segments { | ||
if seg != "" { | ||
filteredSegments = append(filteredSegments, seg) | ||
} | ||
} | ||
normalizedPath := "/" + strings.Join(filteredSegments, "/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semgrep asks:
|
||
|
||
req.URL.Path = normalizedPath | ||
} | ||
|
||
func (f normalizePath) Response(ctx filters.FilterContext) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package normalizepath | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/zalando/skipper/filters/filtertest" | ||
) | ||
|
||
// TestNormalizePath tests the NormalizePath function in accordance to the | ||
// https://opensource.zalando.com/restful-api-guidelines/#136 | ||
// specifically the notion of normalization of request paths: | ||
// | ||
// All services should normalize request paths before processing by removing | ||
// duplicate and trailing slashes. Hence, the following requests should refer | ||
// to the same resource: | ||
// GET /orders/{order-id} | ||
// GET /orders/{order-id}/ | ||
// GET /orders//{order-id} | ||
func TestNormalizePath(t *testing.T) { | ||
urls := []string{ | ||
"/orders/{order-id}", | ||
"/orders/{order-id}/", | ||
"/orders//{order-id}", | ||
"/orders/{order-id}//", | ||
"/orders/{order-id}///", | ||
"/orders///{order-id}//", | ||
} | ||
|
||
for _, u := range urls { | ||
req := &http.Request{URL: &url.URL{Path: u}} | ||
ctx := &filtertest.Context{ | ||
FRequest: req, | ||
} | ||
f, err := NewNormalizePath().CreateFilter(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
f.Request(ctx) | ||
if req.URL.Path != "/orders/{order-id}" { | ||
t.Errorf("failed to normalize the path: %s", req.URL.Path) | ||
} | ||
} | ||
|
||
// Ensure that root paths work as expected | ||
req := &http.Request{URL: &url.URL{Path: "/"}} | ||
ctx := &filtertest.Context{ | ||
FRequest: req, | ||
} | ||
f, err := NewNormalizePath().CreateFilter(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
f.Request(ctx) | ||
if req.URL.Path != "/" { | ||
t.Errorf("unexpected URL path change: %s, expected /", req.URL.Path) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing closing
"
and network backend is not having a path segment.all: * -> normalizePath() -> "https://backend.example.org";