简体   繁体   English

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

[英]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: 加载的模块:

  • access_compat_module (shared) access_compat_module(共享)
  • alias_module (shared) alias_module(共享)
  • auth_basic_module (shared) auth_basic_module(共享)
  • authn_core_module (shared) authn_core_module(共享)
  • authn_file_module (shared) authn_file_module(共享)
  • authz_core_module (shared) authz_core_module(共享)
  • authz_host_module (shared) authz_host_module(共享)
  • autoindex_module (shared) autoindex_module(共享)
  • cgi_module (shared) cgi_module(共享)
  • core_module (static) core_module(静态)
  • deflate_module (shared) deflate_module(共享)
  • dir_module (shared) dir_module(共享)
  • env_module (shared) env_module(共享)
  • expires_module (shared) expires_module(共享)
  • filter_module (shared) filter_module(共享)
  • headers_module (shared) headers_module(共享)
  • http_module (static) http_module(静态)
  • include_module (shared) include_module(共享)
  • log_config_module (shared) log_config_module(共享)
  • mime_module (shared) mime_module(共享)
  • mpm_prefork_module (shared) mpm_prefork_module(共享)
  • negotiation_module (shared) negotiation_module(共享)
  • php7_module (shared) php7_module(共享)
  • proxy_http_module (shared) proxy_http_module(共享)
  • proxy_module (shared) proxy_module(共享)
  • python_module (shared) python_module(共享)
  • remoteip_module (shared) remoteip_module(共享)
  • rewrite_module (shared) rewrite_module(共享)
  • setenvif_module (shared) setenvif_module(共享)
  • so_module (static) so_module(静态)
  • ssl_module (shared) ssl_module(共享)
  • substitute_module (shared) replace_module(共享)
  • systemd_module (shared) systemd_module(共享)
  • unique_id_module (shared) unique_id_module(共享)
  • unixd_module (shared) unixd_module(共享)
  • vhost_alias_module (shared) vhost_alias_module(共享)
  • watchdog_module (shared) watchdog_module(共享)

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.

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