-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Use http.Request.Context() in >= Go 1.7.
- Go 1.7 adds a context.Context to the *http.Request. We now use this instead of gorilla/context for >= Go 1.7. - NOTE: this introduces a minor breaking change to the public API due to the shallow copy made in http.Request.WithContext. UnsafeSkipCheck now returns a *http.Request to allow the copied request to be saved by the caller. Package users who do NOT save the returned request will not skip the CSRF check (fails closed).
- Loading branch information
Showing
5 changed files
with
58 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// +build go1.7 | ||
|
||
package csrf | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func contextGet(r *http.Request, key string) (interface{}, error) { | ||
val := r.Context().Value(key) | ||
if val == nil { | ||
return nil, errors.Errorf("no value exists in the context for key %q", key) | ||
} | ||
|
||
return val, nil | ||
} | ||
|
||
func contextSave(r *http.Request, key string, val interface{}) *http.Request { | ||
ctx := r.Context() | ||
ctx = context.WithValue(ctx, key, val) | ||
return r.WithContext(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build !go1.7 | ||
|
||
package csrf | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/context" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func contextGet(r *http.Request, key string) (interface{}, error) { | ||
if val, ok := context.GetOk(r, key); ok { | ||
return val, nil | ||
} | ||
|
||
return nil, errors.Errorf("no value exists in the context for key %q", key) | ||
} | ||
|
||
func contextSave(r *http.Request, key string, val interface{}) *http.Request { | ||
context.Set(r, key, val) | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters