Skip to content

Commit f74d4fb

Browse files
authored
Merge pull request #3987 from davidfischer/subdomains-https
Subdomains use HTTPS if settings specify
2 parents 4b83d92 + ade7cb2 commit f74d4fb

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

docs/settings.rst

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ Default: :djangosetting:`PUBLIC_DOMAIN`
8383
A special domain for serving public documentation.
8484
If set, public docs will be linked here instead of the `PRODUCTION_DOMAIN`.
8585

86+
87+
PUBLIC_DOMAIN_USES_HTTPS
88+
------------------------
89+
90+
Default: ``False``
91+
92+
If ``True`` and ``PUBLIC_DOMAIN`` is set, that domain will default to
93+
serving public documentation over HTTPS. By default, documentation is
94+
served over HTTP.
95+
96+
8697
ALLOW_ADMIN
8798
-----------
8899

readthedocs/core/resolver.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,17 @@ def resolve(self, project, protocol='http', filename='', private=None,
144144
version_slug = project.get_default_version()
145145
private = self._get_private(project, version_slug)
146146

147+
domain = self.resolve_domain(project, private=private)
148+
149+
# Use HTTPS if settings specify
150+
public_domain = getattr(settings, 'PUBLIC_DOMAIN', None)
151+
use_https = getattr(settings, 'PUBLIC_DOMAIN_USES_HTTPS', False)
152+
if use_https and public_domain and public_domain in domain:
153+
protocol = 'https'
154+
147155
return '{protocol}://{domain}{path}'.format(
148156
protocol=protocol,
149-
domain=self.resolve_domain(project, private=private),
157+
domain=domain,
150158
path=self.resolve_path(project, filename=filename, private=private,
151159
**kwargs),
152160
)

readthedocs/rtd_tests/tests/test_resolver.py

+17
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,23 @@ def test_resolver_public_domain_overrides(self):
593593
url = resolve(project=self.pip, private=False)
594594
self.assertEqual(url, 'http://docs.foobar.com/en/latest/')
595595

596+
@override_settings(
597+
PRODUCTION_DOMAIN='readthedocs.org',
598+
PUBLIC_DOMAIN='readthedocs.io',
599+
USE_SUBDOMAIN=True,
600+
)
601+
def test_resolver_domain_https(self):
602+
with override_settings(PUBLIC_DOMAIN_USES_HTTPS=True):
603+
url = resolve(project=self.pip, private=True)
604+
self.assertEqual(url, 'https://pip.readthedocs.io/en/latest/')
605+
606+
url = resolve(project=self.pip, private=False)
607+
self.assertEqual(url, 'https://pip.readthedocs.io/en/latest/')
608+
609+
with override_settings(PUBLIC_DOMAIN_USES_HTTPS=False):
610+
url = resolve(project=self.pip, private=True)
611+
self.assertEqual(url, 'http://pip.readthedocs.io/en/latest/')
612+
596613

597614
class ResolverAltSetUp(object):
598615

readthedocs/settings/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CommunityBaseSettings(Settings):
4242
# Domains and URLs
4343
PRODUCTION_DOMAIN = 'readthedocs.org'
4444
PUBLIC_DOMAIN = None
45+
PUBLIC_DOMAIN_USES_HTTPS = False
4546
USE_SUBDOMAIN = False
4647
PUBLIC_API_URL = 'https://{0}'.format(PRODUCTION_DOMAIN)
4748

0 commit comments

Comments
 (0)