Add Link header support for preload.
authoryoav@yoav.ws <yoav@yoav.ws@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Jan 2017 07:13:54 +0000 (07:13 +0000)
committeryoav@yoav.ws <yoav@yoav.ws@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Jan 2017 07:13:54 +0000 (07:13 +0000)
https://bugs.webkit.org/show_bug.cgi?id=165521

Reviewed by Alex Christensen.

Source/WebCore:

This patch adds Link header support for preload, by adding a Link
header parser, and calling it from FrameLoader::receivedFirstData.

Test: http/tests/preload/download_resources_from_header.php

* CMakeLists.txt: Add LinkHeader files.
* WebCore.xcodeproj/project.pbxproj: Add LinkHeader files.
* loader/FrameLoader.cpp:
(WebCore::receivedFirstData): Trigger Link based preloading by calling loadLinksFromHeader.
* loader/LinkHeader.cpp: Added.
(WebCore::isWhitespace):
(WebCore::isValidURLChar):
(WebCore::isValidParameterNameChar):
(WebCore::isValidParameterValueEnd):
(WebCore::isValidParameterValueChar):
(WebCore::isExtensionParameter):
(WebCore::parseURL):
(WebCore::invalidParameterDelimiter):
(WebCore::validFieldEnd):
(WebCore::parseParameterDelimiter):
(WebCore::paramterNameFromString):
(WebCore::parseParameterName):
(WebCore::skipQuotesIfNeeded):
(WebCore::parseParameterValue):
(WebCore::LinkHeader::setValue):
(WebCore::findNextHeader):
(WebCore::LinkHeader::LinkHeader):
(WebCore::LinkHeaderSet::LinkHeaderSet):
(WebCore::LinkHeaderSet::init):
* loader/LinkHeader.h: Added.
(WebCore::LinkHeader::url):
(WebCore::LinkHeader::rel):
(WebCore::LinkHeader::as):
(WebCore::LinkHeader::mimeType):
(WebCore::LinkHeader::media):
(WebCore::LinkHeader::crossOrigin):
(WebCore::LinkHeader::valid):
(WebCore::LinkHeaderSet::begin):
(WebCore::LinkHeaderSet::end):
(WebCore::LinkHeaderSet::operator[]):
(WebCore::LinkHeaderSet::size):
* loader/LinkLoader.cpp:
(WebCore::LinkLoader::loadLinksFromHeader): Loads link preload directives that arrive from Link headers.
(WebCore::LinkLoader::preloadIfNeeded): Add return value.
* loader/LinkLoader.h:
* platform/network/HTTPHeaderNames.in: Add "Link" as a header name.

LayoutTests:

* http/tests/preload/download_resources_from_header_iframe-expected.txt: Added
* http/tests/preload/download_resources_from_header_iframe.html: Added
* http/tests/preload/download_resources_from_invalid_headers-expected: Added
* http/tests/preload/download_resources_from_invalid_headers.html: Added
* http/tests/preload/resources/download_resources_from_header.php: Added.
* http/tests/preload/resources/invalid_resources_from_header.php: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@210914 268f45cc-cd09-0410-ab3c-d52691b4dbfc

16 files changed:
LayoutTests/ChangeLog
LayoutTests/http/tests/preload/download_resources_from_header_iframe-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/preload/download_resources_from_header_iframe.html [new file with mode: 0644]
LayoutTests/http/tests/preload/download_resources_from_invalid_headers-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/preload/download_resources_from_invalid_headers.html [new file with mode: 0644]
LayoutTests/http/tests/preload/resources/download_resources_from_header.php [new file with mode: 0644]
LayoutTests/http/tests/preload/resources/invalid_resources_from_header.php [new file with mode: 0644]
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/loader/FrameLoader.cpp
Source/WebCore/loader/LinkHeader.cpp [new file with mode: 0644]
Source/WebCore/loader/LinkHeader.h [new file with mode: 0644]
Source/WebCore/loader/LinkLoader.cpp
Source/WebCore/loader/LinkLoader.h
Source/WebCore/platform/network/HTTPHeaderNames.in

index e4830ee..72d4862 100644 (file)
@@ -1,3 +1,17 @@
+2017-01-18  Yoav Weiss  <yoav@yoav.ws>
+
+        Add Link header support for preload.
+        https://bugs.webkit.org/show_bug.cgi?id=165521
+
+        Reviewed by Alex Christensen.
+
+        * http/tests/preload/download_resources_from_header_iframe-expected.txt: Added
+        * http/tests/preload/download_resources_from_header_iframe.html: Added
+        * http/tests/preload/download_resources_from_invalid_headers-expected: Added
+        * http/tests/preload/download_resources_from_invalid_headers.html: Added
+        * http/tests/preload/resources/download_resources_from_header.php: Added.
+        * http/tests/preload/resources/invalid_resources_from_header.php: Added.
+
 2017-01-18  Joseph Pecoraro  <pecoraro@apple.com>
 
         Web Inspector: console.table only works for the first 5 properties
diff --git a/LayoutTests/http/tests/preload/download_resources_from_header_iframe-expected.txt b/LayoutTests/http/tests/preload/download_resources_from_header_iframe-expected.txt
new file mode 100644 (file)
index 0000000..d7ad40a
--- /dev/null
@@ -0,0 +1,18 @@
+CONSOLE MESSAGE: <link rel=preload> must have a valid `as` value
+
+
+--------
+Frame: '<!--framePath //<!--frame0-->-->'
+--------
+PASS internals.isPreloaded('../resources/dummy.js'); is true
+PASS internals.isPreloaded('../resources/dummy.css'); is true
+PASS internals.isPreloaded('../resources/square.png'); is true
+PASS internals.isPreloaded('../resources/Ahem.ttf'); is true
+PASS internals.isPreloaded('../resources/test.mp4'); is true
+PASS internals.isPreloaded('../security/resources/captions.vtt'); is true
+PASS internals.isPreloaded('../resources/dummy.xml?badvalue'); is false
+PASS internals.isPreloaded('../resources/dummy.xml'); is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/preload/download_resources_from_header_iframe.html b/LayoutTests/http/tests/preload/download_resources_from_header_iframe.html
new file mode 100644 (file)
index 0000000..fdf4253
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<script>
+    if (window.internals)
+        window.internals.setLinkPreloadSupport(true);
+    if (window.testRunner) {
+        testRunner.dumpAsText();
+        testRunner.dumpChildFramesAsText();
+    }
+</script>
+<iframe src="resources/download_resources_from_header.php">
+
diff --git a/LayoutTests/http/tests/preload/download_resources_from_invalid_headers-expected.txt b/LayoutTests/http/tests/preload/download_resources_from_invalid_headers-expected.txt
new file mode 100644 (file)
index 0000000..2fe3e8c
--- /dev/null
@@ -0,0 +1,25 @@
+CONSOLE MESSAGE: Refused to load http://localhost:8000/preload/resources/square.png because it does not appear in the img-src directive of the Content Security Policy.
+
+
+--------
+Frame: '<!--framePath //<!--frame0-->-->'
+--------
+PASS internals.isPreloaded('../resources/dummy.js'); is true
+PASS internals.isPreloaded('../מש×\90בי×\9d/dummy.css'); is false
+PASS internals.isPreloaded('../résôûrcès/square.png'); is false
+PASS internals.isPreloaded('../resources/Ahem{.ttf,.woff}'); is true
+PASS internals.isPreloaded('../resources/test.mp4'); is false
+PASS internals.isPreloaded('../resources/test\f.mp4'); is true
+PASS internals.isPreloaded('../security/resources/cap  tions.vtt'); is true
+PASS internals.isPreloaded('../resources/dummy.xml?badvalue'); is false
+PASS internals.isPreloaded('../resources/dummy   .xml'); is true
+PASS internals.isPreloaded('../resources/dummy.xml'); is false
+PASS internals.isPreloaded('../resources/dumm'); is false
+PASS internals.isPreloaded('http://localhost:8000/preload/resources/square.png'); is false
+PASS internals.isPreloaded('http://localhost:53/preload/resources/dummy.js'); is true
+PASS internals.isPreloaded('#foobar'); is false
+PASS internals.isPreloaded('../resources/Ahem.ttf'); is false
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/preload/download_resources_from_invalid_headers.html b/LayoutTests/http/tests/preload/download_resources_from_invalid_headers.html
new file mode 100644 (file)
index 0000000..4c37d92
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<script>
+    if (window.internals)
+        window.internals.setLinkPreloadSupport(true);
+    if (window.testRunner) {
+        testRunner.dumpAsText();
+        testRunner.dumpChildFramesAsText();
+    }
+</script>
+<iframe src="resources/invalid_resources_from_header.php">
+
diff --git a/LayoutTests/http/tests/preload/resources/download_resources_from_header.php b/LayoutTests/http/tests/preload/resources/download_resources_from_header.php
new file mode 100644 (file)
index 0000000..c9a640b
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+header("Link: <../resources/dummy.js>; rel=preload; as=script", false);
+header("Link:<../resources/dummy.css>; rel=preload; as=style", false);
+header("Link: <../resources/square.png>;rel=preload;as=image", false);
+header("Link: <../resources/Ahem.ttf>; rel=preload; as=font; crossorigin", false);
+header("Link: <../resources/test.mp4>; rel=preload; as=media", false);
+header("link: <../security/resources/captions.vtt>; rel=preload; as=track", false);
+header("Link: <../resources/dummy.xml?badvalue>; rel=preload; as=foobar", false);
+header("Link: <../resources/dummy.xml>; rel=preload", false);
+?>
+<!DOCTYPE html>
+<script src="/js-test-resources/js-test.js"></script>
+<script>
+    shouldBeTrue("internals.isPreloaded('../resources/dummy.js');");
+    shouldBeTrue("internals.isPreloaded('../resources/dummy.css');");
+    shouldBeTrue("internals.isPreloaded('../resources/square.png');");
+    shouldBeTrue("internals.isPreloaded('../resources/Ahem.ttf');");
+    shouldBeTrue("internals.isPreloaded('../resources/test.mp4');");
+    shouldBeTrue("internals.isPreloaded('../security/resources/captions.vtt');");
+    shouldBeFalse("internals.isPreloaded('../resources/dummy.xml?badvalue');");
+    shouldBeTrue("internals.isPreloaded('../resources/dummy.xml');");
+</script>
+<script>
+    if (window.internals)
+        window.internals.setLinkPreloadSupport(false);
+</script>
diff --git a/LayoutTests/http/tests/preload/resources/invalid_resources_from_header.php b/LayoutTests/http/tests/preload/resources/invalid_resources_from_header.php
new file mode 100644 (file)
index 0000000..d025614
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+header("Link: <   ../resources/dummy.js >; rel=preload; as=script", false);
+header("Link: <../משאבים/dummy.css>; rel=preload; as=style", false);
+header("Link: <../résôûrcès/dummy.css>; rel=preload; as=style", false);
+header("Link: <../resources/Ahem{.ttf,.woff}>; rel=preload; as=font; crossorigin", false);
+header("Link: <../resources/test\f.mp4>; rel=preload; as=media", false);
+header("Link: <../security/resources/cap\ttions.vtt>; rel=preload; as=track", false);
+header("Link: <../resources/dummy   .xml>; rel=preload;", false);
+header("Link: <../resources/dumm>y.xml>; rel=preload", false);
+header("Content-Security-Policy: img-src 'none'", false);
+header("Link: <http://localhost:8000/preload/resources/square.png>; rel=preload; as=image", false);
+header("Link: <http://localhost:53/preload/resources/dummy.js>; rel=preload; as=script", false);
+header("Link: <#foobar>; rel=preload; as=style", false);
+header("Link: <>; rel=preload; as=style", false);
+header("Link: <   \t>; rel=preload; as=style", false);
+header("Link: >; rel=preload; as=style", false);
+header("Link: <; rel=preload; as=style", false);
+header("Link: ; rel=preload; as=style", false);
+header("Link <../resources/Ahem.ttf>; rel=preload; as=font; crossorigin", false);
+header("Link: <   ../resources/dummy.js?foobar >; rel=preload; as='", false);
+?>
+<!DOCTYPE html>
+<script src="/js-test-resources/js-test.js"></script>
+<script>
+    shouldBeTrue("internals.isPreloaded('../resources/dummy.js');");
+    shouldBeFalse("internals.isPreloaded('../משאבים/dummy.css');");
+    shouldBeFalse("internals.isPreloaded('../résôûrcès/square.png');");
+    // Invalid URLs get preloaded (and get terminated further down the stack)
+    shouldBeTrue("internals.isPreloaded('../resources/Ahem{.ttf,.woff}');");
+    shouldBeFalse("internals.isPreloaded('../resources/test.mp4');");
+    shouldBeTrue("internals.isPreloaded('../resources/test\f.mp4');");
+    shouldBeTrue("internals.isPreloaded('../security/resources/cap\ttions.vtt');");
+    shouldBeFalse("internals.isPreloaded('../resources/dummy.xml?badvalue');");
+    shouldBeTrue("internals.isPreloaded('../resources/dummy   .xml');");
+    shouldBeFalse("internals.isPreloaded('../resources/dummy.xml');");
+    shouldBeFalse("internals.isPreloaded('../resources/dumm');");
+    shouldBeFalse("internals.isPreloaded('http://localhost:8000/preload/resources/square.png');");
+    // Invalid ports get preloaded (and get terminated further down the stack).
+    shouldBeTrue("internals.isPreloaded('http://localhost:53/preload/resources/dummy.js');");
+    shouldBeFalse("internals.isPreloaded('#foobar');");
+    shouldBeFalse("internals.isPreloaded('../resources/Ahem.ttf');");
+</script>
+<script>
+    if (window.internals)
+        window.internals.setLinkPreloadSupport(false);
+</script>
index c14ec43..1e2466b 100644 (file)
@@ -1856,6 +1856,7 @@ set(WebCore_SOURCES
     loader/FrameLoaderStateMachine.cpp
     loader/HistoryController.cpp
     loader/ImageLoader.cpp
+    loader/LinkHeader.cpp
     loader/LinkLoader.cpp
     loader/LinkPreloadResourceClients.cpp
     loader/LoadTiming.cpp
index 1a74b58..905d71a 100644 (file)
@@ -1,3 +1,57 @@
+2017-01-18  Yoav Weiss  <yoav@yoav.ws>
+
+        Add Link header support for preload.
+        https://bugs.webkit.org/show_bug.cgi?id=165521
+
+        Reviewed by Alex Christensen.
+
+        This patch adds Link header support for preload, by adding a Link
+        header parser, and calling it from FrameLoader::receivedFirstData.
+
+        Test: http/tests/preload/download_resources_from_header.php
+
+        * CMakeLists.txt: Add LinkHeader files.
+        * WebCore.xcodeproj/project.pbxproj: Add LinkHeader files.
+        * loader/FrameLoader.cpp:
+        (WebCore::receivedFirstData): Trigger Link based preloading by calling loadLinksFromHeader.
+        * loader/LinkHeader.cpp: Added.
+        (WebCore::isWhitespace):
+        (WebCore::isValidURLChar):
+        (WebCore::isValidParameterNameChar):
+        (WebCore::isValidParameterValueEnd):
+        (WebCore::isValidParameterValueChar):
+        (WebCore::isExtensionParameter):
+        (WebCore::parseURL):
+        (WebCore::invalidParameterDelimiter):
+        (WebCore::validFieldEnd):
+        (WebCore::parseParameterDelimiter):
+        (WebCore::paramterNameFromString):
+        (WebCore::parseParameterName):
+        (WebCore::skipQuotesIfNeeded):
+        (WebCore::parseParameterValue):
+        (WebCore::LinkHeader::setValue):
+        (WebCore::findNextHeader):
+        (WebCore::LinkHeader::LinkHeader):
+        (WebCore::LinkHeaderSet::LinkHeaderSet):
+        (WebCore::LinkHeaderSet::init):
+        * loader/LinkHeader.h: Added.
+        (WebCore::LinkHeader::url):
+        (WebCore::LinkHeader::rel):
+        (WebCore::LinkHeader::as):
+        (WebCore::LinkHeader::mimeType):
+        (WebCore::LinkHeader::media):
+        (WebCore::LinkHeader::crossOrigin):
+        (WebCore::LinkHeader::valid):
+        (WebCore::LinkHeaderSet::begin):
+        (WebCore::LinkHeaderSet::end):
+        (WebCore::LinkHeaderSet::operator[]):
+        (WebCore::LinkHeaderSet::size):
+        * loader/LinkLoader.cpp:
+        (WebCore::LinkLoader::loadLinksFromHeader): Loads link preload directives that arrive from Link headers.
+        (WebCore::LinkLoader::preloadIfNeeded): Add return value.
+        * loader/LinkLoader.h:
+        * platform/network/HTTPHeaderNames.in: Add "Link" as a header name.
+
 2017-01-18  Filip Pizlo  <fpizlo@apple.com>
 
         JSSegmentedVariableObject and its subclasses should have a sane destruction story
index 40f9bfb..224b67c 100644 (file)
                CB38FD5B1CD2325B00592A3F /* JSPerformanceResourceTiming.h in Headers */ = {isa = PBXBuildFile; fileRef = CB38FD591CD2314500592A3F /* JSPerformanceResourceTiming.h */; };
                CB61C7931D755207001ADC7D /* LinkPreloadResourceClients.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CBB6B2D11CB7ADC6009EDE1A /* LinkPreloadResourceClients.cpp */; };
                CB8CF0181A9358D4000D510B /* Microtasks.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CB8CF0151A934B43000D510B /* Microtasks.cpp */; };
+               CBA9DC0A1DF44DF10005675C /* LinkHeader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CBA9DC081DF44DC40005675C /* LinkHeader.cpp */; };
+               CBA9DC0B1DF44DF40005675C /* LinkHeader.h in Headers */ = {isa = PBXBuildFile; fileRef = CBA9DC091DF44DC40005675C /* LinkHeader.h */; settings = {ATTRIBUTES = (Private, ); }; };
                CBB6B2D41CB7AE51009EDE1A /* LinkPreloadResourceClients.h in Headers */ = {isa = PBXBuildFile; fileRef = CBB6B2D21CB7ADD0009EDE1A /* LinkPreloadResourceClients.h */; settings = {ATTRIBUTES = (Private, ); }; };
                CBC2D22F1CE5B89D00D1880B /* ResourceTimingInformation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CBC2D22D1CE5B77400D1880B /* ResourceTimingInformation.cpp */; };
                CBC2D2301CE5B8A100D1880B /* ResourceTimingInformation.h in Headers */ = {isa = PBXBuildFile; fileRef = CBC2D22E1CE5B77D00D1880B /* ResourceTimingInformation.h */; settings = {ATTRIBUTES = (Private, ); }; };
                CB38FD581CD2314500592A3F /* JSPerformanceResourceTiming.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSPerformanceResourceTiming.cpp; sourceTree = "<group>"; };
                CB38FD591CD2314500592A3F /* JSPerformanceResourceTiming.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSPerformanceResourceTiming.h; sourceTree = "<group>"; };
                CB8CF0151A934B43000D510B /* Microtasks.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Microtasks.cpp; sourceTree = "<group>"; };
+               CBA9DC081DF44DC40005675C /* LinkHeader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LinkHeader.cpp; sourceTree = "<group>"; };
+               CBA9DC091DF44DC40005675C /* LinkHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LinkHeader.h; sourceTree = "<group>"; };
                CBB6B2D11CB7ADC6009EDE1A /* LinkPreloadResourceClients.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LinkPreloadResourceClients.cpp; sourceTree = "<group>"; };
                CBB6B2D21CB7ADD0009EDE1A /* LinkPreloadResourceClients.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LinkPreloadResourceClients.h; sourceTree = "<group>"; };
                CBC2D22D1CE5B77400D1880B /* ResourceTimingInformation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ResourceTimingInformation.cpp; sourceTree = "<group>"; };
                BCB16BFB0979C38700467741 /* loader */ = {
                        isa = PBXGroup;
                        children = (
+                               CBA9DC081DF44DC40005675C /* LinkHeader.cpp */,
+                               CBA9DC091DF44DC40005675C /* LinkHeader.h */,
                                1A8F6BB00DB55CDC001DB794 /* appcache */,
                                512DD8E80D91E691000F89EE /* archive */,
                                A8D2B2521287A56000AF4DDA /* cache */,
                                A8EA7CAF0A192B9C00A8EF5F /* HTMLHRElement.h in Headers */,
                                A871DE270A152AC800B12A68 /* HTMLHtmlElement.h in Headers */,
                                A871DE2A0A152AC800B12A68 /* HTMLIFrameElement.h in Headers */,
+                               CBA9DC0B1DF44DF40005675C /* LinkHeader.h in Headers */,
                                A8EA7D2D0A19385500A8EF5F /* HTMLImageElement.h in Headers */,
                                A8EA7D2B0A19385500A8EF5F /* HTMLImageLoader.h in Headers */,
                                A81369CC097374F600D74463 /* HTMLInputElement.h in Headers */,
                                1402645E0AFDC19B005919E2 /* LoggingMac.mm in Sources */,
                                1A0409F31A43675C009E47F3 /* MachSendRight.cpp in Sources */,
                                932AD70517EFA2C30038F8FF /* MainFrame.cpp in Sources */,
+                               CBA9DC0A1DF44DF10005675C /* LinkHeader.cpp in Sources */,
                                7AE6C93B1BE0C60100E19E03 /* MainThreadSharedTimer.cpp in Sources */,
                                7AE6C9381BE0C5C800E19E03 /* MainThreadSharedTimerCF.cpp in Sources */,
                                1A8F6BC50DB55CDC001DB794 /* ManifestParser.cpp in Sources */,
index f4c961e..14d47da 100644 (file)
@@ -79,6 +79,7 @@
 #include "IgnoreOpensDuringUnloadCountIncrementer.h"
 #include "InspectorController.h"
 #include "InspectorInstrumentation.h"
+#include "LinkLoader.h"
 #include "LoadTiming.h"
 #include "LoaderStrategy.h"
 #include "Logging.h"
@@ -659,6 +660,9 @@ void FrameLoader::receivedFirstData()
     ASSERT(m_frame.document());
     auto& document = *m_frame.document();
 
+    ASSERT(m_frame.document());
+    LinkLoader::loadLinksFromHeader(m_documentLoader->response().httpHeaderField(HTTPHeaderName::Link), m_frame.document()->url(), *m_frame.document());
+
     double delay;
     String urlString;
     if (!parseHTTPRefresh(m_documentLoader->response().httpHeaderField(HTTPHeaderName::Refresh), delay, urlString))
diff --git a/Source/WebCore/loader/LinkHeader.cpp b/Source/WebCore/loader/LinkHeader.cpp
new file mode 100644 (file)
index 0000000..e2546d6
--- /dev/null
@@ -0,0 +1,365 @@
+/*
+ * Copyright 2015 The Chromium Authors. All rights reserved.
+ * Copyright (C) 2016 Akamai Technologies Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "LinkHeader.h"
+
+#include "ParsingUtilities.h"
+
+namespace WebCore {
+
+// LWSP definition in https://www.ietf.org/rfc/rfc0822.txt
+template <typename CharacterType>
+static bool isSpaceOrTab(CharacterType chr)
+{
+    return (chr == ' ') || (chr == '\t');
+}
+
+template <typename CharacterType>
+static bool isNotURLTerminatingChar(CharacterType chr)
+{
+    return (chr != '>');
+}
+
+template <typename CharacterType>
+static bool isValidParameterNameChar(CharacterType chr)
+{
+    // A separator, CTL or '%', '*' or '\'' means the char is not valid.
+    // Definition as attr-char at https://tools.ietf.org/html/rfc5987
+    // CTL and separators are defined in https://tools.ietf.org/html/rfc2616#section-2.2
+    // Valid chars are alpha-numeric and any of !#$&+-.^_`|~"
+    if ((chr >= '^' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || (chr >= '!' && chr <= '$') || chr == '&' || chr == '+' || chr == '-' || chr == '.')
+        return true;
+    return false;
+}
+
+template <typename CharacterType>
+static bool isParameterValueEnd(CharacterType chr)
+{
+    return chr == ';' || chr == ',';
+}
+
+template <typename CharacterType>
+static bool isParameterValueChar(CharacterType chr)
+{
+    return !isSpaceOrTab(chr) && !isParameterValueEnd(chr);
+}
+
+// Verify that the parameter is a link-extension which according to spec doesn't have to have a value.
+static bool isExtensionParameter(LinkHeader::LinkParameterName name)
+{
+    return name >= LinkHeader::LinkParameterUnknown;
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload
+// ^                     ^
+// position              end
+//
+// After (if successful: otherwise the method returns false)
+//
+// <cat.jpg>; rel=preload
+//          ^            ^
+//          position     end
+template <typename CharacterType>
+static std::optional<String> findURLBoundaries(CharacterType*& position, CharacterType* const end)
+{
+    ASSERT(position <= end);
+    skipWhile<CharacterType, isSpaceOrTab>(position, end);
+    if (!skipExactly<CharacterType>(position, end, '<'))
+        return std::nullopt;
+    skipWhile<CharacterType, isSpaceOrTab>(position, end);
+
+    CharacterType* urlStart = position;
+    skipWhile<CharacterType, isNotURLTerminatingChar>(position, end);
+    CharacterType* urlEnd = position;
+    skipUntil<CharacterType>(position, end, '>');
+    if (!skipExactly<CharacterType>(position, end, '>'))
+        return std::nullopt;
+
+    return String(urlStart, urlEnd - urlStart);
+}
+
+template <typename CharacterType>
+static bool invalidParameterDelimiter(CharacterType*& position, CharacterType* const end)
+{
+    ASSERT(position <= end);
+    return (!skipExactly<CharacterType>(position, end, ';') && (position < end) && (*position != ','));
+}
+
+template <typename CharacterType>
+static bool validFieldEnd(CharacterType*& position, CharacterType* const end)
+{
+    ASSERT(position <= end);
+    return (position == end || *position == ',');
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload
+//          ^            ^
+//          position     end
+//
+// After (if successful: otherwise the method returns false, and modifies the isValid boolean accordingly)
+//
+// <cat.jpg>; rel=preload
+//            ^          ^
+//            position  end
+template <typename CharacterType>
+static bool parseParameterDelimiter(CharacterType*& position, CharacterType* const end, bool& isValid)
+{
+    ASSERT(position <= end);
+    isValid = true;
+    skipWhile<CharacterType, isSpaceOrTab>(position, end);
+    if (invalidParameterDelimiter(position, end)) {
+        isValid = false;
+        return false;
+    }
+    skipWhile<CharacterType, isSpaceOrTab>(position, end);
+    if (validFieldEnd(position, end))
+        return false;
+    return true;
+}
+
+static LinkHeader::LinkParameterName paramterNameFromString(String name)
+{
+    if (equalLettersIgnoringASCIICase(name, "rel"))
+        return LinkHeader::LinkParameterRel;
+    if (equalLettersIgnoringASCIICase(name, "anchor"))
+        return LinkHeader::LinkParameterAnchor;
+    if (equalLettersIgnoringASCIICase(name, "crossorigin"))
+        return LinkHeader::LinkParameterCrossOrigin;
+    if (equalLettersIgnoringASCIICase(name, "title"))
+        return LinkHeader::LinkParameterTitle;
+    if (equalLettersIgnoringASCIICase(name, "media"))
+        return LinkHeader::LinkParameterMedia;
+    if (equalLettersIgnoringASCIICase(name, "type"))
+        return LinkHeader::LinkParameterType;
+    if (equalLettersIgnoringASCIICase(name, "rev"))
+        return LinkHeader::LinkParameterRev;
+    if (equalLettersIgnoringASCIICase(name, "hreflang"))
+        return LinkHeader::LinkParameterHreflang;
+    if (equalLettersIgnoringASCIICase(name, "as"))
+        return LinkHeader::LinkParameterAs;
+    return LinkHeader::LinkParameterUnknown;
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload
+//            ^          ^
+//            position   end
+//
+// After (if successful: otherwise the method returns false)
+//
+// <cat.jpg>; rel=preload
+//                ^      ^
+//            position  end
+template <typename CharacterType>
+static bool parseParameterName(CharacterType*& position, CharacterType* const end, LinkHeader::LinkParameterName& name)
+{
+    ASSERT(position <= end);
+    CharacterType* nameStart = position;
+    skipWhile<CharacterType, isValidParameterNameChar>(position, end);
+    CharacterType* nameEnd = position;
+    skipWhile<CharacterType, isSpaceOrTab>(position, end);
+    bool hasEqual = skipExactly<CharacterType>(position, end, '=');
+    skipWhile<CharacterType, isSpaceOrTab>(position, end);
+    name = paramterNameFromString(String(nameStart, nameEnd - nameStart));
+    if (hasEqual)
+        return true;
+    bool validParameterValueEnd = (position == end) || isParameterValueEnd(*position);
+    return validParameterValueEnd && isExtensionParameter(name);
+}
+
+// Before:
+//
+// <cat.jpg>; rel="preload"; type="image/jpeg";
+//                ^                            ^
+//            position                        end
+//
+// After (if the parameter starts with a quote, otherwise the method returns false)
+//
+// <cat.jpg>; rel="preload"; type="image/jpeg";
+//                         ^                   ^
+//                     position               end
+template <typename CharacterType>
+static bool skipQuotesIfNeeded(CharacterType*& position, CharacterType* const end, bool& completeQuotes)
+{
+    ASSERT(position <= end);
+    unsigned char quote;
+    if (skipExactly<CharacterType>(position, end, '\''))
+        quote = '\'';
+    else if (skipExactly<CharacterType>(position, end, '"'))
+        quote = '"';
+    else
+        return false;
+
+    while (!completeQuotes && position < end) {
+        skipUntil(position, end, static_cast<CharacterType>(quote));
+        if (*(position - 1) != '\\')
+            completeQuotes = true;
+        completeQuotes = skipExactly(position, end, static_cast<CharacterType>(quote)) && completeQuotes;
+    }
+    return true;
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload; foo=bar
+//                ^               ^
+//            position            end
+//
+// After (if successful: otherwise the method returns false)
+//
+// <cat.jpg>; rel=preload; foo=bar
+//                       ^        ^
+//                   position     end
+template <typename CharacterType>
+static bool parseParameterValue(CharacterType*& position, CharacterType* const end, String& value)
+{
+    ASSERT(position <= end);
+    CharacterType* valueStart = position;
+    CharacterType* valueEnd = position;
+    bool completeQuotes = false;
+    bool hasQuotes = skipQuotesIfNeeded(position, end, completeQuotes);
+    if (!hasQuotes)
+        skipWhile<CharacterType, isParameterValueChar>(position, end);
+    valueEnd = position;
+    skipWhile<CharacterType, isSpaceOrTab>(position, end);
+    if ((!completeQuotes && valueStart == valueEnd) || (position != end && !isParameterValueEnd(*position))) {
+        value = String("");
+        return false;
+    }
+    if (hasQuotes)
+        ++valueStart;
+    if (completeQuotes)
+        --valueEnd;
+    ASSERT(valueEnd >= valueStart);
+    value = String(valueStart, valueEnd - valueStart);
+    return !hasQuotes || completeQuotes;
+}
+
+void LinkHeader::setValue(LinkParameterName name, String value)
+{
+    switch (name) {
+    case LinkParameterRel:
+        if (!m_rel)
+            m_rel = value;
+        break;
+    case LinkParameterAnchor:
+        m_isValid = false;
+        break;
+    case LinkParameterCrossOrigin:
+        m_crossOrigin = value;
+        break;
+    case LinkParameterAs:
+        m_as = value;
+        break;
+    case LinkParameterType:
+        m_mimeType = value;
+        break;
+    case LinkParameterMedia:
+        m_media = value;
+        break;
+    case LinkParameterTitle:
+    case LinkParameterRev:
+    case LinkParameterHreflang:
+    case LinkParameterUnknown:
+        // These parameters are not yet supported, so they are currently ignored.
+        break;
+    }
+    // FIXME: Add support for more header parameters as neccessary.
+}
+
+template <typename CharacterType>
+static void findNextHeader(CharacterType*& position, CharacterType* const end)
+{
+    ASSERT(position <= end);
+    skipUntil<CharacterType>(position, end, ',');
+    skipExactly<CharacterType>(position, end, ',');
+}
+
+template <typename CharacterType>
+LinkHeader::LinkHeader(CharacterType*& position, CharacterType* const end)
+{
+    ASSERT(position <= end);
+    auto urlResult = findURLBoundaries(position, end);
+    if (urlResult == std::nullopt) {
+        m_isValid = false;
+        findNextHeader(position, end);
+        return;
+    }
+    m_url = urlResult.value();
+
+    while (m_isValid && position < end) {
+        if (!parseParameterDelimiter(position, end, m_isValid)) {
+            findNextHeader(position, end);
+            return;
+        }
+
+        LinkParameterName parameterName;
+        if (!parseParameterName(position, end, parameterName)) {
+            findNextHeader(position, end);
+            m_isValid = false;
+            return;
+        }
+
+        String parameterValue;
+        if (!parseParameterValue(position, end, parameterValue) && !isExtensionParameter(parameterName)) {
+            findNextHeader(position, end);
+            m_isValid = false;
+            return;
+        }
+
+        setValue(parameterName, parameterValue);
+    }
+    findNextHeader(position, end);
+}
+
+LinkHeaderSet::LinkHeaderSet(const String& header)
+{
+    if (header.isNull())
+        return;
+
+    if (header.is8Bit())
+        init(header.characters8(), header.length());
+    else
+        init(header.characters16(), header.length());
+}
+
+template <typename CharacterType>
+void LinkHeaderSet::init(CharacterType* headerValue, size_t length)
+{
+    CharacterType* position = headerValue;
+    CharacterType* const end = headerValue + length;
+    while (position < end)
+        m_headerSet.append(LinkHeader(position, end));
+}
+
+} // namespace WebCore
+
diff --git a/Source/WebCore/loader/LinkHeader.h b/Source/WebCore/loader/LinkHeader.h
new file mode 100644 (file)
index 0000000..8e35474
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2015 The Chromium Authors. All rights reserved.
+ * Copyright (C) 2016 Akamai Technologies Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+class LinkHeader {
+public:
+    template <typename CharacterType>
+    LinkHeader(CharacterType*& position, CharacterType* const end);
+
+    const String& url() const { return m_url; }
+    const String& rel() const { return m_rel; }
+    const String& as() const { return m_as; }
+    const String& mimeType() const { return m_mimeType; }
+    const String& media() const { return m_media; }
+    const String& crossOrigin() const { return m_crossOrigin; }
+    bool valid() const { return m_isValid; }
+
+    enum LinkParameterName {
+        LinkParameterRel,
+        LinkParameterAnchor,
+        LinkParameterTitle,
+        LinkParameterMedia,
+        LinkParameterType,
+        LinkParameterRev,
+        LinkParameterHreflang,
+        // Beyond this point, only link-extension parameters
+        LinkParameterUnknown,
+        LinkParameterCrossOrigin,
+        LinkParameterAs,
+    };
+
+private:
+    void setValue(LinkParameterName, String value);
+
+    String m_url;
+    String m_rel;
+    String m_as;
+    String m_mimeType;
+    String m_media;
+    String m_crossOrigin;
+    bool m_isValid { true };
+};
+
+class LinkHeaderSet {
+public:
+    LinkHeaderSet(const String& header);
+
+    Vector<LinkHeader>::const_iterator begin() const { return m_headerSet.begin(); }
+    Vector<LinkHeader>::const_iterator end() const { return m_headerSet.end(); }
+
+private:
+    template <typename CharacterType>
+    void init(CharacterType* headerValue, size_t length);
+
+    Vector<LinkHeader> m_headerSet;
+};
+
+} // namespace WebCore
+
index fa078b6..1ca124f 100644 (file)
@@ -43,6 +43,7 @@
 #include "Frame.h"
 #include "FrameLoaderClient.h"
 #include "FrameView.h"
+#include "LinkHeader.h"
 #include "LinkPreloadResourceClients.h"
 #include "LinkRelAttribute.h"
 #include "RuntimeEnabledFeatures.h"
@@ -83,6 +84,24 @@ void LinkLoader::notifyFinished(CachedResource& resource)
     m_cachedLinkResource = nullptr;
 }
 
+void LinkLoader::loadLinksFromHeader(const String& headerValue, const URL& baseURL, Document& document)
+{
+    if (headerValue.isEmpty())
+        return;
+    LinkHeaderSet headerSet(headerValue);
+    for (auto& header : headerSet) {
+        if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty())
+            continue;
+
+        LinkRelAttribute relAttribute(header.rel());
+        URL url(baseURL, header.url());
+        // Sanity check to avoid re-entrancy here.
+        if (equalIgnoringFragmentIdentifier(url, baseURL))
+            continue;
+        preloadIfNeeded(relAttribute, url, document, header.as(), header.crossOrigin(), nullptr, nullptr);
+    }
+}
+
 std::optional<CachedResource::Type> LinkLoader::resourceTypeFromAsAttribute(const String& as)
 {
     if (as.isEmpty())
@@ -139,21 +158,22 @@ static std::unique_ptr<LinkPreloadResourceClient> createLinkPreloadResourceClien
     return nullptr;
 }
 
-void LinkLoader::preloadIfNeeded(const LinkRelAttribute& relAttribute, const URL& href, Document& document, const String& as, const String& crossOriginMode)
+std::unique_ptr<LinkPreloadResourceClient> LinkLoader::preloadIfNeeded(const LinkRelAttribute& relAttribute, const URL& href, Document& document, const String& as, const String& crossOriginMode, LinkLoader* loader, LinkLoaderClient* client)
 {
     if (!document.loader() || !relAttribute.isLinkPreload)
-        return;
+        return nullptr;
 
     ASSERT(RuntimeEnabledFeatures::sharedFeatures().linkPreloadEnabled());
     if (!href.isValid()) {
         document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, String("<link rel=preload> has an invalid `href` value"));
-        return;
+        return nullptr;
     }
     auto type = LinkLoader::resourceTypeFromAsAttribute(as);
     if (!type) {
         document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, String("<link rel=preload> must have a valid `as` value"));
-        m_client.linkLoadingErrored();
-        return;
+        if (client)
+            client->linkLoadingErrored();
+        return nullptr;
     }
 
     ResourceRequest resourceRequest(document.completeURL(href));
@@ -164,8 +184,9 @@ void LinkLoader::preloadIfNeeded(const LinkRelAttribute& relAttribute, const URL
     linkRequest.setAsPotentiallyCrossOrigin(crossOriginMode, document);
     CachedResourceHandle<CachedResource> cachedLinkResource = document.cachedResourceLoader().preload(type.value(), WTFMove(linkRequest));
 
-    if (cachedLinkResource)
-        m_preloadResourceClient = createLinkPreloadResourceClient(*cachedLinkResource, *this, type.value());
+    if (cachedLinkResource && loader)
+        return createLinkPreloadResourceClient(*cachedLinkResource, *loader, type.value());
+    return nullptr;
 }
 
 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const URL& href, const String& as, const String& crossOrigin, Document& document)
@@ -178,8 +199,11 @@ bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const URL& href,
             document.frame()->loader().client().prefetchDNS(href.host());
     }
 
-    if (m_client.shouldLoadLink())
-        preloadIfNeeded(relAttribute, href, document, as, crossOrigin);
+    if (m_client.shouldLoadLink()) {
+        auto resourceClient = preloadIfNeeded(relAttribute, href, document, as, crossOrigin, this, &m_client);
+        if (resourceClient)
+            m_preloadResourceClient = WTFMove(resourceClient);
+    }
 
 #if ENABLE(LINK_PREFETCH)
     if ((relAttribute.isLinkPrefetch || relAttribute.isLinkSubresource) && href.isValid() && document.frame()) {
index e773b64..07503d9 100644 (file)
@@ -53,13 +53,14 @@ public:
 
     bool loadLink(const LinkRelAttribute&, const URL&, const String& as, const String& crossOrigin, Document&);
     static std::optional<CachedResource::Type> resourceTypeFromAsAttribute(const String& as);
+    static void loadLinksFromHeader(const String& headerValue, const URL& baseURL, Document&);
 
     WeakPtr<LinkLoader> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
     void triggerEvents(const CachedResource&);
 
 private:
     void notifyFinished(CachedResource&) override;
-    void preloadIfNeeded(const LinkRelAttribute&, const URL& href, Document&, const String& as, const String& crossOriginMode);
+    static std::unique_ptr<LinkPreloadResourceClient> preloadIfNeeded(const LinkRelAttribute&, const URL& href, Document&, const String& as, const String& crossOriginMode, LinkLoader*, LinkLoaderClient*);
 
     LinkLoaderClient& m_client;
     CachedResourceHandle<CachedResource> m_cachedLinkResource;