HTTP 304 response handling in Sitecore MediaRequestHandler

Whilst testing a Sitecore 6.1 site due for release Firebug highlighted some unexpected behaviour from the Sitecore Media Request Handler which takes care of responding to requests for items held in the Sitecore Media Library. Media library items were being returned in full every time that they were requested, each returning a HTTP 200 response status code.

Further digging with Lutz Roeder’s excellent Reflector tool showed that the If-Modified-Since request header field was not being responded to correctly by the MediaRequestHandler. The If-Modified-Since request header is a HTTP-date value whose time component is in the format HH:MM:SS (i.e. no milliseconds). This header field value is used by the Modified method within the MediaRequestHandler to compare to the System.DateTime value that the media library item was last updated. As a System.DateTime value includes a milliseconds component the equality check fails for the majority of media library requests and as a result the item is downloaded to the browser in full.

Sitecore have confirmed that this is a known issue and something that will be fixed in a forthcoming release. In the meantime it’s not difficult to patch in a modified HTTP handler to respond to media library requests and implement HTTP 304 response handling appropriately. Alex de Groot has a good article on his blog that explains how Sitecore interacts with HTTP handlers for media handling.