【问题标题】:Is it possible to set headers inside an Apache output filter?是否可以在 Apache 输出过滤器中设置标题?
【发布时间】:2019-02-12 03:15:14
【问题描述】:

我们使用带有 mod_python 的 Apache 2.4,它用于重写一些 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】:

    这是我自己想出来的。简短的版本是,是的,你可以。之前它对我不起作用的原因是我设置 cookie 的位置不正确。我将那部分从 HTML 处理区域(它本来不属于的地方)移到了 outputfilter 方法中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多