<!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>[206994] 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/206994">206994</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2016-10-10 06:09:56 -0700 (Mon, 10 Oct 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[Fetch API] Memory cache should not bypass redirect mode
https://bugs.webkit.org/show_bug.cgi?id=162959

Patch by Youenn Fablet &lt;youenn@apple.com&gt; on 2016-10-10
Reviewed by Darin Adler.

Source/WebCore:

Test: http/tests/fetch/redirectmode-and-preload.html

Ensure reloading of resources if the redirect modes are different between request and cached resource, and
cached resource has redirections.

As a temporary workaround, we activate resource update for raw resources in
shouldUpdateCachedResourceWithCurrentRequest but disable it in canUpdateFromResource.
This allows handling reloading of resources with different redirection mode in canUpdateFromResource.

A future patch should allow loading cached raw resources from other cached raw resources.

* loader/cache/CachedResource.h:
(WebCore::CachedResource::hasRedirections):
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::shouldUpdateCachedResourceWithCurrentRequest):
(WebCore::canUpdateFromResource):
(WebCore::CachedResourceLoader::updateCachedResourceWithCurrentRequest):

LayoutTests:

* http/tests/fetch/redirectmode-and-preload-expected.txt: Added.
* http/tests/fetch/redirectmode-and-preload.html: Added.
* http/tests/fetch/resources/redirect-with-cache.php:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestshttptestsfetchresourcesredirectwithcachephp">trunk/LayoutTests/http/tests/fetch/resources/redirect-with-cache.php</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</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="#trunkLayoutTestshttptestsfetchredirectmodeandpreloadexpectedtxt">trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestsfetchredirectmodeandpreloadhtml">trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (206993 => 206994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2016-10-10 10:27:27 UTC (rev 206993)
+++ trunk/LayoutTests/ChangeLog        2016-10-10 13:09:56 UTC (rev 206994)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2016-10-10  Youenn Fablet  &lt;youenn@apple.com&gt;
+
+        [Fetch API] Memory cache should not bypass redirect mode
+        https://bugs.webkit.org/show_bug.cgi?id=162959
+
+        Reviewed by Darin Adler.
+
+        * http/tests/fetch/redirectmode-and-preload-expected.txt: Added.
+        * http/tests/fetch/redirectmode-and-preload.html: Added.
+        * http/tests/fetch/resources/redirect-with-cache.php:
+
</ins><span class="cx"> 2016-10-09  Antti Koivisto  &lt;antti@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Enable optimized stylesheet updates in shadow trees
</span></span></pre></div>
<a id="trunkLayoutTestshttptestsfetchredirectmodeandpreloadexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload-expected.txt (0 => 206994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload-expected.txt                                (rev 0)
+++ trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload-expected.txt        2016-10-10 13:09:56 UTC (rev 206994)
</span><span class="lines">@@ -0,0 +1,6 @@
</span><ins>+
+PASS Fetch should check for redirections even if resource is preloaded (same fetch options except for redirect mode) 
+PASS Fetch should check for redirections even if resource is preloaded (different fetch mode, different redirect mode) 
+PASS Fetch should check for redirections even if resource is preloaded (same fetch options except for redirect mode) 
+PASS Fetch should check for redirections even if resource is preloaded (different fetch mode, different redirect mode) 
+
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestsfetchredirectmodeandpreloadhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload.html (0 => 206994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload.html                                (rev 0)
+++ trunk/LayoutTests/http/tests/fetch/redirectmode-and-preload.html        2016-10-10 13:09:56 UTC (rev 206994)
</span><span class="lines">@@ -0,0 +1,50 @@
</span><ins>+&lt;!doctype html&gt;
+&lt;html&gt;
+  &lt;head&gt;
+    &lt;meta charset=&quot;utf-8&quot;&gt;
+    &lt;title&gt;Fetch: retrieve response's body progressively&lt;/title&gt;
+    &lt;meta name=&quot;help&quot; href=&quot;https://fetch.spec.whatwg.org/#main-fetch&quot;&gt;
+    &lt;meta name=&quot;help&quot; href=&quot;https://fetch.spec.whatwg.org/#http-fetch&quot;&gt;
+    &lt;meta name=&quot;author&quot; title=&quot;Canon Research France&quot; href=&quot;https://www.crf.canon.fr&quot;&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;script&gt;
+if (window.internals)
+    internals.setLinkPreloadSupport(true);
+    &lt;/script&gt;
+    &lt;link rel=preload onload=&quot;startTests()&quot; href=&quot;./resources/redirect-with-cache.php?enableCaching&amp;url=http://localhost:8000/security/resources/allow-if-origin.php?allowCache&amp;origin=http%3A%2F%2F127.0.0.1%3A8000&amp;name=alert-fail.js&amp;contentType=text/ascii&quot;&gt;
+  &lt;/head&gt;
+  &lt;body&gt;
+    &lt;script&gt;
+function startTests()
+{
+    var preloadUrl = &quot;./resources/redirect-with-cache.php?enableCaching&amp;url=http://localhost:8000/security/resources/allow-if-origin.php?allowCache&amp;origin=http%3A%2F%2F127.0.0.1%3A8000&amp;name=alert-fail.js&amp;contentType=text/ascii&quot;;
+    promise_test(function(test) {
+        return fetch(preloadUrl, {redirect: &quot;manual&quot;, mode: &quot;no-cors&quot;, credentials: &quot;include&quot;}).then((response) =&gt; {
+            assert_equals(response.type, &quot;opaqueredirect&quot;, &quot;Response's type should be opaqueRedirect&quot;);
+            return response.text();
+        }).then((text) =&gt; {
+            assert_equals(text, &quot;&quot;);
+        });
+    }, &quot;Fetch should check for redirections even if resource is preloaded (same fetch options except for redirect mode)&quot;);
+
+    promise_test(function(test) {
+        return fetch(preloadUrl, {redirect: &quot;manual&quot;, mode: &quot;cors&quot;, credentials: &quot;include&quot;}).then((response) =&gt; {
+            assert_equals(response.type, &quot;opaqueredirect&quot;, &quot;Response's type should be opaqueRedirect&quot;);
+            return response.text();
+        }).then((text) =&gt; {
+            assert_equals(text, &quot;&quot;);
+        });
+    }, &quot;Fetch should check for redirections even if resource is preloaded (different fetch mode, different redirect mode)&quot;);
+
+    promise_test(function(test) {
+        return promise_rejects(test, new TypeError(), fetch(preloadUrl, {redirect: &quot;error&quot;, mode: &quot;no-cors&quot;, credentials: &quot;include&quot;}));
+    }, &quot;Fetch should check for redirections even if resource is preloaded (same fetch options except for redirect mode)&quot;);
+
+    promise_test(function(test) {
+        return promise_rejects(test, new TypeError(), fetch(preloadUrl, {redirect: &quot;error&quot;, mode: &quot;cors&quot;, credentials: &quot;include&quot;}));
+    }, &quot;Fetch should check for redirections even if resource is preloaded (different fetch mode, different redirect mode)&quot;);
+}
+    &lt;/script&gt;
+  &lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestsfetchresourcesredirectwithcachephp"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/fetch/resources/redirect-with-cache.php (206993 => 206994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/fetch/resources/redirect-with-cache.php        2016-10-10 10:27:27 UTC (rev 206993)
+++ trunk/LayoutTests/http/tests/fetch/resources/redirect-with-cache.php        2016-10-10 13:09:56 UTC (rev 206994)
</span><span class="lines">@@ -1,7 +1,7 @@
</span><span class="cx"> &lt;?php
</span><span class="cx">     $url = $_GET[&quot;url&quot;];
</span><span class="cx"> 
</span><del>-    $enableCaching = isset($_SERVER[&quot;ENABLECACHING&quot;]) ? true : false;
</del><ins>+    $enableCaching = isset($_SERVER[&quot;ENABLECACHING&quot;]) || isset($_GET[&quot;enableCaching&quot;]) ? true : false;
</ins><span class="cx">     $code = isset($_GET[&quot;code&quot;]) ? $_GET[&quot;code&quot;] : 302;
</span><span class="cx"> 
</span><span class="cx">     header(&quot;HTTP/1.1 $code&quot;);
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (206993 => 206994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-10-10 10:27:27 UTC (rev 206993)
+++ trunk/Source/WebCore/ChangeLog        2016-10-10 13:09:56 UTC (rev 206994)
</span><span class="lines">@@ -1,5 +1,30 @@
</span><span class="cx"> 2016-10-10  Youenn Fablet  &lt;youenn@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        [Fetch API] Memory cache should not bypass redirect mode
+        https://bugs.webkit.org/show_bug.cgi?id=162959
+
+        Reviewed by Darin Adler.
+
+        Test: http/tests/fetch/redirectmode-and-preload.html
+
+        Ensure reloading of resources if the redirect modes are different between request and cached resource, and
+        cached resource has redirections.
+
+        As a temporary workaround, we activate resource update for raw resources in
+        shouldUpdateCachedResourceWithCurrentRequest but disable it in canUpdateFromResource.
+        This allows handling reloading of resources with different redirection mode in canUpdateFromResource.
+
+        A future patch should allow loading cached raw resources from other cached raw resources.
+
+        * loader/cache/CachedResource.h:
+        (WebCore::CachedResource::hasRedirections):
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::CachedResourceLoader::shouldUpdateCachedResourceWithCurrentRequest):
+        (WebCore::canUpdateFromResource):
+        (WebCore::CachedResourceLoader::updateCachedResourceWithCurrentRequest):
+
+2016-10-10  Youenn Fablet  &lt;youenn@apple.com&gt;
+
</ins><span class="cx">         Attribute getter binding generated code should use more references
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=163179
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResource.h (206993 => 206994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResource.h        2016-10-10 10:27:27 UTC (rev 206993)
+++ trunk/Source/WebCore/loader/cache/CachedResource.h        2016-10-10 13:09:56 UTC (rev 206994)
</span><span class="lines">@@ -234,25 +234,26 @@
</span><span class="cx">     virtual void destroyDecodedData() { }
</span><span class="cx"> 
</span><span class="cx">     void setOwningCachedResourceLoader(CachedResourceLoader* cachedResourceLoader) { m_owningCachedResourceLoader = cachedResourceLoader; }
</span><del>-    
</del><ins>+
</ins><span class="cx">     bool isPreloaded() const { return m_preloadCount; }
</span><span class="cx">     void increasePreloadCount() { ++m_preloadCount; }
</span><span class="cx">     void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
</span><del>-    
</del><ins>+
</ins><span class="cx">     void registerHandle(CachedResourceHandleBase* h);
</span><span class="cx">     WEBCORE_EXPORT void unregisterHandle(CachedResourceHandleBase* h);
</span><del>-    
</del><ins>+
</ins><span class="cx">     bool canUseCacheValidator() const;
</span><span class="cx"> 
</span><span class="cx">     enum class RevalidationDecision { No, YesDueToCachePolicy, YesDueToNoStore, YesDueToNoCache, YesDueToExpired };
</span><span class="cx">     virtual RevalidationDecision makeRevalidationDecision(CachePolicy) const;
</span><span class="cx">     bool redirectChainAllowsReuse(ReuseExpiredRedirectionOrNot) const;
</span><ins>+    bool hasRedirections() const { return m_redirectChainCacheStatus.status != RedirectChainCacheStatus::Status::NoRedirection;  }
</ins><span class="cx"> 
</span><span class="cx">     bool varyHeaderValuesMatch(const ResourceRequest&amp;, const CachedResourceLoader&amp;);
</span><span class="cx"> 
</span><span class="cx">     bool isCacheValidator() const { return m_resourceToRevalidate; }
</span><span class="cx">     CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
</span><del>-    
</del><ins>+
</ins><span class="cx">     // HTTP revalidation support methods for CachedResourceLoader.
</span><span class="cx">     void setResourceToRevalidate(CachedResource*);
</span><span class="cx">     virtual void switchClientsToRevalidatedResource();
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp (206993 => 206994)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2016-10-10 10:27:27 UTC (rev 206993)
+++ trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2016-10-10 13:09:56 UTC (rev 206994)
</span><span class="lines">@@ -561,8 +561,6 @@
</span><span class="cx">         return false;
</span><span class="cx">     case CachedResource::MediaResource:
</span><span class="cx">         return false;
</span><del>-    case CachedResource::RawResource:
-        return false;
</del><span class="cx">     case CachedResource::MainResource:
</span><span class="cx">         return false;
</span><span class="cx"> #if ENABLE(LINK_PREFETCH)
</span><span class="lines">@@ -574,14 +572,43 @@
</span><span class="cx">     default:
</span><span class="cx">         break;
</span><span class="cx">     }
</span><del>-    return resource.options().mode != request.options().mode || request.resourceRequest().httpOrigin() != resource.resourceRequest().httpOrigin();
</del><ins>+
+    if (resource.options().mode != request.options().mode || request.resourceRequest().httpOrigin() != resource.resourceRequest().httpOrigin())
+        return true;
+
+    if (resource.options().redirect != request.options().redirect &amp;&amp; resource.hasRedirections())
+        return true;
+
+    return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static inline bool isResourceSuitableForDirectReuse(const CachedResource&amp; resource, const CachedResourceRequest&amp; request)
+{
+    // FIXME: For being loaded requests, the response tainting may not be correctly computed if the fetch mode is not the same.
+    // Even if the fetch mode is the same, we are not sure that the resource can be reused (Vary: Origin header for instance).
+    // We should find a way to improve this.
+    if (resource.status() != CachedResource::Cached)
+        return false;
+
+    // If the cached resource has not followed redirections, it is incomplete and we should not use it.
+    // Let's make sure the memory cache has no such resource.
+    ASSERT(resource.response().type() != ResourceResponse::Type::Opaqueredirect);
+
+    // We could support redirect modes other than Follow in case of a redirected resource.
+    // This case is rare and is not worth optimizing currently.
+    if (request.options().redirect != FetchOptions::Redirect::Follow &amp;&amp; resource.hasRedirections())
+        return false;
+
+    // FIXME: Implement reuse of cached raw resources.
+    if (resource.type() == CachedResource::Type::RawResource)
+        return false;
+
+    return true;
+}
+
</ins><span class="cx"> CachedResourceHandle&lt;CachedResource&gt; CachedResourceLoader::updateCachedResourceWithCurrentRequest(const CachedResource&amp; resource, CachedResourceRequest&amp;&amp; request)
</span><span class="cx"> {
</span><del>-    // FIXME: For being loaded requests, we currently do not use the same resource, as this may induce errors in the resource response tainting.
-    // We should find a way to improve this.
-    if (resource.status() != CachedResource::Cached) {
</del><ins>+    if (!isResourceSuitableForDirectReuse(resource, request)) {
</ins><span class="cx">         request.setCachingPolicy(CachingPolicy::DisallowCaching);
</span><span class="cx">         return loadResource(resource.type(), WTFMove(request));
</span><span class="cx">     }
</span></span></pre>
</div>
</div>

</body>
</html>