Skip to content

Commit

Permalink
Attempt to guess the MIME type of content served with xsendfile.
Browse files Browse the repository at this point in the history
This is mostly for the benefit of browsers, since the default MIME
type Django uses is text/html which causes the browser to attempt to
render RPMs and such instead of downloading them.

closes #1627
  • Loading branch information
jeremycline committed Feb 8, 2016
1 parent ea5a199 commit 2b1b2ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
9 changes: 8 additions & 1 deletion server/pulp/server/content/web/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mimetypes
import os

from django.http import \
Expand Down Expand Up @@ -68,7 +69,13 @@ def x_send(path):
:rtype: django.http.HttpResponse
"""
if os.access(path, os.R_OK):
reply = HttpResponse()
content_type = mimetypes.guess_type(path)[0]
# If the content type can't be detected by mimetypes, send it as arbitrary
# binary data. See https://tools.ietf.org/html/rfc2046#section-4.5.1 for
# more information.
if content_type is None:
content_type = 'application/octet-stream'
reply = HttpResponse(content_type=content_type)
reply['X-SENDFILE'] = path
else:
reply = HttpResponseForbidden()
Expand Down
18 changes: 12 additions & 6 deletions server/test/unit/server/content/web/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,22 @@ def test_urljoin(self):
self.assertEqual(joined, 'http://redhat.com:123http://host/my/path/?age=10')

@patch('os.access')
@patch(MODULE + '.HttpResponse')
def test_x_send(self, response, access):
def test_x_send(self, access):
path = '/my/path'
response.return_value = {}
access.return_value = True
reply = ContentView.x_send(path)
access.assert_called_once_with(path, os.R_OK)
response.assert_called_once_with()
self.assertEqual(response.return_value['X-SENDFILE'], path)
self.assertEqual(reply, response.return_value)
self.assertEqual(reply['X-SENDFILE'], path)
self.assertEqual(reply['Content-Type'], 'application/octet-stream')

@patch('os.access')
def test_x_send_mime_type(self, access):
path = '/my/path.rpm'
access.return_value = True
reply = ContentView.x_send(path)
access.assert_called_once_with(path, os.R_OK)
self.assertEqual(reply['X-SENDFILE'], path)
self.assertEqual(reply['Content-Type'], 'application/x-rpm')

@patch('os.access')
@patch(MODULE + '.HttpResponseForbidden')
Expand Down

0 comments on commit 2b1b2ae

Please sign in to comment.