diff options
author | Ben Bangert <ben@groovie.org> | 2016-03-24 19:23:19 -0700 |
---|---|---|
committer | Ben Bangert <ben@groovie.org> | 2016-03-24 19:23:19 -0700 |
commit | 381ca746b6c439e7ffaae4d191c4d40f1ef6b3ad (patch) | |
tree | f72e7f10232880ed49ebfec1e6e5be370df6b74e | |
parent | 9adf7e567d77105752028bd2fc6715d7a09ef8c2 (diff) | |
parent | 81705e48824582f0b58a14477bd0732f9e364e73 (diff) | |
download | routes-381ca746b6c439e7ffaae4d191c4d40f1ef6b3ad.tar.gz |
Merge pull request #61 from uvNikita/master
Fix subdomain equivalence check
-rw-r--r-- | CHANGELOG.rst | 1 | ||||
-rw-r--r-- | routes/util.py | 8 | ||||
-rw-r--r-- | tests/test_functional/test_explicit_use.py | 8 |
3 files changed, 14 insertions, 3 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3b08d25..556ec8a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,7 @@ Routes Changelog Release 2.3 (**dev**) ===================== +* Fix sub_domain equivalence check. Patch by Nikita Uvarov * Add support for protocol-relative URLs generation (i.e. starting with double slash ``//``). PR #60. Patch by Sviatoslav Sydorenko. * Add support for the ``middleware`` extra requirement, making possible to 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')) |