cgi.py bug

Joe JoeSalmeri at hotmail.com
Tue Mar 8 11:24:31 EST 2005


While debugging a problem I was having I found a bug in the cgi.py module.

When the environment does not have a correctly set REQUEST_METHOD cgi.py 
prompts
for key=value pairs by reading from sys.stdin.  After the values are read 
from
sys.stdin they are never stored in the FieldStorage.list attribute like they 
are
when the FieldStorage.read_urlencoded or FieldStorage.read_multi methods are 
called.

This causes a problem when FieldStorage.keys() is called because although 
the values
were read from sys.stdin they were never stored in FieldStorage.list.

Although you could argue that REQUEST_METHOD should have been set correctly 
in the
first place, it still seems like if cgi.py is going to handle that situation 
by
actually reading the values from sys.stdin it should store them too.

It "appears" that this can fixed by modifying the read_single method of 
FieldStorage to store
the lines read from sys.stdin just like the other two methods do.

Here is the fix that I proposed that seems to address the problem.

    def read_single(self):
        """Internal: read an atomic part."""
        if self.length >= 0:
            self.read_binary()
            self.skip_lines()
        else:
            self.read_lines()
        self.file.seek(0)

        # Joe's fix
        lines = '&'.join([line.rstrip('\n') for line in 
self.file.readlines()])
        self.file.seek(0)

        self.list = list = []

        for key, value in parse_qsl(lines, self.keep_blank_values,
                                    self.strict_parsing):
            list.append(MiniFieldStorage(key, value))
        # End of Joe's fix

I have tested the fix using a few different combinations and also with an 
immediate EOF and it seems to work in
all cases that I have test.

The bug has been reported on Sourceforge and I submitted the above patch.





















More information about the Python-list mailing list