pf » Supporting If-Modified-Since HTTP header in CFML

Supporting If-Modified-Since HTTP header in CFML

coldfusion

The If-Modified-Since header in the HTTP protocol allows user agents (typically RSS aggregators, or Spiders) to check and see if the content they are about to download has changed since their last visit. The user agent sends something like this in the headers:

If-Modified-Since: Fri, 18 Feb 2005 19:24:26 GMT

You can access this header from CFML, compare the date your content was last changed, and then if the content was not modified since the date passed, you return a 304 status code. If the content has been modified, you just return the content as you normally would. Here's some example CFML you can put at the top of your page, you need to update the pageModificationDate:

<cftry>
<cfif StructKeyExists(GetHttpRequestData().headers, "If-Modified-Since")>
	<cfset ifmodDate = ParseDateTime(GetHttpRequestData().headers["If-Modified-Since"])>
	<cfset pageModificationDate = GetHttpTimeString(ParseDateTime(Now()))>
	<cfif DateDiff("n", pageModificationDate, ifmodDate) GT 0>
		<cfheader statuscode="304" statustext="Not Modified"> 
		<cfabort>
	</cfif>
</cfif>
<cfcatch type="any"></cfcatch>
</cftry>

I use the GetHttpRequestData() function to get the value of the If-Modified-Since header (notice that I have to use StructKeyExists instead of IsDefined due to the dash - in the header name).

Also note that the dates need to be formatted properly, use the GetHttpTimeString() function for this.

I also put in a try/catch block to catch any date parsing errors, that way if there is an error parsing a date sent by a bad client, they still get the content, you may decide to log the error, or give a 500 level status code to such clients.

I implemented this with my RSS feed for this blog, so I should see a decrease in bandwidth on my RSS feed. Well written aggregators will only get content when my feed is updated, and just the headers otherwise.



Related Entries
3 people found this page useful, what do you think?

Trackback Address: 235/D4A8A77B57B94B32D52D62D9D25FAEE5
On 02/20/2005 at 3:47:54 AM MST Roger Benningfield wrote:
1
Pete: A couple notes.

(1) The provided code won't do much good, since you're not setting Last-Modified in the first place. Every request to your feed will continue to produce a 200, and thus a full bandwidth hit.

(2) If-Modified-Since is only half of Conditional GET. You also need to support Etags if you're really interested in minimizing excess 200s.

http://admin.mxblogspace.journurl.com/?mode=article&entry=1853

Once you have Etags working, the next step is RFC3229, which can be leveraged to deliver feed deltas to clients that send the appropriate header. (Most of them, at this point.) However, 3229 might be overkill for the average, individual blog... it's more useful for services like JournURL, that are producing thousands of different feeds.

On 02/24/2005 at 6:31:50 AM MST Anonymous wrote:
2
used in meta




  



Spell Checker by Foundeo





Subscribe to my RSS Feed: solosub RSS
Tags