[Zope3-checkins] SVN: Zope3/trunk/src/zope/publisher/ Have the IVirtualHostRequest mutation methods emit an event so applications can respond to virtual host changes

Martijn Pieters mj at zopatista.com
Sun Jul 30 03:48:10 EDT 2006


Log message for revision 69296:
  Have the IVirtualHostRequest mutation methods emit an event so applications can respond to virtual host changes

Changed:
  U   Zope3/trunk/src/zope/publisher/DEPENDENCIES.cfg
  U   Zope3/trunk/src/zope/publisher/http.py
  U   Zope3/trunk/src/zope/publisher/interfaces/http.py
  U   Zope3/trunk/src/zope/publisher/tests/test_http.py

-=-
Modified: Zope3/trunk/src/zope/publisher/DEPENDENCIES.cfg
===================================================================
--- Zope3/trunk/src/zope/publisher/DEPENDENCIES.cfg	2006-07-30 02:32:50 UTC (rev 69295)
+++ Zope3/trunk/src/zope/publisher/DEPENDENCIES.cfg	2006-07-30 07:48:09 UTC (rev 69296)
@@ -1,4 +1,5 @@
 zope.component
+zope.event
 zope.exceptions
 zope.i18n
 zope.interface

Modified: Zope3/trunk/src/zope/publisher/http.py
===================================================================
--- Zope3/trunk/src/zope/publisher/http.py	2006-07-30 02:32:50 UTC (rev 69295)
+++ Zope3/trunk/src/zope/publisher/http.py	2006-07-30 07:48:09 UTC (rev 69296)
@@ -25,7 +25,7 @@
 import logging
 from tempfile import TemporaryFile
 
-from zope import component, interface
+from zope import component, interface, event
 from zope.deprecation import deprecation
 
 from zope.publisher import contenttype
@@ -33,6 +33,7 @@
 from zope.publisher.interfaces.http import IHTTPRequest
 from zope.publisher.interfaces.http import IHTTPApplicationRequest
 from zope.publisher.interfaces.http import IHTTPPublisher
+from zope.publisher.interfaces.http import IHTTPVirtualHostChanged
 
 from zope.publisher.interfaces import Redirect
 from zope.publisher.interfaces.http import IHTTPResponse
@@ -75,6 +76,15 @@
         dict['PATH_INFO'] = dict['PATH_INFO'].decode('utf-8')
     return dict
 
+class HTTPVirtualHostChanged(object):
+    interface.implements(IHTTPVirtualHostChanged)
+    component.adapts(IHTTPApplicationRequest)
+    
+    request = None
+    
+    def __init__(self, request):
+        self.request = request
+
 # Possible HTTP status responses
 status_reasons = {
 100: 'Continue',
@@ -552,6 +562,7 @@
         if port and str(port) != DEFAULT_PORTS.get(proto):
             host = '%s:%s' % (host, port)
         self._app_server = '%s://%s' % (proto, host)
+        event.notify(HTTPVirtualHostChanged(self))
 
     def shiftNameToApplication(self):
         """Add the name being traversed to the application name
@@ -562,6 +573,7 @@
         """
         if len(self._traversed_names) == 1:
             self._app_names.append(self._traversed_names.pop())
+            event.notify(HTTPVirtualHostChanged(self))
             return
 
         raise ValueError("Can only shift leading traversal "
@@ -571,6 +583,7 @@
         del self._traversed_names[:]
         self._vh_root = self._last_obj_traversed
         self._app_names = list(names)
+        event.notify(HTTPVirtualHostChanged(self))
 
     def getVirtualHostRoot(self):
         return self._vh_root

Modified: Zope3/trunk/src/zope/publisher/interfaces/http.py
===================================================================
--- Zope3/trunk/src/zope/publisher/interfaces/http.py	2006-07-30 02:32:50 UTC (rev 69295)
+++ Zope3/trunk/src/zope/publisher/interfaces/http.py	2006-07-30 07:48:09 UTC (rev 69296)
@@ -392,3 +392,12 @@
         Note that this function can be only requested once, since it is
         constructed from the result.
         """
+        
+class IHTTPVirtualHostChanged(Interface):
+    """The host, port and/or the application path have changed.
+    
+    The request referred to in this event implements at least the 
+    IHTTPAppliationRequest interface.
+    """
+    request = Attribute(u'The application request whose virtual host info has '
+                        u'been altered')

Modified: Zope3/trunk/src/zope/publisher/tests/test_http.py
===================================================================
--- Zope3/trunk/src/zope/publisher/tests/test_http.py	2006-07-30 02:32:50 UTC (rev 69295)
+++ Zope3/trunk/src/zope/publisher/tests/test_http.py	2006-07-30 07:48:09 UTC (rev 69296)
@@ -18,6 +18,7 @@
 """
 import unittest
 
+import zope.event
 from zope.interface import implements
 from zope.publisher.interfaces.logginginfo import ILoggingInfo
 from zope.publisher.http import HTTPRequest, HTTPResponse
@@ -368,6 +369,8 @@
         self.assertEqual(r.method, 'EGGS')
 
     def test_setApplicationServer(self):
+        events = []
+        zope.event.subscribers.append(events.append)
         req = self._createRequest()
         req.setApplicationServer('foo')
         self.assertEquals(req._app_server, 'http://foo')
@@ -385,22 +388,36 @@
         self.assertEquals(req._app_server, 'http://foo')
         req.setApplicationServer('foo', proto='telnet', port=80)
         self.assertEquals(req._app_server, 'telnet://foo:80')
+        zope.event.subscribers.pop()
+        self.assertEquals(len(events), 8)
+        for event in events:
+            self.assertEquals(event.request, req)
 
     def test_setApplicationNames(self):
+        events = []
+        zope.event.subscribers.append(events.append)
         req = self._createRequest()
         names = ['x', 'y', 'z']
         req.setVirtualHostRoot(names)
         self.assertEquals(req._app_names, ['x', 'y', 'z'])
         names[0] = 'muahahahaha'
         self.assertEquals(req._app_names, ['x', 'y', 'z'])
+        zope.event.subscribers.pop()
+        self.assertEquals(len(events), 1)
+        self.assertEquals(events[0].request, req)
 
     def test_setVirtualHostRoot(self):
+        events = []
+        zope.event.subscribers.append(events.append)
         req = self._createRequest()
         req._traversed_names = ['x', 'y']
         req._last_obj_traversed = object()
         req.setVirtualHostRoot()
         self.failIf(req._traversed_names)
         self.assertEquals(req._vh_root, req._last_obj_traversed)
+        zope.event.subscribers.pop()
+        self.assertEquals(len(events), 1)
+        self.assertEquals(events[0].request, req)
 
     def test_getVirtualHostRoot(self):
         req = self._createRequest()



More information about the Zope3-Checkins mailing list