Skip to content

Commit d4f504e

Browse files
authored
Merge pull request johnsensible#54 from Proper-Job/url-quote-fix
Added url quoting to X-Accel-Redirect and Location header
2 parents 32834e3 + 14f13b0 commit d4f504e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

sendfile/backends/_internalredirect.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
from django.conf import settings
21
import os.path
32

3+
from django.conf import settings
4+
from django.utils.encoding import smart_text, smart_bytes
5+
6+
try:
7+
from urllib.parse import quote
8+
except ImportError:
9+
from urllib import quote
10+
411

512
def _convert_file_to_url(filename):
613
relpath = os.path.relpath(filename, settings.SENDFILE_ROOT)
@@ -11,4 +18,7 @@ def _convert_file_to_url(filename):
1118
relpath, head = os.path.split(relpath)
1219
url.insert(1, head)
1320

14-
return u'/'.join(url)
21+
# Python3 urllib.parse.quote accepts both unicode and bytes, while Python2 urllib.quote only accepts bytes.
22+
# So use bytes for quoting and then go back to unicode.
23+
url = [smart_bytes(url_component) for url_component in url]
24+
return smart_text(quote(b'/'.join(url)))

sendfile/tests.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
import shutil
1010
from sendfile import sendfile as real_sendfile, _get_sendfile
1111

12+
try:
13+
from urllib.parse import unquote
14+
except ImportError:
15+
from urllib import unquote
16+
1217

1318
def sendfile(request, filename, **kwargs):
1419
# just a simple response with the filename
@@ -127,4 +132,26 @@ def test_xaccelredirect_header_containing_unicode(self):
127132
filepath = self.ensure_file(u'péter_là_gueule.txt')
128133
response = real_sendfile(HttpRequest(), filepath)
129134
self.assertTrue(response is not None)
130-
self.assertEqual(u'/private/péter_là_gueule.txt'.encode('utf-8'), response['X-Accel-Redirect'])
135+
self.assertEqual(u'/private/péter_là_gueule.txt'.encode('utf-8'), unquote(response['X-Accel-Redirect']))
136+
137+
138+
class TestModWsgiBackend(TempFileTestCase):
139+
140+
def setUp(self):
141+
super(TestModWsgiBackend, self).setUp()
142+
settings.SENDFILE_BACKEND = 'sendfile.backends.mod_wsgi'
143+
settings.SENDFILE_ROOT = self.TEMP_FILE_ROOT
144+
settings.SENDFILE_URL = '/private'
145+
_get_sendfile.clear()
146+
147+
def test_correct_url_in_location_header(self):
148+
filepath = self.ensure_file('readme.txt')
149+
response = real_sendfile(HttpRequest(), filepath)
150+
self.assertTrue(response is not None)
151+
self.assertEqual('/private/readme.txt', response['Location'])
152+
153+
def test_location_header_containing_unicode(self):
154+
filepath = self.ensure_file(u'péter_là_gueule.txt')
155+
response = real_sendfile(HttpRequest(), filepath)
156+
self.assertTrue(response is not None)
157+
self.assertEqual(u'/private/péter_là_gueule.txt'.encode('utf-8'), unquote(response['Location']))

0 commit comments

Comments
 (0)