Skip navigation.

My 2008 Resolution: To Be More EclecticAll recent postsPhilly Code Camp Sample Code

So How Do You Set The Last-Modified Header? Round II

Right went I thought I was done messing with the HttpCachePolicy class and the Last-Modified / If-Modified-Since headers, I ran into another fun issue.

Suppose you want contents of a file cached on the client. The idea is to set the Last-Modified HTTP header, as described in the previous post. The browser will then send an If-Modified-Since header the next time this file is requested. You compare the two dates, and if they are one and the same, you send a 403 (“Not Modified”) status code back. If the file had changed, you stream new contents back to the client, and the cycle repeats.

This is precisely the approach I use with “variables” in style sheets. Here’s a stripped down snippet:

FileInfo  fi = new FileInfo (templateFile);
DateTime  utcLastWritten = fi.LastWriteTimeUtc;

if (contentHandler.ContentIsUpToDate (utcLastWritten))
{
   contentHandler.NotifyClient_ContentIsUpToDate ();
   return;
}

// ... read contents of a file into the css variable
contentHandler.CacheContentOnClient (utcLastWritten);
contentHandler.WriteContentAsCss (css);

Note that I’m taking the LastWriteTimeUtc attribute of the physical file and compare it with the date from the If-Modified-Since header. Here’s the bare-bones request/response log:

Initial request

GET /styles.aspx?main HTTP/1.1
Accept: */*
...

HTTP/1.1 200 OK
Cache-Control: public
Last-Modified: Tue, 30 Oct 2007 18:46:52 GMT
Content-Type: text/css; charset=utf-8
...

Subsequent requests

GET /styles.aspx?main HTTP/1.1
Accept: */*
If-Modified-Since: Tue, 30 Oct 2007 18:46:52 GMT
...

HTTP/1.1 304 Not Modified
...

Note that the date must be in the RFC1123 format which corresponds to the “r” and “R” format strings in .NET. If you peek into HttpCachePolicy with Relfector again, you see this:

internal static string FormatHttpDateTimeUtc(DateTime dt)
{
   return dt.ToString("R", DateTimeFormatInfo.InvariantInfo);
}

In this case, the method yeilds “Tue, 30 Oct 2007 18:46:52 GMT” per the spec, i.e. “ddd, dd MMM yyyy HH’:’mm’:’ss ’GMT’”. Imagine me goings slightly insane when this date was failing to pass an equality test against the exact same date from If-Modified-Since. You look at the two dates: they are exactly the same, yet something is off.

Long story short, the file’s LastWriteTimeUtc had milliseconds (naturally!). If you look closely, the outgoing date doesn’t.

The solution is simply to rebuild LastWriteTimeUtc without milliseconds.

Related Reading

Caching Tutorial for Web Authors and Webmasters

Comments

No comments yet

Emails and Notifications

Would you like to be notified when somebody responds to this post? 

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):