librelist archives

« back to archive

WSGI Middleware and Responses

WSGI Middleware and Responses

From:
Trey Long
Date:
2013-01-23 @ 23:46
From the existing middlewares in Flask and Werkzeug I was able to figure 
out that in order to properly decorate a response with a header I have to 
first capture the start_response call and proxy it, save the body and then
modify the headers and re-issue the real start_response call.

This is demonstrated with my example Middleware here:
https://gist.github.com/4614733

Question 1: 
Is this the only (or best) way to do this?

Question 2:
Is there or could there be some sort of helper.

I know the immediate con for a helper is that you cannot have portable 
solutions to other WSGI based application layers but at the same time it 
makes writing something like a CORS handling middleware extremely tiring. 
Something that is very easy in Django or WebOb or <insert other framework 
here> seems a awkward in Flask.

But, maybe I'm missing something obvious.

Thanks, Trey.

Re: [flask] WSGI Middleware and Responses

From:
Audrius Kažukauskas
Date:
2013-01-24 @ 12:55
On Wed, 2013-01-23 at 18:46:35 -0500, Trey Long wrote:
> Question 1: 
> Is this the only (or best) way to do this?
> 
> Question 2:
> Is there or could there be some sort of helper.

Nothing wrong with middleware approach, but if you want to go the Flask
way, check these snippets:

- generic HTTP headers decorator: http://flask.pocoo.org/snippets/100/
- decorator for CORS: http://flask.pocoo.org/snippets/56/

-- 
Audrius Kažukauskas
http://neutrino.lt/