Adding headers with unicode_literals enabled seems to fail with Nginx, uWSGI and a simple Flask app:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import Flask, make_response
app = Flask('test')
@app.route('/')
def index():
response = make_response()
response.status_code = 401
response.headers = {'WWW-Authenticate': 'Basic realm="test"'} # Fail
# response.headers = {b'WWW-Authenticate': b'Basic realm="test"'} # Succeed
return response
if __name__ == '__main__':
app.run(debug=True)
The app is available directly for debug purpose or through Nginx -> uWSGI -> Flask and works well.
- When I use a browser to connect directly to the app, I've got a login dialog and the
WWW-Authenticate
header is correct. - The same request going through Nginx returns a header
Transfert-Encoding: chunked
and discard theWWW-Authenticate
header.
Forcing bytestring (b'...') format to add the header make the app works as expected in both cases.
The file is encoded in UTF-8 and there's a
coding` declaration for the Python interpreter.
We're using Python 2.7.3, Nginx 1.4.2 and uWSGI 1.3.
Is there any known incompatibility between Nginx or uWSGI, Flask and unicode_literals? Thanks!
edit: The problem seems to come from uWSGI ( https://github.com/unbit/uwsgi/blob/master/plugins/python/wsgi_headers.c#L116), since it only checks for PyString and not PyUnicode for Python2, if I understand this code correctly.
edit:
Armin Ronacher has fixed a similar bug (https://github.com/mitsuhiko/flask/issues/758) 5 months ago, but I didn't find the commit in werkzeug git log yet. I don't know if the fix is scoped to the redirect()
function or more broadly on headers handling. I'm using Werkzeug 0.9.4 and Flask 0.10.1.