Skip to content

Commit 1b294f0

Browse files
authored
Merge pull request #212 from sevdog/xaccel-headers
Allow XResponses to keep original headers provided to base response
2 parents 71488c4 + 51deef0 commit 1b294f0

17 files changed

Lines changed: 130 additions & 10 deletions

File tree

demo/demoproject/apache/tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ def test_response(self):
4343
basename="hello-world.txt",
4444
file_path="/apache-optimized-by-decorator/hello-world.txt",
4545
)
46+
47+
48+
class ModifiedHeadersTestCase(django.test.TestCase):
49+
def test_response(self):
50+
"""'apache:modified_headers' returns X-Sendfile response."""
51+
setup_file()
52+
url = reverse("apache:modified_headers")
53+
response = self.client.get(url)
54+
assert_x_sendfile(
55+
self,
56+
response,
57+
content_type="text/plain; charset=utf-8",
58+
basename="hello-world.txt",
59+
file_path="/apache-modified-headers/hello-world.txt",
60+
)
61+
self.assertEqual(response['X-Test'], 'header')

demo/demoproject/apache/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@
1515
views.optimized_by_decorator,
1616
name="optimized_by_decorator",
1717
),
18+
path(
19+
"modified_headers/",
20+
views.modified_headers,
21+
name="modified_headers",
22+
),
1823
]

demo/demoproject/apache/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@
2222
source_url=storage.base_url,
2323
destination_dir="/apache-optimized-by-decorator/",
2424
)
25+
26+
27+
def _modified_headers(request):
28+
view = StorageDownloadView.as_view(storage=storage, path="hello-world.txt")
29+
response = view(request)
30+
response["X-Test"] = 'header'
31+
return response
32+
33+
34+
modified_headers = x_sendfile(
35+
_modified_headers,
36+
source_url=storage.base_url,
37+
destination_dir="/apache-modified-headers/",
38+
)

demo/demoproject/lighttpd/tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ def test_response(self):
4343
basename="hello-world.txt",
4444
file_path="/lighttpd-optimized-by-decorator/hello-world.txt",
4545
)
46+
47+
48+
class ModifiedHeadersTestCase(django.test.TestCase):
49+
def test_response(self):
50+
"""'lighttpd:modified_headers' returns X-Sendfile response."""
51+
setup_file()
52+
url = reverse("lighttpd:modified_headers")
53+
response = self.client.get(url)
54+
assert_x_sendfile(
55+
self,
56+
response,
57+
content_type="text/plain; charset=utf-8",
58+
basename="hello-world.txt",
59+
file_path="/lighttpd-modified-headers/hello-world.txt",
60+
)
61+
self.assertEqual(response['X-Test'], 'header')

demo/demoproject/lighttpd/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@
1515
views.optimized_by_decorator,
1616
name="optimized_by_decorator",
1717
),
18+
path(
19+
"modified_headers/",
20+
views.modified_headers,
21+
name="modified_headers",
22+
),
1823
]

demo/demoproject/lighttpd/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@
2222
source_url=storage.base_url,
2323
destination_dir="/lighttpd-optimized-by-decorator/",
2424
)
25+
26+
27+
def _modified_headers(request):
28+
view = StorageDownloadView.as_view(storage=storage, path="hello-world.txt")
29+
response = view(request)
30+
response["X-Test"] = 'header'
31+
return response
32+
33+
34+
modified_headers = x_sendfile(
35+
_modified_headers,
36+
source_url=storage.base_url,
37+
destination_dir="/lighttpd-modified-headers/",
38+
)

demo/demoproject/nginx/tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,23 @@ def test_response(self):
5151
with_buffering=None,
5252
limit_rate=None,
5353
)
54+
55+
56+
class ModifiedHeadersTestCase(django.test.TestCase):
57+
def test_response(self):
58+
"""'nginx:modified_headers' returns X-Sendfile response."""
59+
setup_file()
60+
url = reverse("nginx:modified_headers")
61+
response = self.client.get(url)
62+
assert_x_accel_redirect(
63+
self,
64+
response,
65+
content_type="text/plain; charset=utf-8",
66+
charset="utf-8",
67+
basename="hello-world.txt",
68+
redirect_url="/nginx-modified-headers/hello-world.txt",
69+
expires=None,
70+
with_buffering=None,
71+
limit_rate=None,
72+
)
73+
self.assertEqual(response['X-Test'], 'header')

demo/demoproject/nginx/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
views.optimized_by_decorator,
1717
name="optimized_by_decorator",
1818
),
19+
path(
20+
"modified_headers/",
21+
views.modified_headers,
22+
name="modified_headers",
23+
),
1924
]

demo/demoproject/nginx/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@
2222
source_url=storage.base_url,
2323
destination_url="/nginx-optimized-by-decorator/",
2424
)
25+
26+
27+
def _modified_headers(request):
28+
view = StorageDownloadView.as_view(storage=storage, path="hello-world.txt")
29+
response = view(request)
30+
response["X-Test"] = 'header'
31+
return response
32+
33+
34+
modified_headers = x_accel_redirect(
35+
_modified_headers,
36+
source_url=storage.base_url,
37+
destination_url="/nginx-modified-headers/",
38+
)

django_downloadview/apache/middlewares.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ def process_download_response(self, request, response):
3232
content_type=response["Content-Type"],
3333
basename=response.basename,
3434
attachment=response.attachment,
35+
headers=response.headers,
3536
)

0 commit comments

Comments
 (0)