Bug 468594 - Fix heuristic query freshness; r=cbiesinger
authorBjarne <bjarne@runitsoft.com>
Tue, 17 Mar 2009 15:23:01 +0100
changeset 26252 01987cfc569b7a2eb76e3dc1089bf5483012353c
parent 26251 95497d18ce825f6d1b2e896a5793ad5ac0d3847f
child 26253 0659666e3aa4fa0cfd9ea55b935ad9db9f68bd8f
push id5975
push usersgautherie.bz@free.fr
push dateTue, 17 Mar 2009 14:23:33 +0000
treeherdermozilla-central@01987cfc569b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscbiesinger
bugs468594
milestone1.9.2a1pre
Bug 468594 - Fix heuristic query freshness; r=cbiesinger
netwerk/protocol/http/src/nsHttpChannel.cpp
netwerk/test/unit/test_bug468594.js
--- a/netwerk/protocol/http/src/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/src/nsHttpChannel.cpp
@@ -2062,16 +2062,38 @@ nsHttpChannel::CheckCache()
                 doValidation = fromPreviousSession;
         }
         else
             doValidation = PR_TRUE;
 
         LOG(("%salidating based on expiration time\n", doValidation ? "V" : "Not v"));
     }
 
+    // RFC 2616, section 13.9 states that GET-requests with a query-url
+    // MUST NOT be treated as fresh unless the server explicitly provides
+    // an expiration-time in the response. See bug #468594
+    // Section 13.2.1 (6th paragraph) defines "explicit expiration time"
+    if (!doValidation && mRequestHead.Method() == nsHttp::Get) {
+        nsCAutoString query;
+        nsCOMPtr<nsIURL> url = do_QueryInterface(mURI);
+        rv = url->GetQuery(query);
+        if (NS_SUCCEEDED(rv) && !query.IsEmpty()) {
+            PRUint32 tmp; // we don't need the value, just whether it's set
+            rv = mCachedResponseHead->GetExpiresValue(&tmp);
+            if (NS_FAILED(rv)) {
+                rv = mCachedResponseHead->GetMaxAgeValue(&tmp);
+                if (NS_FAILED(rv)) {
+                    LOG(("Validating based on RFC 2616 section 13.9 "
+                         "(query-url w/o explicit expiration-time)\n"));
+                    doValidation = PR_TRUE;
+                }
+            }
+        }
+    }
+    
     if (!doValidation) {
         //
         // Check the authorization headers used to generate the cache entry.
         // We must validate the cache entry if:
         //
         // 1) the cache entry was generated prior to this session w/
         //    credentials (see bug 103402).
         // 2) the cache entry was generated w/o credentials, but would now
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit/test_bug468594.js
@@ -0,0 +1,132 @@
+//
+// This script emulates the test called "Freshness"
+// by Mark Nottingham, located at
+//
+// http://mnot.net/javascript/xmlhttprequest/cache.html
+//
+// The issue with Mr. Nottinghams page is that the server
+// always seems to send an Expires-header in the response,
+// breaking the finer details of the test. This script has
+// full control of response-headers, however, and can perform
+// the intended testing plus some extra stuff.
+//
+// Please see RFC 2616 section 13.2.1 6th paragraph for the
+// definition of "explicit expiration time" being used here.
+
+do_import_script("netwerk/test/httpserver/httpd.js");
+
+var httpserver = new nsHttpServer();
+var index = 0;
+var tests = [
+    {url: "/freshness",   server: "0", expected: "0"},
+    {url: "/freshness",   server: "1", expected: "0"}, // cached
+
+    // RFC 2616 section 13.9 2nd paragraph
+    {url: "/freshness?a", server: "2", expected: "2"},
+    {url: "/freshness?a", server: "3", expected: "3"},
+
+    // explicit expiration dates in the future should be cached
+    {url: "/freshness?b", server: "4", expected: "4",
+     responseheader: "Expires: "+getDateString(1)},
+    {url: "/freshness?b", server: "5", expected: "4"},// cached due to Expires
+    
+    {url: "/freshness?c", server: "6", expected: "6",
+     responseheader: "Cache-Control: max-age=3600"},
+    {url: "/freshness?c", server: "7", expected: "6"}, // cached due to max-age
+
+    // explicit expiration dates in the past should NOT be cached
+    {url: "/freshness?d", server: "8", expected: "8",
+     responseheader: "Expires: "+getDateString(-1)},
+    {url: "/freshness?d", server: "9", expected: "9"},
+    
+    {url: "/freshness?e", server: "10", expected: "10",
+     responseheader: "Cache-Control: max-age=0"},
+    {url: "/freshness?e", server: "11", expected: "11"},
+
+    {url: "/freshness",   server: "99", expected: "0"}, // cached
+];
+
+function getCacheService()
+{
+    return Components.classes["@mozilla.org/network/cache-service;1"].
+                      getService(Components.interfaces.nsICacheService);
+}
+
+function logit(i, data) {
+    dump(tests[i].url + "\t requested [" + tests[i].server + "]" +
+         " got [" + data + "] expected [" + tests[i].expected + "]");
+    if (tests[i].responseheader)
+        dump("\t[" + tests[i].responseheader + "]");
+    dump("\n");
+}
+
+function setupChannel(suffix, value) {
+    var ios = Components.classes["@mozilla.org/network/io-service;1"].
+                         getService(Ci.nsIIOService);
+    var chan = ios.newChannel("http://localhost:4444" + suffix, "", null);
+    var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
+    httpChan.requestMethod = "GET";
+    httpChan.setRequestHeader("x-request", value, false);
+    return httpChan;
+}
+
+function triggerNextTest() {
+    var channel = setupChannel(tests[index].url, tests[index].server);
+    channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null),null);
+}
+
+function checkValueAndTrigger(request, data, ctx) {
+    logit(index, data);
+    do_check_eq(tests[index].expected, data);
+
+    if (index < tests.length-1) {
+        index++;
+        triggerNextTest();
+    } else {
+        do_test_finished();
+        httpserver.stop();
+    }
+}
+
+function run_test() {
+    httpserver.registerPathHandler("/freshness", handler);
+    httpserver.start(4444);
+
+    // clear cache
+    getCacheService().
+        evictEntries(Components.interfaces.nsICache.STORE_ANYWHERE);
+    triggerNextTest();
+
+    do_test_pending();
+}
+
+function handler(metadata, response) {
+    var body = metadata.getHeader("x-request");
+    response.setHeader("Content-Type", "text/plain", false);
+    response.setHeader("Date", getDateString(0), false);
+    
+    var header = tests[index].responseheader;
+    if (header == null) {
+        response.setHeader("Last-Modified", getDateString(-1), false);
+    } else {
+        var splitHdr = header.split(": ");
+        response.setHeader(splitHdr[0], splitHdr[1], false);
+    }
+    
+    response.setStatusLine(metadata.httpVersion, 200, "OK");
+    response.bodyOutputStream.write(body, body.length);
+}
+ 
+function getDateString(yearDelta) {
+    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
+                  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
+    var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
+
+    var d = new Date();
+    return days[d.getUTCDay()] + ", " +
+            d.getUTCDate() + " " +
+            months[d.getUTCMonth()] + " " +
+            (d.getUTCFullYear() + yearDelta) + " " +
+            d.getUTCHours() + ":" + d.getUTCMinutes() +":" +
+            d.getUTCSeconds() + " UTC";
+}