Skip to content

Commit

Permalink
Keep correct domain for redirects
Browse files Browse the repository at this point in the history
The function redirect_filename always inserted the *.readthedocs.org
subdomain. We let this function only return the path and re-use the domain
used for the current request to build the redirect path.

Related readthedocs#1025.
  • Loading branch information
gregmuellegger committed Sep 17, 2015
1 parent 8fcb031 commit 87f0297
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
26 changes: 11 additions & 15 deletions readthedocs/redirects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

def redirect_filename(project, filename=None):
"""
Return a url for a page. Always use http for now,
to avoid content warnings.
Return a path for a page. No protocol/domain is returned.
"""
protocol = "http"
# Handle explicit http redirects
Expand All @@ -24,19 +23,9 @@ def redirect_filename(project, filename=None):
use_subdomain = getattr(settings, 'USE_SUBDOMAIN', False)
if use_subdomain:
if project.single_version:
return "%s://%s/%s" % (
protocol,
project.subdomain,
filename,
)
return "/%s" % (filename,)
else:
return "%s://%s/%s/%s/%s" % (
protocol,
project.subdomain,
lang,
version,
filename,
)
return "/%s/%s/%s" % (lang, version, filename,)
else:
if project.single_version:
return reverse('docs_detail', kwargs={
Expand All @@ -53,6 +42,10 @@ def redirect_filename(project, filename=None):


def get_redirect_url(project, path):
"""
Redirect the given path for the given project. Will always return absolute
paths, without domain.
"""
for project_redirect in project.redirects.all():
if project_redirect.redirect_type == 'prefix':
if path.startswith(project_redirect.from_url):
Expand Down Expand Up @@ -108,6 +101,9 @@ def get_redirect_response(request, full_path=None):

if project:
new_path = get_redirect_url(project, full_path)
if not new_path is not None:
if new_path is not None:
# Re-use the domain and protocol used in the current request.
# Redirects shouldn't change the domain, version or language.
new_path = request.build_absolute_uri(new_path)
return HttpResponseRedirect(new_path)
return None
13 changes: 13 additions & 0 deletions readthedocs/rtd_tests/tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ def test_redirect_page(self):
self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/latest/tutorial/install.html')

@override_settings(USE_SUBDOMAIN=True)
def test_redirect_recognizes_custom_cname(self):
Redirect.objects.create(
project=self.pip, redirect_type='page', from_url='/install.html',
to_url='/tutorial/install.html')
r = self.client.get('/install.html',
HTTP_HOST='pip.pypa.io',
HTTP_X_RTD_SLUG='pip')
self.assertEqual(r.status_code, 302)
self.assertEqual(
r['Location'],
'http://pip.pypa.io/en/latest/tutorial/install.html')

@override_settings(USE_SUBDOMAIN=True, PYTHON_MEDIA=True)
def test_redirect_html(self):
Redirect.objects.create(
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/rtd_tests/tests/test_redirects_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_redirects_no_subdomain(self):
def test_redirects_with_subdomain(self):
self.assertEqual(
redirect_filename(self.proj, 'faq.html'),
'http://read-the-docs.rtfd.org/en/latest/faq.html'
'/en/latest/faq.html'
)

@override_settings(
Expand All @@ -40,7 +40,7 @@ def test_single_version_with_subdomain(self):
self.proj.single_version = True
self.assertEqual(
redirect_filename(self.proj, 'faq.html'),
'http://read-the-docs.rtfd.org/faq.html'
'/faq.html'
)

def test_single_version_no_subdomain(self):
Expand Down

0 comments on commit 87f0297

Please sign in to comment.