Skip to content

Commit

Permalink
Merge pull request #54 from uptrace/fix/tweak-uri-encoded-matching
Browse files Browse the repository at this point in the history
fix: use RawPath when available
  • Loading branch information
vmihailenco authored Jan 19, 2022
2 parents 027fc60 + 9859bc7 commit f7da255
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func (r *Router) ServeHTTPError(w http.ResponseWriter, req *http.Request) error
}

func (r *Router) lookup(w http.ResponseWriter, req *http.Request) (HandlerFunc, Params) {
path := req.URL.Path
path := req.URL.RawPath
if path == "" {
path = req.URL.Path
}

node, handler, wildcardLen := r.tree.findRoute(req.Method, path)
if node == nil {
Expand Down
27 changes: 27 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,33 @@ func TestNamedAndWildcard(t *testing.T) {
})
}

// https://github.com/golang/go/issues/3659
func TestURIEncodedFilepath(t *testing.T) {
router := New()
var file string

router.GET("/files/:file", func(w http.ResponseWriter, req Request) error {
file = req.Param("file")
return nil
})

t.Run("one", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/files/foo%252fbar", nil)
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "foo%2fbar", file)
})

t.Run("two", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/files/foo%2fbar", nil)
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "foo%2fbar", file)
})
}

func TestSplitRoute(t *testing.T) {
type Test struct {
route string
Expand Down

0 comments on commit f7da255

Please sign in to comment.