Skip to content

Commit c9183aa

Browse files
mikelikesbikeskisielk
authored andcommitted
use req.URL.EscapedPath() instead of getPath(req) (#306)
This change drops support of go < 1.5. go1.5 has been officially unsupported since go1.7 was released 2016/08/15.
1 parent 10490f5 commit c9183aa

File tree

2 files changed

+4
-27
lines changed

2 files changed

+4
-27
lines changed

mux.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/http"
1111
"path"
1212
"regexp"
13-
"strings"
1413
)
1514

1615
var (
@@ -94,7 +93,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
9493
if !r.skipClean {
9594
path := req.URL.Path
9695
if r.useEncodedPath {
97-
path = getPath(req)
96+
path = req.URL.EscapedPath()
9897
}
9998
// Clean path to canonical form and redirect.
10099
if p := cleanPath(path); p != path {
@@ -409,28 +408,6 @@ func setCurrentRoute(r *http.Request, val interface{}) *http.Request {
409408
// Helpers
410409
// ----------------------------------------------------------------------------
411410

412-
// getPath returns the escaped path if possible; doing what URL.EscapedPath()
413-
// which was added in go1.5 does
414-
func getPath(req *http.Request) string {
415-
if req.RequestURI != "" {
416-
// Extract the path from RequestURI (which is escaped unlike URL.Path)
417-
// as detailed here as detailed in https://golang.org/pkg/net/url/#URL
418-
// for < 1.5 server side workaround
419-
// http://localhost/path/here?v=1 -> /path/here
420-
path := req.RequestURI
421-
path = strings.TrimPrefix(path, req.URL.Scheme+`://`)
422-
path = strings.TrimPrefix(path, req.URL.Host)
423-
if i := strings.LastIndex(path, "?"); i > -1 {
424-
path = path[:i]
425-
}
426-
if i := strings.LastIndex(path, "#"); i > -1 {
427-
path = path[:i]
428-
}
429-
return path
430-
}
431-
return req.URL.Path
432-
}
433-
434411
// cleanPath returns the canonical path for p, eliminating . and .. elements.
435412
// Borrowed from the net/http package.
436413
func cleanPath(p string) string {

regexp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ type routeRegexp struct {
141141
matchQuery bool
142142
// The strictSlash value defined on the route, but disabled if PathPrefix was used.
143143
strictSlash bool
144-
// Determines whether to use encoded path from getPath function or unencoded
144+
// Determines whether to use encoded req.URL.EnscapedPath() or unencoded
145145
// req.URL.Path for path matching
146146
useEncodedPath bool
147147
// Expanded regexp.
@@ -162,7 +162,7 @@ func (r *routeRegexp) Match(req *http.Request, match *RouteMatch) bool {
162162
}
163163
path := req.URL.Path
164164
if r.useEncodedPath {
165-
path = getPath(req)
165+
path = req.URL.EscapedPath()
166166
}
167167
return r.regexp.MatchString(path)
168168
}
@@ -272,7 +272,7 @@ func (v *routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route)
272272
}
273273
path := req.URL.Path
274274
if r.useEncodedPath {
275-
path = getPath(req)
275+
path = req.URL.EscapedPath()
276276
}
277277
// Store path variables.
278278
if v.path != nil {

0 commit comments

Comments
 (0)