[mod_python] Specifying GET/POST method for FieldStorage

Graham Dumpleton grahamd at dscpl.com.au
Sun Mar 12 14:36:21 EST 2006


Can you explain why you would want to distinguish between the two?

If you want simply not to allow form args in the URL for a POST,
you could check whether req.args is non empty and return a BAD
request if it is. This would be preferable to silently ignoring
the values if any were provided.

If trying to use the URL args and POST content args as different
distinct input methods within the same request, it sounds a bit dodgy.

Graham

On 12/03/2006, at 10:59 PM, Frédéric Jolliton wrote:

> Hi,
>
> [I'm not sure if python-dev was more appropriate.]
>
> Say I've the following case: I've a form with POST method, where
> action is an URL with a query string.
>
> Currently, with util.FieldStorage it's not possible to know which
> values come from the query string and which values come from the POST
> request.
>
> So, here is a suggestion: What about adding a keyword 'from_method' to
> util.FieldStorage.__init__, then use it as follow:
>
> getFieldStorage = util.FieldStorage( from_method = 'GET' )
> postFieldStorage = util.FieldStorage( from_method = 'POST' )
>
> ?
>
> And, of course, let unchanged the current behavior if from_method is
> not specified or set to None.
>
> Here is a trivial patch to support this keyword, against 3.2.8:
>
> --- mod_python-3.2.8.orig/lib/python/mod_python/util.py	2006-02-02 
> 06:31:45.000000000 +0100
> +++ mod_python-3.2.8/lib/python/mod_python/util.py	2006-03-12 
> 12:35:10.000000000 +0100
> @@ -91,7 +91,7 @@
>
>  class FieldStorage:
>
> -   def __init__(self, req, keep_blank_values=0, strict_parsing=0, 
> file_callback=None, field_callback=None):
> +   def __init__(self, req, keep_blank_values=0, strict_parsing=0, 
> file_callback=None, field_callback=None, from_method=None):
>         #
>         # Whenever readline is called ALWAYS use the max size EVEN 
> when not expecting a long line.
>         # - this helps protect against malformed content from 
> exhausting memory.
> @@ -100,7 +100,7 @@
>         self.list = []
>
>         # always process GET-style parameters
> -       if req.args:
> +       if req.args and (from_method is None or from_method.upper() == 
> "GET"):
>             pairs = parse_qsl(req.args, keep_blank_values)
>             for pair in pairs:
>                 file = cStringIO.StringIO(pair[1])
> @@ -109,6 +109,9 @@
>         if req.method != "POST":
>             return
>
> +       if from_method is not None and from_method.upper() != "POST":
> +           return
> +
>         try:
>             clen = int(req.headers_in["content-length"])
>         except (KeyError, ValueError):




More information about the Mod_python mailing list