Conditional GET Request
The HTTP Protocol defines a caching mechanism, in which the proxy web-servers can cache pages, files, images etc. Since caching is in place, There is a method which the servers are asked to return the document, either the “cached” or “live” document.
This request of asking the server for a document considering a specific parameter is called a Conditional GET Request. In this request, a specific request header is sent If-Modified-Since. This header sends a RFC 2822 formatted date as the value. The proxy which is between the Server and the client checks the date, and the cached document, if the condition matches, A 304 Not Modified header is sent back to the client in the response.
So consider a document /sample.html on example.com. Consider the very first request of the Client, Since this is the first request, the client does not know about the modified time, etc…
Here goes the request header as follows…
GET /sample.html HTTP/1.1
Host: example.com
Now the response that comes from the server is the document with the response headers. The response headers would be…
HTTP/1.x 200 OK
Via: The-proxy-name
Content-Length: 32859
Expires: Tue, 27 Dec 2005 11:25:11 GMT
Date: Tue, 27 Dec 2005 05:25:11 GMT
Content-Type: text/html; charset=iso-8859-1
Server: Apache/1.3.33 (Unix) PHP/4.3.10
Cache-Control: max-age=21600
Last-Modified: Wed, 01 Sep 2004 13:24:52 GMT
Etag: “4135cda4″
Lets check what these headers mean..
- Cache-Control: It tells the client the maximum time in seconds to cache the document.
- Last-Modified: The document’s last modified date
- Etag: A unique hash for the document.
Once the Server responds, the client caches the document and stores it for the specified amount of time, In this case for 21600 seconds.
Next time when the user calls for the same document /sample.html within the specified cache time frame. The browser(client) will make a conditional get request, try to ask the server that if the document is modified after the specified time zone whose hashed value was the Etag value, ONLY THEN return a new document or else confirm that it is an old document.
So the request header would be as…
GET /sample.html HTTP/1.1
Host: example.com
If-Modified-Since: Wed, 01 Sep 2004 13:24:52 GMT
If-None-Match: “4135cda4″
The header is self explanatory, it is asking for a new document which modified after Wed, 01 Sep 2004 13:24:52. The If-None-Match specifies that the client has mapped the document with that Entity value. RFC 2616 specifies that if If-None-Match is not accompanied with If-Modified-Since then the server must not send a 304 (Not Modified) header.
The response to the above request would be as.
HTTP/1.x 304 Not Modified
Via: The-proxy-server
Expires: Tue, 27 Dec 2005 11:25:19 GMT
Date: Tue, 27 Dec 2005 05:25:19 GMT
Server: Apache/1.3.33 (Unix) PHP/4.3.10
Keep-Alive: timeout=2, max=99
Etag: “4135cda4″
Cache-Control: max-age=21600
The server confirms the that the document is not modified and sends a 304 Not Modified header.
The client checks the 304 response header, and renders the document from the cache.
January 11, 2006 at 10:23 am
[...] When calling for a Document, Page, etc. Its best to use a URL (A unique Identifier for that document). Its the way the web is structured. Try using Apache’s mod_rewrite to make the URL readable. DO NOT FETCH SUCH DOCUMENTS WITH AJAX.The standard GET request is structured in such a way that if you are opening the same page, document again. The browser may not waste the bandwidth at all. Unlike AJAX. The browser could simply display from the cache. See: Conditional GET Request [...]
January 24, 2007 at 10:10 pm
Nice overview and details, really clear explanation of the 304 issue.
You might enjoy this article about server-side caching http://www.askapache.com/2006/htaccess/speed-up-sites-with-htaccess-caching.html
August 13, 2007 at 10:06 pm
From your post: “RFC 2616 specifies that if If-None-Match is not accompanied with If-Modified-Since then the server must not send a 304 (Not Modified) header.”
I don’t think that’s what it says. I believe it says that if a client supplies both If-None-Match and If-Modified-Since, then both must match the expected value. But the client is not compelled to supply both.
January 18, 2008 at 6:55 am
[...] your resource server allows it, you can take advantage of conditional GETs via rufus-verbs’ ConditionalEndPoint [...]