diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 4e98f858..3604bdd1 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -734,3 +734,14 @@ Here are some example log calls:: The attached :attr:`~flask.Flask.logger` is a standard logging :class:`~logging.Logger`, so head over to the official stdlib documentation for more information. + +Hooking in WSGI Middlewares +--------------------------- + +If you want to add a WSGI middleware to your application you can wrap the +internal WSGI application. For example if you want to one of the +middlewares from the Werkzeug package to work around bugs in lighttpd, you +can do it like this:: + + from werkzeug.contrib.fixers import LighttpdCGIRootFix + app.wsgi_app = LighttpdCGIRootFix(app.wsgi_app) diff --git a/flask/app.py b/flask/app.py index 351c0c3a..21235897 100644 --- a/flask/app.py +++ b/flask/app.py @@ -724,6 +724,16 @@ class Flask(_PackageBoundObject): return self.response_class(*rv) return self.response_class.force_type(rv, request.environ) + def create_url_adapter(self, request): + """Creates a URL adapter for the given request. The URL adapter + is created at a point where the request context is not yet set up + so the request is passed explicitly. + + .. versionadded:: 0.6 + """ + return self.url_map.bind_to_environ(request.environ, + server_name=self.config['SERVER_NAME']) + def preprocess_request(self): """Called before the actual request dispatching and will call every as :meth:`before_request` decorated function. diff --git a/flask/ctx.py b/flask/ctx.py index 854503af..1b17086c 100644 --- a/flask/ctx.py +++ b/flask/ctx.py @@ -28,9 +28,8 @@ class _RequestContext(object): def __init__(self, app, environ): self.app = app - self.url_adapter = app.url_map.bind_to_environ(environ, - server_name=app.config['SERVER_NAME']) self.request = app.request_class(environ) + self.url_adapter = app.create_url_adapter(self.request) self.session = app.open_session(self.request) if self.session is None: self.session = _NullSession()