From eb33597bcddc51633ff9630508b2095667e34bbb Mon Sep 17 00:00:00 2001 From: Roimar Rafael Urbano Date: Wed, 18 Dec 2024 19:23:42 -0500 Subject: [PATCH 1/2] The error 'Parse Error: Invalid character in chunk size' occurs because both response.content_length and 'Transfer-Encoding': 'chunked' are being set in the server response. This creates a conflict, as Transfer-Encoding: chunked does not require a Content-Length. By removing response.content_length = len(image_data), this conflict is avoided, and the error is resolved. --- navigator/actions/zammad.py | 1 - 1 file changed, 1 deletion(-) diff --git a/navigator/actions/zammad.py b/navigator/actions/zammad.py index 83c2311..03ed389 100644 --- a/navigator/actions/zammad.py +++ b/navigator/actions/zammad.py @@ -441,7 +441,6 @@ async def get_attachment_img(self, attachment: str, request: Request): 'Expires': expiring_date.strftime('%a, %d %b %Y %H:%M:%S GMT'), } ) - response.content_length = len(image_data) await response.prepare(request) await response.write(image_data) await response.write_eof() From 9fff4c02374ad032d585164493a9b6ad548ec3b9 Mon Sep 17 00:00:00 2001 From: Roimar Rafael Urbano Date: Wed, 18 Dec 2024 20:58:22 -0500 Subject: [PATCH 2/2] Added file scaling system with Transfer-Encoding: chunked and resolved the conflict by removing the use of content_length in the response. --- navigator/actions/zammad.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/navigator/actions/zammad.py b/navigator/actions/zammad.py index 03ed389..10809b4 100644 --- a/navigator/actions/zammad.py +++ b/navigator/actions/zammad.py @@ -427,6 +427,8 @@ async def get_attachment_img(self, attachment: str, request: Request): image_data = image # Ya es un objeto binario vĂ¡lido expiring_date = datetime.now() + timedelta(days=2) + chunk_size = 16384 + content_length = len(image_data) # Crear y devolver la respuesta HTTP response = StreamResponse( status=200, @@ -441,11 +443,24 @@ async def get_attachment_img(self, attachment: str, request: Request): 'Expires': expiring_date.strftime('%a, %d %b %Y %H:%M:%S GMT'), } ) - await response.prepare(request) - await response.write(image_data) - await response.write_eof() - return response + response.headers[ + "Content-Range" + ] = f"bytes 0-{chunk_size}/{content_length}" + try: + i = 0 + await response.prepare(request) + while True: + chunk = image_data[i: i + chunk_size] + i += chunk_size + if not chunk: + break + await response.write(chunk) + await response.drain() # deprecated + await response.write_eof() + return response + except Exception as e: + raise ConfigError(f"Error while writing attachment: {e}") from e except KeyError as e: raise ConfigError(f"Missing required header: {e}") from e except Exception as e: - raise ConfigError(f"Unexpected error while fetching attachment: {e}") from e + raise ConfigError(f"Unexpected error while fetching attachment: {e}") from e \ No newline at end of file