Example #1
0
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))
Example #2
0
 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
Example #3
0
    def genearte_resetapi_info(self, handler):
        """生成api处理类各方法的信息"""
        handler_api_doc = []
        for method in _ALL_METHOD:
            method_handler = getattr(handler, method, None)
            if not (method_handler and self.is_resetapi_method(method_handler)):
                continue
            api_method_doc = self.parse_method_doc(to_unicode(method_handler.__doc__))
            api_method_doc['method'] = method
            handler_api_doc.append(api_method_doc)

        return handler_api_doc
Example #4
0
    def generate_restapi_list(self):
        all_handlers = self.__get_all_handlers()
        all_api_handlers = {}
        for url, handler in all_handlers:
            _api_handler_info = self.genearte_resetapi_info(handler)
            if not _api_handler_info: # 表示该handler不包含reset api信息
                continue

            module_name = handler.__module__
            _module = __import__(module_name, fromlist = [module_name])
            module_doc = getattr(_module, 'API_MODULE_DOC', None)
            if module_doc:
                module_name += '(%s)' % (to_unicode(module_doc), )

            all_api_handlers.setdefault(module_name,
                    []).append((url, handler, _api_handler_info))

        return all_api_handlers
Example #5
0
 def is_resetapi_method(self, method):
     """通过解析method的__doc__来得出这是不是个rest api"""
     doc = to_unicode((method.__doc__ or '').strip())
     if doc == '':
         return False
     return bool(re_resetapi_flag.match(doc))