Skip to content

Commit

Permalink
update NativeHandlerChain to avoid a shallow copy
Browse files Browse the repository at this point in the history
- updated to avoid shallow copies with context
  • Loading branch information
Dean Karn authored and Dean Karn committed Oct 3, 2016
1 parent ea42f83 commit b29f7cf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
##LARS
<img align="right" src="https://raw.githubusercontent.com/go-playground/lars/master/examples/README/test.gif">
![Project status](https://img.shields.io/badge/version-3.5.0-green.svg)
![Project status](https://img.shields.io/badge/version-3.6.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/projects/4351aa2d-2f94-40be-a6ef-85c248490378/679708/badge.svg)](https://semaphoreci.com/joeybloggs/lars)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/lars/badge.svg?branch=master)](https://coveralls.io/github/go-playground/lars?branch=master)
[![Go Report Card](https://goreportcard.com/badge/go-playground/lars)](https://goreportcard.com/report/go-playground/lars)
Expand Down
12 changes: 6 additions & 6 deletions context_17.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
// golang.org/x/net/context contained on this Context.
// It is a shortcut for context.WithValue(..., ...)
func (c *Ctx) Set(key interface{}, value interface{}) {
*c.request = *c.request.WithContext(context.WithValue(c.request.Context(), key, value)) // temporarily shallow copying to avoid problems with external libraries
c.request = c.request.WithContext(context.WithValue(c.request.Context(), key, value)) // temporarily shallow copying to avoid problems with external libraries
}

// Get returns the value for the given key and is a shortcut
Expand All @@ -108,7 +108,7 @@ func (c *Ctx) Context() context.Context {
// WithContext updates the underlying request's context with to ctx
// The provided ctx must be non-nil.
func (c *Ctx) WithContext(ctx context.Context) {
*c.request = *c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
}

// Deadline calls the underlying golang.org/x/net/context Deadline()
Expand All @@ -135,29 +135,29 @@ func (c *Ctx) Value(key interface{}) interface{} {
// updates context on the containing las.Context object.
func (c *Ctx) WithCancel() context.CancelFunc {
ctx, cf := context.WithCancel(c.request.Context())
*c.request = *c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
return cf
}

// WithDeadline calls golang.org/x/net/context WithDeadline and automatically
// updates context on the containing las.Context object.
func (c *Ctx) WithDeadline(deadline time.Time) context.CancelFunc {
ctx, cf := context.WithDeadline(c.request.Context(), deadline)
*c.request = *c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
return cf
}

// WithTimeout calls golang.org/x/net/context WithTimeout and automatically
// updates context on the containing las.Context object.
func (c *Ctx) WithTimeout(timeout time.Duration) context.CancelFunc {
ctx, cf := context.WithTimeout(c.request.Context(), timeout)
*c.request = *c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
return cf
}

// WithValue calls golang.org/x/net/context WithValue and automatically
// updates context on the containing las.Context object.
// Can also use Set() function on Context object (Recommended)
func (c *Ctx) WithValue(key interface{}, val interface{}) {
*c.request = *c.request.WithContext(context.WithValue(c.request.Context(), key, val)) // temporarily shallow copying to avoid problems with external libraries
c.request = c.request.WithContext(context.WithValue(c.request.Context(), key, val)) // temporarily shallow copying to avoid problems with external libraries
}
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var NativeChainHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
c := GetContext(w)
b := c.BaseContext()

*b.request = *r
b.request = r

if b.index+1 < len(b.handlers) {
c.Next()
Expand Down

0 comments on commit b29f7cf

Please sign in to comment.