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

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

提问人:WebNinja 提问时间:9/7/2018 最后编辑:Brian Tompsett - 汤莱恩WebNinja 更新时间:9/11/2018 访问量:257

问:

我们将 Apache 2.4 与 mod_python 一起使用,它用于重写某些 HTML 输出的输出过滤器。我们目前正在使用 JS 中的 document.cookie 设置 cookie,但这不是最佳选择。理想情况下,我们希望通过标头设置 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)
apache cookies mod-python mod-filter

评论


答:

1赞 WebNinja 9/7/2018 #1

我自己想通了。简短的版本是,是的,你可以。它以前对我不起作用的原因是我设置 cookie 的位置不正确。我从 HTML 处理区域(无论如何它都不属于那里)移动了它,并将其直接放在方法中。outputfilter

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