Skip to content

Redirect index files in proxito instead of serving #6387

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 5 commits into from
Nov 19, 2019
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
38 changes: 35 additions & 3 deletions readthedocs/proxito/tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,49 @@ def test_custom_robots_txt(self, storage_mock):
def test_directory_indexes(self, storage_mock):
self.project.versions.update(active=True, built=True)
storage_mock()().exists.return_value = True
storage_mock()().open().read.return_value = 'foo'
# Confirm we've serving from storage for the `index-exists/index.html` file
response = self.client.get(
reverse('serve_error_404', kwargs={'proxito_path': '/en/latest/index-exists'}),
HTTP_HOST='project.readthedocs.io',
)
self.assertEqual(
response.content, b'foo'
response.status_code, 302
)
self.assertEqual(
response.status_code, 200
response['location'], '/en/latest/index-exists/',
)

@mock.patch('readthedocs.proxito.views.get_storage_class')
def test_directory_indexes_readme_serving(self, storage_mock):
self.project.versions.update(active=True, built=True)

storage_mock()().exists.side_effect = [False, True]
# Confirm we've serving from storage for the `index-exists/index.html` file
response = self.client.get(
reverse('serve_error_404', kwargs={'proxito_path': '/en/latest/readme-exists'}),
HTTP_HOST='project.readthedocs.io',
)
self.assertEqual(
response.status_code, 302
)
self.assertEqual(
response['location'], '/en/latest/readme-exists/README.html',
)

@mock.patch('readthedocs.proxito.views.get_storage_class')
def test_directory_indexes_get_args(self, storage_mock):
self.project.versions.update(active=True, built=True)
storage_mock()().exists.return_value = True
# Confirm we've serving from storage for the `index-exists/index.html` file
response = self.client.get(
reverse('serve_error_404', kwargs={'proxito_path': '/en/latest/index-exists?foo=bar'}),
HTTP_HOST='project.readthedocs.io',
)
self.assertEqual(
response.status_code, 302
)
self.assertEqual(
response['location'], '/en/latest/index-exists/?foo=bar',
)

@mock.patch('readthedocs.proxito.views.get_storage_class')
Expand Down
11 changes: 9 additions & 2 deletions readthedocs/proxito/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,19 @@ def serve_error_404(request, proxito_path, template_name='404.html'):
)
if storage.exists(storage_filename_path):
log.info(
'Serving index file: project=%s version=%s, url=%s',
'Redirecting to index file: project=%s version=%s, url=%s',
final_project.slug,
version_slug,
storage_filename_path,
)
resp = HttpResponse(storage.open(storage_filename_path).read())
# Use urlparse so that we maintain GET args in our redirect
parts = urlparse(proxito_path)
if tryfile == 'README.html':
new_path = os.path.join(parts.path, tryfile)
else:
new_path = parts.path + '/'
new_parts = parts._replace(path=new_path)
resp = HttpResponseRedirect(new_parts.geturl())
return resp

# If that doesn't work, attempt to serve the 404 of the current version (version_slug)
Expand Down