apig-wsgi icon indicating copy to clipboard operation
apig-wsgi copied to clipboard

Errors if Django gzip middleware used

Open OJFord opened this issue 1 year ago • 6 comments

Python Version

3.11

Package Version

2.18.0

Description

[ERROR] UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
Traceback (most recent call last):
  File "/var/task/app/handlers.py", line 102, in app
    return _real_handler(event, context)
  File "/var/task/apig_wsgi/__init__.py", line 99, in handler
    return response.as_apig_response()
  File "/var/task/apig_wsgi/__init__.py", line 329, in as_apig_response
    response["body"] = self.body.getvalue().decode("utf-8")

Called like:

        from apig_wsgi import make_lambda_handler
        from django.core.wsgi import get_wsgi_application

        application = get_wsgi_application()
        _real_handler = make_lambda_handler(application)

with "django.middleware.gzip.GZipMiddleware", in middleware.

OJFord avatar Aug 08 '24 17:08 OJFord

Ah do we perhaps just need binary_support=False? This is with ALB, so I gather the implicit None there is enabling it.

OJFord avatar Aug 08 '24 18:08 OJFord

I believe this was fixed in #80 , back in version 2.3.0 (2019-08-19).

~You’re not using version 2.18.0 as you report because the source in your traceback does not match. Please check which version you are actually using.~

Ah, no, sorry the source does match.

adamchainz avatar Aug 08 '24 18:08 adamchainz

I think binary_support=True may fix it, but ideally we would send binary automatically. This method should be returning True, but isn’t:

https://github.com/adamchainz/apig-wsgi/blob/dc220949c4bf9b0aeb0eb49fdd4767af66fdb776/src/apig_wsgi/init.py#L276-L289

adamchainz avatar Aug 08 '24 18:08 adamchainz

That could be a problem with my headers rather than that detection though I suppose.

I will try it shortly, thanks for getting back to me so quickly.

OJFord avatar Aug 08 '24 19:08 OJFord

It's because of the first

if not self.binary_support:
    return False

isn't it? I'm not setting it, and so: https://github.com/adamchainz/apig-wsgi/blob/dc220949c4bf9b0aeb0eb49fdd4767af66fdb776/src/apig_wsgi/init.py#L74-L76

(which seems contrary to the readme, hence I got it the wrong way around in my first reply: https://github.com/adamchainz/apig-wsgi/blob/dc220949c4bf9b0aeb0eb49fdd4767af66fdb776/README.rst?plain=1#L81?)

OJFord avatar Aug 08 '24 19:08 OJFord

I think binary_support=True may fix it

and I can confirm it does indeed! Thanks again.

As above though I think the intention was for version == "alb" to do binary_support != False rather than or False, i.e. to default it True when None, so it wouldn't have been necessary for me to set it explicitly.

OJFord avatar Aug 09 '24 00:08 OJFord

Okay, I just came back to this. I think I fixed it in #528, releasing shortly. Would you be able to check?

adamchainz avatar Oct 07 '24 21:10 adamchainz

Yep seems fine now without the binary_support=True arg too, thanks!

OJFord avatar Oct 15 '24 10:10 OJFord