
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
django-compression-middleware
Advanced tools
Django middleware to compress responses using several algorithms.
Supply Chain Security
Vulnerability
Quality
Maintenance
License
This middleware implements compressed content encoding for HTTP. It is similar
to Django's GZipMiddleware
(documentation
_), but additionally supports
other compression methods. It is meant to be a drop-in replacement for Django's
GZipMiddleware
. Its documentation — including security warnings — therefore
apply here as well.
The middleware is focussed on the task of compressing typical Django responses
such as HTML, JSON, etc. Both normal (bulk) and streaming responses are
supported. For static file compression, have a look at other projects such as
WhiteNoise
_.
Zstandard is a new method for compression with little client support so far.
Most browsers now support Brotli compression (check support status on Can I use... Brotli
_). The middleware will choose the best compression method
supported by the client as indicated in the request's Accept-Encoding
header. In order of preference:
Summary of the project status:
.. _documentation
: https://docs.djangoproject.com/en/dev/ref/middleware/#module-django.middleware.gzip
.. _WhiteNoise
: https://whitenoise.readthedocs.io/
.. _Can I use... Brotli
: http://caniuse.com/#search=brotli
The following requirements are supported and tested in all reasonable combinations:
.. code:: shell
pip install --upgrade django-compression-middleware
To apply compression to all the views served by Django, add
compression_middleware.middleware.CompressionMiddleware
to the
MIDDLEWARE
setting:
.. code:: python
MIDDLEWARE = [
# ...
'compression_middleware.middleware.CompressionMiddleware',
# ...
]
Remove GZipMiddleware
and BrotliMiddleware
if you used it before.
Consult the Django documentation on the correct ordering of middleware
_.
.. _ordering of middleware
: https://docs.djangoproject.com/en/dev/ref/middleware/#middleware-ordering
Alternatively you can decorate views individually to serve them with compression:
.. code:: python
from compression_middleware.decorators import compress_page
@compress_page
def index_view(request):
...
Note that your browser might not send the br
entry in the Accept-Encoding
header when you test without HTTPS (common on localhost). You can force it to
send the header, though. In Firefox, visit about:config
and set
network.http.accept-encoding
to indicate support. Note that you might
encounter some problems on the web with such a setting (which is why Brotli is
only supported on secure connections by default).
The code and tests in this project are based on Django's GZipMiddleware
and
Vašek Dohnal's django-brotli
. For compression, it uses the following modules
to bind to fast C modules:
zstandard
_ bindings. It supports both a C module (for CPython) and CFFI
which should be appropriate for PyPy. See the documentation for full details.Brotli
_ bindings or brotlipy
_. The latter is preferred on PyPy since
it is implemented using cffi. But both should work on both Python
implementations.gzip
_ module... _zstandard: https://pypi.org/project/zstandard/ .. _Brotli: https://pypi.org/project/Brotli/ .. _brotlipy: https://pypi.org/project/brotlipy/ .. _gzip: https://docs.python.org/3/library/gzip.html
Further readding on Wikipedia:
HTTP compression <https://en.wikipedia.org/wiki/HTTP_compression>
__Zstandard <http://www.zstd.net/>
__Brotli <https://en.wikipedia.org/wiki/Brotli>
__gzip <https://en.wikipedia.org/wiki/Gzip>
__git clone ...
)pip install --upgrade -r requirements_dev.txt
pytest
, and afterwards
preferably tox
to test the full test matrix. Consider installing as many
supported interpreters as possible (having them in your PATH
is often
sufficient).The MPL 2.0 License
Copyright (c) 2019-2023 Friedel Wolff <https://fwolff.net.za/>
_.
FAQs
Django middleware to compress responses using several algorithms.
We found that django-compression-middleware demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.