awsgi icon indicating copy to clipboard operation
awsgi copied to clipboard

flask_compress support

Open rog555 opened this issue 7 years ago • 3 comments

Hi - great library!

Any way to get it working with flask_compress?

#!/usr/bin/env python
import awsgi
from flask import Flask
from flask import jsonify
from flask_compress import Compress

app = Flask(__name__)
app.config['COMPRESS_MIN_SIZE'] = 0
Compress(app)


@app.route('/')
def index():
    return jsonify(status=200, message='OK')


def lambda_handler(event, context):
    return awsgi.response(app, event, context)


print(lambda_handler({
    'body': None,
    'httpMethod': 'GET',
    'path': '/',
    'queryStringParameters': {},
    'headers': {
        'host': 'localhost',
        'x-forwarded-proto': 'http',
        'Accept-Encoding': 'gzip'
    }
}, None))

Fails with

Traceback (most recent call last):
  File "./test_compress.py", line 31, in <module>
    }, None))
  File "./test_compress.py", line 18, in lambda_handler
    return awsgi.response(app, event, context)
  File "/Users/rog555/Library/Python/2.7/lib/python/site-packages/awsgi/__init__.py", line 22, in response
    return sr.response(output)
  File "/Users/rog555/Library/Python/2.7/lib/python/site-packages/awsgi/__init__.py", line 40, in response
    'body': self.body.getvalue() + ''.join(map(convert_str, output)),
UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 1: ordinal not in range(128)

using aws-wsgi-0.0.7

rog555 avatar May 21 '18 15:05 rog555

Hi, @rog555. I realize it's been a very long time since you posted this issue. Sorry about that.

There have been some encoding-related changes in the code since then. Do you know if your problem has been resolved?

slank avatar Mar 14 '19 16:03 slank

New versions have base64_content_types although that doesn't help much in this case.

Maybe it should also use base64 if there's a content-encoding?

AstraLuma avatar Jul 27 '19 23:07 AstraLuma

With #31 (0.2.0), a .use_binary_response() got added to handle the "when to use a base64 response" policy, so this should be relatively easy to implement.

AstraLuma avatar Aug 23 '19 15:08 AstraLuma