def parse_boundary_stream(stream, max_header_size):
 chunk = stream.read(max_header_size)
 header_end = chunk.find('\r\n\r\n')
 def _parse_header(line):
  (main_value_pair, params) = parse_header(line)
  try:
   (name, value) = main_value_pair.split(':', 1)
  except:
   raise ValueError(('Invalid header: %r' % line))
  return (name, (value, params))
 if (header_end == (-1)):
  stream.unget(chunk)
  return (RAW, {}, stream)
 header = chunk[:header_end]
 stream.unget(chunk[(header_end + 4):])
 TYPE = RAW
 outdict = {}
 for line in header.split('\r\n'):
  try:
   (name, (value, params)) = _parse_header(line)
  except:
   continue
  if (name == 'content-disposition'):
   TYPE = FIELD
   if params.get('filename'):
    TYPE = FILE
  outdict[name] = (value, params)
 if (TYPE == RAW):
  stream.unget(chunk)
 return (TYPE, outdict, stream)

def parse_boundary_stream ( stream , max_header_size ) : chunk = stream . read ( max_header_size ) header_end = chunk . find ( '\r\n\r\n' ) def _parse_header ( line ) : ( main_value_pair , params ) = parse_header ( line ) try : ( name , value ) = main_value_pair . split ( ':' , 1 ) except : raise ValueError ( ( 'Invalid header: %r' % line ) ) return ( name , ( value , params ) ) if ( header_end == ( - 1 ) ) : stream . unget ( chunk ) return ( RAW , { } , stream ) header = chunk [ : header_end ] stream . unget ( chunk [ ( header_end + 4 ) : ] ) TYPE = RAW outdict = { } for line in header . split ( '\r\n' ) : try : ( name , ( value , params ) ) = _parse_header ( line ) except : continue if ( name == 'content-disposition' ) : TYPE = FIELD if params . get ( 'filename' ) : TYPE = FILE outdict [ name ] = ( value , params ) if ( TYPE == RAW ) : stream . unget ( chunk ) return ( TYPE , outdict , stream )
parses one and exactly one stream that encapsulates a boundary .