Commit ba498122 authored by Julien Muchembled's avatar Julien Muchembled Committed by Cédric Le Ninivin

Implement Balancer Cookie

parent 7590c250
...@@ -70,6 +70,31 @@ def getLoginURL(self): ...@@ -70,6 +70,31 @@ def getLoginURL(self):
CookieCrumbler.getLoginURL = getLoginURL CookieCrumbler.getLoginURL = getLoginURL
def balancer_cookie_hook(ob, req, resp):
"""Post publishing traversal hook to automatically (un)set balancer cookie
If authenticated, then cookie is set to use the same zope on next request,
for a better use of caches. Otherwise, if anonymous, expire cookie so that
the balancer redirects us on any zope.
XXX: Because we only have persistent traversal hooks and we don't want to
complicate code with automatic upgrade, this one is implemented by
pluging into CookieCrumbler, although what they are quite unrelated.
"""
balancer_cookie = req.get('HTTP_X_BALANCER_CURRENT_COOKIE')
if balancer_cookie:
try:
path = ob.aq_parent.absolute_url_path()
except AttributeError:
path = '/'
if req['AUTHENTICATED_USER'].getUserName() == 'Anonymous User':
if balancer_cookie in req.cookies:
resp.expireCookie(balancer_cookie, path=path)
else:
server_id = req['HTTP_X_BALANCER_CURRENT_SERVER']
if server_id != req.cookies.get(balancer_cookie):
resp.setCookie(balancer_cookie, server_id, path=path);
def modifyRequest(self, req, resp): def modifyRequest(self, req, resp):
"""Copies cookie-supplied credentials to the basic auth fields. """Copies cookie-supplied credentials to the basic auth fields.
...@@ -83,6 +108,8 @@ def modifyRequest(self, req, resp): ...@@ -83,6 +108,8 @@ def modifyRequest(self, req, resp):
or req.environ.has_key('WEBDAV_SOURCE_PORT')): or req.environ.has_key('WEBDAV_SOURCE_PORT')):
raise CookieCrumblerDisabled raise CookieCrumblerDisabled
req.post_traverse(balancer_cookie_hook, (self, req, resp))
# attempt may contain information about an earlier attempt to # attempt may contain information about an earlier attempt to
# authenticate using a higher-up cookie crumbler within the # authenticate using a higher-up cookie crumbler within the
# same request. # same request.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment