Skip to content

Commit 2a83be0

Browse files
authored
Merge pull request #6387 from readthedocs/proxito-redirect
Redirect index files in proxito instead of serving
2 parents c9e9070 + 7d1eaaf commit 2a83be0

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

readthedocs/proxito/tests/test_full.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,49 @@ def test_custom_robots_txt(self, storage_mock):
189189
def test_directory_indexes(self, storage_mock):
190190
self.project.versions.update(active=True, built=True)
191191
storage_mock()().exists.return_value = True
192-
storage_mock()().open().read.return_value = 'foo'
193192
# Confirm we've serving from storage for the `index-exists/index.html` file
194193
response = self.client.get(
195194
reverse('serve_error_404', kwargs={'proxito_path': '/en/latest/index-exists'}),
196195
HTTP_HOST='project.readthedocs.io',
197196
)
198197
self.assertEqual(
199-
response.content, b'foo'
198+
response.status_code, 302
200199
)
201200
self.assertEqual(
202-
response.status_code, 200
201+
response['location'], '/en/latest/index-exists/',
202+
)
203+
204+
@mock.patch('readthedocs.proxito.views.get_storage_class')
205+
def test_directory_indexes_readme_serving(self, storage_mock):
206+
self.project.versions.update(active=True, built=True)
207+
208+
storage_mock()().exists.side_effect = [False, True]
209+
# Confirm we've serving from storage for the `index-exists/index.html` file
210+
response = self.client.get(
211+
reverse('serve_error_404', kwargs={'proxito_path': '/en/latest/readme-exists'}),
212+
HTTP_HOST='project.readthedocs.io',
213+
)
214+
self.assertEqual(
215+
response.status_code, 302
216+
)
217+
self.assertEqual(
218+
response['location'], '/en/latest/readme-exists/README.html',
219+
)
220+
221+
@mock.patch('readthedocs.proxito.views.get_storage_class')
222+
def test_directory_indexes_get_args(self, storage_mock):
223+
self.project.versions.update(active=True, built=True)
224+
storage_mock()().exists.return_value = True
225+
# Confirm we've serving from storage for the `index-exists/index.html` file
226+
response = self.client.get(
227+
reverse('serve_error_404', kwargs={'proxito_path': '/en/latest/index-exists?foo=bar'}),
228+
HTTP_HOST='project.readthedocs.io',
229+
)
230+
self.assertEqual(
231+
response.status_code, 302
232+
)
233+
self.assertEqual(
234+
response['location'], '/en/latest/index-exists/?foo=bar',
203235
)
204236

205237
@mock.patch('readthedocs.proxito.views.get_storage_class')

readthedocs/proxito/views.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,19 @@ def serve_error_404(request, proxito_path, template_name='404.html'):
385385
)
386386
if storage.exists(storage_filename_path):
387387
log.info(
388-
'Serving index file: project=%s version=%s, url=%s',
388+
'Redirecting to index file: project=%s version=%s, url=%s',
389389
final_project.slug,
390390
version_slug,
391391
storage_filename_path,
392392
)
393-
resp = HttpResponse(storage.open(storage_filename_path).read())
393+
# Use urlparse so that we maintain GET args in our redirect
394+
parts = urlparse(proxito_path)
395+
if tryfile == 'README.html':
396+
new_path = os.path.join(parts.path, tryfile)
397+
else:
398+
new_path = parts.path + '/'
399+
new_parts = parts._replace(path=new_path)
400+
resp = HttpResponseRedirect(new_parts.geturl())
394401
return resp
395402

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

0 commit comments

Comments
 (0)