Skip to content
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

feat: Handle uri directly instead of redirecting #1083

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/app/server/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ func WithRedirectTrailingSlash(b bool) config.Option {
}}
}

// WithFixTrailingSlash sets fixTrailingSlash.
//
// If enabled, the router tries to fix the current request path, if no
// handle is registered for it.
// For example if /foo is requested but a route only exists for /foo/, the
// client requests /foo/ without redirecting for all request methods.
// This option conflicts with RedirectTrailingSlash
func WithFixTrailingSlash(b bool) config.Option {
return config.Option{F: func(o *config.Options) {
o.FixTrailingSlash = b
if b {
o.RedirectTrailingSlash = !b
}
}}
}

// WithRedirectFixedPath sets redirectFixedPath.
//
// If enabled, the router tries to fix the current request path, if no
Expand Down
1 change: 1 addition & 0 deletions pkg/common/config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Options struct {
WriteTimeout time.Duration
IdleTimeout time.Duration
RedirectTrailingSlash bool
FixTrailingSlash bool
MaxRequestBodySize int
MaxKeepBodySize int
GetOnly bool
Expand Down
28 changes: 27 additions & 1 deletion pkg/route/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import (
"sync"
"sync/atomic"

"github.com/cloudwego/hertz/pkg/route/param"

"github.com/cloudwego/hertz/internal/bytesconv"
"github.com/cloudwego/hertz/internal/bytestr"
"github.com/cloudwego/hertz/internal/nocopy"
Expand Down Expand Up @@ -721,7 +723,6 @@ func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext) {
if engine.PanicHandler != nil {
defer engine.recv(ctx)
}

rPath := string(ctx.Request.URI().Path())

// align with https://datatracker.ietf.org/doc/html/rfc2616#section-5.2
Expand Down Expand Up @@ -770,6 +771,10 @@ func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext) {
redirectTrailingSlash(ctx)
return
}
if value.tsr && engine.options.FixTrailingSlash {
directTrailingSlash(c, ctx, t[i], paramsPointer, unescape)
return
}
if engine.options.RedirectFixedPath && redirectFixedPath(ctx, t[i].root, engine.options.RedirectFixedPath) {
return
}
Expand Down Expand Up @@ -823,6 +828,27 @@ func trailingSlashURL(ts string) string {
return tmpURI
}

func directTrailingSlash(c context.Context, ctx *app.RequestContext, r *router, paramsPointer *param.Params, unescape bool) {
p := bytesconv.B2s(ctx.Request.URI().Path())
if prefix := utils.CleanPath(bytesconv.B2s(ctx.Request.Header.Peek("X-Forwarded-Prefix"))); prefix != "." {
p = prefix + "/" + p
}

tmpURI := trailingSlashURL(p)

query := ctx.Request.URI().QueryString()
if len(query) > 0 {
tmpURI = tmpURI + "?" + bytesconv.B2s(query)
}
tmpURI = "/" + strings.TrimLeft(tmpURI, "/")
v := r.find(tmpURI, paramsPointer, unescape)
if v.handlers != nil {
ctx.SetHandlers(v.handlers)
ctx.SetFullPath(v.fullPath)
ctx.Next(c)
}
}

func redirectTrailingSlash(c *app.RequestContext) {
p := bytesconv.B2s(c.Request.URI().Path())
if prefix := utils.CleanPath(bytesconv.B2s(c.Request.Header.Peek("X-Forwarded-Prefix"))); prefix != "." {
Expand Down
52 changes: 52 additions & 0 deletions pkg/route/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,58 @@ func TestRouteNotOK3(t *testing.T) {
testRouteNotOK3(consts.MethodTrace, t)
}

func TestRouteFixTrailingSlash(t *testing.T) {
router := NewEngine(config.NewOptions(nil))
router.options.RedirectFixedPath = false
router.options.RedirectTrailingSlash = false
router.options.FixTrailingSlash = true
router.GET("/path", func(c context.Context, ctx *app.RequestContext) {})
router.GET("/path2/", func(c context.Context, ctx *app.RequestContext) {})
router.POST("/path3", func(c context.Context, ctx *app.RequestContext) {})
router.PUT("/path4/", func(c context.Context, ctx *app.RequestContext) {})

w := performRequest(router, consts.MethodGet, "/path/")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodGet, "/path2")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodPost, "/path3/")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodPut, "/path4")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodGet, "/path")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodGet, "/path2/")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodPost, "/path3")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodPut, "/path4/")
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
assert.DeepEqual(t, consts.StatusOK, w.Code)

w = performRequest(router, consts.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
assert.DeepEqual(t, consts.StatusOK, w.Code)

router.options.FixTrailingSlash = false

w = performRequest(router, consts.MethodGet, "/path/")
assert.DeepEqual(t, consts.StatusNotFound, w.Code)
w = performRequest(router, consts.MethodGet, "/path2")
assert.DeepEqual(t, consts.StatusNotFound, w.Code)
w = performRequest(router, consts.MethodPost, "/path3/")
assert.DeepEqual(t, consts.StatusNotFound, w.Code)
w = performRequest(router, consts.MethodPut, "/path4")
assert.DeepEqual(t, consts.StatusNotFound, w.Code)
}

func TestRouteRedirectTrailingSlash(t *testing.T) {
router := NewEngine(config.NewOptions(nil))
router.options.RedirectFixedPath = false
Expand Down