From 1dd523357a100d97bbe4abc8f0e0316150895adb Mon Sep 17 00:00:00 2001 From: Nikita Uvarov Date: Thu, 24 Mar 2016 16:44:56 +0200 Subject: Fix subdomain equivalence check. Use regex instead of startswith Problem occurred when new subdomain was left substring of current one. In this case subdomain was not replaced. --- routes/util.py | 8 +++++--- tests/test_functional/test_explicit_use.py | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/routes/util.py b/routes/util.py index f3d2dc4..c48445f 100644 --- a/routes/util.py +++ b/routes/util.py @@ -93,10 +93,12 @@ def _subdomain_check(kargs, mapper, environ): port = '' if len(hostmatch) > 1: port += ':' + hostmatch[1] - sub_match = re.compile('^.+?\.(%s)$' % mapper.domain_match) - domain = re.sub(sub_match, r'\1', host) + + match = re.match('^(.+?)\.(%s)$' % mapper.domain_match, host) + host_subdomain, domain = match.groups() if match else (None, host) + subdomain = as_unicode(subdomain, mapper.encoding) - if subdomain and not host.startswith(subdomain) and \ + if subdomain and host_subdomain != subdomain and \ subdomain not in mapper.sub_domains_ignore: kargs['_host'] = subdomain + '.' + domain + port elif (subdomain in mapper.sub_domains_ignore or \ diff --git a/tests/test_functional/test_explicit_use.py b/tests/test_functional/test_explicit_use.py index 32579d8..ccd3b7a 100644 --- a/tests/test_functional/test_explicit_use.py +++ b/tests/test_functional/test_explicit_use.py @@ -52,6 +52,14 @@ class TestUtils(unittest.TestCase): url = URLGenerator(m, environ.copy()) assert_raises(GenerationException, lambda: url.current(qualified=True)) + environ = {'HTTP_HOST': 'subdomain.localhost.com'} + url = URLGenerator(m, environ.copy()) + eq_('http://sub.localhost.com/hi/smith', url(fred='smith', sub_domain='sub', qualified=True)) + + environ = {'HTTP_HOST': 'sub.sub.localhost.com'} + url = URLGenerator(m, environ.copy()) + eq_('http://new.localhost.com/hi/smith', url(fred='smith', sub_domain='new', qualified=True)) + url = URLGenerator(m, {}) eq_('/hi/smith', url(fred='smith', sub_domain=u'home')) -- cgit v1.2.1