是否可以在 Apache 输出过滤器中设置 headers?

Is it possible to set headers inside an Apache output filter?

我们将 Apache 2.4 与 mod_python 一起使用,它用于重写某些 HTML 输出的输出过滤器。我们目前在 JS 中使用 document.cookie 设置 cookie,但这不是最佳选择。理想情况下,我们希望通过 headers 设置 cookie。我们已经尝试使用 filter.req.headers_out['SetCookie']Cookie.add_cookie,但无济于事。

这可能吗?如果没有,什么是更好的选择?我们坚持使用 Apache 2.4 和 mod_python 作为我们唯一的选择。


可用的 Apache 模块:

加载的模块:

我目前尝试设置 cookie 的方式(在开发中):

def add_cookie(req, name, value, domain=None, expires=None):
    """Adds a cookie
    Arguments:
        req -- the request
        name -- the cookie name
        value -- the cookie value
        domain -- (optional) the domain the cookie is applicable to
        expires -- (optional) the time in minutes the cookie is set to expire, defaults to current session
    """
    cookie = Cookie.Cookie(name, value)
    # Always set the path to the root
    cookie.path = '/'
    # Set domain if present
    if domain is not None:
        cookie.domain = domain
    # Set expires if present
    if expires is not None:
        expires = int(expires)
        cookie.expires = time.time() + (60 * expires)
    # Add a freshly-baked cookie
    Cookie.add_cookie(req, cookie)

这是我自己想出来的。简短的版本是,是的,你可以。它之前对我不起作用的原因是我设置 cookie 的位置不正确。我把那个位从 HTML 处理区域(无论如何它都不属于)移到 outputfilter 方法中。

我希望这对某人有所帮助。