[Zope3-checkins] SVN: Zope3/trunk/ Fix for issue #679: use the new IHTTPVirtualHostChanged event to make sure cookies are re-set every time the virtual host info changes so the path remains correct

Martijn Pieters mj at zopatista.com
Sun Jul 30 08:52:14 EDT 2006


Log message for revision 69299:
  Fix for issue #679: use the new IHTTPVirtualHostChanged event to make sure cookies are re-set every time the virtual host info changes so the path remains correct

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/session/configure.zcml
  U   Zope3/trunk/src/zope/app/session/http.py
  U   Zope3/trunk/src/zope/app/session/tests.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-07-30 12:37:29 UTC (rev 69298)
+++ Zope3/trunk/doc/CHANGES.txt	2006-07-30 12:52:13 UTC (rev 69299)
@@ -77,6 +77,10 @@
       - Fixed issue 543: add 'decimal'-package to
         zope/app/security/globalmodules.zcml
 
+      - Fixed issue 679: using the new IHTTPVirtualHostChanged event, session's
+        cliend id cookies fix up the set cookie path whenever the virtual host
+        information changes during a request.
+
     Much thanks to everyone who contributed to this release:
 
       Jim Fulton, Dmitry Vasiliev

Modified: Zope3/trunk/src/zope/app/session/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/session/configure.zcml	2006-07-30 12:37:29 UTC (rev 69298)
+++ Zope3/trunk/src/zope/app/session/configure.zcml	2006-07-30 12:52:13 UTC (rev 69299)
@@ -88,6 +88,11 @@
       for="zope.app.appsetup.IDatabaseOpenedEvent"
       handler=".bootstrap.bootStrapSubscriber"
       />
+      
+  <subscriber
+      for="zope.publisher.interfaces.http.IHTTPVirtualHostChanged"
+      handler=".http.notifyVirtualHostChanged"
+      />
 
   <include file="browser.zcml" />
 

Modified: Zope3/trunk/src/zope/app/session/http.py
===================================================================
--- Zope3/trunk/src/zope/app/session/http.py	2006-07-30 12:37:29 UTC (rev 69298)
+++ Zope3/trunk/src/zope/app/session/http.py	2006-07-30 12:52:13 UTC (rev 69299)
@@ -25,9 +25,11 @@
 
 import zope.location
 from persistent import Persistent
-from zope import schema
+from zope import schema, component
 from zope.interface import implements
+from zope.publisher.interfaces.http import IHTTPRequest
 from zope.publisher.interfaces.http import IHTTPApplicationRequest
+from zope.publisher.interfaces.http import IHTTPVirtualHostChanged
 from zope.annotation.interfaces import IAttributeAnnotatable
 
 from zope.app.i18n import ZopeMessageFactory as _
@@ -272,3 +274,50 @@
                     path=request.getApplicationURL(path_only=True)
                     )
 
+ at component.adapter(IHTTPVirtualHostChanged)
+def notifyVirtualHostChanged(event):
+    """Adjust cookie paths when IVirtualHostRequest information changes.
+    
+    Given a event, this method should call a CookieClientIdManager's 
+    setRequestId if a cookie is present in the response for that manager. To
+    demonstrate we create a dummy manager object and event:
+    
+        >>> class DummyManager(object):
+        ...     implements(ICookieClientIdManager)
+        ...     namespace = 'foo'
+        ...     request_id = None
+        ...     def setRequestId(self, request, id):
+        ...         self.request_id = id
+        ...
+        >>> manager = DummyManager()
+        >>> from zope.app.testing import ztapi
+        >>> ztapi.provideUtility(IClientIdManager, manager)
+        >>> from zope.publisher.http import HTTPRequest
+        >>> class DummyEvent (object):
+        ...     request = HTTPRequest(StringIO(''), {}, None)
+        >>> event = DummyEvent()
+        
+    With no cookies present, the manager should not be called:
+    
+        >>> notifyVirtualHostChanged(event)
+        >>> manager.request_id is None
+        True
+        
+    However, when a cookie *has* been set, the manager is called so it can
+    update the cookie if need be:
+    
+        >>> event.request.response.setCookie('foo', 'bar')
+        >>> notifyVirtualHostChanged(event)
+        >>> manager.request_id
+        'bar'
+        
+    """
+    # the event sends us a IHTTPApplicationRequest, but we need a
+    # IHTTPRequest for the response attribute, and so does the cookie-
+    # manager.
+    request = IHTTPRequest(event.request, None)
+    manager = component.queryUtility(IClientIdManager)
+    if manager and request and ICookieClientIdManager.providedBy(manager):
+        cookie = request.response.getCookie(manager.namespace)
+        if cookie:
+            manager.setRequestId(request, cookie['value'])

Modified: Zope3/trunk/src/zope/app/session/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/session/tests.py	2006-07-30 12:37:29 UTC (rev 69298)
+++ Zope3/trunk/src/zope/app/session/tests.py	2006-07-30 12:52:13 UTC (rev 69299)
@@ -89,11 +89,15 @@
 
     ''' % (open(os.path.join(os.path.dirname(__file__), 'api.txt')).read(),)
 
+def sessionTestTearDown(test=None):
+    ztapi.unprovideUtility(IClientIdManager)
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(TestBootstrap))
     suite.addTest(doctest.DocTestSuite())
-    suite.addTest(doctest.DocTestSuite('zope.app.session.session'))
+    suite.addTest(doctest.DocTestSuite('zope.app.session.session',
+                                       tearDown=sessionTestTearDown))
     suite.addTest(doctest.DocTestSuite('zope.app.session.http'))
     return suite
 



More information about the Zope3-Checkins mailing list