granian icon indicating copy to clipboard operation
granian copied to clipboard

[Feature request] Decompress `Content-Encoding` request bodies

Open MarkusSintonen opened this issue 6 months ago • 5 comments

Thank you for the great work! 🙏

It would be awesome if Granian could decompress request bodies with Content-Encoding header set. Many of the Python web frameworks can not do it out-of-the-box (eg FastAPI only supports doing it the other way for the responses). It might also have some performance benefits if its done inside the Granian server instead of later in the Python framework side.

This feature might require additional settings to opt-in for specific decompressors (eg zstd/gzip). Also it might require adding a config for maximum decompressed payload size. So we would decompress the received body in chunks and error if the decompressed size grows too big per user configured limit.

MarkusSintonen avatar May 26 '25 10:05 MarkusSintonen

While I think this might be quite useful, I don't think it can be done without some proper support from the protocols. On RSGI we can upgrade the protocol easily, but on ASGI this would require at least an extension on the protocol spec. Otherwise it will make Granian ASGI incompatible, as the content-encoding header will still contain the original information, while the actual encoding of the body has changed.

So my advise would be to open a discussion on this in https://github.com/django/asgiref.

gi0baro avatar May 26 '25 23:05 gi0baro

Ok! Is it so that ASGI requires keeping the headers intact? Ie Granian can not decompress the request body and pop out the content-encoding header (so that header is no longer visible in ASGI app side).

MarkusSintonen avatar May 27 '25 05:05 MarkusSintonen

So, to your perspective the application should be completely unaware of the thing? Changing the headers might require more work, eg with the content-length.

gi0baro avatar May 27 '25 09:05 gi0baro

Yes, or could also add some info to asgi extension dict (https://asgi.readthedocs.io/en/latest/extensions.html)

MarkusSintonen avatar May 27 '25 19:05 MarkusSintonen

or could also add some info to asgi extension dict (https://asgi.readthedocs.io/en/latest/extensions.html)

That's why I suggested to open a discussion in asgiref repo: the contents of the extension dict are defined by the ASGI protocol spec.

gi0baro avatar May 28 '25 12:05 gi0baro