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.

About these ads

17 Responses to “Conditional GET Request”

  1. GET / HTTP/1.1 » Blog Archive » Ajax Mistakes Says:

    […] 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 […]

  2. George Forma Says:

    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

  3. Chris Says:

    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.

  4. gem : rufus-verbs « processi Says:

    […] your resource server allows it, you can take advantage of conditional GETs via rufus-verbs’ ConditionalEndPoint […]

  5. diamonds Says:

    How can the ETag be implemented?

  6. Nico Steiner - Frontend-Entwicklung und -Architektur Says:

    Expires Header richtig verstehen…

    Anhand eines unserer aktuellen Projekte möchte ich zeigen, wie die genannte Technologie in Hinblick auf die Performance einer Website zu verstehen ist. Folgende Statistik habe ich mit der Firefox Extension YSlow erstellt. Oben ist die Verteilung der Re…

  7. Sun Yi Says:

    Nice post, but I was wondering if the same conditional get applies to RSS? I’m currently working on a project and was looking for ways to handle RSS downloads by downloading only when there’s an update since last checked.

    Any hints on how to go about it?

  8. valashiya Says:

    Hmm, you have an interesting post, but unfortunately it is just all theoretical explanation and no practical example. I just tried following your explanation but ran into issues as not all RSS and HTTPs support Conditional GET.

    It means your last comment is misleading. Would be nice to see a code sample on how you have used Conditional GET yourself.

    • ruturajv Says:

      For using Conditional GET, we need to do 2 important things
      1. Save the Last-Modified or the ETag of the request (the first time)
      2. Every subsequent request should be sent using.. If-Modified-Since header

      So the first time the request is made, it can be cached with its ‘attributes’ ie the ETag and the Last-Modified time. in side a DB or a flat file, Next time u call it, just pick the attributes from the cache first and then send the request.

      Remember, not all Web Frameworks work with Conditional GET, so don’t be disappointed if some fail to return a 304 Not Modified even if the content hasn’t changed.

  9. G.K. Says:

    I have a general guestion about the “conditional GET”:
    It is supposed that the proxy server is used in order to avoid a new connection between the client and the remote server every time the client request a file. If this file is already in the proxy then the proxy will send it to the client. In this way, the traffic on the public network is reduced and, also, the total time to obtain the file is reduced.
    In the case of “conditional GET”, the proxy “asks” the remote server if the specific (requested) file is modified. And if it is so then it will be re-sent. My qestion is: If this happens in every reguested file then we don’t avoid the continuous communication between the proxy and the remote server. So how the traffic or the service time are reduced??? (The only benefit is that the file itself will not be re-sent if it’s not modified… But the connection exists and reqest-responce messages are exchanged between them…)
    Is the proxy set for sending “conditional GET” only for specific types of files??? (E.g. it could send this in the case of documents and not in the case of photos.) Is this the case??? (If so then the connections between the proxy and the remote server will be much less.)

    • ruturajv Says:

      Requests
      A conditional request is a deliberate request made by the client, ’cause its unsure of its state of the cache/document. So a Request HAS to be made to validate the cache

      Proxy and Connection
      The conditional request happens between the Browser/Client and the Proxy as well, if the proxy is a good one it will stick to the protocol of sending the conditional request response.

  10. ou Says:

    Thanks for cleanly explanation

  11. Analysis of HTTP-based cache control support in Linked Data servers « SourceForge.net: Project squin Says:

    […] supports the different options for cache control that are provided by the HTTP protocol (e.g. conditional GET, Last-Modified headers, ETag headers, etc.). During the preparation of this talk, I was curious […]

  12. minslovey's me2day Says:

    민달의 생각…

    HTTP 304 NOT_MOFIDIED가 동작하는 방식에 관한 자세하고 명쾌한 설명…

  13. Jiropole Says:

    Thanks, this really helped me optimize my project via HTTP mechanisms — pretty new to me!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: