adamchainz / apig-wsgi

Wrap a WSGI application in an AWS Lambda handler function for running on API Gateway or an ALB.
MIT License
161 stars 21 forks source link

Errors if Django gzip middleware used #513

Open OJFord opened 1 month ago

OJFord commented 1 month ago

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 commented 1 month ago

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

adamchainz commented 1 month ago

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 commented 1 month ago

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

OJFord commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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.