cgi.py bug

J

Joe

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top