Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to remove a Pragma no-cache response header once it has been set by SetCacheability method?

I have an MVC4 GET action method that returns a FileStreamResult. A requirement exists to only use SSL and to not allow caching of the served document so SSL it is and I've also used the OutputCache filter with the following properties:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None", Location = OutputCacheLocation.None)]

This behaves as expected and produces the following response headers:

Cache-Control: no-cache, no-store
Expires: -1
Pragma: no-cache

All was well until asked to also support IE8 and as many here have also encountered the documents just won't download with both no-cache set and SSL in the mix. The workaround for IE8 and below is to add some registry setting which is not really viable, or to remove the no-cache headers which breaks a fundamental requirement.

I experimented with Fiddler and IE8 and was able to download a document if I just removed the pragma: no-cache header but left the Cache-Control header intact. This didn't appear to leave a copy of the document in my temporary internet files but I might need to test this some more.

With this information in mind I thought it might be a simple task to remove the pragma using a filter on the action but it seems no matter what I do I cannot change whatever the OutputCache is going to set. I've even removed the OutputCache attribute and used:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Using this method alone ensures I get the same cache settings as before but they are not set at the point of this method call. This merely sets up the cache policy which gets applied at some point in the response pipeline but I just don't know where.

Does anyone know if there is a way of hooking into the response pipeline to alter the cache headers as they are being written?

EDIT I've added a simple custom IHttpModule into the pipeline that looks for and removes any pragma header in the response NameValueCollection and whilst the cache-control is set the pragma is not there. Does this mean that IIS 7.5 is inserting the pragma itself based upon what it sees in the cache-control perhaps? I know for sure I have not set anything beyond defaults for a simple web site.

EDIT Checked the Cache-Control header value within the module and it is set private so the cache headers haven't been applied to the response yet. So it would appear the cache headers get added after modules are executed perhaps?

like image 212
Mark Avatar asked Oct 11 '12 13:10

Mark


People also ask

How do you fix incomplete or no-cache-control and pragma HTTP header set?

Solution. – Make sure the 'Cache-control' HTTP header is set with 'no-cache, no-store, must-revalidate' and the 'Pragma' header is set to 'no-cache' on HTTP response where possible.

What is Pragma no-cache header?

"The Pragma: no-cache header field is an HTTP/1.0 header intended for use in requests. It is a means for the browser to tell the server and any intermediate caches that it wants a fresh version of the resource, not for the server to tell the browser not to cache the resource.

What is the use of pragma no-cache?

This allows the client to request the most up-to-date document from the original web server, without receiving a cached copy from an intermediate proxy server. The Pragma header is an HTTP 1.0 feature, and is maintained in HTTP 1.1 for backward compatibility.

How can set cache-control no-cache No-store in asp net?

AppendHeader("Pragma", "no-cache"); Response. AppendHeader("Expires", "0"); The first line sets Cache-control to no-cache , and the second line adds the other attributes no-store, must-revalidate . This may not be the only way, but does provide an alternative method if the more straightforward Response.


1 Answers

I was troubleshooting this same issue and ran into the same issue removing the pragma header. When .NET renders a Page object, it outputs the cache headers. The cache handling is controlled by an HttpModule. I've tried several ways to remove the pragma header, but to no avail.

One method I haven't tried yet that looks like it might work, but also looks like a PITA is to implement a filter on the Response output stream via Response.Filter = new MyCustomFilter(...).

Prior to this I tried checking the headers in various locations, but the output cache processing had not been executed yet and pragma header did not exist and so could not be removed. Notably the HttpApplication event PreSendRequestHeaders did not work.

Some other options include implementing your own OutputCache module instead of using the built-in framework version, or somehow overriding the System.Web.HttpCachePolicy class where the pragma header is rendered.

The pragma header is rendered as part of the HttpCacheability.NoCache option:

  if (httpCacheability == HttpCacheability.NoCache || httpCacheability == HttpCacheability.Server)
  {
    if (HttpCachePolicy.s_headerPragmaNoCache == null)
      HttpCachePolicy.s_headerPragmaNoCache = new HttpResponseHeader(4, "no-cache");
    this._headerPragma = HttpCachePolicy.s_headerPragmaNoCache;
    if (this._allowInHistory != 1)
    {
      if (HttpCachePolicy.s_headerExpiresMinus1 == null)
        HttpCachePolicy.s_headerExpiresMinus1 = new HttpResponseHeader(18, "-1");
      this._headerExpires = HttpCachePolicy.s_headerExpiresMinus1;
    }
  }

The only pragmatic option I've found is to set the cache-control to private and also set a short expiration for the URL. It doesn't address the root cause on either end, but it does end up with almost the same desired effect.

like image 72
ulty4life Avatar answered Oct 12 '22 20:10

ulty4life