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

Add support for percent-encoded slashes in parameter values #140

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions requestpath_go15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build go1.5

package httprouter

import (
"net/http"
)

func getPath(r *http.Request) string {
return r.URL.EscapedPath()
}

func addTrailingSlash(r *http.Request) {
r.URL.RawPath += "/"
r.URL.Path += "/"
}

func removeTrailingSlash(r *http.Request) {
rawPath := r.URL.EscapedPath()
r.URL.RawPath = rawPath[:len(rawPath)-1]
r.URL.Path = r.URL.Path[:len(r.URL.Path)-1]
}
27 changes: 27 additions & 0 deletions requestpath_pre15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build !go1.5

package httprouter

import (
"net/http"
"strings"
)

// BUG(): In go1.4 and earlier, percent-encoded slashes are only handled
// correctly for server requests. The http.NewRequest() function used to
// create client requests does not populate the RequestURI field.

func getPath(r *http.Request) string {
if r.RequestURI != "" {
return strings.SplitN(r.RequestURI, "?", 2)[0]
}
return r.URL.Path
}

func addTrailingSlash(r *http.Request) {
r.URL.Path = r.URL.Path + "/"
}

func removeTrailingSlash(r *http.Request) {
r.URL.Path = r.URL.Path[:len(r.URL.Path)-1]
}
8 changes: 5 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,15 @@ func (r *Router) allowed(path, reqMethod string) (allow string) {
return
}

// BUG(): Redirects do not preserve percent-encoded slashes in go1.4 and earlier.

// ServeHTTP makes the router implement the http.Handler interface.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r.PanicHandler != nil {
defer r.recv(w, req)
}

path := req.URL.Path
path := getPath(req)

if root := r.trees[req.Method]; root != nil {
if handle, ps, tsr := root.getValue(path); handle != nil {
Expand All @@ -353,9 +355,9 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {

if tsr && r.RedirectTrailingSlash {
if len(path) > 1 && path[len(path)-1] == '/' {
req.URL.Path = path[:len(path)-1]
removeTrailingSlash(req)
} else {
req.URL.Path = path + "/"
addTrailingSlash(req)
}
http.Redirect(w, req, req.URL.String(), code)
return
Expand Down
35 changes: 21 additions & 14 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,31 @@ func TestParams(t *testing.T) {
}

func TestRouter(t *testing.T) {
router := New()
testNames := map[string]Params{
"gopher": Params{Param{"name", "gopher"}},
"go%2f1.6": Params{Param{"name", "go/1.6"}},
}

routed := false
router.Handle("GET", "/user/:name", func(w http.ResponseWriter, r *http.Request, ps Params) {
routed = true
want := Params{Param{"name", "gopher"}}
if !reflect.DeepEqual(ps, want) {
t.Fatalf("wrong wildcard values: want %v, got %v", want, ps)
}
})
for name, want := range testNames {
router := New()

w := new(mockResponseWriter)
routed := false
router.Handle("GET", "/user/:name", func(w http.ResponseWriter, r *http.Request, ps Params) {
routed = true
if !reflect.DeepEqual(ps, want) {
t.Fatalf("wrong wildcard values: want %v, got %v", want, ps)
}
})

req, _ := http.NewRequest("GET", "/user/gopher", nil)
router.ServeHTTP(w, req)
w := new(mockResponseWriter)

req, _ := http.NewRequest("GET", "/user/" + name, nil)
req.RequestURI = "/user/" + name // Manually populate RequestURI to simulate a server request
router.ServeHTTP(w, req)

if !routed {
t.Fatal("routing failed")
if !routed {
t.Fatal("routing failed on /user/" + name)
}
}
}

Expand Down
41 changes: 40 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ walk: // outer loop for walking the tree
i := len(p)
p = p[:i+1] // expand slice within preallocated capacity
p[i].Key = n.path[1:]
p[i].Value = path[:end]
p[i].Value = unencode(path[:end])

// we need to go deeper!
if end < len(path) {
Expand Down Expand Up @@ -647,3 +647,42 @@ walk: // outer loop for walking the tree
}
return ciPath, false
}

func unencode(s string) string {
unencoded := make([]byte, len(s))
j := 0
for i := 0; i < len(s); j++ {
if s[i] == '%' && i+2 < len(s) && ishex(s[i+1]) && ishex(s[i+2]) {
unencoded[j] = unhex(s[i+1])<<4 | unhex(s[i+2])
i += 3
} else {
unencoded[j] = s[i]
i++
}
}
return string(unencoded[:j])
}

func ishex(c byte) bool {
switch {
case '0' <= c && c <= '9':
return true
case 'a' <= c && c <= 'f':
return true
case 'A' <= c && c <= 'F':
return true
}
return false
}

func unhex(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}