~ubuntu-branches/ubuntu/utopic/hockeypuck/utopic-proposed

« back to all changes in this revision

Viewing changes to build/src/code.google.com/p/gorilla/context/context.go

  • Committer: Package Import Robot
  • Author(s): Casey Marshall
  • Date: 2014-04-13 20:06:01 UTC
  • Revision ID: package-import@ubuntu.com-20140413200601-oxdlqn1gy0x8m55u
Tags: 1.0~rel20140413+7a1892a~trusty
Hockeypuck 1.0 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012 The Gorilla Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package context
 
6
 
 
7
import (
 
8
        "net/http"
 
9
        "sync"
 
10
)
 
11
 
 
12
// Original implementation by Brad Fitzpatrick:
 
13
// http://groups.google.com/group/golang-nuts/msg/e2d679d303aa5d53
 
14
 
 
15
// DefaultContext is a default context instance.
 
16
var DefaultContext = new(Context)
 
17
 
 
18
// Context stores values for requests.
 
19
type Context struct {
 
20
        l sync.Mutex
 
21
        m map[*http.Request]map[interface{}]interface{}
 
22
}
 
23
 
 
24
// Set stores a value for a given key in a given request.
 
25
func (c *Context) Set(req *http.Request, key, val interface{}) {
 
26
        c.l.Lock()
 
27
        defer c.l.Unlock()
 
28
        if c.m == nil {
 
29
                c.m = make(map[*http.Request]map[interface{}]interface{})
 
30
        }
 
31
        if c.m[req] == nil {
 
32
                c.m[req] = make(map[interface{}]interface{})
 
33
        }
 
34
        c.m[req][key] = val
 
35
}
 
36
 
 
37
// Get returns a value registered for a given key in a given request.
 
38
func (c *Context) Get(req *http.Request, key interface{}) interface{} {
 
39
        c.l.Lock()
 
40
        defer c.l.Unlock()
 
41
        if c.m != nil && c.m[req] != nil {
 
42
                return c.m[req][key]
 
43
        }
 
44
        return nil
 
45
}
 
46
 
 
47
// Delete removes the value for a given key in a given request.
 
48
func (c *Context) Delete(req *http.Request, key interface{}) {
 
49
        c.l.Lock()
 
50
        defer c.l.Unlock()
 
51
        if c.m != nil && c.m[req] != nil {
 
52
                delete(c.m[req], key)
 
53
        }
 
54
}
 
55
 
 
56
// Clear removes all values for a given request.
 
57
func (c *Context) Clear(req *http.Request) {
 
58
        c.l.Lock()
 
59
        defer c.l.Unlock()
 
60
        if c.m != nil {
 
61
                delete(c.m, req)
 
62
        }
 
63
}