From af8f45f01ac025f318e78cda6005982870dd5247 Mon Sep 17 00:00:00 2001 From: Haydn Greatnews Date: Mon, 4 Sep 2023 18:56:33 +1200 Subject: [PATCH] Fix Content-Length header to count bytes (#143) Co-authored-by: Adam Johnson --- CHANGELOG.rst | 4 ++++ src/django_minify_html/middleware.py | 2 +- tests/views.py | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 366217c..6c315c5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ Changelog ========= +* Fix the value in the ``Content-Length`` header to correctly count bytes, rather than unicode characters. + + Thanks to Haydn Greatnews in `PR #143 `__. + 1.6.0 (2023-06-14) ------------------ diff --git a/src/django_minify_html/middleware.py b/src/django_minify_html/middleware.py index b3af2fd..26b21de 100644 --- a/src/django_minify_html/middleware.py +++ b/src/django_minify_html/middleware.py @@ -55,7 +55,7 @@ def maybe_minify(self, request: HttpRequest, response: HttpResponseBase) -> None minified_content = minify_html.minify(content, **self.minify_args) response.content = minified_content if "Content-Length" in response: - response["Content-Length"] = len(minified_content) + response["Content-Length"] = len(response.content) minify_args = { "minify_css": True, diff --git a/tests/views.py b/tests/views.py index ecd3cdb..5eed8f2 100644 --- a/tests/views.py +++ b/tests/views.py @@ -5,8 +5,8 @@ from django_minify_html.decorators import no_html_minification -basic_html = b"

Hi

" -basic_html_minified = b"

Hi" +basic_html = "

Hi 👋

".encode() +basic_html_minified = "

Hi 👋".encode() def streaming(request):