From ad8e60d3ed524141c58f0304cf0fc7d25d947e5d Mon Sep 17 00:00:00 2001 From: Helmut Irle Date: Tue, 22 Oct 2019 19:51:50 +0200 Subject: [PATCH 1/3] Added compress_exempt decorator, companion logic in middleware, and unit test. --- compression_middleware/decorators.py | 10 ++++++++++ compression_middleware/middleware.py | 8 +++++--- tests/test_decorator.py | 13 ++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/compression_middleware/decorators.py b/compression_middleware/decorators.py index 425359a..f636b38 100644 --- a/compression_middleware/decorators.py +++ b/compression_middleware/decorators.py @@ -1,6 +1,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +from functools import wraps __all__ = ['compress_page'] @@ -12,3 +13,12 @@ compress_page = decorator_from_middleware(CompressionMiddleware) compress_page.__doc__ = "Decorator to compress the view response if the client supports it." +def compress_exempt(view_func): + """Prevent a view from being compressed by the middleware.""" + @wraps(view_func) + def _wrapped_view_func(request, *args, **kwargs): + # request.compress_exempt = True + response = view_func(request, *args, **kwargs) + return response + + return _wrapped_view_func diff --git a/compression_middleware/middleware.py b/compression_middleware/middleware.py index 9ab8b6c..602b4f2 100644 --- a/compression_middleware/middleware.py +++ b/compression_middleware/middleware.py @@ -95,9 +95,11 @@ def process_response(self, request, response): # Test a few things before we even try: # - content is already encoded # - really short responses are not worth it - if \ - response.has_header('Content-Encoding') or \ - (not response.streaming and len(response.content) < MIN_LEN): + if ( + response.has_header('Content-Encoding') or + (not response.streaming and len(response.content) < MIN_LEN) or + getattr(request, "compress_exempt", False) is True + ): return response patch_vary_headers(response, ('Accept-Encoding',)) diff --git a/tests/test_decorator.py b/tests/test_decorator.py index ebad990..66df77b 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -6,7 +6,8 @@ from django.http import HttpRequest, HttpResponse, StreamingHttpResponse from django.test import RequestFactory, SimpleTestCase, TestCase -from compression_middleware.decorators import compress_page +from compression_middleware.decorators import compress_page, compress_exempt +from compression_middleware.middleware import MIN_LEN class CompressPageDecoratorTest(SimpleTestCase): @@ -49,6 +50,16 @@ def a_view(request): self.assertEqual(r.get('Content-Length'), str(len(r.content))) self.assertTrue(brotli.decompress(r.content), self.compressible_string) + def test_exempt_page(self): + + @compress_exempt + def exempt_view(request): + return self.resp + + r = exempt_view(self.req) + self.assertFalse(r.has_header('Content-Encoding')) + self.assertEqual(r.content, self.resp.content) + def test_streaming_page(self): @compress_page def a_streaming_view(request): From 2ae3935f88ee92bf4912383634d8fcfe28167e7b Mon Sep 17 00:00:00 2001 From: Helmut Irle Date: Tue, 22 Oct 2019 19:56:59 +0200 Subject: [PATCH 2/3] Added decorator to __all__. --- compression_middleware/decorators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compression_middleware/decorators.py b/compression_middleware/decorators.py index f636b38..717d5f0 100644 --- a/compression_middleware/decorators.py +++ b/compression_middleware/decorators.py @@ -1,10 +1,10 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -from functools import wraps -__all__ = ['compress_page'] +__all__ = ['compress_page', 'compress_exempt'] +from functools import wraps from .middleware import CompressionMiddleware from django.utils.decorators import decorator_from_middleware From 10609f935da05b955d40461840ba373b3a07de97 Mon Sep 17 00:00:00 2001 From: Helmut Irle Date: Tue, 22 Oct 2019 19:57:11 +0200 Subject: [PATCH 3/3] Removed unused imports. --- tests/test_decorator.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 66df77b..e3dea02 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -3,11 +3,10 @@ import brotli -from django.http import HttpRequest, HttpResponse, StreamingHttpResponse -from django.test import RequestFactory, SimpleTestCase, TestCase +from django.http import HttpResponse, StreamingHttpResponse +from django.test import RequestFactory, SimpleTestCase from compression_middleware.decorators import compress_page, compress_exempt -from compression_middleware.middleware import MIN_LEN class CompressPageDecoratorTest(SimpleTestCase):