2014-05-15 22:37:08 -07:00
|
|
|
from Cookie import SimpleCookie, CookieError
|
2014-05-13 17:07:41 -07:00
|
|
|
|
|
|
|
|
|
|
|
#=================================================================
|
2014-09-30 12:42:11 -07:00
|
|
|
class WbUrlBaseCookieRewriter(object):
|
|
|
|
""" Base Cookie rewriter for wburl-based requests.
|
2014-05-13 17:07:41 -07:00
|
|
|
"""
|
|
|
|
def __init__(self, url_rewriter):
|
|
|
|
self.url_rewriter = url_rewriter
|
|
|
|
|
|
|
|
def rewrite(self, cookie_str, header='Set-Cookie'):
|
|
|
|
results = []
|
2014-05-15 22:37:08 -07:00
|
|
|
cookie = SimpleCookie()
|
|
|
|
try:
|
|
|
|
cookie.load(cookie_str)
|
|
|
|
except CookieError:
|
|
|
|
return results
|
2014-05-13 17:07:41 -07:00
|
|
|
|
|
|
|
for name, morsel in cookie.iteritems():
|
2014-09-30 12:42:11 -07:00
|
|
|
morsel = self.rewrite_cookie(name, morsel)
|
|
|
|
if morsel:
|
|
|
|
results.append((header, morsel.OutputString()))
|
2014-05-13 17:07:41 -07:00
|
|
|
|
|
|
|
return results
|
2014-09-30 12:42:11 -07:00
|
|
|
|
2014-10-12 03:07:54 -07:00
|
|
|
def _remove_age_opts(self, morsel):
|
|
|
|
# remove expires as it refers to archived time
|
|
|
|
if morsel.get('expires'):
|
|
|
|
del morsel['expires']
|
|
|
|
|
|
|
|
# don't use max-age, just expire at end of session
|
|
|
|
if morsel.get('max-age'):
|
|
|
|
del morsel['max-age']
|
|
|
|
|
2015-03-05 16:18:56 -08:00
|
|
|
# for now, also remove secure to avoid issues when
|
|
|
|
# proxying over plain http (TODO: detect https?)
|
|
|
|
if morsel.get('secure'):
|
|
|
|
del morsel['secure']
|
|
|
|
|
2014-09-30 12:42:11 -07:00
|
|
|
|
2015-04-14 18:04:51 -07:00
|
|
|
#=================================================================
|
|
|
|
class RemoveAllCookiesRewriter(WbUrlBaseCookieRewriter):
|
|
|
|
def rewrite(self, cookie_str, header='Set-Cookie'):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2014-09-30 12:42:11 -07:00
|
|
|
#=================================================================
|
|
|
|
class MinimalScopeCookieRewriter(WbUrlBaseCookieRewriter):
|
|
|
|
"""
|
|
|
|
Attempt to rewrite cookies to minimal scope possible
|
|
|
|
|
|
|
|
If path present, rewrite path to current rewritten url only
|
|
|
|
If domain present, remove domain and set to path prefix
|
|
|
|
"""
|
|
|
|
|
|
|
|
def rewrite_cookie(self, name, morsel):
|
|
|
|
# if domain set, no choice but to expand cookie path to root
|
|
|
|
if morsel.get('domain'):
|
|
|
|
del morsel['domain']
|
|
|
|
morsel['path'] = self.url_rewriter.rel_prefix
|
|
|
|
# else set cookie to rewritten path
|
|
|
|
elif morsel.get('path'):
|
|
|
|
morsel['path'] = self.url_rewriter.rewrite(morsel['path'])
|
|
|
|
|
2014-10-12 03:07:54 -07:00
|
|
|
self._remove_age_opts(morsel)
|
2014-09-30 12:42:11 -07:00
|
|
|
return morsel
|
|
|
|
|
|
|
|
|
2014-11-13 09:24:34 -08:00
|
|
|
#=================================================================
|
|
|
|
class ExactPathCookieRewriter(WbUrlBaseCookieRewriter):
|
|
|
|
"""
|
|
|
|
Rewrite cookies only using exact path, useful for live rewrite
|
|
|
|
without a timestamp and to minimize cookie pollution
|
|
|
|
|
|
|
|
If path or domain present, simply remove
|
|
|
|
"""
|
|
|
|
|
|
|
|
def rewrite_cookie(self, name, morsel):
|
|
|
|
if morsel.get('domain'):
|
|
|
|
del morsel['domain']
|
|
|
|
# else set cookie to rewritten path
|
|
|
|
if morsel.get('path'):
|
|
|
|
del morsel['path']
|
|
|
|
|
|
|
|
self._remove_age_opts(morsel)
|
|
|
|
return morsel
|
2014-12-23 15:14:03 -08:00
|
|
|
|
|
|
|
|
2014-09-30 12:42:11 -07:00
|
|
|
#=================================================================
|
|
|
|
class RootScopeCookieRewriter(WbUrlBaseCookieRewriter):
|
|
|
|
"""
|
|
|
|
Sometimes it is necessary to rewrite cookies to root scope
|
|
|
|
in order to work across time boundaries and modifiers
|
|
|
|
|
|
|
|
This rewriter simply sets all cookies to be in the root
|
|
|
|
"""
|
|
|
|
def rewrite_cookie(self, name, morsel):
|
|
|
|
# get root path
|
|
|
|
morsel['path'] = self.url_rewriter.root_path
|
2014-10-12 03:07:54 -07:00
|
|
|
|
2014-09-30 12:42:11 -07:00
|
|
|
# remove domain
|
|
|
|
if morsel.get('domain'):
|
|
|
|
del morsel['domain']
|
|
|
|
|
2014-10-12 03:07:54 -07:00
|
|
|
self._remove_age_opts(morsel)
|
2014-09-30 12:42:11 -07:00
|
|
|
return morsel
|
|
|
|
|
|
|
|
|
|
|
|
#=================================================================
|
2014-10-06 10:14:45 -07:00
|
|
|
def get_cookie_rewriter(cookie_scope):
|
|
|
|
if cookie_scope == 'root':
|
2014-09-30 12:42:11 -07:00
|
|
|
return RootScopeCookieRewriter
|
2014-11-13 09:24:34 -08:00
|
|
|
elif cookie_scope == 'exact':
|
|
|
|
return ExactPathCookieRewriter
|
2015-04-14 18:04:51 -07:00
|
|
|
elif cookie_scope == 'removeall':
|
|
|
|
return RemoveAllCookiesRewriter
|
2014-09-30 12:42:11 -07:00
|
|
|
else:
|
|
|
|
return MinimalScopeCookieRewriter
|