Skip to content

Commit

Permalink
Use of cleaned path just when redirect to the fixed path #40
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Sep 3, 2020
1 parent b0e5c87 commit 893131d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
4 changes: 3 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ func (r *Router) tryRedirect(ctx *fasthttp.RequestCtx, tree *radix.Tree, tsr boo

// Try to fix the request path
if r.RedirectFixedPath {
path := gotils.B2S(ctx.Request.URI().Path())

uri := bytebufferpool.Get()
found := tree.FindCaseInsensitivePath(
cleanPath(path),
Expand Down Expand Up @@ -391,7 +393,7 @@ func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
defer r.recv(ctx)
}

path := gotils.B2S(ctx.Request.URI().Path())
path := gotils.B2S(ctx.Request.URI().PathOriginal())
method := gotils.B2S(ctx.Request.Header.Method())
methodIndex := r.methodIndexOf(method)

Expand Down
37 changes: 20 additions & 17 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,13 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
router.Handle(method, "/static/{filepath:*}", handlerFunc)

// Moved Permanently, request with GET method
codeRedirect := fasthttp.StatusMovedPermanently
if method != fasthttp.MethodGet {
expectedCode := fasthttp.StatusMovedPermanently
if method == fasthttp.MethodConnect {
// CONNECT method does not allow redirects, so Not Found (404)
expectedCode = fasthttp.StatusNotFound
} else if method != fasthttp.MethodGet {
// Permanent Redirect, request with same method
codeRedirect = fasthttp.StatusPermanentRedirect
expectedCode = fasthttp.StatusPermanentRedirect
}

type testRoute struct {
Expand All @@ -543,24 +546,24 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
}

testRoutes := []testRoute{
{"", fasthttp.StatusOK, ""}, // TSR +/ (Not clean by router, this path is cleaned by fasthttp `ctx.Path()`)
{"/../path", fasthttp.StatusOK, ""}, // CleanPath (Not clean by router, this path is cleaned by fasthttp `ctx.Path()`)
{"/nope", fasthttp.StatusNotFound, ""}, // NotFound
{"", fasthttp.StatusOK, ""}, // TSR +/ (Not clean by router, this path is cleaned by fasthttp `ctx.Path()`)
{"/../path", expectedCode, buildLocation(host, "/path")}, // CleanPath (Not clean by router, this path is cleaned by fasthttp `ctx.Path()`)
{"/nope", fasthttp.StatusNotFound, ""}, // NotFound
}

if method != fasthttp.MethodConnect {
testRoutes = append(testRoutes, []testRoute{
{"/path/", codeRedirect, buildLocation(host, "/path")}, // TSR -/
{"/dir", codeRedirect, buildLocation(host, "/dir/")}, // TSR +/
{"/PATH", codeRedirect, buildLocation(host, "/path")}, // Fixed Case
{"/DIR/", codeRedirect, buildLocation(host, "/dir/")}, // Fixed Case
{"/PATH/", codeRedirect, buildLocation(host, "/path")}, // Fixed Case -/
{"/DIR", codeRedirect, buildLocation(host, "/dir/")}, // Fixed Case +/
{"/paTh/?name=foo", codeRedirect, buildLocation(host, "/path?name=foo")}, // Fixed Case With Query Params +/
{"/paTh?name=foo", codeRedirect, buildLocation(host, "/path?name=foo")}, // Fixed Case With Query Params +/
{"/sergio/status/", codeRedirect, buildLocation(host, "/sergio/StaTus")}, // Fixed Case With Params -/
{"/users/atreugo/eNtriEs", codeRedirect, buildLocation(host, "/USERS/atreugo/enTRies/")}, // Fixed Case With Params +/
{"/STatiC/test.go", codeRedirect, buildLocation(host, "/static/test.go")}, // Fixed Case Wildcard
{"/path/", expectedCode, buildLocation(host, "/path")}, // TSR -/
{"/dir", expectedCode, buildLocation(host, "/dir/")}, // TSR +/
{"/PATH", expectedCode, buildLocation(host, "/path")}, // Fixed Case
{"/DIR/", expectedCode, buildLocation(host, "/dir/")}, // Fixed Case
{"/PATH/", expectedCode, buildLocation(host, "/path")}, // Fixed Case -/
{"/DIR", expectedCode, buildLocation(host, "/dir/")}, // Fixed Case +/
{"/paTh/?name=foo", expectedCode, buildLocation(host, "/path?name=foo")}, // Fixed Case With Query Params +/
{"/paTh?name=foo", expectedCode, buildLocation(host, "/path?name=foo")}, // Fixed Case With Query Params +/
{"/sergio/status/", expectedCode, buildLocation(host, "/sergio/StaTus")}, // Fixed Case With Params -/
{"/users/atreugo/eNtriEs", expectedCode, buildLocation(host, "/USERS/atreugo/enTRies/")}, // Fixed Case With Params +/
{"/STatiC/test.go", expectedCode, buildLocation(host, "/static/test.go")}, // Fixed Case Wildcard
}...)
}

Expand Down

0 comments on commit 893131d

Please sign in to comment.