-
Notifications
You must be signed in to change notification settings - Fork 25
/
cookies.py
49 lines (39 loc) · 1.41 KB
/
cookies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Cookie
import logging
import os
def get_cookie_value(key):
cookies = None
try:
cookies = Cookie.BaseCookie(os.environ.get('HTTP_COOKIE',''))
except Cookie.CookieError, error:
logging.debug("Ignoring Cookie Error, skipping get cookie: '%s'" % error)
if not cookies:
return None
cookie = cookies.get(key)
if not cookie:
return None
return cookie.value
# Cookie handling from http://appengine-cookbook.appspot.com/recipe/a-simple-cookie-class/
def set_cookie_value(key, value='', max_age=None,
path='/', domain=None, secure=None, httponly=False,
version=None, comment=None):
cookies = Cookie.BaseCookie()
cookies[key] = value
for var_name, var_value in [
('max-age', max_age),
('path', path),
('domain', domain),
('secure', secure),
#('HttpOnly', httponly), Python 2.6 is required for httponly cookies
('version', version),
('comment', comment),
]:
if var_value is not None and var_value is not False:
cookies[key][var_name] = str(var_value)
if max_age is not None:
cookies[key]['expires'] = max_age
cookies_header = cookies[key].output(header='').lstrip()
if httponly:
# We have to manually add this part of the header until GAE uses Python 2.6.
cookies_header += "; HttpOnly"
return cookies_header