HttpCachePolicy in an IHttpModule

I’m having some trouble sorting out image caching for JPEGs that are served through an IHttpModule.

A quirk of the site I’m working on at the moment is that product images are served via a third-party image hosting provider (Scene7, who have been very helpful and so deserve a credit). In order to save having to reference the URI stem all over our code, I wrote an IHttpModule that parses all requests on the basis of:

   HttpContext context = ((HttpApplication) sender).Context;
   HttpRequest request = context.Request;

   Regex r = new Regex(
      @"/images/products/(?<sku>[A-Z0-9-]+)_(?<size>\d)\.jpg",
      RegexOptions.Compiled | RegexOptions.IgnoreCase);
   Match m = r.Match(request.RawUrl);

   if (m.Success && request.HttpMethod == "GET")
   {
      // Handle the Scene7-specific stuff
   }
   else
   {
      // Set cacheability
   }

Before I was doing anything in this else clause, Fiddler was showing me that the following headers were being sent with all non-Scene7 JPEGs:

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

Now, if I add the following two lines to my else clause:

   HttpCachePolicy cache = context.Response.Cache;
   cache.SetAllowResponseInBrowserHistory(true);

it gets rid of the Expires: -1 header. Adding any / all of the following, though, won’t shift the Pragma: and Cache-Control: headers — they all seem to have no effect on my actual HTTP transaction, so it’s still not being cached properly:

   cache.SetCacheability(HttpCacheability.Public);
   cache.SetMaxAge(new TimeSpan(30, 0, 0, 0));
   cache.SetExpires(DateTime.Now.AddDays(30));
   cache.SetLastModifiedFromFileDependencies();
   cache.SetValidUntilExpires(true);

Does anyone have any real experience with setting HttpCachePolicy properties from within an IHttpModule? Am I missing something really obvious?

If you don’t want to leave thoughts as a comment on here, feel free to email me (owen dot blacker at wheel dot co dot uk). Cross-posted to and .

Thanks everyone!

Advertisements

Ymatebwch

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s