Example #1
0
def parse_body(f, headers):
    """Return HTTP body parsed from a file object, given HTTP header dict."""
    if headers.get('transfer-encoding', '').lower() == 'chunked':
        l = []
        found_end = False
        while 1:
            try:
                sz = f.readline().split(None, 1)[0]
            except IndexError:
                raise dpkt.UnpackError('missing chunk size')
            n = int(sz, 16)
            if n == 0:
                found_end = True
            buf = f.read(n)
            if f.readline().strip():
                break
            if n and len(buf) == n:
                l.append(buf)
            else:
                break
        if not found_end:
            raise dpkt.NeedData('premature end of chunked body')
        body = ''.join(l)
    elif 'content-length' in headers:
        n = int(headers['content-length'])
        body = f.read(n)
        if len(body) != n:
            raise dpkt.NeedData('short body (missing %d bytes)' %
                                (n - len(body)))
    elif 'content-type' in headers:
        body = f.read()
    else:
        # XXX - need to handle HTTP/0.9
        body = ''
    return body
Example #2
0
 def unpack(self, buf):
     dpkt.Packet.unpack(self, buf)
     if self.ast != 0x2a:
         raise dpkt.UnpackError('invalid FLAP header')
     if len(self.data) < self.len:
         raise dpkt.NeedData('%d left, %d needed' %
                             (len(self.data), self.len))
Example #3
0
 def unpack(self, buf):
     dpkt.Packet.unpack(self, buf)
     n = self.length - self.__hdr_len__
     if n > len(self.data):
         raise dpkt.NeedData('short message (missing %d bytes)' %
                             (n - len(self.data)))
     self.msg = self.data[:n]
     self.data = self.data[n:]
Example #4
0
 def unpack(self, buf):
     dpkt.Packet.unpack(self, buf)
     n = self.len - 4
     if n > len(self.data):
         raise dpkt.NeedData('not enough data')
     self.msg, self.data = self.data[:n], self.data[n:]
     try:
         p = self._msgsw[self.msgid](self.msg)
         setattr(self, p.__class__.__name__.lower(), p)
     except (KeyError, dpkt.UnpackError):
         pass
Example #5
0
 def unpack(self, buf):
     dpkt.Packet.unpack(self, buf)
     header_length = self.__hdr_len__
     self.data = buf[header_length:header_length + self.length]
     # make sure buffer was long enough
     if len(self.data) != self.length:
         raise dpkt.NeedData('TLSRecord data was too short.')
     # assume compressed and encrypted when it's been parsed from
     # raw data
     self.compressed = True
     self.encrypted = True