optimize a little bit headers parsing

This commit is contained in:
benoitc 2010-08-11 09:24:16 +02:00
parent 9b4ab138fd
commit d1858d2284

View File

@ -36,15 +36,7 @@ class Message(object):
headers = [] headers = []
# Split lines on \r\n keeping the \r\n on each line # Split lines on \r\n keeping the \r\n on each line
lines = [] lines = [line + "\r\n" for line in data.split("\r\n")]
while len(data):
pos = data.find("\r\n")
if pos < 0:
lines.append(data)
data = ""
else:
lines.append(data[:pos+2])
data = data[pos+2:]
# Parse headers into key/value pairs paying attention # Parse headers into key/value pairs paying attention
# to continuation lines. # to continuation lines.
@ -139,19 +131,27 @@ class Request(Message):
buf.truncate(0) buf.truncate(0)
buf.write(rest) buf.write(rest)
# Headers # Headers
pos = 0
idx = buf.getvalue().find("\r\n\r\n") idx = buf.getvalue().find("\r\n\r\n")
done = buf.getvalue()[:2] == "\r\n" done = buf.getvalue()[:2] == "\r\n"
while idx < 0 and not done: while idx < 0 and not done:
pos = buf.tell() - 4
self.get_data(unreader, buf) self.get_data(unreader, buf)
idx = buf.getvalue().find("\r\n\r\n") idx = buf.getvalue()[pos:].find("\r\n\r\n")
done = buf.getvalue()[:2] == "\r\n" done = buf.getvalue()[:2] == "\r\n"
if done: if done:
self.unreader.unread(buf.getvalue()[2:]) self.unreader.unread(buf.getvalue()[2:])
return "" return ""
self.headers = self.parse_headers(buf.getvalue()[:idx])
ret = buf.getvalue()[idx+4:] end = pos + idx
self.headers = self.parse_headers(buf.getvalue()[:end])
ret = buf.getvalue()[end+4:]
buf.truncate(0) buf.truncate(0)
return ret return ret