From b035c86b3127ba5fe8f035c83e464652dcdeef8a Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Sun, 10 Aug 2025 11:18:12 +0200 Subject: [PATCH 1/2] http.response.start goes AFTER http.response.body --- src/suou/sass.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/suou/sass.py b/src/suou/sass.py index d96621a..21dfc7c 100644 --- a/src/suou/sass.py +++ b/src/suou/sass.py @@ -11,7 +11,7 @@ from sass import CompileError from sassutils.builder import Manifest from importlib.metadata import version as _get_version -from .codecs import quote_css_string +from .codecs import quote_css_string, want_bytes from .validators import must_be from .asgi import _MiddlewareFactory, ASGIApp, ASGIReceive, ASGIScope, ASGISend from . import __version__ as _suou_version @@ -83,13 +83,6 @@ class SassAsyncMiddleware(_MiddlewareFactory): break except CompileError as e: logger.error(str(e)) - await send({ - 'type': 'http.response.start', - 'status': self.error_status, - 'headers': [ - (b'Content-Type', b'text/css; charset=utf-8'), - ] - }) await send({ 'type': 'http.response.body', 'body': '\n'.join([ @@ -110,6 +103,13 @@ class SassAsyncMiddleware(_MiddlewareFactory): '}' ]).encode('utf-8') }) + await send({ + 'type': 'http.response.start', + 'status': self.error_status, + 'headers': [ + (b'Content-Type', b'text/css; charset=utf-8'), + ] + }) async def _read_file(path): with open(path, 'rb') as f: @@ -120,22 +120,19 @@ class SassAsyncMiddleware(_MiddlewareFactory): else: break - resp_body = b'' async for chunk in _read_file(os.path.join(package_dir, result)): - resp_body += chunk + await send({ + 'type': 'http.response.body', + 'body': chunk + }) await send({ 'type': 'http.response.start', 'status': 200, 'headers': [ - (b'Content-Type', b'text/css; charset=utf-8'), + (b'Content-Type', b'text/css; charset=utf-8') ] }) - - await send({ - 'type': 'http.response.body', - 'body': resp_body - }) await self.app(scope, receive, send) From 13589ab8195761dac99b4368ff22611d039deb7b Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Sun, 10 Aug 2025 11:35:36 +0200 Subject: [PATCH 2/2] add Content-Length header --- src/suou/sass.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/suou/sass.py b/src/suou/sass.py index d96621a..63442d4 100644 --- a/src/suou/sass.py +++ b/src/suou/sass.py @@ -11,7 +11,7 @@ from sass import CompileError from sassutils.builder import Manifest from importlib.metadata import version as _get_version -from .codecs import quote_css_string +from .codecs import quote_css_string, want_bytes from .validators import must_be from .asgi import _MiddlewareFactory, ASGIApp, ASGIReceive, ASGIScope, ASGISend from . import __version__ as _suou_version @@ -83,32 +83,35 @@ class SassAsyncMiddleware(_MiddlewareFactory): break except CompileError as e: logger.error(str(e)) + resp_body = '\n'.join([ + '/*', + str(e), + '***', + f'libsass {_libsass_version} + suou {_suou_version} {datetime.datetime.now().isoformat()}', + '*/', + '', + 'body::before {', + f' content: {quote_css_string(str(e))};', + ' color: maroon;', + ' background-color: white;', + ' white-space: pre-wrap;', + ' display: block;', + ' font-family: monospace;', + ' user-select: text;' + '}' + ]).encode('utf-8') + await send({ 'type': 'http.response.start', 'status': self.error_status, 'headers': [ (b'Content-Type', b'text/css; charset=utf-8'), + (b'Content-Length', want_bytes(f'{len(resp_body)}')) ] }) await send({ 'type': 'http.response.body', - 'body': '\n'.join([ - '/*', - str(e), - '***', - f'libsass {_libsass_version} + suou {_suou_version} {datetime.datetime.now().isoformat()}', - '*/', - '', - 'body::before {', - f' content: {quote_css_string(str(e))};', - ' color: maroon;', - ' background-color: white;', - ' white-space: pre-wrap;', - ' display: block;', - ' font-family: monospace;', - ' user-select: text;' - '}' - ]).encode('utf-8') + 'body': resp_body }) async def _read_file(path): @@ -129,6 +132,7 @@ class SassAsyncMiddleware(_MiddlewareFactory): 'status': 200, 'headers': [ (b'Content-Type', b'text/css; charset=utf-8'), + (b'Content-Length', want_bytes(f'{len(resp_body)}')) ] })