def __init__(self, log_fp, label):
    ''' log_fp is the file object to the message log (pos at 0) '''

    fileutil.FileFilter.__init__(self, None)
    self.log_fp = log_fp
    self.label = label

    # skip block 1
    MbReader(log_fp).complete()

    # parse header
    self.hdr_fp = MbReader(log_fp)
    filesoc = fileutil.FileSocket(self.hdr_fp)
    httpRsp = httplib.HTTPResponse(filesoc)
    httpRsp.begin()
    self.hdr_fp.complete()

    enc = httpRsp.getheader('content-encoding','').strip(' ').lower()
    te = httpRsp.getheader('transfer-encoding','').strip(' ').lower()

    # get content block
    self.fp = MbReader(log_fp)

    if te == 'chunked':
        self.fp = readChunked(self.fp)

    if enc == 'gzip' or enc == 'x-gzip':
        self.fp = gzip.GzipFile('content', 'r', fileobj=self.fp)

    elif (not enc) or (enc == 'identity'):
        pass

    else:
        # unknown encoding or multiple encoding
        raise IOError, 'Unsupported content-encoding: %s' % enc
class RspReader:
  """ Open multi block files and make file objects for the response parts. """

  def __init__(self, fp, label):
    self.fp = fp
    self.label = label
    MbReader(fp).complete()     # skip block 1
    self.hdr_fp = MbReader(fp)  # first position on rsp header

  def _seek_response(self):
    self.hdr_fp.complete()
    self.hdr_fp = None
    self.rsp_fp = MbReader(self.fp)

  def read(self,*args):
    if self.hdr_fp:
        data = self.hdr_fp.read(*args)
        if data: return data
        self._seek_response()
    return self.rsp_fp.read(*args)

  def readline(self,*args):
    if self.hdr_fp:
        data = self.hdr_fp.readline(*args)
        if data: return data
        self._seek_response()
    return self.rsp_fp.readline(*args)

  def __iter__(self):
    return self

  def next(self):
    data = self.readline()
    if not data: raise StopIteration
    return data

  def close(self):
    self.fp.close()

  def __str__(self):
    return '<RspReader %s>' % self.label
 def _seek_response(self):
   self.hdr_fp.complete()
   self.hdr_fp = None
   self.rsp_fp = MbReader(self.fp)
 def __init__(self, fp, label):
   self.fp = fp
   self.label = label
   MbReader(fp).complete()     # skip block 1
   self.hdr_fp = MbReader(fp)  # first position on rsp header