Patches item #1159139, was opened at 2005-03-08 11:04 Message generated for change (Comment added) made by joesalmeri You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1159139&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Library (Lib) Group: Python 2.4 Status: Open Resolution: None Priority: 5 Private: No Submitted By: Joe (joesalmeri) Assigned to: Martin v. Löwis (loewis) Summary: cgi.py invalid REQUEST_METHOD set Initial Comment: Python Version 2.4 OS Windows XP SP 2 + WindowsUpdates Patch for lastest CVS Tree 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. This fix stores the values read. ---------------------------------------------------------------------- >Comment By: Joe (joesalmeri) Date: 2007-05-02 12:12 Message: Logged In: YES user_id=1235002 Originator: YES Was the test script sufficient or did you need any more information? ---------------------------------------------------------------------- Comment By: Joe (joesalmeri) Date: 2007-04-02 10:14 Message: Logged In: YES user_id=1235002 Originator: YES Test Script that shows problem: # Start of Script import cgi import os os.environ['REQUEST_METHOD'] = 'cgi' field = cgi.FieldStorage() print field print field.keys() for k in field.keys(): print k, field[k] # End Of Script When you run that script it will hang because it is waiting on the read from self.fp (sys.stdin), press ctrl-z (EOF) on Windows and hit enter (or you could enter some key/value pairs but they are not stored in FieldStorage). cgi.py will then fail as follows: ^Z FieldStorage(None, None, '') Traceback (most recent call last): File "Q:\cgifix\FixTest\t2.py", line 9, in ? print field.keys() File "P:\SW\Python\lib\cgi.py", line 601, in keys raise TypeError, "not indexable" TypeError: not indexable Now modify the above script to use the NEW cgi.py I provided. When prompted for the values on sys.stdin enter your pairs enter the following (press enter after each line) a=1 b=2 c=3 now press ctrl-z (EOF) and enter and you will see that cgi.py has been patch to fix the bug. FieldStorage(None, None, 'a=1\nb=2\nc=3\n') ['a', 'b', 'c'] a MiniFieldStorage('a', '1') b MiniFieldStorage('b', '2') c MiniFieldStorage('c', '3') I hope that makes the problem and solution more clear so that this can be included in the next release. Thanks! ---------------------------------------------------------------------- Comment By: Joe (joesalmeri) Date: 2007-04-02 09:54 Message: Logged In: YES user_id=1235002 Originator: YES It been a long time since I submitted this patch but I meant that REQUEST_METHOD is not set although I believe it also happens when it is set incorrectly. Regarding your question with the prompting I thought I explained that in the original post but here are more details: When the REQUEST_METHOD is not set the __init__ method for the FieldStorage class ends up setting the self.fp to sys.stdin. When the read_single method is called it calls self.read_lines(). Since self.fp is equal to sys.stdin at that point and since there is not input pending on stdin it ends up prompting for input until it reaches EOF. The bug was that although it prompted for input by reading from sys.stdin, the values that it read were never stored in FieldStorage like they are when the values are read from the normal sources. Since someone went to the trouble of coding cgi.py to read the values from stdin when the REQUEST_METHOD was invalid it seems to make sense to store them. As jimjjewett point out this addresses the issue of allowing a cgi script to be called from the command line which is useful for debugging of if no web server is running. Since it is counter-intuitive to read the values or not store them the other option would be not to set self.fp = sys.stdin and instead through an error that REQUEST_METHOD was not set properly however, it seems more benefitial to read from sys.stdin and then store the values read in FieldStorage. Does that make more sense now? Thanks. ---------------------------------------------------------------------- Comment By: SourceForge Robot (sf-robot) Date: 2007-03-20 22:20 Message: Logged In: YES user_id=1312539 Originator: NO This Tracker item was closed automatically by the system. It was previously set to a Pending status, and the original submitter did not respond within 14 days (the time period specified by the administrator of this Tracker). ---------------------------------------------------------------------- Comment By: Martin v. Löwis (loewis) Date: 2007-03-06 08:23 Message: Logged In: YES user_id=21627 Originator: NO I must be completely missing the point of this patch. Where in the code does it currently prompt for key=value pairs if REQUEST_METHOD is not correctly set? By "not correctly set", do you mean "not set" or "set incorrectly"? If the latter, to what value? You seem to be modifying the read_single function. This is meant to read a single body of the CGI request (as sent by the HTTP client), not the key-value-pairs. I've tried to come up with a demo application of this new functionality, but failed: I couldn't make this new code do anything useful. What Python script should I use, how should I invoke it, what environment variables should I give, and what standard input? Tentatively rejecting the patch. ---------------------------------------------------------------------- Comment By: Jim Jewett (jimjjewett) Date: 2005-03-14 17:54 Message: Logged In: YES user_id=764593 +1 on supporting use without a REQUEST_METHOD, if only to match Perl on "what happens if my .cgi was called straight from the command line"? ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1159139&group_id=5470 _______________________________________________ Patches mailing list Patches@python.org http://mail.python.org/mailman/listinfo/patches