5
5
package mux
6
6
7
7
import (
8
+ "context"
8
9
"errors"
9
10
"fmt"
10
11
"net/http"
@@ -58,8 +59,7 @@ type Router struct {
58
59
59
60
// If true, do not clear the request context after handling the request.
60
61
//
61
- // Deprecated: No effect when go1.7+ is used, since the context is stored
62
- // on the request itself.
62
+ // Deprecated: No effect, since the context is stored on the request itself.
63
63
KeepContext bool
64
64
65
65
// Slice of middlewares to be called after a match is found
@@ -195,8 +195,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
195
195
var handler http.Handler
196
196
if r .Match (req , & match ) {
197
197
handler = match .Handler
198
- req = setVars (req , match .Vars )
199
- req = setCurrentRoute (req , match .Route )
198
+ req = requestWithVars (req , match .Vars )
199
+ req = requestWithRoute (req , match .Route )
200
200
}
201
201
202
202
if handler == nil && match .MatchErr == ErrMethodMismatch {
@@ -426,7 +426,7 @@ const (
426
426
427
427
// Vars returns the route variables for the current request, if any.
428
428
func Vars (r * http.Request ) map [string ]string {
429
- if rv := contextGet ( r , varsKey ); rv != nil {
429
+ if rv := r . Context (). Value ( varsKey ); rv != nil {
430
430
return rv .(map [string ]string )
431
431
}
432
432
return nil
@@ -438,18 +438,20 @@ func Vars(r *http.Request) map[string]string {
438
438
// after the handler returns, unless the KeepContext option is set on the
439
439
// Router.
440
440
func CurrentRoute (r * http.Request ) * Route {
441
- if rv := contextGet ( r , routeKey ); rv != nil {
441
+ if rv := r . Context (). Value ( routeKey ); rv != nil {
442
442
return rv .(* Route )
443
443
}
444
444
return nil
445
445
}
446
446
447
- func setVars (r * http.Request , val interface {}) * http.Request {
448
- return contextSet (r , varsKey , val )
447
+ func requestWithVars (r * http.Request , vars map [string ]string ) * http.Request {
448
+ ctx := context .WithValue (r .Context (), varsKey , vars )
449
+ return r .WithContext (ctx )
449
450
}
450
451
451
- func setCurrentRoute (r * http.Request , val interface {}) * http.Request {
452
- return contextSet (r , routeKey , val )
452
+ func requestWithRoute (r * http.Request , route * Route ) * http.Request {
453
+ ctx := context .WithValue (r .Context (), routeKey , route )
454
+ return r .WithContext (ctx )
453
455
}
454
456
455
457
// ----------------------------------------------------------------------------
0 commit comments