繁体   English   中英

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

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

我们将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(共享)
  • replace_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方法中。

我希望这可以帮助别人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM