Skip to content

Commit

Permalink
Merge pull request #6609 from readthedocs/handle-infinite-redirect-404
Browse files Browse the repository at this point in the history
Raise exception when we get an InfiniteRedirect
  • Loading branch information
ericholscher authored Jan 29, 2020
2 parents e47e9d9 + 04af76d commit 3481c80
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion readthedocs/proxito/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from django.utils.encoding import iri_to_uri
from django.views.static import serve

from readthedocs.redirects.exceptions import InfiniteRedirectException

log = logging.getLogger(__name__) # noqa


Expand Down Expand Up @@ -167,7 +169,7 @@ def get_redirect_response(self, request, redirect_path, proxito_path, http_statu
'Infinite Redirect: FROM URL is the same than TO URL. url=%s',
new_path,
)
return HttpResponse('Infinite Redirect.', status=404)
raise InfiniteRedirectException()

if http_status and http_status == 301:
return HttpResponsePermanentRedirect(new_path)
Expand Down
7 changes: 6 additions & 1 deletion readthedocs/proxito/views/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.projects import constants
from readthedocs.projects.templatetags.projects_tags import sort_version_aware
from readthedocs.redirects.exceptions import InfiniteRedirectException

from .mixins import ServeDocsMixin, ServeRedirectMixin

Expand Down Expand Up @@ -243,7 +244,11 @@ def get(self, request, proxito_path, template_name='404.html'):
full_path=proxito_path,
)
if redirect_path and http_status:
return self.get_redirect_response(request, redirect_path, proxito_path, http_status)
try:
return self.get_redirect_response(request, redirect_path, proxito_path, http_status)
except InfiniteRedirectException:
# Continue with our normal 404 handling in this case
pass

# If that doesn't work, attempt to serve the 404 of the current version (version_slug)
# Secondly, try to serve the 404 page for the default version
Expand Down
4 changes: 4 additions & 0 deletions readthedocs/redirects/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class InfiniteRedirectException(Exception):
"""
Exception raised when a redirect loops forever
"""

0 comments on commit 3481c80

Please sign in to comment.