[Patches] [ python-Patches-747364 ] BaseHTTPServer doesn't need StringIO intermediary

SourceForge.net noreply@sourceforge.net
Sun, 13 Jul 2003 19:10:55 -0700


Patches item #747364, was opened at 2003-06-02 02:40
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=747364&group_id=5470

Category: Library (Lib)
Group: Python 2.4
Status: Open
>Resolution: Accepted
Priority: 5
Submitted By: Andrew Dalke (dalke)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: BaseHTTPServer doesn't need StringIO intermediary

Initial Comment:
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.


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-07-13 21:10

Message:
Logged In: YES 
user_id=80475

Okay, will post patch after Py2.3 goes out.

----------------------------------------------------------------------

Comment By: Chris Lawrence (lordsutch)
Date: 2003-07-13 17:17

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

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-07-13 05:06

Message:
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.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-07-12 20:05

Message:
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?

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-07-01 00:27

Message:
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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=747364&group_id=5470