def __handle_multipart_body(self): if not self.__multipart: self.__multipart = http_multipart.parser(self.__multipart_boundary) self.__multipart.input(self.__reader.read()) while 1: self.__multipart.parse() if not self.__multipart.is_start(): break if not self.__multipart.can_parse: break is_file = self.__multipart.is_file() if is_file: self.__handle_multipart_file() else: self.__handle_multipart_nofile() return
def __handle_multipart_body(self): if not self.recv_ok(): if not self.__tmpfile_fd: tmpname = self.__get_tmpfile_name() self.__tmpfile_name = tmpname path = "%s/%s" % ( self.__tmp_dir, tmpname, ) self.__tmpfile_fd = open(path, "wb") self.__tmpfile_fd.write(self.__reader.read()) return multipart = http_multipart.parser(self.__tmpfile_fd, self.__multipart_boundary) is_get_hdr = False info = None tmpfile_fd = None is_file = False data_list = [] while not multipart.all_ok(): if not multipart.single_ok() and not is_get_hdr: is_get_hdr = True info = multipart.get_info() is_file = info["is_file"] if is_file: tmpname = self.__get_tmpfile_name() path = "%s/%s" % ( self.__tmp_dir, tmpname, ) tmpfile_fd = open(path, "wb") '''''' if is_get_hdr and not multipart.single_ok(): try: multipart.parse() except http_multipart.MultipartErr: # 关闭占用的文件资源 if tmpfile_fd: tmpfile_fd.close() raise RequestErr("wrong multipart format") # 去除结尾的\r\n if multipart.single_ok(): is_get_hdr = False part_data = multipart.get_part_data()[0:-2] else: part_data = multipart.get_part_data() tmpfile_fd.write(part_data) name = info["name"] if multipart.single_ok(): multipart.reset() if not is_file: try: sts = b"".join(data_list).decode() except UnicodeDecodeError: raise RequestErr("wrong UTF coding") if name not in self.__stream_params: self.__stream_params[name] = [] self.__stream_params[name].append(sts) data_list = [] else: tmpfile_fd.close() tmpfile_fd = None if name not in self.__files: self.__files[name] = [] self.__files[name].append({ "tmp_name": info["tmp_name"], "filename": info["filename"], "content_type": info["content_type"], }) is_file = False """""" """""" return