<!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>[213672] 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/213672">213672</a></dd>
<dt>Author</dt> <dd>yoav@yoav.ws</dd>
<dt>Date</dt> <dd>2017-03-09 14:29:39 -0800 (Thu, 09 Mar 2017)</dd>
</dl>
<h3>Log Message</h3>
<pre>[link preload] Double downloads of preloaded CSS
https://bugs.webkit.org/show_bug.cgi?id=169274
Reviewed by Antti Koivisto.
Source/WebCore:
Avoid reloading link preloads in case of a charset mismatch.
Charset mismatch can happen for header based preloads, as they are requested before
the HTML's `<meta charset>` tag is processed. This change makes sure that in those
cases, we modify the resource's encoding setting instead of reloading it.
Test: http/tests/preload/single_download_preload_headers.php
* loader/cache/CachedResource.cpp:
(WebCore::CachedResource::CachedResource): Initialize m_unknownCharset to be the same as the preload flag.
* loader/cache/CachedResource.h:
(WebCore::CachedResource::hasUnknownEncoding):
(WebCore::CachedResource::setHasUnknownEncoding):
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::determineRevalidationPolicy): In case of a charset
mismatch, set the encoding of the Resource instead of reloading it if the charset is unknown.
LayoutTests:
Added tests making sure that header based preloads also trigger a single download,
and that we properly handle multiple charsets for the same preloaded resource.
* http/tests/preload/single_download_preload_headers-expected.txt: Added.
* http/tests/preload/single_download_preload_headers.php: Added.
* http/tests/preload/preload-encoding-expected.txt: Added.
* http/tests/preload/preload-encoding.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="#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="#trunkLayoutTestshttptestspreloadpreloadencodingexpectedtxt">trunk/LayoutTests/http/tests/preload/preload-encoding-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadpreloadencodingphp">trunk/LayoutTests/http/tests/preload/preload-encoding.php</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadresourcessuccessjs">trunk/LayoutTests/http/tests/preload/resources/success.js</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadsingle_download_preload_headersexpectedtxt">trunk/LayoutTests/http/tests/preload/single_download_preload_headers-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadsingle_download_preload_headersphp">trunk/LayoutTests/http/tests/preload/single_download_preload_headers.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (213671 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2017-03-09 22:24:26 UTC (rev 213671)
+++ trunk/LayoutTests/ChangeLog        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -1,3 +1,18 @@
</span><ins>+2017-03-09 Yoav Weiss <yoav@yoav.ws>
+
+ [link preload] Double downloads of preloaded CSS
+ https://bugs.webkit.org/show_bug.cgi?id=169274
+
+ Reviewed by Antti Koivisto.
+
+ Added tests making sure that header based preloads also trigger a single download,
+ and that we properly handle multiple charsets for the same preloaded resource.
+
+ * http/tests/preload/single_download_preload_headers-expected.txt: Added.
+ * http/tests/preload/single_download_preload_headers.php: Added.
+ * http/tests/preload/preload-encoding-expected.txt: Added.
+ * http/tests/preload/preload-encoding.php: Added.
+
</ins><span class="cx"> 2017-03-09 Jiewen Tan <jiewen_tan@apple.com>
</span><span class="cx">
</span><span class="cx"> Implement PBKDF2 in WebCrypto
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadpreloadencodingexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/preload/preload-encoding-expected.txt (0 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/preload-encoding-expected.txt         (rev 0)
+++ trunk/LayoutTests/http/tests/preload/preload-encoding-expected.txt        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -0,0 +1,11 @@
</span><ins>+CONSOLE MESSAGE: line 1: SyntaxError: Invalid character '\u8307'
+First load a script with a wrong charset then again with the right one. Second attempt should work and 'scriptSuccess' should be set to true. 'successfullyParsed' will be undefined.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS scriptSuccess is true
+FAIL successfullyParsed should be true (of type boolean). Was undefined (of type undefined).
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadpreloadencodingphp"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/preload/preload-encoding.php (0 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/preload-encoding.php         (rev 0)
+++ trunk/LayoutTests/http/tests/preload/preload-encoding.php        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -0,0 +1,31 @@
</span><ins>+<?php
+header("Link: <resources/success.js>; rel=preload; as=script", false);
+?>
+<script src="../../resources/js-test-pre.js"></script>
+<script>
+jsTestIsAsync = true;
+
+description("First load a script with a wrong charset then again with the right one. Second attempt should work and 'scriptSuccess' should be set to true. 'successfullyParsed' will be undefined.");
+
+function appendScriptWithCharset(charset, onload)
+{
+ var script = document.createElement("script");
+ script.src = "resources/success.js";
+ script.setAttribute("charset", charset);
+ script.onload = onload;
+ script.onerror = onload;
+ document.body.appendChild(script);
+}
+
+function test()
+{
+ appendScriptWithCharset("utf-16", function () {
+ appendScriptWithCharset("utf-8", function () {
+ shouldBeTrue("scriptSuccess");
+ finishJSTest();
+ });
+ });
+}
+</script>
+<body onload="test()">
+<script src="../../resources/js-test-post.js"></script>
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadresourcessuccessjs"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/preload/resources/success.js (0 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/resources/success.js         (rev 0)
+++ trunk/LayoutTests/http/tests/preload/resources/success.js        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+scriptSuccess = true;
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadsingle_download_preload_headersexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/preload/single_download_preload_headers-expected.txt (0 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/single_download_preload_headers-expected.txt         (rev 0)
+++ trunk/LayoutTests/http/tests/preload/single_download_preload_headers-expected.txt        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -0,0 +1,5 @@
</span><ins>+CONSOLE MESSAGE: <link rel=preload> must have a valid `as` value
+
+
+PASS Makes sure that preloaded resources are not downloaded again when used
+
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadsingle_download_preload_headersphp"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/preload/single_download_preload_headers.php (0 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/single_download_preload_headers.php         (rev 0)
+++ trunk/LayoutTests/http/tests/preload/single_download_preload_headers.php        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -0,0 +1,64 @@
</span><ins>+<?php
+header("Link: <http://127.0.0.1:8000/resources/dummy.js>; rel=preload; as=script", false);
+header("LiNk:<http://127.0.0.1:8000/resources/dummy.css>; rel=preload; as=style", false);
+header("Link: <http://127.0.0.1:8000/resources/square100.png>;rel=preload;as=image", false);
+header("Link: <http://127.0.0.1:8000/resources/square100.png?background>;rel=preload;as=image", false);
+header("Link: <http://127.0.0.1:8000/resources/Ahem.woff>; rel=preload; as=font; crossorigin", false);
+header("Link: <http://127.0.0.1:8000/resources/test.mp4>; rel=preload; as=media", false);
+header("Link: <http://127.0.0.1:8000/resources/test.oga>; rel=preload; as=media", false);
+header("link: <http://127.0.0.1:8000/security/resources/captions.vtt>; rel=preload; as=track", false);
+header("Link: <http://127.0.0.1:8000/resources/dummy.xml?foobar>; rel=preload; as=foobar", false);
+header("Link: <http://127.0.0.1:8000/resources/dummy.xml>; crossorigin; rel=preload", false);
+?>
+<!DOCTYPE html>
+<meta charset="utf-8">
+<script src="/js-test-resources/testharness.js"></script>
+<script src="/js-test-resources/testharnessreport.js"></script>
+<script>
+ var t = async_test('Makes sure that preloaded resources are not downloaded again when used');
+</script>
+<script src="http://127.0.0.1:8000/resources/slow-script.pl?delay=200"></script>
+<style>
+ #background {
+ width: 200px;
+ height: 200px;
+ background-image: url(http://127.0.0.1:8000/resources/square100.png?background);
+ }
+ @font-face {
+ font-family:ahem;
+ src: url(http://127.0.0.1:8000/resources/Ahem.woff);
+ }
+ span { font-family: ahem, Arial; }
+</style>
+<link rel="stylesheet" href="http://127.0.0.1:8000/resources/dummy.css">
+<script src="http://127.0.0.1:8000/resources/dummy.js"></script>
+<div id="background"></div>
+<img src="http://127.0.0.1:8000/resources/square100.png">
+<video src="http://127.0.0.1:8000/resources/test.mp4">
+ <track kind=subtitles src="http://127.0.0.1:8000/security/resources/captions.vtt" srclang=en>
+</video>
+<audio src="http://127.0.0.1:8000/resources/test.oga"></audio>
+<script>
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET", "http://127.0.0.1:8000/resources/dummy.xml");
+ xhr.send();
+
+ window.addEventListener("load", t.step_func(function() {
+ function verifyDownloadNumber(url, number) {
+ assert_equals(performance.getEntriesByName(url).length, number, url);
+ }
+ setTimeout(t.step_func(function() {
+ verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.js", 1);
+ verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.css", 1);
+ verifyDownloadNumber("http://127.0.0.1:8000/resources/square100.png", 1);
+ verifyDownloadNumber("http://127.0.0.1:8000/resources/square100.png?background", 1);
+ verifyDownloadNumber("http://127.0.0.1:8000/resources/Ahem.woff", 1);
+ verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.xml?foobar", 0);
+ verifyDownloadNumber("http://127.0.0.1:8000/security/resources/captions.vtt", 1);
+ // FIXME: XHR should trigger a single download, but it downloads 2 resources instead.
+ verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.xml", 2);
+ // FIXME: We should verify for video and audio as well, but they seem to (flakily?) trigger multiple partial requests.
+ t.done();
+ }), 100);
+ }));
+</script>
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (213671 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-03-09 22:24:26 UTC (rev 213671)
+++ trunk/Source/WebCore/ChangeLog        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -1,3 +1,27 @@
</span><ins>+2017-03-09 Yoav Weiss <yoav@yoav.ws>
+
+ [link preload] Double downloads of preloaded CSS
+ https://bugs.webkit.org/show_bug.cgi?id=169274
+
+ Reviewed by Antti Koivisto.
+
+ Avoid reloading link preloads in case of a charset mismatch.
+
+ Charset mismatch can happen for header based preloads, as they are requested before
+ the HTML's `<meta charset>` tag is processed. This change makes sure that in those
+ cases, we modify the resource's encoding setting instead of reloading it.
+
+ Test: http/tests/preload/single_download_preload_headers.php
+
+ * loader/cache/CachedResource.cpp:
+ (WebCore::CachedResource::CachedResource): Initialize m_unknownCharset to be the same as the preload flag.
+ * loader/cache/CachedResource.h:
+ (WebCore::CachedResource::hasUnknownEncoding):
+ (WebCore::CachedResource::setHasUnknownEncoding):
+ * loader/cache/CachedResourceLoader.cpp:
+ (WebCore::CachedResourceLoader::determineRevalidationPolicy): In case of a charset
+ mismatch, set the encoding of the Resource instead of reloading it if the charset is unknown.
+
</ins><span class="cx"> 2017-03-09 Jiewen Tan <jiewen_tan@apple.com>
</span><span class="cx">
</span><span class="cx"> Implement PBKDF2 in WebCrypto
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResource.cpp (213671 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResource.cpp        2017-03-09 22:24:26 UTC (rev 213671)
+++ trunk/Source/WebCore/loader/cache/CachedResource.cpp        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -123,6 +123,7 @@
</span><span class="cx"> , m_origin(request.releaseOrigin())
</span><span class="cx"> , m_initiatorName(request.initiatorName())
</span><span class="cx"> , m_isLinkPreload(request.isLinkPreload())
</span><ins>+ , m_hasUnknownEncoding(request.isLinkPreload())
</ins><span class="cx"> , m_type(type)
</span><span class="cx"> {
</span><span class="cx"> ASSERT(sessionID.isValid());
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResource.h (213671 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResource.h        2017-03-09 22:24:26 UTC (rev 213671)
+++ trunk/Source/WebCore/loader/cache/CachedResource.h        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -232,6 +232,8 @@
</span><span class="cx"> void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
</span><span class="cx"> bool isLinkPreload() { return m_isLinkPreload; }
</span><span class="cx"> void setLinkPreload() { m_isLinkPreload = true; }
</span><ins>+ bool hasUnknownEncoding() { return m_hasUnknownEncoding; }
+ void setHasUnknownEncoding(bool hasUnknownEncoding) { m_hasUnknownEncoding = hasUnknownEncoding; }
</ins><span class="cx">
</span><span class="cx"> void registerHandle(CachedResourceHandleBase*);
</span><span class="cx"> WEBCORE_EXPORT void unregisterHandle(CachedResourceHandleBase*);
</span><span class="lines">@@ -334,6 +336,7 @@
</span><span class="cx"> bool m_inCache { false };
</span><span class="cx"> bool m_loading { false };
</span><span class="cx"> bool m_isLinkPreload { false };
</span><ins>+ bool m_hasUnknownEncoding { false };
</ins><span class="cx">
</span><span class="cx"> bool m_switchingClientsToRevalidatedResource { false };
</span><span class="cx">
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp (213671 => 213672)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2017-03-09 22:24:26 UTC (rev 213671)
+++ trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2017-03-09 22:29:39 UTC (rev 213672)
</span><span class="lines">@@ -922,8 +922,12 @@
</span><span class="cx"> return Reload;
</span><span class="cx">
</span><span class="cx"> auto* textDecoder = existingResource->textResourceDecoder();
</span><del>- if (textDecoder && !textDecoder->hasEqualEncodingForCharset(cachedResourceRequest.charset()))
- return Reload;
</del><ins>+ if (textDecoder && !textDecoder->hasEqualEncodingForCharset(cachedResourceRequest.charset())) {
+ if (!existingResource->hasUnknownEncoding())
+ return Reload;
+ existingResource->setHasUnknownEncoding(false);
+ existingResource->setEncoding(cachedResourceRequest.charset());
+ }
</ins><span class="cx">
</span><span class="cx"> // FIXME: We should use the same cache policy for all resource types. The raw resource policy is overly strict
</span><span class="cx"> // while the normal subresource policy is too loose.
</span></span></pre>
</div>
</div>
</body>
</html>