gpt4 book ai didi

http - 我们如何跨所有浏览器控制网页缓存?

转载 作者:行者123 更新时间:2023-12-01 04:49:23 30 4
gpt4 key购买 nike

我们的调查表明,并非所有浏览器都以统一的方式遵守 HTTP 缓存指令。

出于安全原因,我们不希望缓存应用程序中的某些页面,曾经,通过网络浏览器。这必须至少适用于以下浏览器:

  • Internet Explorer 6+
  • 火狐 1.5+
  • Safari 3+
  • 歌剧9+
  • Chrome

  • 我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存页面。

    最佳答案

    介绍
    适用于所有提到的客户端(和代理)的正确的最小 header 集:

    Cache-Control: no-cache, no-store, must-revalidate
    Pragma: no-cache
    Expires: 0
    Cache-Control 根据客户端和代理的 HTTP 1.1 规范(并且 Expires 旁边的某些客户端隐式要求)。 Pragma 符合史前客户端的 HTTP 1.0 规范。 Expires 根据客户端和代理的 HTTP 1.0 和 1.1 规范。在 HTTP 1.1 中, Cache-Control优先于 Expires ,所以它毕竟仅适用于 HTTP 1.0 代理。
    如果您在仅使用 no-store 通过 HTTPS 提供页面时不关心 IE6 及其损坏的缓存,那么你可以省略 Cache-Control: no-cache .
    Cache-Control: no-store, must-revalidate
    Pragma: no-cache
    Expires: 0
    如果您不关心 IE6 或 HTTP 1.0 客户端(HTTP 1.1 于 1997 年引入),那么您可以省略 Pragma .
    Cache-Control: no-store, must-revalidate
    Expires: 0
    如果您也不关心 HTTP 1.0 代理,那么您可以省略 Expires .
    Cache-Control: no-store, must-revalidate
    另一方面,如果服务器自动包含一个有效的 Date标题,那么理论上你可以省略 Cache-Control也靠 Expires只要。
    Date: Wed, 24 Aug 2016 18:32:02 GMT
    Expires: 0
    但这可能会失败,例如最终用户操纵操作系统数据,而客户端软件依赖于它。
    其他 Cache-Control max-age等参数如果上述 Cache-Control 无关紧要参数被指定。 Last-Modified 此处包含在大多数其他答案中的标题只有在您 时才有趣其实想要缓存请求,因此您根本不需要指定它。
    如何设置?
    使用 PHP:
    header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
    header("Pragma: no-cache"); // HTTP 1.0.
    header("Expires: 0"); // Proxies.
    使用 Java Servlet 或 Node.js:
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    response.setHeader("Expires", "0"); // Proxies.
    使用 ASP.NET-MVC
    Response.Cache.SetCacheability(HttpCacheability.NoCache);  // HTTP 1.1.
    Response.Cache.AppendCacheExtension("no-store, must-revalidate");
    Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
    Response.AppendHeader("Expires", "0"); // Proxies.
    使用 ASP.NET Web API:
    // `response` is an instance of System.Net.Http.HttpResponseMessage
    response.Headers.CacheControl = new CacheControlHeaderValue
    {
    NoCache = true,
    NoStore = true,
    MustRevalidate = true
    };
    response.Headers.Pragma.ParseAdd("no-cache");
    // We can't use `response.Content.Headers.Expires` directly
    // since it allows only `DateTimeOffset?` values.
    response.Content?.Headers.TryAddWithoutValidation("Expires", 0.ToString());
    使用 ASP.NET:
    Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
    Response.AppendHeader("Expires", "0"); // Proxies.
    使用 ASP.NET Core v3
    // using Microsoft.Net.Http.Headers
    Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate";
    Response.Headers[HeaderNames.Expires] = "0";
    Response.Headers[HeaderNames.Pragma] = "no-cache";
    使用 ASP:
    Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" ' HTTP 1.1.
    Response.addHeader "Pragma", "no-cache" ' HTTP 1.0.
    Response.addHeader "Expires", "0" ' Proxies.
    使用 Ruby on Rails:
    headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
    headers["Pragma"] = "no-cache" # HTTP 1.0.
    headers["Expires"] = "0" # Proxies.
    使用 Python/Flask:
    response = make_response(render_template(...))
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
    response.headers["Pragma"] = "no-cache" # HTTP 1.0.
    response.headers["Expires"] = "0" # Proxies.
    使用 Python/Django:
    response["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
    response["Pragma"] = "no-cache" # HTTP 1.0.
    response["Expires"] = "0" # Proxies.
    使用 Python/金字塔:
    request.response.headerlist.extend(
    (
    ('Cache-Control', 'no-cache, no-store, must-revalidate'),
    ('Pragma', 'no-cache'),
    ('Expires', '0')
    )
    )
    使用 Go:
    responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
    responseWriter.Header().Set("Pragma", "no-cache") // HTTP 1.0.
    responseWriter.Header().Set("Expires", "0") // Proxies.
    使用 Clojure(需要 Ring utils):
    (require '[ring.util.response :as r])
    (-> response
    (r/header "Cache-Control" "no-cache, no-store, must-revalidate")
    (r/header "Pragma" "no-cache")
    (r/header "Expires" 0))
    使用 Apache .htaccess文件:
    <IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
    </IfModule>
    使用 HTML:
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    HTML 元标记与 HTTP 响应 header
    重要的是要知道,当 HTML 页面通过 HTTP 连接提供时,标题出现在 中。两者 HTTP 响应头和 HTML <meta http-equiv>标签,那么在 HTTP 响应头中指定的标签将优先于 HTML 元标签。 HTML 元标记仅在通过 file:// 从本地磁盘文件系统查看页面时使用。网址。另见 W3 HTML spec chapter 5.2.2 .当您不以编程方式指定它们时请注意这一点,因为网络服务器可以包含一些默认值。
    一般来说,你最好只是 不是 指定 HTML 元标记以避免初学者混淆并依赖硬 HTTP 响应 header 。此外,特别是那些 <meta http-equiv>标签是 invalid在 HTML5 中。只有 http-equiv HTML5 specification 中列出的值被允许。
    验证实际的 HTTP 响应 header
    要验证一个和另一个,您可以在 Web 浏览器的开发人员工具集的 HTTP 流量监视器中查看/调试它们。您可以通过在 Chrome/Firefox23+/IE9+ 中按 F12 到达那里,然后打开“网络”或“网络”选项卡面板,然后单击感兴趣的 HTTP 请求以显示有关 HTTP 请求和响应的所有详细信息。 below screenshot来自 Chrome:
    Chrome developer toolset HTTP traffic monitor showing HTTP response headers on stackoverflow.com
    我也想在文件下载中设置这些标题
    首先,此问答针对“网页”(HTML 页面),而不是“文件下载”(PDF、zip、Excel 等)。您最好将它们缓存起来,并利用 URI 路径或查询字符串中某处的某个文件版本标识符来强制重新下载已更改的文件。无论如何在文件下载上应用这些无缓存 header 时,请注意通过 HTTPS 而不是 HTTP 提供文件下载时的 IE7/8 错误。详情见 IE cannot download foo.jsf. IE was not able to open this internet site. The requested site is either unavailable or cannot be found .

    关于http - 我们如何跨所有浏览器控制网页缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15241551/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com