Skip to content

Subdomains use HTTPS if settings specify #3987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ Default: :djangosetting:`PUBLIC_DOMAIN`
A special domain for serving public documentation.
If set, public docs will be linked here instead of the `PRODUCTION_DOMAIN`.


PUBLIC_DOMAIN_USES_HTTPS
------------------------

Default: ``False``

If ``True`` and ``PUBLIC_DOMAIN`` is set, that domain will default to
serving public documentation over HTTPS. By default, documentation is
served over HTTP.


ALLOW_ADMIN
-----------

Expand Down
10 changes: 9 additions & 1 deletion readthedocs/core/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,17 @@ def resolve(self, project, protocol='http', filename='', private=None,
version_slug = project.get_default_version()
private = self._get_private(project, version_slug)

domain = self.resolve_domain(project, private=private)

# Use HTTPS if settings specify
public_domain = getattr(settings, 'PUBLIC_DOMAIN', None)
use_https = getattr(settings, 'PUBLIC_DOMAIN_USES_HTTPS', False)
if use_https and public_domain and public_domain in domain:
protocol = 'https'

return '{protocol}://{domain}{path}'.format(
protocol=protocol,
domain=self.resolve_domain(project, private=private),
domain=domain,
path=self.resolve_path(project, filename=filename, private=private,
**kwargs),
)
Expand Down
17 changes: 17 additions & 0 deletions readthedocs/rtd_tests/tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,23 @@ def test_resolver_public_domain_overrides(self):
url = resolve(project=self.pip, private=False)
self.assertEqual(url, 'http://docs.foobar.com/en/latest/')

@override_settings(
PRODUCTION_DOMAIN='readthedocs.org',
PUBLIC_DOMAIN='readthedocs.io',
USE_SUBDOMAIN=True,
)
def test_resolver_domain_https(self):
with override_settings(PUBLIC_DOMAIN_USES_HTTPS=True):
url = resolve(project=self.pip, private=True)
self.assertEqual(url, 'https://pip.readthedocs.io/en/latest/')

url = resolve(project=self.pip, private=False)
self.assertEqual(url, 'https://pip.readthedocs.io/en/latest/')

with override_settings(PUBLIC_DOMAIN_USES_HTTPS=False):
url = resolve(project=self.pip, private=True)
self.assertEqual(url, 'http://pip.readthedocs.io/en/latest/')


class ResolverAltSetUp(object):

Expand Down
1 change: 1 addition & 0 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CommunityBaseSettings(Settings):
# Domains and URLs
PRODUCTION_DOMAIN = 'readthedocs.org'
PUBLIC_DOMAIN = None
PUBLIC_DOMAIN_USES_HTTPS = False
USE_SUBDOMAIN = False
PUBLIC_API_URL = 'https://{0}'.format(PRODUCTION_DOMAIN)

Expand Down