<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[173173] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/173173">173173</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2014-09-02 09:00:14 -0700 (Tue, 02 Sep 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>CachedResourceLoader should check redirections to reuse or not cached resources
https://bugs.webkit.org/show_bug.cgi?id=131757

Patch by Youenn Fablet &lt;youenn.fablet@crf.canon.fr&gt; on 2014-09-02
Reviewed by Antti Koivisto.

Source/WebCore:

Added cache-control redirection check to properly determine revalidation policy.
Tightened redirection cache-control header check by testing for no-cache and must-revalidate directives.
Added redirection freshness check.

Test: http/tests/cache/cache-redirections.html

* loader/cache/CachedRawResource.cpp:
(WebCore::CachedRawResource::canReuse): Removed redirection check (now handled by CachedResource::redirectChainAllowsReuse).
* loader/cache/CachedResource.cpp:
(WebCore::currentAge): Moved from WebCore::CachedResource::currentAge method to static function. Added response and responseTimestamp as parameters.
(WebCore::CachedResource::CachedResource): Initialized redirection chain status (no redirection and expiracy date set to never).
(WebCore::CachedResource::isExpired): Updated according new currentAge/freshnessLifetime method parameters.
(WebCore::CachedResource::freshnessLifetime): Added response as parameter.
(WebCore::CachedResource::willSendRequest): Computes whether a redirection can be cached, and if cached for how long it will remain fresh.
(WebCore::CachedResource::redirectChainAllowsReuse): Return false if any of the redirection in the redirection chain cannot be cached or expired.
* loader/cache/CachedResource.h: Added cache chain member that stores whether the redirection chain can be cached and if so when it will be expired.
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::determineRevalidationPolicy): Added check of the redirection chain.

LayoutTests:

Added test checks that fresh redirections allow reuse of cached resoure and expired or not cacheable redirections trigger reloading of resources.

* http/tests/cache/cache-redirections-expected.txt: Added.
* http/tests/cache/cache-redirections.html: Added.
* http/tests/cache/resources/cache-control-redirect.php: Added.
* http/tests/cache/resources/cacheable-random-text.php: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedRawResourcecpp">trunk/Source/WebCore/loader/cache/CachedRawResource.cpp</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourcecpp">trunk/Source/WebCore/loader/cache/CachedResource.cpp</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourceh">trunk/Source/WebCore/loader/cache/CachedResource.h</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourceLoadercpp">trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestshttptestscachecacheredirectionsexpectedtxt">trunk/LayoutTests/http/tests/cache/cache-redirections-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestscachecacheredirectionshtml">trunk/LayoutTests/http/tests/cache/cache-redirections.html</a></li>
<li><a href="#trunkLayoutTestshttptestscacheresourcescachecontrolredirectphp">trunk/LayoutTests/http/tests/cache/resources/cache-control-redirect.php</a></li>
<li><a href="#trunkLayoutTestshttptestscacheresourcescacheablerandomtextphp">trunk/LayoutTests/http/tests/cache/resources/cacheable-random-text.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (173172 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/LayoutTests/ChangeLog        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2014-09-02  Youenn Fablet  &lt;youenn.fablet@crf.canon.fr&gt;
+
+        CachedResourceLoader should check redirections to reuse or not cached resources
+        https://bugs.webkit.org/show_bug.cgi?id=131757
+
+        Reviewed by Antti Koivisto.
+
+        Added test checks that fresh redirections allow reuse of cached resoure and expired or not cacheable redirections trigger reloading of resources.
+
+        * http/tests/cache/cache-redirections-expected.txt: Added.
+        * http/tests/cache/cache-redirections.html: Added.
+        * http/tests/cache/resources/cache-control-redirect.php: Added.
+        * http/tests/cache/resources/cacheable-random-text.php: Added.
+
</ins><span class="cx"> 2014-09-02  Manuel Rego Casasnovas  &lt;rego@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [CSS Grid Layout] Test coverage for first-line pseudo-element
</span></span></pre></div>
<a id="trunkLayoutTestshttptestscachecacheredirectionsexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/cache/cache-redirections-expected.txt (0 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/cache/cache-redirections-expected.txt                                (rev 0)
+++ trunk/LayoutTests/http/tests/cache/cache-redirections-expected.txt        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -0,0 +1,4 @@
</span><ins>+ 
+
+PASS Testing cache reloading strategy according redirections with different cache state 
+
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestscachecacheredirectionshtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/cache/cache-redirections.html (0 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/cache/cache-redirections.html                                (rev 0)
+++ trunk/LayoutTests/http/tests/cache/cache-redirections.html        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -0,0 +1,73 @@
</span><ins>+&lt;html&gt;
+&lt;head&gt;
+   &lt;title&gt;Testing cache reloading strategy according redirections with different cache state&lt;/title&gt;
+    &lt;script src=&quot;/js-test-resources/testharness.js&quot;&gt;&lt;/script&gt;
+    &lt;script src=&quot;/js-test-resources/testharnessreport.js&quot;&gt;&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;iframe id=&quot;iframe1&quot;&gt;&lt;/iframe&gt;
+&lt;iframe id=&quot;iframe2&quot;&gt;&lt;/iframe&gt;
+&lt;div id=&quot;log&quot;&gt;&lt;/div&gt;
+&lt;script&gt;
+
+var test = async_test();
+test.step(function() {
+
+var counter = 0;
+var resource = &quot;No&quot;;
+var baseURL = &quot;resources/cache-control-redirect?code=302&amp;url=cacheable-random-text.php&amp;random_id=1&quot;;
+var testCases = [
+{name : &quot;Should reuse resource (cacheable redirection)&quot;, expected : true, url : baseURL + &quot;&amp;cache_control=public, max-age=1000&quot;, reloadDelay : 0},
+{name : &quot;Should reload resource (no-store redirection)&quot;, expected : false, url: baseURL + &quot;&amp;cache_control=no-store&amp;max_age=1000&quot;, reloadDelay : 0},
+{name : &quot;Should reload resource (no-cache redirection)&quot;, expected : false, url: baseURL + &quot;&amp;cache_control=no-cache&amp;max_age=1000&quot;, reloadDelay : 0},
+{name : &quot;Should reload resource (must-revalidate redirection)&quot;, expected : false, url: baseURL + &quot;&amp;cache_control=must-revalidate&amp;max_age=1000&quot;, reloadDelay : 0},
+{name : &quot;Should reload resource (expired redirection)&quot;, expected : false,  url: baseURL + &quot;&amp;cache_control=public, max-age=1&quot;, reloadDelay : 1000},
+];
+
+function loadResource()
+{
+    setFrame1Loader();
+        document.getElementById('iframe1').src = testCases[counter].url;
+}
+
+function reloadResource()
+{
+    setFrame2Loader();
+        document.getElementById('iframe2').src  = testCases[counter].url;
+}
+
+function setFrame1Loader() 
+{
+    document.getElementById('iframe2').onload  = null;
+    document.getElementById('iframe1').onload  = test.step_func(function()
+    {
+        resource = document.getElementById('iframe1').contentDocument.body.innerHTML;
+        setTimeout(test.step_func(reloadResource), testCases[counter].reloadDelay);
+    });
+}
+
+function setFrame2Loader() 
+{
+    document.getElementById('iframe1').onload  = null;
+    document.getElementById('iframe2').onload  = test.step_func(function ()
+    {
+        var testResult = resource == document.getElementById('iframe2').contentDocument.body.innerHTML;
+        assert_true(testResult == testCases[counter].expected, testCases[counter].name);
+
+        document.getElementById('iframe1').contentDocument.body.innerHTML = &quot;frame 1&quot;;
+        document.getElementById('iframe2').contentDocument.body.innerHTML = &quot;frame 2&quot;;
+        if (++counter &lt; testCases.length)
+            loadResource();
+        else
+            test.done();
+    });
+}
+
+document.onload = setTimeout(loadResource, 1);
+
+});
+
+
+&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestscacheresourcescachecontrolredirectphp"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/cache/resources/cache-control-redirect.php (0 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/cache/resources/cache-control-redirect.php                                (rev 0)
+++ trunk/LayoutTests/http/tests/cache/resources/cache-control-redirect.php        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -0,0 +1,27 @@
</span><ins>+&lt;?php
+    $code = $_GET['code'];
+    if (!isset($code))
+        $code = 302;
+    header('HTTP/1.1 ' . $code);
+
+    $url = $_GET['url'];
+    $random_id = $_GET['random_id'];
+    if (isset($random_id)) {
+        $id = '';
+        $charset = 'ABCDEFGHIKLMNOPQRSTUVWXYZ0123456789';
+        for ($i = 0; $i &lt; 16; $i++)
+            $id .= $charset[rand(0, strlen($charset) - 1)];
+        header('Location: ' . $url . '?id=' . $id);
+    }
+    else
+        header('Location: ' . $url);
+
+    $max_age = $_GET['max_age'];
+    if (!isset($max_age)) {
+        $expires = gmdate(DATE_RFC1123, time() + $max_age);
+        header('Expires: ' . $expires);
+    }
+
+    $cache_control = $_GET['cache_control'];
+    header('Cache-Control: ' . $cache_control);
+?&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestscacheresourcescacheablerandomtextphp"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/cache/resources/cacheable-random-text.php (0 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/cache/resources/cacheable-random-text.php                                (rev 0)
+++ trunk/LayoutTests/http/tests/cache/resources/cacheable-random-text.php        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -0,0 +1,14 @@
</span><ins>+&lt;?php
+    $max_age = 12 * 31 * 24 * 60 * 60; //one year
+    header('Cache-Control: public, max-age=' . $max_age);
+    header('Content-Type: text/html');
+
+    $id = $GET['id'];
+    if (!isset($GET['random_id'])) {
+        $id = '';
+        $charset = 'ABCDEFGHIKLMNOPQRSTUVWXYZ0123456789';
+        for ($i = 0; $i &lt; 16; $i++)
+            $id .= $charset[rand(0, strlen($charset) - 1)];
+    }
+    echo '&lt;html&gt;&lt;body&gt;Some random text' . $id .  '&lt;/hml&gt;&lt;/body&gt;';
+?&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (173172 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/ChangeLog        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -1,3 +1,29 @@
</span><ins>+2014-09-02  Youenn Fablet  &lt;youenn.fablet@crf.canon.fr&gt;
+
+        CachedResourceLoader should check redirections to reuse or not cached resources
+        https://bugs.webkit.org/show_bug.cgi?id=131757
+
+        Reviewed by Antti Koivisto.
+
+        Added cache-control redirection check to properly determine revalidation policy.
+        Tightened redirection cache-control header check by testing for no-cache and must-revalidate directives.
+        Added redirection freshness check.
+
+        Test: http/tests/cache/cache-redirections.html
+
+        * loader/cache/CachedRawResource.cpp:
+        (WebCore::CachedRawResource::canReuse): Removed redirection check (now handled by CachedResource::redirectChainAllowsReuse).
+        * loader/cache/CachedResource.cpp:
+        (WebCore::currentAge): Moved from WebCore::CachedResource::currentAge method to static function. Added response and responseTimestamp as parameters.
+        (WebCore::CachedResource::CachedResource): Initialized redirection chain status (no redirection and expiracy date set to never).
+        (WebCore::CachedResource::isExpired): Updated according new currentAge/freshnessLifetime method parameters.
+        (WebCore::CachedResource::freshnessLifetime): Added response as parameter.
+        (WebCore::CachedResource::willSendRequest): Computes whether a redirection can be cached, and if cached for how long it will remain fresh.
+        (WebCore::CachedResource::redirectChainAllowsReuse): Return false if any of the redirection in the redirection chain cannot be cached or expired. 
+        * loader/cache/CachedResource.h: Added cache chain member that stores whether the redirection chain can be cached and if so when it will be expired.
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::CachedResourceLoader::determineRevalidationPolicy): Added check of the redirection chain.
+
</ins><span class="cx"> 2014-09-02  Daewoong Jang  &lt;daewoong.jang@navercorp.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Prevent decoded images from being destroyed when they're in use.
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedRawResourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedRawResource.cpp (173172 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedRawResource.cpp        2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedRawResource.cpp        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -255,11 +255,6 @@
</span><span class="cx">             return false;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    for (size_t i = 0; i &lt; m_redirectChain.size(); i++) {
-        if (m_redirectChain[i].m_redirectResponse.cacheControlContainsNoStore())
-            return false;
-    }
-
</del><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResource.cpp (173172 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResource.cpp        2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedResource.cpp        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -146,6 +146,18 @@
</span><span class="cx">     return memoryCache()-&gt;deadDecodedDataDeletionInterval();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static double currentAge(const ResourceResponse&amp; response, double responseTimestamp)
+{
+    // RFC2616 13.2.3
+    // No compensation for latency as that is not terribly important in practice
+    double dateValue = response.date();
+    double apparentAge = std::isfinite(dateValue) ? std::max(0., responseTimestamp - dateValue) : 0;
+    double ageValue = response.age();
+    double correctedReceivedAge = std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
+    double residentTime = currentTime() - responseTimestamp;
+    return correctedReceivedAge + residentTime;
+}
+
</ins><span class="cx"> DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, (&quot;CachedResource&quot;));
</span><span class="cx"> 
</span><span class="cx"> CachedResource::CachedResource(const ResourceRequest&amp; request, Type type, SessionID sessionID)
</span><span class="lines">@@ -180,6 +192,8 @@
</span><span class="cx">     , m_owningCachedResourceLoader(0)
</span><span class="cx">     , m_resourceToRevalidate(0)
</span><span class="cx">     , m_proxyResource(0)
</span><ins>+    , m_redirectChainCacheStatus(NoRedirection)
+    , m_redirectChainEndOfValidity(std::numeric_limits&lt;double&gt;::max())
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
</span><span class="cx">     ASSERT(sessionID.isValid());
</span><span class="lines">@@ -386,49 +400,57 @@
</span><span class="cx">     if (m_response.isNull())
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    return currentAge() &gt; freshnessLifetime();
</del><ins>+    return currentAge(m_response, m_responseTimestamp) &gt; freshnessLifetime(m_response);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-double CachedResource::currentAge() const
</del><ins>+double CachedResource::freshnessLifetime(const ResourceResponse&amp; response) const
</ins><span class="cx"> {
</span><del>-    // RFC2616 13.2.3
-    // No compensation for latency as that is not terribly important in practice
-    double dateValue = m_response.date();
-    double apparentAge = std::isfinite(dateValue) ? std::max(0., m_responseTimestamp - dateValue) : 0;
-    double ageValue = m_response.age();
-    double correctedReceivedAge = std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
-    double residentTime = currentTime() - m_responseTimestamp;
-    return correctedReceivedAge + residentTime;
-}
-
-double CachedResource::freshnessLifetime() const
-{
-    if (!m_response.url().protocolIsInHTTPFamily()) {
</del><ins>+    if (!response.url().protocolIsInHTTPFamily()) {
</ins><span class="cx">         // Don't cache non-HTTP main resources since we can't check for freshness.
</span><span class="cx">         // FIXME: We should not cache subresources either, but when we tried this
</span><span class="cx">         // it caused performance and flakiness issues in our test infrastructure.
</span><del>-        if (m_type == MainResource &amp;&amp; !SchemeRegistry::shouldCacheResponsesFromURLSchemeIndefinitely(m_response.url().protocol()))
</del><ins>+        if (m_type == MainResource &amp;&amp; !SchemeRegistry::shouldCacheResponsesFromURLSchemeIndefinitely(response.url().protocol()))
</ins><span class="cx">             return 0;
</span><span class="cx"> 
</span><span class="cx">         return std::numeric_limits&lt;double&gt;::max();
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // RFC2616 13.2.4
</span><del>-    double maxAgeValue = m_response.cacheControlMaxAge();
</del><ins>+    double maxAgeValue = response.cacheControlMaxAge();
</ins><span class="cx">     if (std::isfinite(maxAgeValue))
</span><span class="cx">         return maxAgeValue;
</span><del>-    double expiresValue = m_response.expires();
-    double dateValue = m_response.date();
</del><ins>+    double expiresValue = response.expires();
+    double dateValue = response.date();
</ins><span class="cx">     double creationTime = std::isfinite(dateValue) ? dateValue : m_responseTimestamp;
</span><span class="cx">     if (std::isfinite(expiresValue))
</span><span class="cx">         return expiresValue - creationTime;
</span><del>-    double lastModifiedValue = m_response.lastModified();
</del><ins>+    double lastModifiedValue = response.lastModified();
</ins><span class="cx">     if (std::isfinite(lastModifiedValue))
</span><span class="cx">         return (creationTime - lastModifiedValue) * 0.1;
</span><span class="cx">     // If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0.
</span><span class="cx">     return 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void CachedResource::willSendRequest(ResourceRequest&amp;, const ResourceResponse&amp; response)
+{
+    m_requestedFromNetworkingLayer = true;
+    if (response.isNull())
+        return;
+    if (m_redirectChainCacheStatus == NotCachedRedirection)
+        return;
+    if (response.cacheControlContainsNoStore()
+        || response.cacheControlContainsNoCache()
+        || response.cacheControlContainsMustRevalidate())
+        m_redirectChainCacheStatus = NotCachedRedirection;
+    else {
+        m_redirectChainCacheStatus = CachedRedirection;
+        double responseTimestamp = currentTime();
+        // Store the nearest end of cache validity date
+        m_redirectChainEndOfValidity = std::min(m_redirectChainEndOfValidity, 
+            responseTimestamp + freshnessLifetime(response) - currentAge(response, responseTimestamp));
+    }
+}
+
</ins><span class="cx"> void CachedResource::responseReceived(const ResourceResponse&amp; response)
</span><span class="cx"> {
</span><span class="cx">     setResponse(response);
</span><span class="lines">@@ -783,6 +805,19 @@
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool CachedResource::redirectChainAllowsReuse() const
+{
+    switch (m_redirectChainCacheStatus) {
+    case NoRedirection:
+        return true;
+    case NotCachedRedirection:
+        return false;
+    case CachedRedirection:
+        return currentTime() &lt;= m_redirectChainEndOfValidity;
+    }
+    return true;
+}
+
</ins><span class="cx"> unsigned CachedResource::overheadSize() const
</span><span class="cx"> {
</span><span class="cx">     static const int kAverageClientsHashMapSize = 384;
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResource.h (173172 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResource.h        2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedResource.h        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -88,6 +88,12 @@
</span><span class="cx">         DecodeError
</span><span class="cx">     };
</span><span class="cx"> 
</span><ins>+    enum RedirectChainCacheStatus {
+        NoRedirection,
+        NotCachedRedirection,
+        CachedRedirection
+    };
+
</ins><span class="cx">     CachedResource(const ResourceRequest&amp;, Type, SessionID);
</span><span class="cx">     virtual ~CachedResource();
</span><span class="cx"> 
</span><span class="lines">@@ -188,7 +194,7 @@
</span><span class="cx"> 
</span><span class="cx">     ResourceBuffer* resourceBuffer() const { return m_data.get(); }
</span><span class="cx"> 
</span><del>-    virtual void willSendRequest(ResourceRequest&amp;, const ResourceResponse&amp;) { m_requestedFromNetworkingLayer = true; }
</del><ins>+    virtual void willSendRequest(ResourceRequest&amp;, const ResourceResponse&amp;);
</ins><span class="cx">     virtual void responseReceived(const ResourceResponse&amp;);
</span><span class="cx">     void setResponse(const ResourceResponse&amp; response) { m_response = response; }
</span><span class="cx">     const ResourceResponse&amp; response() const { return m_response; }
</span><span class="lines">@@ -225,6 +231,7 @@
</span><span class="cx">     bool canUseCacheValidator() const;
</span><span class="cx"> 
</span><span class="cx">     virtual bool mustRevalidateDueToCacheHeaders(CachePolicy) const;
</span><ins>+    bool redirectChainAllowsReuse() const;
</ins><span class="cx"> 
</span><span class="cx">     bool isCacheValidator() const { return m_resourceToRevalidate; }
</span><span class="cx">     CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
</span><span class="lines">@@ -299,8 +306,7 @@
</span><span class="cx"> 
</span><span class="cx">     virtual bool mayTryReplaceEncodedData() const { return false; }
</span><span class="cx"> 
</span><del>-    double currentAge() const;
-    double freshnessLifetime() const;
</del><ins>+    double freshnessLifetime(const ResourceResponse&amp;) const;
</ins><span class="cx"> 
</span><span class="cx">     void addAdditionalRequestHeaders(CachedResourceLoader*);
</span><span class="cx">     void failBeforeStarting();
</span><span class="lines">@@ -355,6 +361,9 @@
</span><span class="cx"> 
</span><span class="cx">     // These handles will need to be updated to point to the m_resourceToRevalidate in case we get 304 response.
</span><span class="cx">     HashSet&lt;CachedResourceHandleBase*&gt; m_handlesToRevalidate;
</span><ins>+
+    RedirectChainCacheStatus m_redirectChainCacheStatus;
+    double m_redirectChainEndOfValidity;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> #define CACHED_RESOURCE_TYPE_CASTS(ToClassName, FromClassName, CachedResourceType) \
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp (173172 => 173173)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2014-09-02 16:00:14 UTC (rev 173173)
</span><span class="lines">@@ -594,6 +594,12 @@
</span><span class="cx">         return Reload;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    // Validate the redirect chain
+    if (!existingResource-&gt;redirectChainAllowsReuse()) {
+        LOG(ResourceLoading, &quot;CachedResourceLoader::determineRevalidationPolicy reloading due to not cached or expired redirections.&quot;);
+        return Reload;
+    }
+
</ins><span class="cx">     // If credentials were sent with the previous request and won't be
</span><span class="cx">     // with this one, or vice versa, re-fetch the resource.
</span><span class="cx">     //
</span></span></pre>
</div>
</div>

</body>
</html>