[mod_python] Specifying GET/POST method for FieldStorage

Frédéric Jolliton mod_python at frederic.jolliton.com
Sun Mar 12 06:59:25 EST 2006


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):

-- 
Frédéric Jolliton



More information about the Mod_python mailing list