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

Support matching parameters that containing the delimiter #811

Open
wants to merge 4 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,15 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
for idx := 0; idx < len(nds); idx++ {
xn = nds[idx]

segmentlen := strings.IndexByte(xsearch, '/')
if segmentlen == -1 {
segmentlen = len(xsearch)
} else {
segmentlen += 1 // just keep the old code working
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how the "old code" was exactly :) Can you explain what this does (and why) in the comment?

}

// label for param nodes is the delimiter byte
p := strings.IndexByte(xsearch, xn.tail)
p := strings.LastIndexByte(xsearch[:segmentlen], xn.tail)

if p < 0 {
if xn.tail == '/' {
Expand Down
15 changes: 13 additions & 2 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func TestTree(t *testing.T) {
hHubView1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hHubView2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hHubView3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hUserInfo0 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hUserInfo1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hUserInfoN := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

tr := &node{}

Expand Down Expand Up @@ -77,6 +80,10 @@ func TestTree(t *testing.T) {
tr.InsertRoute(mGET, "/hubs/{hubID}/*", sr)
tr.InsertRoute(mGET, "/hubs/{hubID}/users", hHubView3)

tr.InsertRoute(mGET, "/info/{user_name}", hUserInfo0)
tr.InsertRoute(mGET, "/info/{user_name}.info", hUserInfo1)
tr.InsertRoute(mGET, "/info/{user_name}.{ext}", hUserInfoN)

tests := []struct {
r string // input request path
h http.Handler // output matched handler
Expand Down Expand Up @@ -118,6 +125,10 @@ func TestTree(t *testing.T) {
{r: "/users/123/profile", h: hUserProfile, k: []string{"userID"}, v: []string{"123"}},
{r: "/users/super/123/okay/yes", h: hUserSuper, k: []string{"*"}, v: []string{"123/okay/yes"}},
{r: "/users/123/okay/yes", h: hUserAll, k: []string{"*"}, v: []string{"123/okay/yes"}},

{r: "/info/some_user", h: hUserInfo0, k: []string{"user_name"}, v: []string{"some_user"}},
{r: "/info/some.user.info", h: hUserInfo1, k: []string{"user_name"}, v: []string{"some.user"}},
{r: "/info/some.user.ext1", h: hUserInfoN, k: []string{"user_name", "ext"}, v: []string{"some.user", "ext1"}},
}

// log.Println("~~~~~~~~~")
Expand Down Expand Up @@ -221,8 +232,8 @@ func TestTreeMoar(t *testing.T) {
{m: mGET, r: "/articles/456/data.json", h: hStub11, k: []string{"id"}, v: []string{"456"}},

{m: mGET, r: "/articles/files/file.zip", h: hStub12, k: []string{"file", "ext"}, v: []string{"file", "zip"}},
{m: mGET, r: "/articles/files/photos.tar.gz", h: hStub12, k: []string{"file", "ext"}, v: []string{"photos", "tar.gz"}},
{m: mGET, r: "/articles/files/photos.tar.gz", h: hStub12, k: []string{"file", "ext"}, v: []string{"photos", "tar.gz"}},
{m: mGET, r: "/articles/files/photos.tar.gz", h: hStub12, k: []string{"file", "ext"}, v: []string{"photos.tar", "gz"}},
6543 marked this conversation as resolved.
Show resolved Hide resolved
{m: mGET, r: "/articles/files/photos.tar.gz", h: hStub12, k: []string{"file", "ext"}, v: []string{"photos.tar", "gz"}},

{m: mPUT, r: "/articles/me", h: hStub13, k: []string{}, v: []string{}},
{m: mGET, r: "/articles/me", h: hStub, k: []string{"id"}, v: []string{"me"}},
Expand Down