0

This question is based on this article

response header

HTTP/1.1 200 OK
Last-Modified: Tue, 12 Dec 2006 03:03:59 GMT
ETag: "10c24bc-4ab-457e1c1f"
Content-Length: 12195

request header

GET /i/yahoo.gif HTTP/1.1
Host: us.yimg.com
If-Modified-Since: Tue, 12 Dec 2006 03:03:59 GMT
If-None-Match: "10c24bc-4ab-457e1c1f"    
HTTP/1.1 304 Not Modified

In this case browser is sending both If-None-Match and If-Modified-Since. My question is on the server side do I need to match BOTH etag and If-Modified-Since before I send 304.

Or

Should I just look at etag and send 304 if etag is a match. In this case I am ignoring If-Modified-Since .

4 Answers 4

2

Excerpt from RFC 2616 "Hypertext Transfer Protocol -- HTTP/1.1"

13.3.4 Rules for When to Use Entity Tags and Last-Modified Dates

...

An HTTP/1.1 origin server, upon receiving a conditional request that includes both a Last-Modified date (e.g., in an If-Modified-Since or If-Unmodified-Since header field) and one or more entity tags (e.g., in an If-Match, If-None-Match, or If-Range header field) as cache validators, MUST NOT return a response status of 304 (Not Modified) unless doing so is consistent with all of the conditional header fields in the request.

0

You only need to send and conversely check the pair in which you are interested. So, if you have an ETag, send an ETag header and check the If-None-Match header. If you have a Last-Modified date, send a Last-Modified date and check the If-Modified-Since header.

1
  • Question is about what the server should reply, not what the client should request. Apr 10, 2012 at 19:52
0

If you can get Last-Modified before generating the content, and you generate your etag from the content, you should check Last-Modified before content generation, and abort the generation if it match's to save sever recourse's espically with graphics operations, I have a PHP script that takes about .8s retrieve an image, here i use a sever cache an generate the image taking about 1.6s if its out of date, otherwise I return that, however, in the if-modified-since date is more than or equal to the modified date, i don't even retrive it from my sever cache, and finally I use an md5 checksum of the content as an etag, i the etag I generate match the If-None-Match request header, I don't bother sending the data, to save sever bandwidth.

1
  • Good setup, but MD5 sounds awefully expensive for that purpose
    – Joris
    Aug 19, 2010 at 3:23
0

The server can use either. Presumably since you provided both, you can theoretically check both on the server, and of course the browser is right to provide both as well.

You might as well check whichever is faster to check. If these are files on disk, for example, checking last-modification time (mtime) will be faster than MD5'ing the content.

But in other systems the MD5 can be stored somewhere as meta-data, e.g. in a database or a header like in S3, in which case the ETag might actually be just as fast.

Of course you're also not required to do anything. That is, you can elect to ignore either one and just return a normal 200 code with content. So if those things are inconvenient for you, no worries.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .