This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: BaseHTTPServer doesn't need StringIO intermediary
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: dalke, loewis, lordsutch, rhettinger
Priority: normal Keywords: patch

Created on 2003-06-02 07:40 by dalke, last changed 2022-04-10 16:08 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
BaseHTTPServer.py.diff dalke, 2003-06-02 07:40 remove unneeded StringIO in BaseHTTPServer
Messages (7)
msg43876 - (view) Author: Andrew Dalke (dalke) * (Python committer) Date: 2003-06-02 07:40
This is Python2.3b1, from CVS

I looked at the implementation of the BaseHTTPServer.py code
where it does the actual parsing.  I see that it has

        # Deal with pipelining
        bytes = ""
        while 1:
            line = self.rfile.readline()
            bytes = bytes + line
            if line == '\r\n' or line == '\n' or line == '':
                break

        # Examine the headers and look for a Connection directive
        hfile = cStringIO.StringIO(bytes)
        self.headers = self.MessageClass(hfile)

The MessageClass is mimetools.Message, which uses rfc822.Message 
to
parse the headers.  The Message reads the input stream up to and
including the end-of-header blank line, but no further, using

        while 1:
            ...
            line = self.fp.readline()
            ...
            elif self.islast(line):
                 # Note! No pushback here!  The delimiter line gets eaten
                 break



    def islast(self, line):
        """Determine whether a line is a legal end of RFC 2822 headers.

and checks for '\r\n' or '\n'

so it seems the temporary copy into a StringIO isn't needed since the
Message can deal with it correctly.

Plus, Message takes a 'seekable' parameter which can turn off seeking
on the input stream, and has for a long time (since well before 
Martin's
"Deal with pipelining code").

The proof, as they say, is in the pudding.  Thought I don't know why.
Anyway, I replaced the "bytes ... " code and used self.rfile rather than
the temporary StringIO hfile, as in

        self.headers = self.MessageClass(self.rfile)
        print "Does it work?", repr(self.rfile.readline())

(I added the print to make sure the data wasn't eaten)

I tested it with the module's mainline self-test, and it seems to
work just fine.  Attached patch does the same, only without the
debugging print statement.
msg43877 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-07-01 05:27
Logged In: YES 
user_id=80475

"the proof is in the pudding" lost its clarity when it got 
shortened from "the proof of the pudding is in the eating" 
meaning that recipes are best judged by their results.

The patch looks fine to me but it is too late in the 
development cycle to include in Py2.3.  Marking as a Py2.4 
idea.
msg43878 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-07-13 01:05
Logged In: YES 
user_id=80475

Martin, you added this code last year (ver 1.19) in response 
to www.python.org/sf/430706 .   Was the += build from 
readline and the trip through CStringIO necessary?
msg43879 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2003-07-13 10:06
Logged In: YES 
user_id=21627

I agree this is a reasonable change, and I also agree that
it comes too late for 2.3. I don't know what Chris Lawrence'
rationale was to wrap the header in a StringIO; I'll ask him.
msg43880 - (view) Author: Chris Lawrence (lordsutch) Date: 2003-07-13 22:17
Logged In: YES 
user_id=6757

I honestly don't remember why I used the StringIO wrapper.  
It could have been that the seekable parameter was 
undocumented at the time, or added while I was evolving the 
patch locally, so I wasn't aware of it.

(I'd probably change the parameter to False, since this is a 
2.3+ patch, but that's a cosmetic issue.)
msg43881 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-07-14 02:10
Logged In: YES 
user_id=80475

Okay, will post patch after Py2.3 goes out.
msg43882 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-08-09 05:04
Logged In: YES 
user_id=80475

Modified to also remove the import for cStringIO.

Did not change seekable argument from 0 to False so that 
the style stayed consistent with mimetools.py and rfc822.py.

Applied as:  Lib/BaseHTTPServer.py 1.28
History
Date User Action Args
2022-04-10 16:08:59adminsetgithub: 38578
2003-06-02 07:40:48dalkecreate