[Web-SIG] WSGI - alternate ideas, part II

Peter Hunt floydophone at gmail.com
Thu Sep 16 05:06:04 CEST 2004


I know we've come a long way fleshing out WSGI, so remember, these are
just ideas. I'm not saying we should trash what we have, but I just
wanted to throw this out there.

I've been programming my own "web development kit", that is, a
platform (i.e. cgi, fastcgi, mod_python) independent templating and
controller system. Basically, it, along with lots of other efforts,
simply require a standard "request" and "response" object. In
addition, I think the application should call the gateway, instead of
the other way around. I also propose that the API be simple and use as
much standard, prewritten code as possible. Finally, it should be
extensible, such that we don't load, say, sessions if they aren't
needed for a certain application.

Thus, here is my "WSGI-X" proposal. The application will call the
gateway, opposite of WSGI. For example, a CGI WSGI-X application may
begin with:

#!/usr/bin/env python
if __name__ == "__main__":
      from wsgix import cgi
      req = cgi.get_request()

The req object is the core of the interface. In essence, it's
extremely simple. The Request class has four attributes:
fs - an object which mimics cgi.FieldStorage
environ - a dictionary corresponding to the CGI environment
stdout - the raw, unbuffered direct output stream to the client
finish_hooks - list or iterable of functions that are called when
finish() is called

It also declares one method, which may or may not be needed to be
overridden by subclasses specific to the gateway:
finish() - finish the request

Now we have a basic interface to interact with HTTP. If one wants to
write an extension to provide services like simplified cookie
handling, sessions, or buffered headers and content, they write an
extension function. A simple one for cookies would look like:

def cookie_extension(req):
      if not hasattr(req, "cookie"):
            req.cookie = Cookie.SimpleCookie(req.environ.get("HTTP_COOKIE",""))

It modifies the request object if it hasn't already been modified.
This saves us a bit of overhead so we won't need to parse the cookie
again in case it is called twice (as it will if other extensions
depend on it). Finish hooks have the same signature and execute when
the finish() method is called. For example, a buffering extension
would flush the buffer. Extensions can also add methods to the request
object, for items such as add_header().

There's my proposal. Tear it apart :) I'm going to post some example
code tomorrow or the day after, most likely.


More information about the Web-SIG mailing list