Skip to content

Commit

Permalink
Proxito redirects: pass full_path instead of re-creating it. (#9557)
Browse files Browse the repository at this point in the history
* Pass full_path to exact redirect

* Don't re-create full_path, we already have it.

* Fix submodule

* Remove debug logging

* Be more defensive & add test

* Fix lint

* Remove storage exists that isn't used

* Raise Http404 in fast_404 so that we still process redirects..

* Mark test xfail for now, and revert breaking test change
  • Loading branch information
ericholscher authored Aug 29, 2022
1 parent fec34b6 commit b9defe5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
22 changes: 21 additions & 1 deletion readthedocs/proxito/tests/test_old_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,27 @@ def test_redirect_exact(self):
'http://project.dev.readthedocs.io/en/latest/tutorial/install.html',
)

@pytest.mark.xfail(
reason="This is hitting fast_404 and not triggering the nginx handler in testing. It works in prod."
)
def test_redirect_exact_looks_like_version(self):
fixture.get(
Redirect,
project=self.project,
redirect_type="exact",
from_url="/en/versions.json",
to_url="/en/latest/versions.json",
)
r = self.client.get(
"/en/versions.json",
HTTP_HOST="project.dev.readthedocs.io",
)
self.assertEqual(r.status_code, 302)
self.assertEqual(
r["Location"],
"http://project.dev.readthedocs.io/en/latest/versions.json",
)

def test_redirect_exact_with_rest(self):
"""
Exact redirects can have a ``$rest`` in the ``from_url``.
Expand Down Expand Up @@ -914,7 +935,6 @@ def test_redirect_with_301_status(self):
"http://project.dev.readthedocs.io/en/latest/tutorial/install.html",
)


@override_settings(
PYTHON_MEDIA=True,
PUBLIC_DOMAIN="dev.readthedocs.io",
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/proxito/views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def fast_404(request, *args, **kwargs):
This stops us from running RTD logic in our error handling. We already do
this in RTD prod when we fallback to it.
"""
return HttpResponse('Not Found.', status=404)
return HttpResponse("Not Found.", status=404)


def proxito_404_page_handler(request, exception=None, template_name='404.html'):
Expand Down
26 changes: 13 additions & 13 deletions readthedocs/redirects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,18 @@ def get_full_path(self, filename, language=None, version_slug=None, allow_crossd
filename=filename,
)

def get_redirect_path(self, path, language=None, version_slug=None):
def get_redirect_path(self, path, full_path=None, language=None, version_slug=None):
method = getattr(
self,
'redirect_{type}'.format(
type=self.redirect_type,
),
)
return method(path, language=language, version_slug=version_slug)
return method(
path, full_path=full_path, language=language, version_slug=version_slug
)

def redirect_prefix(self, path, language=None, version_slug=None):
def redirect_prefix(self, path, full_path, language=None, version_slug=None):
if path.startswith(self.from_url):
log.debug("Redirecting...", redirect=self)
# pep8 and blank don't agree on having a space before :.
Expand All @@ -189,7 +191,7 @@ def redirect_prefix(self, path, language=None, version_slug=None):
)
return to

def redirect_page(self, path, language=None, version_slug=None):
def redirect_page(self, path, full_path, language=None, version_slug=None):
if path == self.from_url:
log.debug('Redirecting...', redirect=self)
to = self.get_full_path(
Expand All @@ -200,11 +202,7 @@ def redirect_page(self, path, language=None, version_slug=None):
)
return to

def redirect_exact(self, path, language=None, version_slug=None):
full_path = path
if language and version_slug:
# reconstruct the full path for an exact redirect
full_path = self.get_full_path(path, language, version_slug, allow_crossdomain=False)
def redirect_exact(self, path, full_path, language=None, version_slug=None):
if full_path == self.from_url:
log.debug('Redirecting...', redirect=self)
return self.to_url
Expand All @@ -215,7 +213,7 @@ def redirect_exact(self, path, language=None, version_slug=None):
cut_path = full_path.replace(match, self.to_url, 1)
return cut_path

def redirect_sphinx_html(self, path, language=None, version_slug=None):
def redirect_sphinx_html(self, path, full_path, language=None, version_slug=None):
for ending in ['/', '/index.html']:
if path.endswith(ending):
log.debug('Redirecting...', redirect=self)
Expand All @@ -228,9 +226,11 @@ def redirect_sphinx_html(self, path, language=None, version_slug=None):
allow_crossdomain=False,
)

def redirect_sphinx_htmldir(self, path, language=None, version_slug=None):
if path.endswith('.html'):
log.debug('Redirecting...', redirect=self)
def redirect_sphinx_htmldir(
self, path, full_path, language=None, version_slug=None
):
if path.endswith(".html"):
log.debug("Redirecting...", redirect=self)
path = path[1:] # Strip leading slash.
to = re.sub('.html$', '/', path)
return self.get_full_path(
Expand Down
1 change: 1 addition & 0 deletions readthedocs/redirects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def get_redirect_path_with_status(
for redirect in queryset.select_related('project'):
new_path = redirect.get_redirect_path(
path=normalized_path,
full_path=normalized_full_path,
language=language,
version_slug=version_slug,
)
Expand Down

0 comments on commit b9defe5

Please sign in to comment.