text: """
  http.py
  
  Modifications copyright (c) 2007 The Honeynet Project
  
  This file is a modified http.py from dpkt by Dug Song.     
  
  Original code:
  
  Copyright (c) 2004 Dug Song <dugsong@monkey.org>
  All rights reserved, all wrongs reversed.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. The names of the authors and copyright holders may not be used to
     endorse or promote products derived from this software without
     specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  """
  
  # $Id$
  
  import cStringIO
  import dpkt
  
  def parse_headers(f):
      """Return dict of HTTP headers parsed from a file object."""
      d = {}
      while 1:
          line = f.readline()
          if not line:
              raise dpkt.NeedData('premature end of headers')
          line = line.strip()
          if not line:
              break
          l = line.split(':', 1)
          if not len(l) == 2:
              raise dpkt.UnpackError('invalid header: %r' % line)
          l = [ x.strip() for x in l ]
          l[0] = l[0] + ':'          
          k = l[0][:-1].lower()
          d[k] = len(l) != 1 and l[1] or ''
      return d
  
  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)))
              # we don't care if we miss some data, so plough on
              pass
      elif 'content-type' in headers:
          body = f.read()
      else:
          # XXX - need to handle HTTP/0.9
          body = ''
      return body
  
  class Message(dpkt.Packet):
      """Hypertext Transfer Protocol headers + body."""
      __metaclass__ = type
      __hdr_defaults__ = {}
      headers = None
      body = None
  
      def __init__(self, *args, **kwargs):
          if args:
              self.unpack(args[0])
          else:
              self.headers = {}
              self.body = ''
              for k, v in self.__hdr_defaults__.iteritems():
                  setattr(self, k, v)
              for k, v in kwargs.iteritems():
                  setattr(self, k, v)
  
      def unpack(self, buf):
          f = cStringIO.StringIO(buf)
          # Parse headers
          self.headers = parse_headers(f)
          # Parse body
          self.body = parse_body(f, self.headers)
          # Save the rest
          self.data = f.read()
      
      def pack_hdr(self):
          return ''.join([ '%s: %s\r\n' % t for t in self.headers.iteritems() ])
      
      def __len__(self):
          return len(str(self))
      
      def __str__(self):
          return '%s\r\n%s' % (self.pack_hdr(), self.body)
  
  class Request(Message):
      """Hypertext Transfer Protocol Request."""
      __hdr_defaults__ = {
          'method':'GET',
          'uri':'/',
          'version':'1.0',
          }
      __methods = dict.fromkeys((
          'GET', 'PUT', 'ICY',
          'COPY', 'HEAD', 'LOCK', 'MOVE', 'POLL', 'POST',
          'BCOPY', 'BMOVE', 'MKCOL', 'TRACE', 'LABEL', 'MERGE',
          'DELETE', 'SEARCH', 'UNLOCK', 'REPORT', 'UPDATE', 'NOTIFY',
          'BDELETE', 'CONNECT', 'OPTIONS', 'CHECKIN',
          'PROPFIND', 'CHECKOUT', 'CCM_POST',
          'SUBSCRIBE', 'PROPPATCH', 'BPROPFIND',
          'BPROPPATCH', 'UNCHECKOUT', 'MKACTIVITY',
          'MKWORKSPACE', 'UNSUBSCRIBE', 'RPC_CONNECT',
          'VERSION-CONTROL',
          'BASELINE-CONTROL'
          ))
      __proto = 'HTTP'
      
      def unpack(self, buf):
          f = cStringIO.StringIO(buf)
          line = f.readline()
          l = line.strip().split()
          if len(l) != 3 or l[0] not in self.__methods or \
             not l[2].startswith(self.__proto):
              raise dpkt.UnpackError('invalid request: %r' % line)
          self.method = l[0]
          self.uri = l[1]
          self.version = l[2][len(self.__proto)+1:]
          Message.unpack(self, f.read())
      
      def __str__(self):
          return '%s %s %s/%s\r\n' % (self.method, self.uri, self.__proto,
                                      self.version) + Message.__str__(self)
  
  class Response(Message):
      """Hypertext Transfer Protocol Response."""
      __hdr_defaults__ = {
          'version':'1.0',
          'status':'200',
          'reason':'OK'
          }
      __proto = 'HTTP'
      
      def unpack(self, buf):
          f = cStringIO.StringIO(buf)
          line = f.readline()
          l = line.strip().split(None, 2)
          if len(l) < 2 or not l[0].startswith(self.__proto) or not l[1].isdigit():
              raise dpkt.UnpackError('invalid response: %r' % line)
          self.version = l[0][len(self.__proto)+1:]
          self.status = l[1]
          if len(l) == 2:
              self.reason = ''
          else:
              self.reason = l[2]
          Message.unpack(self, f.read())
      
      def __str__(self):
          return '%s/%s %s %s\r\n' % (self.__proto, self.version, self.status,
                                      self.reason) + Message.__str__(self)