Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors if Django gzip middleware used #513

Closed
OJFord opened this issue Aug 8, 2024 · 8 comments · Fixed by #528
Closed

Errors if Django gzip middleware used #513

OJFord opened this issue Aug 8, 2024 · 8 comments · Fixed by #528

Comments

@OJFord
Copy link

OJFord commented Aug 8, 2024

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
Copy link
Author

OJFord commented Aug 8, 2024

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

@adamchainz
Copy link
Owner

adamchainz commented Aug 8, 2024

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
Copy link
Owner

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

def _should_send_binary(self) -> bool:
"""
Determines if binary response should be sent to API Gateway
"""
if not self.binary_support:
return False
content_type = self._get_content_type()
if not content_type.startswith(self.non_binary_content_type_prefixes):
return True
content_encoding = self._get_content_encoding()
# Content type is non-binary but the content encoding might be.
return "gzip" in content_encoding.lower()

@OJFord
Copy link
Author

OJFord commented Aug 8, 2024

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
Copy link
Author

OJFord commented Aug 8, 2024

It's because of the first

if not self.binary_support:
    return False

isn't it? I'm not setting it, and so:

if version in ("1.0", "alb"):
# Binary support deafults 'off' on version 1
event_binary_support = binary_support or False

(which seems contrary to the readme, hence I got it the wrong way around in my first reply:

* ALB’s support binary responses by default.
?)

@OJFord
Copy link
Author

OJFord commented Aug 9, 2024

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.

@adamchainz
Copy link
Owner

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

@OJFord
Copy link
Author

OJFord commented Oct 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants