[Web-SIG] help with the implementation of a WSGI middleware

Manlio Perillo manlio_perillo at libero.it
Mon Jul 7 21:58:59 CEST 2008


As I have informally written in previous messages, I'm writing a small 
WSGI framework.

The framework is available here (a Mercurial repository):
http://hg.mperillo.ath.cx/wsgix


In wsgix I have written two middleware that I find interesting since I 
have learned a bit more about how to write middlewares
(and Eby concerns about WSGI 1.0).

One of this middleware is wsgix.contrib.messages:
http://hg.mperillo.ath.cx/wsgix/file/tip/wsgix/contrib/messages.py


The purpose of this middleware is to support sending messages to a client.

The idea originates from Django, however in wsgix I use cookies (since I 
find not a really good idea to use a database for this) and messages can 
be sent to every user (Django sends messages only to authenticated 
users, if I'm correct).


The wsgix support for messages consist of two parts.
The first is the implementation of a simple API for sending an 
retrieving messages (only Unicode strings are supported):

message_push(environ, message)
message_pop(environ) # this returns and remove the messages

These functions does not actually manage cookies: the messages are 
stored in environ['wsgix.messages'], as a list.


The latter is the implementation of a middleware that take care of 
cookies handling.


The problem is that, if I have well understood, a middleware is allowed 
to entirely replace the environ dictionary.

This means that if such a middleware is presend before the messages 
middleware is called, messages are not sent to the client.


Is this true?
In this case the first solution is to use this middleware as a 
decorator, instead of a full middleware.

The other solution is to implement an additional interface:

message_push(environ, start_response, headers, message)

that explicitly handle the cookie (this is possible but harder to 
implement and less flexibile to use).


Any suggestions?


Thanks   Manlio Perillo


More information about the Web-SIG mailing list