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