From 6701dcd6bfd92eaabb833864f1f7447d0d27ced3 Mon Sep 17 00:00:00 2001 From: Jonas Linter Date: Wed, 8 Oct 2025 14:36:21 +0200 Subject: [PATCH] Probably added gzip --- src/alpine_bits_python/api.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/alpine_bits_python/api.py b/src/alpine_bits_python/api.py index f68bf09..f527e6e 100644 --- a/src/alpine_bits_python/api.py +++ b/src/alpine_bits_python/api.py @@ -555,6 +555,7 @@ async def handle_xml_upload( ): """Endpoint for receiving XML files for conversion processing. Requires basic authentication and saves XML files to log directory. + Supports gzip compression via Content-Encoding header. """ try: # Get the raw body content @@ -565,6 +566,20 @@ async def handle_xml_upload( status_code=400, detail="ERROR: No XML content provided" ) + # Check if content is gzip compressed + content_encoding = request.headers.get("content-encoding", "").lower() + is_gzipped = content_encoding == "gzip" + + # Decompress if gzipped + if is_gzipped: + try: + body = gzip.decompress(body) + except Exception as e: + raise HTTPException( + status_code=400, + detail=f"ERROR: Failed to decompress gzip content: {e}", + ) from e + # Try to decode as UTF-8 try: xml_content = body.decode("utf-8") @@ -594,21 +609,20 @@ async def handle_xml_upload( _LOGGER.info("XML file saved to %s by user %s", log_filename, username) - return { - "status": "success", - "message": "XML file received and saved", - "filename": log_filename.name, - "size_bytes": len(body), - "authenticated_user": username, + response_headers = { + "Content-Type": "application/xml; charset=utf-8", + "X-AlpineBits-Server-Accept-Encoding": "gzip", } + return Response( + content="Xml received", headers=response_headers, status_code=200 + ) + except HTTPException: raise - except Exception as e: + except Exception: _LOGGER.exception("Error in handle_xml_upload") - raise HTTPException( - status_code=500, detail="Error processing XML upload" - ) from e + raise HTTPException(status_code=500, detail="Error processing XML upload") # UNUSED