@@ -56,7 +56,7 @@ type Context interface {
5656
5757// Ctx encapsulates the http request, response context
5858type Ctx struct {
59- context.Context
59+ netContext context.Context
6060 request * http.Request
6161 response * Response
6262 websocket * websocket.Conn
@@ -115,8 +115,7 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
115115 c .request = r
116116 c .response .reset (w )
117117 c .params = c .params [0 :0 ]
118- c .Context = context .Background ()
119- // c.store = nil
118+ c .netContext = context .Background () // in go 1.7 will call r.Context(), netContext will go away and be replaced with the Request objects Context
120119 c .index = - 1
121120 c .handlers = nil
122121 c .formParsed = false
@@ -187,14 +186,14 @@ func (c *Ctx) ParseMultipartForm(maxMemory int64) error {
187186// golang.org/x/net/context contained on this Context.
188187// It is a shortcut for context.WithValue(..., ...)
189188func (c * Ctx ) Set (key interface {}, value interface {}) {
190- c .Context = context .WithValue (c .Context , key , value )
189+ c .netContext = context .WithValue (c .netContext , key , value )
191190}
192191
193192// Get returns the value for the given key and is a shortcut
194193// for the golang.org/x/net/context context.Value(...) ... but it
195194// also returns if the value was found or not.
196195func (c * Ctx ) Get (key interface {}) (value interface {}, exists bool ) {
197- value = c .Context .Value (key )
196+ value = c .netContext .Value (key )
198197 exists = value != nil
199198 return
200199}
@@ -407,36 +406,66 @@ func (c *Ctx) Inline(r io.Reader, filename string) (err error) {
407406 return
408407}
409408
410- // golang.org/x/net/context Overrides to keep context update on lars.Context object
409+ // golang.org/x/net/context functions to comply with context.Context interface and keep context update on lars.Context object
410+
411+ // Context returns the request's context. To change the context, use
412+ // WithContext.
413+ //
414+ // The returned context is always non-nil.
415+ func (c * Ctx ) Context () context.Context {
416+ return c .netContext // TODO: in go 1.7 return c.request.Context()
417+ }
418+
419+ // WithContext updates the underlying request's context with to ctx
420+ // The provided ctx must be non-nil.
421+ func (c * Ctx ) WithContext (ctx context.Context ) {
422+ c .netContext = ctx // TODO: in go 1.7 must update Request object after calling c.request.WithContext(...)
423+ }
424+
425+ // Deadline calls the underlying golang.org/x/net/context Deadline()
426+ func (c * Ctx ) Deadline () (deadline time.Time , ok bool ) {
427+ return c .netContext .Deadline ()
428+ }
429+
430+ // Done calls the underlying golang.org/x/net/context Done()
431+ func (c * Ctx ) Done () <- chan struct {} {
432+ return c .netContext .Done ()
433+ }
434+
435+ // Err calls the underlying golang.org/x/net/context Err()
436+ func (c * Ctx ) Err () error {
437+ return c .netContext .Err ()
438+ }
439+
440+ // Value calls the underlying golang.org/x/net/context Value()
441+ func (c * Ctx ) Value (key interface {}) interface {} {
442+ return c .netContext .Value (key )
443+ }
411444
412- // WithCancel calls embedded golang.org/x/net/context WithCancel and automatically
445+ // WithCancel calls golang.org/x/net/context WithCancel and automatically
413446// updates context on the containing las.Context object.
414- func (c * Ctx ) WithCancel (parent context.Context ) (ctx context.Context , cf context.CancelFunc ) {
415- c .Context , cf = context .WithCancel (parent )
416- ctx = c
447+ func (c * Ctx ) WithCancel () (cf context.CancelFunc ) {
448+ c .netContext , cf = context .WithCancel (c .netContext )
417449 return
418450}
419451
420- // WithDeadline calls embedded golang.org/x/net/context WithDeadline and automatically
452+ // WithDeadline calls golang.org/x/net/context WithDeadline and automatically
421453// updates context on the containing las.Context object.
422- func (c * Ctx ) WithDeadline (parent context.Context , deadline time.Time ) (ctx context.Context , cf context.CancelFunc ) {
423- c .Context , cf = context .WithDeadline (parent , deadline )
424- ctx = c
454+ func (c * Ctx ) WithDeadline (deadline time.Time ) (cf context.CancelFunc ) {
455+ c .netContext , cf = context .WithDeadline (c .netContext , deadline )
425456 return
426457}
427458
428- // WithTimeout calls embedded golang.org/x/net/context WithTimeout and automatically
459+ // WithTimeout calls golang.org/x/net/context WithTimeout and automatically
429460// updates context on the containing las.Context object.
430- func (c * Ctx ) WithTimeout (parent context.Context , timeout time.Duration ) (ctx context.Context , cf context.CancelFunc ) {
431- c .Context , cf = context .WithTimeout (parent , timeout )
432- ctx = c
461+ func (c * Ctx ) WithTimeout (timeout time.Duration ) (cf context.CancelFunc ) {
462+ c .netContext , cf = context .WithTimeout (c .netContext , timeout )
433463 return
434464}
435465
436- // WithValue calls embedded golang.org/x/net/context WithValue and automatically
466+ // WithValue calls golang.org/x/net/context WithValue and automatically
437467// updates context on the containing las.Context object.
438468// Can also use Set() function on Context object (Recommended)
439- func (c * Ctx ) WithValue (parent context.Context , key interface {}, val interface {}) context.Context {
440- c .Context = context .WithValue (parent , key , val )
441- return c .Context
469+ func (c * Ctx ) WithValue (key interface {}, val interface {}) {
470+ c .netContext = context .WithValue (c .netContext , key , val )
442471}
0 commit comments