Skip to content

Commit

Permalink
Force resolver to use PUBLIC_DOMAIN over HTTPS if not Domain.https
Browse files Browse the repository at this point in the history
Argument ``require_https_domain`` added to force the resolver to use
https URLs with PUBLIC_DOMAIN when the Domain is not https.
  • Loading branch information
humitos committed Aug 28, 2018
1 parent dde104d commit 6fd22b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 10 additions & 3 deletions readthedocs/core/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def resolve_domain(self, project, private=None):
return self._get_project_subdomain(canonical_project)
return getattr(settings, 'PRODUCTION_DOMAIN')

def resolve(self, project, protocol='http', filename='', private=None,
def resolve(self, project, require_https_domain=False, filename='', private=None,
**kwargs):
if private is None:
version_slug = kwargs.get('version_slug')
Expand All @@ -149,15 +149,22 @@ def resolve(self, project, protocol='http', filename='', private=None,

# This duplication from resolve_domain is for performance purposes
# in order to check whether a custom domain should be HTTPS
if custom_domain:
use_custom_domain = any([
(custom_domain and not require_https_domain),
(custom_domain and require_https_domain and custom_domain.https),
])
if use_custom_domain:
domain = custom_domain.domain
elif self._use_subdomain():
domain = self._get_project_subdomain(canonical_project)
else:
domain = getattr(settings, 'PRODUCTION_DOMAIN')

protocol = 'http'
if custom_domain:
protocol = 'https' if custom_domain.https else 'http'
# Rely on the ``Domain.https`` field or force it if specified
if custom_domain.https or require_https_domain:
protocol = 'https'
else:
# Use HTTPS if settings specify
public_domain = getattr(settings, 'PUBLIC_DOMAIN', None)
Expand Down
8 changes: 8 additions & 0 deletions readthedocs/rtd_tests/tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ def test_resolver_domain(self):
url = resolve(project=self.pip)
self.assertEqual(url, 'http://docs.foobar.com/en/latest/')

# Do not use the ``Domain.domain`` when ``require_https_domain=True``
url = resolve(project=self.pip, require_https_domain=True)
self.assertEqual(url, 'https://pip.readthedocs.org/en/latest/')

@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
def test_resolver_domain_https(self):
self.domain = fixture.get(
Expand All @@ -487,6 +491,10 @@ def test_resolver_domain_https(self):
url = resolve(project=self.pip)
self.assertEqual(url, 'https://docs.foobar.com/en/latest/')

# Use the ``Domain.domain`` when ``require_https_domain=True``
url = resolve(project=self.pip, require_https_domain=True)
self.assertEqual(url, 'https://docs.foobar.com/en/latest/')

@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
def test_resolver_subproject(self):
with override_settings(USE_SUBDOMAIN=False):
Expand Down

0 comments on commit 6fd22b0

Please sign in to comment.