Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 105 lines (100 sloc) 3.94 kb
290502a @ianloic make it almost work
authored
1 import UserDict
2 from Cookie import BaseCookie
3 class Cookies(UserDict.DictMixin):
4 def __init__(self,handler,**policy):
5 self.response = handler.response
6 self._in = handler.request.cookies
7 self.policy = policy
8 if 'secure' not in policy and handler.request.environ.get('HTTPS', '').lower() in ['on', 'true']:
9 policy['secure']=True
10 self._out = {}
11 def __getitem__(self, key):
12 if key in self._out:
13 return self._out[key]
14 if key in self._in:
15 return self._in[key]
16 raise KeyError(key)
17 def __setitem__(self, key, item):
18 self._out[key] = item
19 self.set_cookie(key, item, **self.policy)
20 def __contains__(self, key):
21 return key in self._in or key in self._out
22 def keys(self):
23 return self._in.keys() + self._out.keys()
24 def __delitem__(self, key):
25 if key in self._out:
26 del self._out[key]
27 self.unset_cookie(key)
28 if key in self._in:
29 del self._in[key]
30 p = {}
31 if 'path' in self.policy: p['path'] = self.policy['path']
32 if 'domain' in self.policy: p['domain'] = self.policy['domain']
33 self.delete_cookie(key, **p)
34 #begin WebOb functions
35 def set_cookie(self, key, value='', max_age=None,
36 path='/', domain=None, secure=None, httponly=False,
37 version=None, comment=None):
38 """
39 Set (add) a cookie for the response
40 """
41 cookies = BaseCookie()
42 cookies[key] = value
43 for var_name, var_value in [
44 ('max-age', max_age),
45 ('path', path),
46 ('domain', domain),
47 ('secure', secure),
48 ('HttpOnly', httponly),
49 ('version', version),
50 ('comment', comment),
51 ]:
52 if var_value is not None and var_value is not False:
53 cookies[key][var_name] = str(var_value)
54 if max_age is not None:
55 cookies[key]['expires'] = max_age
56 header_value = cookies[key].output(header='').lstrip()
57 self.response.headers._headers.append(('Set-Cookie', header_value))
58 def delete_cookie(self, key, path='/', domain=None):
59 """
60 Delete a cookie from the client. Note that path and domain must match
61 how the cookie was originally set.
62 This sets the cookie to the empty string, and max_age=0 so
63 that it should expire immediately.
64 """
65 self.set_cookie(key, '', path=path, domain=domain,
66 max_age=0)
67 def unset_cookie(self, key):
68 """
69 Unset a cookie with the given name (remove it from the
70 response). If there are multiple cookies (e.g., two cookies
71 with the same name and different paths or domains), all such
72 cookies will be deleted.
73 """
74 existing = self.response.headers.get_all('Set-Cookie')
75 if not existing:
76 raise KeyError(
77 "No cookies at all have been set")
78 del self.response.headers['Set-Cookie']
79 found = False
80 for header in existing:
81 cookies = BaseCookie()
82 cookies.load(header)
83 if key in cookies:
84 found = True
85 del cookies[key]
86 header = cookies.output(header='').lstrip()
87 if header:
88 self.response.headers.add('Set-Cookie', header)
89 if not found:
90 raise KeyError(
91 "No cookie has been set with the name %r" % key)
9ec193f @ianloic more and better
authored
92 #end WebOb functions
93
94
95 # JSON encoded cookies
96 from django.utils import simplejson as json
97 import urllib
98 class JSONCookies(Cookies):
99 def __getitem__(self, key):
100 return json.loads(urllib.unquote(Cookies.__getitem__(self, key)))
101 def __setitem__(self, key, item):
102 Cookies.__setitem__(self, key, urllib.quote(json.dumps(item)))
103
104
Something went wrong with that request. Please try again.