是否可以在 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 模块:
加载的模块:
- access_compat_module(共享)
- alias_module(共享)
- auth_basic_module(共享)
- authn_core_module(共享)
- authn_file_module(共享)
- authz_core_module(共享)
- authz_host_module(共享)
- autoindex_module(共享)
- cgi_module(共享)
- core_module(静态)
- deflate_module(共享)
- dir_module(共享)
- env_module(共享)
- expires_module(共享)
- filter_module(共享)
- headers_module(共享)
- http_module(静态)
- include_module(共享)
- log_config_module(共享)
- mime_module(共享)
- mpm_prefork_module(共享)
- negotiation_module(共享)
- php7_module(共享)
- proxy_http_module(共享)
- proxy_module(共享)
- python_module(共享)
- remoteip_module(共享)
- rewrite_module(共享)
- setenvif_module(共享)
- so_module(静态)
- ssl_module(共享)
- substitute_module(共享)
- systemd_module(共享)
- unique_id_module(共享)
- unixd_module(共享)
- vhost_alias_module(共享)
- watchdog_module(共享)
我目前尝试设置 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
方法中。
我希望这对某人有所帮助。
我们将 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 模块:
加载的模块:
- access_compat_module(共享)
- alias_module(共享)
- auth_basic_module(共享)
- authn_core_module(共享)
- authn_file_module(共享)
- authz_core_module(共享)
- authz_host_module(共享)
- autoindex_module(共享)
- cgi_module(共享)
- core_module(静态)
- deflate_module(共享)
- dir_module(共享)
- env_module(共享)
- expires_module(共享)
- filter_module(共享)
- headers_module(共享)
- http_module(静态)
- include_module(共享)
- log_config_module(共享)
- mime_module(共享)
- mpm_prefork_module(共享)
- negotiation_module(共享)
- php7_module(共享)
- proxy_http_module(共享)
- proxy_module(共享)
- python_module(共享)
- remoteip_module(共享)
- rewrite_module(共享)
- setenvif_module(共享)
- so_module(静态)
- ssl_module(共享)
- substitute_module(共享)
- systemd_module(共享)
- unique_id_module(共享)
- unixd_module(共享)
- vhost_alias_module(共享)
- watchdog_module(共享)
我目前尝试设置 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
方法中。
我希望这对某人有所帮助。