In our Asp.Net Application we have an IHttpHandler handling requests for Images. The Handler is called with a special id which identifies an image in our Image Cache. Once an image is put into the cash it never changes. My question is:
Is it okay to always return the http status code 304 for requests with a "If-Modified-Since" header without actually checking the given date? The reasoning is that the browser must already have a copy of the image (since it provided the modified-since header).
This would make life easier, because we do not (yet) track the image creation date.
Here is the actual code (Update: I included the if-modified header now in the server response as recommended by Aristos):
public void ProcessRequest(HttpContext context)
{
if (!String.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"])) {
//Is this okay?
context.Response.StatusCode = 304;
context.Response.StatusDescription = "Not Modified";
return;
}
var thumbnailId = context.Request.QueryString["thumbnail"];
using (var thumbnailCache = new CThumbnailCache()) {
var imageBytes = thumbnailCache.GetImageById(thumbnailId);
context.Response.ContentType = "image/png";
var outputStream = context.Response.OutputStream;
outputStream.Write(imageBytes, 0, imageBytes.Count());
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetLastModified(DateTime.UtcNow);
// added after Aristos post
context.Response.AddHeader("If-Modified-Since", DateTime.UtcNow.ToString("r"));
const int maxAge = 86400 * 14; // 14 Tage
context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(maxAge));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 0, maxAge));
context.Response.CacheControl = "private";
context.Response.Cache.SetValidUntilExpires(true);
}
}
The HTTP 304 Not Modified client redirection response code indicates that there is no need to retransmit the requested resources. It is an implicit redirection to a cached resource.
An HTTP 304 not modified status code means that the website you're requesting hasn't been updated since the last time you accessed it. Typically, your browser will save (or cache) web pages so it doesn't have to repeatedly download the same information.
The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.
its ok, from the moment you get the If-Modified-Since
the resource is on browser and you decide if you let it be as it is.
I like here to say you two thinks. You do not seems that you set this header so you probably not get it by the browser.
To make this actual works you need to add this line
context.Response.AddHeader("If-Modified-Since", LastModifledOfImage.ToString());
or as you do send the current date-time:
context.Response.AddHeader("If-Modified-Since", DateTime.UtcNow.ToString());
when you send the image.
I see that you use the SetLastModified
but this is set the Last-Modified
header not the one you check.
Consider to make actually static this images if possible because even this If-Modified-Since
make one call to the server, but the actually static resource gets the image direct from the browser cache with out asking the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With