Skip to content

Commit

Permalink
SiteAccess: Make VirtualHostMonster support IPv6 (#314)
Browse files Browse the repository at this point in the history
from https://bugs.launchpad.net/zope2/+bug/699865 :

VirtualHostMonster does not work with IPv6 named hosts.

In case of such rewrite configuration:

RewriteRule (.*) http://10.0.243.129:9280/VirtualHostBase/https/[%{SERVER_ADDR}]:4080$1 [L,P]

When SERVER_ADDR is fd00::74ba VirtualHostMonster dies with:

Traceback (most recent call last):
  File "/eggs/Zope2-2.12.14-py2.6-linux-x86_64.egg/ZPublisher/BeforeTraverse.py", line 145, in __call__
    meth(*(container, request, None)[:args])
  File "/eggs/Zope2-2.12.14-py2.6-linux-x86_64.egg/Products/SiteAccess/VirtualHostMonster.py", line 154, in __call__
    host, port = host.split(':')
ValueError: too many values to unpack

This is because IPv6 addresses contain ":" in them.

--
This is backport of #314 to
2.13 branch

This also include part of:
2e9d01e - flake8 Products/Shared/ZTUtils.
removing test_suite function from the test
  • Loading branch information
lukenowak authored and perrinjerome committed Nov 7, 2018
1 parent 0c4b5d6 commit 338f6d0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
7 changes: 2 additions & 5 deletions src/Products/SiteAccess/VirtualHostMonster.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ZPublisher.BeforeTraverse import registerBeforeTraverse
from ZPublisher.BeforeTraverse import unregisterBeforeTraverse
from ZPublisher.BaseRequest import quote
from ZPublisher.HTTPRequest import splitport
from zExceptions import BadRequest

class VirtualHostMonster(Persistent, Item, Implicit):
Expand Down Expand Up @@ -150,11 +151,7 @@ def __call__(self, client, request, response=None):
stack.pop()
protocol = stack.pop()
host = stack.pop()
if ':' in host:
host, port = host.split(':')
request.setServerURL(protocol, host, port)
else:
request.setServerURL(protocol, host)
request.setServerURL(protocol, *splitport(host))
path = list(stack)

# Find and convert VirtualHostRoot directive
Expand Down
65 changes: 60 additions & 5 deletions src/Products/SiteAccess/tests/testVirtualHostMonster.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,66 @@ def test(self, vaddr=vaddr, vr=vr, _vh=_vh, p=p, ubase=ubase):

setattr(VHMRegressions, 'testTraverse%s' % i, test)

class VHMPort(unittest.TestCase):

def setUp(self):
import transaction
from Testing.makerequest import makerequest
from Testing.ZopeTestCase.ZopeLite import app
transaction.begin()
self.app = makerequest(app())
if 'virtual_hosting' not in self.app.objectIds():
# If ZopeLite was imported, we have no default virtual
# host monster
from Products.SiteAccess.VirtualHostMonster \
import manage_addVirtualHostMonster
manage_addVirtualHostMonster(self.app, 'virtual_hosting')
self.app.manage_addFolder('folder')
self.app.folder.manage_addDTMLMethod('doc', '')
self.app.REQUEST.set('PARENTS', [self.app])
self.traverse = self.app.REQUEST.traverse

def tearDown(self):
import transaction
transaction.abort()
self.app._p_jar.close()

def testIPv4(self):
ob = self.traverse('/VirtualHostBase/http/www.mysite.com:80'
'/folder/')
self.assertEqual(self.app.REQUEST['ACTUAL_URL'],
'http://www.mysite.com/folder/')

def testIPv4Noport(self):
ob = self.traverse('/VirtualHostBase/http/www.mysite.com'
'/folder/')
self.assertEqual(self.app.REQUEST['ACTUAL_URL'],
'http://www.mysite.com/folder/')

def testPassedPortIPv4(self):
ob = self.traverse('/VirtualHostBase/http/www.mysite.com:81'
'/folder/')
self.assertEqual(self.app.REQUEST['ACTUAL_URL'],
'http://www.mysite.com:81/folder/')

def testIPv6(self):
ob = self.traverse('/VirtualHostBase/http/[::1]:80'
'/folder/')
self.assertEqual(self.app.REQUEST['ACTUAL_URL'],
'http://[::1]/folder/')

def testIPv6NoPort(self):
ob = self.traverse('/VirtualHostBase/http/[::1]'
'/folder/')
self.assertEqual(self.app.REQUEST['ACTUAL_URL'],
'http://[::1]/folder/')

def testIPv6PassedPort(self):
ob = self.traverse('/VirtualHostBase/http/[::1]:81'
'/folder/')
self.assertEqual(self.app.REQUEST['ACTUAL_URL'],
'http://[::1]:81/folder/')


class VHMAddingTests(unittest.TestCase):

Expand Down Expand Up @@ -198,8 +258,3 @@ def test_add_manage_addVirtualHostMonster(self):
hook = queryBeforeTraverse(self.root, VirtualHostMonster.meta_type)
self.assertTrue(hook)

def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(VHMRegressions))
suite.addTest(unittest.makeSuite(VHMAddingTests))
return suite

0 comments on commit 338f6d0

Please sign in to comment.