new class: HttpPacket; add packetClass to Session:__init__

This commit is contained in:
pi-rho 2012-06-07 21:56:20 -05:00
parent 7768f73f1b
commit 519797a008
1 changed files with 39 additions and 7 deletions

View File

@ -617,15 +617,47 @@ class Packet(UserDict.DictMixin):
raise AttributeError('Opcode %s unknown' % self.opcode) raise AttributeError('Opcode %s unknown' % self.opcode)
class Session: class HttpPacket(Packet):
def parse(self, data):
# We need the entire block
if not '\r\n\r\n' in data:
raise NeedMoreData()
if data[:4].hasgaps():
self.payload = data
self.opcode = 'drop'
return
four = str(data[:4])
if four in ('POST', 'HTTP', 'GET '):
# XXX: make a gapstr.file class
f = StringIO.BytesIO(str(data))
self['request'] = f.readline()
self.m = rfc822.Message(f)
l = int(self.m.getheader('content-length', '0'))
pos = f.tell()
content = data[pos:]
if len(content) < l:
raise NeedMoreData()
self.handle_content(content[:l])
return content[l:]
else:
self.handle_weirdo(data)
def handle_content(self, content):
self.payload = content
class Session(object):
"""Base class for a binary protocol session.""" """Base class for a binary protocol session."""
# Override this, duh def __init__(self, frame, packetClass=Packet):
Packet = Packet self.Packet = packetClass
def __init__(self, frame):
self.firstframe = frame self.firstframe = frame
self.lastframe = [None, None] self.lastframe = [None, None]
self.srv = frame.daddr
self.basename = os.path.join(transfers, frame.src_addr) self.basename = os.path.join(transfers, frame.src_addr)
self.basename2 = os.path.join(transfers, frame.dst_addr) self.basename2 = os.path.join(transfers, frame.dst_addr)
self.pending = {} self.pending = {}
@ -720,8 +752,8 @@ class Session:
class HtmlSession(Session): class HtmlSession(Session):
def __init__(self, frame): def __init__(self, frame, packetClass=Packet):
Session.__init__(self, frame) Session.__init__(self, frame, packetClass)
self.sessfd = self.open_out('session.html') self.sessfd = self.open_out('session.html')
self.sessfd.write('''<?xml version="1.0" encoding="UTF-8"?> self.sessfd.write('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html <!DOCTYPE html