diff options
author | Nikita Uvarov <uv.nikita@gmail.com> | 2016-03-24 16:44:56 +0200 |
---|---|---|
committer | Nikita Uvarov <uv.nikita@gmail.com> | 2016-03-24 16:58:35 +0200 |
commit | 1dd523357a100d97bbe4abc8f0e0316150895adb (patch) | |
tree | 1e8adfaa5f1e1e6ee16c7c53d32fc05421c339b4 | |
parent | 9adf7e567d77105752028bd2fc6715d7a09ef8c2 (diff) | |
download | routes-1dd523357a100d97bbe4abc8f0e0316150895adb.tar.gz |
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.
-rw-r--r-- | routes/util.py | 8 | ||||
-rw-r--r-- | 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')) |