def parse_multipart_form_data(content_type, body, args, files): boundary = escape.utf8(content_type.split('boundary=')[-1]) end_index = body.rfind(b'--' + boundary + b'--') if end_index == -1: # 如果没有结尾符的话,则说明这个主体格式是错误的 gen_log.error('Invalid multipart/form-data: no final boundary') return parts = body[:end_index].split(b'--' + boundary + CRLF) for _part in parts: if not _part: continue _args_part, _body_part = _part.split(CRLF * 2)[:2] body = _body_part[:-2] headers = HTTPHeaders._parse_headers(_args_part) # 这里的headers是指某一个数据块的头部信息 con_dis = headers.get(b'Content-Disposition', None) if not con_dis: gen_log.error('must have Content-Disposition') return con_dis = parse_content_disposition(con_dis) name = escape.to_unicode(con_dis[b'name']) if b'filename' in con_dis: # 如果有filename,则表示这是一个文件 filename = con_dis.get(b'filename') if not filename: continue files.setdefault(name, []).append( HTTPFile(filename = escape.to_unicode(filename), body = body, content_type = escape.to_unicode(headers.get(b'Content-Type', 'application/octet-stream')))) else: args.setdefault(name, []).append(escape.to_unicode(body))
def __init__(self, template_string, name="<string>", loader=None, compress_whitespace=None, autoescape=_UNSET): self.name = name if compress_whitespace is None: compress_whitespace = name.endswith(".html") or \ name.endswith(".js") if autoescape is not _UNSET: self.autoescape = autoescape elif loader: self.autoescape = loader.autoescape else: self.autoescape = _DEFAULT_AUTOESCAPE self.namespace = loader.namespace if loader else {} reader = _TemplateReader(name, escape.native_str(template_string)) self.file = _File(self, _parse(reader, self)) self.code = self._generate_python(loader, compress_whitespace) self.loader = loader try: # Under python2.5, the fake filename used here must match # the module name used in __name__ below. # The dont_inherit flag prevents template.py's future imports # from being applied to the generated code. self.compiled = compile( escape.to_unicode(self.code), "%s.generated.py" % self.name.replace('.', '_'), "exec", dont_inherit=True) except Exception: formatted_code = _format_code(self.code).rstrip() gen_log.error("%s code:\n%s", self.name, formatted_code) raise