def walk(msg, callback, data): partnum = 0 for part in msg.walk(): # multipart/* are just containers if part.get_content_maintype() == "multipart": continue ctype = part.get_content_type() if ctype is None: ctype = OCTET_TYPE filename = part.get_filename() if not filename: filename = PART_FN_TPL % (partnum) headers = dict(part) LOG.debug(headers) headers["Content-Type"] = ctype payload = util.fully_decoded_payload(part) callback(data, filename, payload, headers) partnum = partnum + 1
def walk(msg, callback, data): partnum = 0 for part in msg.walk(): # multipart/* are just containers if part.get_content_maintype() == 'multipart': continue ctype = part.get_content_type() if ctype is None: ctype = OCTET_TYPE filename = part.get_filename() if not filename: filename = PART_FN_TPL % (partnum) headers = dict(part) LOG.debug(headers) headers['Content-Type'] = ctype payload = util.fully_decoded_payload(part) callback(data, filename, payload, headers) partnum = partnum + 1
def _process_msg(self, base_msg, append_msg): def find_ctype(payload): return handlers.type_from_starts_with(payload) for part in base_msg.walk(): if is_skippable(part): continue ctype = None ctype_orig = part.get_content_type() payload = util.fully_decoded_payload(part) was_compressed = False # When the message states it is of a gzipped content type ensure # that we attempt to decode said payload so that the decompressed # data can be examined (instead of the compressed data). if ctype_orig in DECOMP_TYPES: try: payload = util.decomp_gzip(payload, quiet=False) # At this point we don't know what the content-type is # since we just decompressed it. ctype_orig = None was_compressed = True except util.DecompressionError as e: LOG.warning( "Failed decompressing payload from %s of" " length %s due to: %s", ctype_orig, len(payload), e) continue # Attempt to figure out the payloads content-type if not ctype_orig: ctype_orig = UNDEF_TYPE if ctype_orig in TYPE_NEEDED: ctype = find_ctype(payload) if ctype is None: ctype = ctype_orig # In the case where the data was compressed, we want to make sure # that we create a new message that contains the found content # type with the uncompressed content since later traversals of the # messages will expect a part not compressed. if was_compressed: maintype, subtype = ctype.split("/", 1) n_part = MIMENonMultipart(maintype, subtype) n_part.set_payload(payload) # Copy various headers from the old part to the new one, # but don't include all the headers since some are not useful # after decoding and decompression. if part.get_filename(): _set_filename(n_part, part.get_filename()) for h in ('Launch-Index', ): if h in part: _replace_header(n_part, h, str(part[h])) part = n_part if ctype != ctype_orig: _replace_header(part, CONTENT_TYPE, ctype) if ctype in INCLUDE_TYPES: self._do_include(payload, append_msg) continue if ctype in ARCHIVE_TYPES: self._explode_archive(payload, append_msg) continue # TODO(harlowja): Should this be happening, shouldn't # the part header be modified and not the base? _replace_header(base_msg, CONTENT_TYPE, ctype) self._attach_part(append_msg, part)
def _process_msg(self, base_msg, append_msg): def find_ctype(payload): return handlers.type_from_starts_with(payload) for part in base_msg.walk(): if is_skippable(part): continue ctype = None ctype_orig = part.get_content_type() payload = util.fully_decoded_payload(part) was_compressed = False # When the message states it is of a gzipped content type ensure # that we attempt to decode said payload so that the decompressed # data can be examined (instead of the compressed data). if ctype_orig in DECOMP_TYPES: try: payload = util.decomp_gzip(payload, quiet=False) # At this point we don't know what the content-type is # since we just decompressed it. ctype_orig = None was_compressed = True except util.DecompressionError as e: LOG.warn( "Failed decompressing payload from %s of length" " %s due to: %s", ctype_orig, len(payload), e ) continue # Attempt to figure out the payloads content-type if not ctype_orig: ctype_orig = UNDEF_TYPE if ctype_orig in TYPE_NEEDED: ctype = find_ctype(payload) if ctype is None: ctype = ctype_orig # In the case where the data was compressed, we want to make sure # that we create a new message that contains the found content # type with the uncompressed content since later traversals of the # messages will expect a part not compressed. if was_compressed: maintype, subtype = ctype.split("/", 1) n_part = MIMENonMultipart(maintype, subtype) n_part.set_payload(payload) # Copy various headers from the old part to the new one, # but don't include all the headers since some are not useful # after decoding and decompression. if part.get_filename(): _set_filename(n_part, part.get_filename()) for h in ("Launch-Index",): if h in part: _replace_header(n_part, h, str(part[h])) part = n_part if ctype != ctype_orig: _replace_header(part, CONTENT_TYPE, ctype) if ctype in INCLUDE_TYPES: self._do_include(payload, append_msg) continue if ctype in ARCHIVE_TYPES: self._explode_archive(payload, append_msg) continue # TODO(harlowja): Should this be happening, shouldn't # the part header be modified and not the base? _replace_header(base_msg, CONTENT_TYPE, ctype) self._attach_part(append_msg, part)