[Web-SIG] Proposal: Handling POST forms in WSGI
Ian Bicking
ianb at colorstudy.com
Wed Nov 1 18:03:45 CET 2006
I've updated this specification in response to input, mostly changing
language but not the meat of the specification itself:
http://wsgi.org/wsgi/Specifications/handling_post_forms
http://wsgi.org/wsgi/Specifications/handling_post_forms?action=diff
One thing that occurred to me is that wsgi.input might be a better
container for the data than an environ key, like:
fs = getattr(environ['wsgi.input'], 'cgi_FieldStorage', None)
if fs is None: # parse and replace wsgi.input...
I actually have reason to want to generalize this, maybe like:
def parse_form(environ):
# Check for POST, CONTENT_TYPE...
decoded = getattr(environ['wsgi.input'], 'decoded', None)
if decoded is not None:
decoded_type, decoded_value = decoded
if decoded_type == 'cgi.FieldStorage':
return decoded_value
decoded_value = cgi.FieldStorage(...)
input = FakeInput(None, 'cgi.FieldStorage', decoded_value)
environ['wsgi.input'] = input
return decoded_value
class FakeInput(object):
def __init__(self, file_body, decoded_type, decoded_value):
self._body = file_body
self.decoded = (decoded_type, decoded_value)
def read(self, *args):
if self._body is None:
raise EOFError("wsgi.input has already been consumed to
produce %r" % self.decoded)
return self._body.read(*args)
# other file methods...
This allows for internal WSGI requests with bodies like text/json to
totally avoid serializing and deserializing. In some cases there'd be
exceptions raised (like this case), but in other cases you can lazily
serialize to deal with intermediaries.
I don't know why this didn't occur to me, since I've actually been doing
this in WSGIRemote, but I like it more than my current proposal. If it
sounds like a good idea to other people I'll drop the current proposal
and create this more generalized proposal with a section on POST forms.
--
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org
More information about the Web-SIG
mailing list