[Patches] [ python-Patches-747364 ] BaseHTTPServer doesn't need StringIO intermediary
SourceForge.net
noreply@sourceforge.net
Mon, 02 Jun 2003 00:40:48 -0700
Patches item #747364, was opened at 2003-06-02 01:40
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=747364&group_id=5470
Category: Library (Lib)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Andrew Dalke (dalke)
Assigned to: Nobody/Anonymous (nobody)
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.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=747364&group_id=5470