add compatibility middleware for werkzeug 0.8, #170

This commit is contained in:
Martin Zimmermann 2015-02-24 21:12:21 +01:00
parent 83d3c7443c
commit c3c519ad0e
2 changed files with 29 additions and 1 deletions

View File

@ -48,6 +48,9 @@ from os.path import dirname, join
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import partial, reduce from functools import partial, reduce
import pkg_resources
werkzeug = pkg_resources.get_distribution("werkzeug")
from itsdangerous import URLSafeTimedSerializer from itsdangerous import URLSafeTimedSerializer
from werkzeug.routing import Map 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"), allowed=("Origin", "Referer", "Content-Type"),
exposed=("X-Set-Cookie", "Date"))) 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) return reduce(lambda x, f: f(x), wrapper, isso)

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import sys
import socket import socket
try: try:
@ -149,6 +150,27 @@ class CORSMiddleware(object):
return self.app(environ, add_cors_headers) 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): class Request(_Request):
# Assuming UTF-8, comments with 65536 characters would consume # Assuming UTF-8, comments with 65536 characters would consume