Skip to content

Commit 4488e52

Browse files
committed
backends-nginx: Add support for Nginx versions older than 1.5.9.
In this commit we are adding support for Nginx version older than 1.5.9. This is achieved through conditionally quoting the URL's in X-Accel-Redirect headers according to compatibility with Nginx. Since newer versions of Nginx expect URL's in X-Accel-Redirect to be quoted unlike the versions older that Nginx 1.5.9. From this commit onwards we start to expect a 'NGINX_VERSION' config setting to be setup in the django settings in order to facilitate decision making regarding quoting URL's. If such a setting is not found we just assume Nginx version to be greater then 1.5.9 and make decision accordingly and then cache the decision by making use of a decorator. Using this result we decide whether the outgoing URL should be quoted or not. Fixes: #56, #58. Closes #45.
1 parent b99f963 commit 4488e52

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

sendfile/backends/_internalredirect.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,36 @@
22

33
from django.conf import settings
44
from django.utils.encoding import smart_text, smart_bytes
5+
from django.conf import settings
56

67
try:
78
from urllib.parse import quote
89
except ImportError:
910
from urllib import quote
1011

1112

13+
def _decision(fn):
14+
_cached_decision = []
15+
def _decorated():
16+
if not _cached_decision:
17+
_cached_decision.append(fn())
18+
return _cached_decision[0]
19+
return _decorated
20+
21+
@_decision
22+
def should_be_quoted():
23+
backend = getattr(settings, 'SENDFILE_BACKEND', None)
24+
if backend == 'sendfile.backends.nginx':
25+
nginx_version = getattr(settings, 'NGINX_VERSION', None)
26+
if nginx_version:
27+
nginx_version = map(int, nginx_version.split('.'))
28+
# Since Starting with Nginx 1.5.9, quoted url's are expected to be
29+
# sent with X-Accel-Redirect headers, we will not quote url's for
30+
# versions of Nginx before 1.5.9
31+
if nginx_version < [1, 5, 9]:
32+
return False
33+
return True
34+
1235
def _convert_file_to_url(filename):
1336
relpath = os.path.relpath(filename, settings.SENDFILE_ROOT)
1437

@@ -21,4 +44,7 @@ def _convert_file_to_url(filename):
2144
# Python3 urllib.parse.quote accepts both unicode and bytes, while Python2 urllib.quote only accepts bytes.
2245
# So use bytes for quoting and then go back to unicode.
2346
url = [smart_bytes(url_component) for url_component in url]
24-
return smart_text(quote(b'/'.join(url)))
47+
if should_be_quoted():
48+
return smart_text(quote(b'/'.join(url)))
49+
else:
50+
return smart_text(b'/'.join(url))

0 commit comments

Comments
 (0)