From c3c519ad0ee4a436fbf2ef2469cbaadeae72b947 Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Tue, 24 Feb 2015 21:12:21 +0100 Subject: [PATCH] add compatibility middleware for werkzeug 0.8, #170 --- isso/__init__.py | 8 +++++++- isso/wsgi.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/isso/__init__.py b/isso/__init__.py index 0df6af2..baf7aa6 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -48,6 +48,9 @@ from os.path import dirname, join from argparse import ArgumentParser from functools import partial, reduce +import pkg_resources +werkzeug = pkg_resources.get_distribution("werkzeug") + from itsdangerous import URLSafeTimedSerializer from werkzeug.routing import Map @@ -192,7 +195,10 @@ def make_app(conf=None, threading=True, multiprocessing=False, uwsgi=False): allowed=("Origin", "Referer", "Content-Type"), exposed=("X-Set-Cookie", "Date"))) - wrapper.extend([wsgi.SubURI, ProxyFix]) + wrapper.extend([wsgi.SubURI, ProxyFix, wsgi.LegacyWerkzeugMiddleware]) + + if werkzeug.version.startswith("0.8"): + wrapper.append(wsgi.LegacyWerkzeugMiddleware) return reduce(lambda x, f: f(x), wrapper, isso) diff --git a/isso/wsgi.py b/isso/wsgi.py index 50214f3..49f54f6 100644 --- a/isso/wsgi.py +++ b/isso/wsgi.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals +import sys import socket try: @@ -149,6 +150,27 @@ class CORSMiddleware(object): return self.app(environ, add_cors_headers) +class LegacyWerkzeugMiddleware(object): + # Add compatibility with werkzeug 0.8 + # -- https://github.com/posativ/isso/pull/170 + + def __init__(self, app): + self.app = app + + def __call__(self, environ, start_response): + + def to_native(x, charset=sys.getdefaultencoding(), errors='strict'): + if x is None or isinstance(x, str): + return x + return x.decode(charset, errors) + + def fix_headers(status, headers, exc_info=None): + headers = [(to_native(key), value) for key, value in headers] + return start_response(status, headers, exc_info) + + return self.app(environ, fix_headers) + + class Request(_Request): # Assuming UTF-8, comments with 65536 characters would consume