[英]Is it possible to set headers inside an Apache output filter?
We're using Apache 2.4 with mod_python, which is used for an output filter that is rewriting some of the HTML output. 我们将Apache 2.4与mod_python结合使用,该模块用于输出过滤器,用于重写一些HTML输出。 We're currently setting cookies using document.cookie in JS, but this isn't optimal. 我们目前正在JS中使用document.cookie设置cookie,但这并不是最佳选择。 We'd ideally like to set cookies via headers. 理想情况下,我们希望通过标题设置cookie。 We've tried using filter.req.headers_out['SetCookie']
and Cookie.add_cookie
, but to no avail. 我们尝试使用filter.req.headers_out['SetCookie']
和Cookie.add_cookie
,但无济于事。
Is this even possible? 这有可能吗? If not, what's a better option? 如果没有,还有什么更好的选择? We're stuck with Apache 2.4 and mod_python as our only options. 我们只能使用Apache 2.4和mod_python。
Available Apache modules: 可用的Apache模块:
Loaded Modules: 加载的模块:
How I'm currently trying to set cookies (in dev): 我目前如何尝试设置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)
I figured this out on my own. 我自己解决了这个问题。 The short version is, yes you can. 简短的版本是,可以。 The reason it wasn't working for me before was the location I was setting cookies was incorrect. 之前对我不起作用的原因是我设置Cookie的位置不正确。 I moved that bit from the HTML processing area (where it didn't belong anyway) and put it directly in the outputfilter
method. 我从HTML处理区域(无论如何不属于该区域)移动了这一位,并将其直接放在outputfilter
方法中。
I hope this helps someone. 我希望这可以帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.