summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gorilla/mux/mux.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/gorilla/mux/mux.go')
-rw-r--r--vendor/github.com/gorilla/mux/mux.go51
1 files changed, 23 insertions, 28 deletions
diff --git a/vendor/github.com/gorilla/mux/mux.go b/vendor/github.com/gorilla/mux/mux.go
index fb69196db..49de78923 100644
--- a/vendor/github.com/gorilla/mux/mux.go
+++ b/vendor/github.com/gorilla/mux/mux.go
@@ -10,11 +10,11 @@ import (
"net/http"
"path"
"regexp"
- "strings"
)
var (
ErrMethodMismatch = errors.New("method is not allowed")
+ ErrNotFound = errors.New("no matching route was found")
)
// NewRouter returns a new router instance.
@@ -65,7 +65,17 @@ type Router struct {
useEncodedPath bool
}
-// Match matches registered routes against the request.
+// Match attempts to match the given request against the router's registered routes.
+//
+// If the request matches a route of this router or one of its subrouters the Route,
+// Handler, and Vars fields of the the match argument are filled and this function
+// returns true.
+//
+// If the request does not match any of this router's or its subrouters' routes
+// then this function returns false. If available, a reason for the match failure
+// will be filled in the match argument's MatchErr field. If the match failure type
+// (eg: not found) has a registered handler, the handler is assigned to the Handler
+// field of the match argument.
func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
for _, route := range r.routes {
if route.Match(req, match) {
@@ -73,16 +83,23 @@ func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
}
}
- if match.MatchErr == ErrMethodMismatch && r.MethodNotAllowedHandler != nil {
- match.Handler = r.MethodNotAllowedHandler
- return true
+ if match.MatchErr == ErrMethodMismatch {
+ if r.MethodNotAllowedHandler != nil {
+ match.Handler = r.MethodNotAllowedHandler
+ return true
+ } else {
+ return false
+ }
}
// Closest match for a router (includes sub-routers)
if r.NotFoundHandler != nil {
match.Handler = r.NotFoundHandler
+ match.MatchErr = ErrNotFound
return true
}
+
+ match.MatchErr = ErrNotFound
return false
}
@@ -94,7 +111,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !r.skipClean {
path := req.URL.Path
if r.useEncodedPath {
- path = getPath(req)
+ path = req.URL.EscapedPath()
}
// Clean path to canonical form and redirect.
if p := cleanPath(path); p != path {
@@ -409,28 +426,6 @@ func setCurrentRoute(r *http.Request, val interface{}) *http.Request {
// Helpers
// ----------------------------------------------------------------------------
-// getPath returns the escaped path if possible; doing what URL.EscapedPath()
-// which was added in go1.5 does
-func getPath(req *http.Request) string {
- if req.RequestURI != "" {
- // Extract the path from RequestURI (which is escaped unlike URL.Path)
- // as detailed here as detailed in https://golang.org/pkg/net/url/#URL
- // for < 1.5 server side workaround
- // http://localhost/path/here?v=1 -> /path/here
- path := req.RequestURI
- path = strings.TrimPrefix(path, req.URL.Scheme+`://`)
- path = strings.TrimPrefix(path, req.URL.Host)
- if i := strings.LastIndex(path, "?"); i > -1 {
- path = path[:i]
- }
- if i := strings.LastIndex(path, "#"); i > -1 {
- path = path[:i]
- }
- return path
- }
- return req.URL.Path
-}
-
// cleanPath returns the canonical path for p, eliminating . and .. elements.
// Borrowed from the net/http package.
func cleanPath(p string) string {