# HG changeset patch # User Florent # Date 1240995973 -7200 # Node ID dca9817bb33765201c4edf283e675ea59affd581 # Parent 2965fcfaeca129019d01db7c6f01a13ebfb58580 fix use-request-subdomain option behaviour and add tests diff -r 2965fcfaeca1 -r dca9817bb337 etwist/server.py --- a/etwist/server.py Wed Apr 29 10:11:53 2009 +0200 +++ b/etwist/server.py Wed Apr 29 11:06:13 2009 +0200 @@ -44,6 +44,14 @@ # ensure no tasks will be further added repo._looping_tasks = () +def host_prefixed_baseurl(baseurl, host): + scheme, netloc, url, query, fragment = urlsplit(baseurl) + netloc_domain = '.' + '.'.join(netloc.split('.')[-2:]) + if host.endswith(netloc_domain): + netloc = host + baseurl = urlunsplit((scheme, netloc, url, query, fragment)) + return baseurl + class LongTimeExpiringFile(static.File): """overrides static.File and sets a far futre ``Expires`` date @@ -155,14 +163,6 @@ else: return threads.deferToThread(self.render_request, request) - def _host_prefixed_base_url(self, base_url): - scheme, netloc, url, query, fragment = urlsplit(baseurl) - if '.' in netloc: - netloc = '.'.join(host.split('.')[:1] + netloc.split('.')[1:]) - baseurl = urlunsplit((scheme, netloc, url, query, fragment)) - self.warning('base_url is %s for this request', baseurl) - return base_url - def render_request(self, request): origpath = request.path host = request.host @@ -177,7 +177,8 @@ https = False baseurl = self.base_url if self.config['use-request-subdomain']: - base_url = self._host_prefixed_base_url(base_url) + baseurl = host_prefixed_baseurl(baseurl, host) + self.warning('used baseurl is %s for this request', baseurl) req = CubicWebTwistedRequestAdapter(request, self.appli.vreg, https, baseurl) if req.authmode == 'http': # activate realm-based auth diff -r 2965fcfaeca1 -r dca9817bb337 etwist/test/unittest_server.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/etwist/test/unittest_server.py Wed Apr 29 11:06:13 2009 +0200 @@ -0,0 +1,35 @@ +from cubicweb.devtools.apptest import EnvBasedTC +from cubicweb.etwist.server import host_prefixed_baseurl + + +class HostPrefixedBaseURLTC(EnvBasedTC): + + def _check(self, baseurl, host, waited): + self.assertEquals(host_prefixed_baseurl(baseurl, host), waited, + 'baseurl %s called through host %s should be considered as %s' + % (baseurl, host, waited)) + + def test1(self): + self._check('http://www.cubicweb.org/hg/', 'code.cubicweb.org', + 'http://code.cubicweb.org/hg/') + + def test2(self): + self._check('http://www.cubicweb.org/hg/', 'cubicweb.org', + 'http://www.cubicweb.org/hg/') + + def test3(self): + self._check('http://cubicweb.org/hg/', 'code.cubicweb.org', + 'http://code.cubicweb.org/hg/') + + def test4(self): + self._check('http://www.cubicweb.org/hg/', 'localhost', + 'http://www.cubicweb.org/hg/') + + def test5(self): + self._check('http://www.cubicweb.org/cubes/', 'hg.code.cubicweb.org', + 'http://hg.code.cubicweb.org/cubes/') + + def test6(self): + self._check('http://localhost:8080/hg/', 'code.cubicweb.org', + 'http://localhost:8080/hg/') +