-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1180 from jefer94/development
Development
- Loading branch information
Showing
5 changed files
with
99 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import functools | ||
import gzip | ||
from io import BytesIO | ||
import os | ||
import sys | ||
from django.utils.deprecation import MiddlewareMixin | ||
|
||
IS_TEST = os.getenv('ENV', '') not in ['production', 'staging', 'development'] | ||
ENABLE_LIST_OPTIONS = ['true', '1', 'yes', 'y'] | ||
|
||
|
||
@functools.lru_cache(maxsize=1) | ||
def is_compression_enabled(): | ||
return os.getenv('COMPRESSION', '1').lower() in ENABLE_LIST_OPTIONS | ||
|
||
|
||
@functools.lru_cache(maxsize=1) | ||
def min_compression_size(): | ||
return int(os.getenv('MIN_COMPRESSION_SIZE', '10')) | ||
|
||
|
||
def must_compress(data): | ||
size = min_compression_size() | ||
if size == 0: | ||
return True | ||
|
||
return sys.getsizeof(data) / 1024 > size | ||
|
||
|
||
class CompressResponseMiddleware(MiddlewareMixin): | ||
|
||
def process_response(self, request, response): | ||
# If the response is already compressed, do nothing | ||
if 'Content-Encoding' in response.headers or is_compression_enabled() is False or must_compress( | ||
response.content) is False or IS_TEST: | ||
return response | ||
|
||
# Compress the response if it's large enough | ||
if response.content: | ||
accept_encoding = request.META.get('HTTP_ACCEPT_ENCODING', '') | ||
if 'gzip' in accept_encoding: | ||
buffer = BytesIO() | ||
with gzip.GzipFile(fileobj=buffer, mode='wb') as f: | ||
f.write(response.content) | ||
compressed_content = buffer.getvalue() | ||
|
||
response.content = compressed_content | ||
response['Content-Encoding'] = 'gzip' | ||
response['Content-Length'] = str(len(compressed_content)) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters