#NEWLINE# def parse( self ) : #NEWLINE# #INDENT# '\n        Parse the POST data and break it into a FILES MultiValueDict and a POST\n        MultiValueDict.\n\n        Returns a tuple containing the POST and FILES dictionary, respectively.\n        ' #NEWLINE# from django.http import QueryDict #NEWLINE# encoding = self . _encoding #NEWLINE# handlers = self . _upload_handlers #NEWLINE# if ( self . _content_length == 0 ) : #NEWLINE# #INDENT# return ( QueryDict ( '' , encoding = self . _encoding ) , MultiValueDict ( ) ) #NEWLINE# #UNINDENT# for handler in handlers : #NEWLINE# #INDENT# result = handler . handle_raw_input ( self . _input_data , self . _meta , self . _content_length , self . _boundary , encoding ) #NEWLINE# if ( result is not None ) : #NEWLINE# #INDENT# return ( result [ 0 ] , result [ 1 ] ) #NEWLINE# #UNINDENT# #UNINDENT# self . _post = QueryDict ( '' , mutable = True ) #NEWLINE# self . _files = MultiValueDict ( ) #NEWLINE# stream = LazyStream ( ChunkIter ( self . _input_data , self . _chunk_size ) ) #NEWLINE# old_field_name = None #NEWLINE# counters = ( [ 0 ] * len ( handlers ) ) #NEWLINE# try : #NEWLINE# #INDENT# for ( item_type , meta_data , field_stream ) in Parser ( stream , self . _boundary ) : #NEWLINE# #INDENT# if old_field_name : #NEWLINE# #INDENT# self . handle_file_complete ( old_field_name , counters ) #NEWLINE# old_field_name = None #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# disposition = meta_data [ 'content-disposition' ] [ 1 ] #NEWLINE# field_name = disposition [ 'name' ] . strip ( ) #NEWLINE# #UNINDENT# except ( KeyError , IndexError , AttributeError ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# transfer_encoding = meta_data . get ( 'content-transfer-encoding' ) #NEWLINE# if ( transfer_encoding is not None ) : #NEWLINE# #INDENT# transfer_encoding = transfer_encoding [ 0 ] . strip ( ) #NEWLINE# #UNINDENT# field_name = force_text ( field_name , encoding , errors = 'replace' ) #NEWLINE# if ( item_type == FIELD ) : #NEWLINE# #INDENT# if ( transfer_encoding == 'base64' ) : #NEWLINE# #INDENT# raw_data = field_stream . read ( ) #NEWLINE# try : #NEWLINE# #INDENT# data = str ( raw_data ) . decode ( 'base64' ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# data = raw_data #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# data = field_stream . read ( ) #NEWLINE# #UNINDENT# self . _post . appendlist ( field_name , force_text ( data , encoding , errors = 'replace' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( item_type == FILE ) : #NEWLINE# #INDENT# file_name = disposition . get ( 'filename' ) #NEWLINE# if ( not file_name ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# file_name = force_text ( file_name , encoding , errors = 'replace' ) #NEWLINE# file_name = self . IE_sanitize ( unescape_entities ( file_name ) ) #NEWLINE# content_type = meta_data . get ( 'content-type' , ( '' , ) ) [ 0 ] . strip ( ) #NEWLINE# try : #NEWLINE# #INDENT# charset = meta_data . get ( 'content-type' , ( 0 , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) ) [ 1 ] . get ( 'charset' , None ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# charset = None #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# content_length = int ( meta_data . get ( 'content-length' ) [ 0 ] ) #NEWLINE# #UNINDENT# except ( IndexError , TypeError , ValueError ) : #NEWLINE# #INDENT# content_length = None #NEWLINE# #UNINDENT# counters = ( [ 0 ] * len ( handlers ) ) #NEWLINE# try : #NEWLINE# #INDENT# for handler in handlers : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# handler . new_file ( field_name , file_name , content_type , content_length , charset ) #NEWLINE# #UNINDENT# except StopFutureHandlers : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# for chunk in field_stream : #NEWLINE# #INDENT# if ( transfer_encoding == 'base64' ) : #NEWLINE# #INDENT# over_bytes = ( len ( chunk ) % 4 ) #NEWLINE# if over_bytes : #NEWLINE# #INDENT# over_chunk = field_stream . read ( ( 4 - over_bytes ) ) #NEWLINE# chunk += over_chunk #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# chunk = base64 . b64decode ( chunk ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# msg = ( 'Could not decode base64 data: %r' % e ) #NEWLINE# six . reraise ( MultiPartParserError , MultiPartParserError ( msg ) , sys . exc_info ( ) [ 2 ] ) #NEWLINE# #UNINDENT# #UNINDENT# for ( i , handler ) in enumerate ( handlers ) : #NEWLINE# #INDENT# chunk_length = len ( chunk ) #NEWLINE# chunk = handler . receive_data_chunk ( chunk , counters [ i ] ) #NEWLINE# counters [ i ] += chunk_length #NEWLINE# if ( chunk is None ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except SkipFile : #NEWLINE# #INDENT# exhaust ( field_stream ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# old_field_name = field_name #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# exhaust ( stream ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except StopUpload as e : #NEWLINE# #INDENT# if ( not e . connection_reset ) : #NEWLINE# #INDENT# exhaust ( self . _input_data ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# exhaust ( self . _input_data ) #NEWLINE# #UNINDENT# for handler in handlers : #NEWLINE# #INDENT# retval = handler . upload_complete ( ) #NEWLINE# if retval : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# return ( self . _post , self . _files )