extension.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:sanic-cors 作者: ashleysommer 项目源码 文件源码
def _make_cors_request_middleware_function(plugin, debug):
    @plugin.middleware(relative="pre", attach_to='request', with_context=True)
    def cors_request_middleware(req, context):
        if req.method == 'OPTIONS':
            try:
                path = req.path
            except AttributeError:
                path = req.url
            resources = context.resources
            for res_regex, res_options in resources:
                if res_options.get('automatic_options') and try_match(path, res_regex):
                    debug("Request to '{:s}' matches CORS resource '{}'. "
                          "Using options: {}".format(
                           path, get_regexp_pattern(res_regex), res_options))
                    resp = response.HTTPResponse()
                    set_cors_headers(req, resp, context, res_options)
                    return resp
            else:
                debug('No CORS rule matches')
    return cors_request_middleware
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号