|
|
|
|
@ -5,6 +5,7 @@
@@ -5,6 +5,7 @@
|
|
|
|
|
package mux |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"net/http" |
|
|
|
|
@ -195,8 +196,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
@@ -195,8 +196,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
var handler http.Handler |
|
|
|
|
if r.Match(req, &match) { |
|
|
|
|
handler = match.Handler |
|
|
|
|
req = setVars(req, match.Vars) |
|
|
|
|
req = setCurrentRoute(req, match.Route) |
|
|
|
|
req = requestWithVars(req, match.Vars) |
|
|
|
|
req = requestWithRoute(req, match.Route) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if handler == nil && match.MatchErr == ErrMethodMismatch { |
|
|
|
|
@ -426,7 +427,7 @@ const (
@@ -426,7 +427,7 @@ const (
|
|
|
|
|
|
|
|
|
|
// Vars returns the route variables for the current request, if any.
|
|
|
|
|
func Vars(r *http.Request) map[string]string { |
|
|
|
|
if rv := contextGet(r, varsKey); rv != nil { |
|
|
|
|
if rv := r.Context().Value(varsKey); rv != nil { |
|
|
|
|
return rv.(map[string]string) |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
@ -438,18 +439,20 @@ func Vars(r *http.Request) map[string]string {
@@ -438,18 +439,20 @@ func Vars(r *http.Request) map[string]string {
|
|
|
|
|
// after the handler returns, unless the KeepContext option is set on the
|
|
|
|
|
// Router.
|
|
|
|
|
func CurrentRoute(r *http.Request) *Route { |
|
|
|
|
if rv := contextGet(r, routeKey); rv != nil { |
|
|
|
|
if rv := r.Context().Value(routeKey); rv != nil { |
|
|
|
|
return rv.(*Route) |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func setVars(r *http.Request, val interface{}) *http.Request { |
|
|
|
|
return contextSet(r, varsKey, val) |
|
|
|
|
func requestWithVars(r *http.Request, val interface{}) *http.Request { |
|
|
|
|
ctx := context.WithValue(r.Context(), varsKey, val) |
|
|
|
|
return r.WithContext(ctx) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func setCurrentRoute(r *http.Request, val interface{}) *http.Request { |
|
|
|
|
return contextSet(r, routeKey, val) |
|
|
|
|
func requestWithRoute(r *http.Request, val interface{}) *http.Request { |
|
|
|
|
ctx := context.WithValue(r.Context(), routeKey, val) |
|
|
|
|
return r.WithContext(ctx) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|