Skip to content

Commit

Permalink
Fix Content-Length header to count bytes (#143)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Johnson <[email protected]>
  • Loading branch information
haydngreatnews and adamchainz authored Sep 4, 2023
1 parent 6803307 commit af8f45f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/adamchainz/django-minify-html/pull/143>`__.

1.6.0 (2023-06-14)
------------------

Expand Down
2 changes: 1 addition & 1 deletion src/django_minify_html/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from django_minify_html.decorators import no_html_minification

basic_html = b"<!doctype html><html><body><p>Hi</p></body></html>"
basic_html_minified = b"<!doctypehtml><body><p>Hi"
basic_html = "<!doctype html><html><body><p>Hi 👋</p></body></html>".encode()
basic_html_minified = "<!doctypehtml><body><p>Hi 👋".encode()


def streaming(request):
Expand Down

0 comments on commit af8f45f

Please sign in to comment.