检测补间中的静态资源

时间:2015-07-31 15:47:07

标签: pyramid

我正在写一个补间,我需要知道请求是否是静态资源(add_static_view)。我不想在静态资产上设置会话cookie,因为这会阻止缓存它们。在静态资产的响应处理中找出或清除set-cookie标头的好方法是什么?

实施例::

class ReferralCookieTweenFactory:
    """Tween to capture referral links and """
    def __init__(self, handler, registry):
        self.handler = handler
        self.registry = registry

    def __call__(self, request):

        if request.method == "GET":
            # We are only interested in incoming links with a referrer
            q_name = config.get_query_parameter_name(self.registry)
            ref = request.GET.get(q_name, None)

            # TODO: detect static resources here and bail out, as we don't want to 
            # do any sessions when the user is accessing static resources

            # We capture only the first referrer
            if not "referral" in request.session:
                # XXX create a session
                # set cookie, etc.

        response = self.handler(request)
        return response

1 个答案:

答案 0 :(得分:1)

如果您知道静态资源的路径前缀,只需检查request.path_info是否与静态资源的路由匹配。

对于生产环境,根本不使用wsgi服务器提供静态资源,而是将此任务留给http服务器,如nginx / apache。