[mod_python] util.FieldStorage wrong about method restrictions

Graham Dumpleton grahamd at dscpl.com.au
Mon Aug 14 06:33:06 EDT 2006

On 14/08/2006, at 8:28 AM, Nic James Ferrier wrote:

> My version of mod_python (Debian package 3.1.3-3) says this in
> util.py:
>         # always process GET-style parameters
>         if req.args:
>             pairs = parse_qsl(req.args, keep_blank_values)
>             for pair in pairs:
>                 file = cStringIO.StringIO(pair[1])
>                 self.list.append(Field(pair[0], file, "text/plain",  
> {},
>                                        None, {}))
>         if req.method == "POST" or req.method == "PUT":
>             try:
>                 clen = int(req.headers_in["content-length"])
>             except (KeyError, ValueError):
>                 # absent content-length is not acceptable
>                 raise apache.SERVER_RETURN,  
> The current version says something similar.
> This is wrong. Or at least restictive.

It may be restrictive but the cgi.FieldStorage class in the main Python
distribution also only supports POST. Unless you can show some sort
of precedent in the way of some other equivalent forms processing  
that supports PUT, I can't see any convincing reason to change it. Some
sort of documentation as to what can be provided in a PUT request as
content would also help.

> I noticed this because I am PUTting the representation of a resource
> as application/x-www-form-urlencoded and I wasn't allowed to do that
> using FieldStorage.

You can do it, you just need to use an intermediate wrapper for the
request object to make it think that the POST method was used. Ie.,
something like:

   class _Request:
     def __init__(self, req):
       self.__req = req
     def __getattr__(self, name):
       if name == 'method':
         return 'POST'
       return getattr(self.__req, name)

   form = util.FieldStorage(_Request(req))

This technique can also be used where processing of query string and
body POST parameters must be done separately so as to distinguish
parameters defined by both means, or where it is necessary to ignore
the query string parameters for a POST.


More information about the Mod_python mailing list