def _extract_files_from_response(self, response): files = [] ct = None boundary = None fn = None for header in response.info().headers: if header.startswith('Content-Type'): ct = header.split(': ')[1].strip() if header.startswith('Content-Disposition'): fn = header.split('; ')[1].split('=')[1] fn = fn.split('"')[1] # A 'header' line is terminated with a '\r'. This split takes care of that. fn = fn.strip() # Just to be sure... if not ct.startswith('multipart'): raw_data = response.read() file_info = {} file_info['name'] = fn file_info['buffer'] = raw_data files.append(file_info) return files else: boundary = ct.split('; ')[1].split('=')[1] data = response.read() # print 'BOUNDARY: ', boundary raw_data = mp.mpUnpack(data, boundary) # print('#files in response: ' + str(len(raw_data))) for file_info in raw_data: info = file_info[0] offset = file_info[1] length = file_info[2] filename = info['content-disposition'].split(';')[1].split('"')[1] end = offset + length # print 'end:' + str(end) filebuffer = data[offset:end] file_info = {} file_info['name'] = filename file_info['buffer'] = filebuffer files.append(file_info) # print('filename: ' + filename) # print('Offset:' + str(offset)) # print('Length:' + str(length)) # print('Buffer: \n\n' + str(buffer)) # print('\n\n') return files
def _unpackMultipartContent( self , response ) : content = response.content content_type = response['Content-Type'] # check the content type if getMimeType( content_type ) \ not in ( "multipart/mixed" , "multipart/related" ) : raise Exception , "Received content is neither mixed nor related multipart! Content-Type: %s" % content_type # extract multipart boundary boundary = getMultipartBoundary( content_type ) # unpack the multipart content for header,offset,size in mpUnpack(content,boundary) : match = RE_MIME_TYPE_XML.match( _getMime(header['content-type']) ) if match is not None : # store XML response self.xmlData = content[offset:(offset+size)] else : # store coverage data self.imageData = content[offset:(offset+size)]