Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(573)

Unified Diff: Source/core/loader/LinkHeader.cpp

Issue 889883002: Add a parser for Link headers (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Self review + added expectations Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/loader/LinkHeader.cpp
diff --git a/Source/core/loader/LinkHeader.cpp b/Source/core/loader/LinkHeader.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..ae6c9adbba0c631f9c2ce7a4112cd5278da942d3
--- /dev/null
+++ b/Source/core/loader/LinkHeader.cpp
@@ -0,0 +1,184 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/loader/LinkHeader.h"
+
+#include "platform/ParsingUtilities.h"
+
+namespace blink {
+
+// LWSP definition in https://www.ietf.org/rfc/rfc0822.txt
+template <typename CharType>
+static bool isWhitespace(CharType chr)
+{
+ return (chr == ' ') || (chr == '\t');
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload
+// ^ ^
+// position end
+//
+// After (if successful: otherwise the method returns false)
+//
+// <cat.jpg>; rel=preload
+// ^ ^
+// position end
+template <typename CharType>
+static bool parseHeaderStart(CharType*& position, CharType* end)
+{
+ skipWhile<CharType, isWhitespace<CharType>>(position, end);
+ if (*position != '<')
+ return false;
Mike West 2015/02/02 13:14:54 I'd prefer `skipExactly` here, rather than hiding
+ skipWhile<CharType, isWhitespace<CharType>>(++position, end);
+ return true;
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload
+// ^ ^
+// position end
+//
+// After (if successful: otherwise the method returns a null pointer)
+//
+// <cat.jpg>; rel=preload
+// ^ ^
+// position end
+template <typename CharType>
+static CharType* parseURL(CharType*& position, CharType* end)
Mike West 2015/02/02 13:14:54 It's somewhat strange that parsing a URL includes
+{
+ skipUntil<CharType, isWhitespace<CharType>>(position, end, '>');
+ CharType* urlEnd = position;
+ skipUntil<CharType>(position, end, '>');
+ if (position == end)
+ return nullptr;
+ ++position;
Mike West 2015/02/02 13:14:54 Prefer `skipExactly` to `++position`. The nice thi
+ return urlEnd;
Mike West 2015/02/02 13:14:53 Both SRI and CSP use `String&` out parameters (e.g
+}
+
+// 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 CharType>
+static bool parseParameterDelimiter(CharType*& position, CharType* end, bool& isValidDelimiter)
+{
+ skipWhile<CharType, isWhitespace<CharType>>(position, end);
+ if (*position != ';' && position != end) {
+ isValidDelimiter = false;
Mike West 2015/02/02 13:14:54 You never set this to true. Is that intentional?
+ return false;
+ }
+ skipWhile<CharType, isWhitespace<CharType>>(++position, end);
Mike West 2015/02/02 13:14:54 skipExactly.
+ if (position == end)
+ return false;
+ return true;
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload
+// ^ ^
+// position end
+//
+// After (if successful: otherwise the method returns a null pointer)
+//
+// <cat.jpg>; rel=preload
+// ^ ^
+// position end
+template <typename CharType>
+static CharType* parseParameterName(CharType*& position, CharType* end)
+{
+ skipUntil<CharType, isWhitespace<CharType>>(position, end, '=');
Mike West 2015/02/02 13:14:54 What does this mean? I think it means `skipWhile<C
+ CharType* nameEnd = position;
+ skipWhile<CharType, isWhitespace<CharType>>(position, end);
+ if (*position != '=')
+ return nullptr;
+ skipWhile<CharType, isWhitespace<CharType>>(++position, end);
Mike West 2015/02/02 13:14:53 skipExactly.
+ return nameEnd;
+}
+
+// Before:
+//
+// <cat.jpg>; rel=preload; foo=bar
+// ^ ^
+// position end
+//
+// After (if successful: otherwise the method returns a null pointer)
+//
+// <cat.jpg>; rel=preload; foo=bar
+// ^ ^
+// position end
+template <typename CharType>
+static CharType* parseParameterValue(CharType*& position, CharType* end)
+{
+ CharType* valueStart = position;
+ skipUntil<CharType, isWhitespace<CharType>>(position, end, ';');
Mike West 2015/02/02 13:14:53 Ditto.
+ CharType* valueEnd = position;
+ skipWhile<CharType, isWhitespace<CharType>>(position, end);
+ if ((valueEnd == valueStart) || (*position != ';' && position != end))
Mike West 2015/02/02 13:14:54 `skipExactly` should take care of this whole claus
+ return nullptr;
+ return valueEnd;
+}
+
+template <typename CharType>
+bool LinkHeader::init(CharType* headerValue, unsigned len)
+{
+ CharType* position = headerValue;
+ CharType* end = headerValue + len;
+
+ if (!parseHeaderStart(position, end))
+ return false;
+
+ CharType* urlStart = position;
+ CharType* urlEnd = parseURL(position, end);
+ if (!urlEnd)
+ return false;
+ m_url = String(urlStart, urlEnd - urlStart);
+
+ while (position < end) {
+ bool isValidDelimiter = true;
+ if (!parseParameterDelimiter(position, end, isValidDelimiter))
+ return isValidDelimiter;
+
+ CharType* nameStart = position;
+ CharType* nameEnd = parseParameterName(position, end);
+ if (!nameEnd)
+ return false;
+
+ CharType* valueStart = position;
+ CharType* valueEnd = parseParameterValue(position, end);
+ if (!valueEnd)
+ return false;
+
+ if (String(nameStart, nameEnd - nameStart) == "rel")
Mike West 2015/02/02 13:14:54 Is this hard-coded because 'rel' is the only accep
+ m_rel = String(valueStart, valueEnd - valueStart);
+ }
+
+ return true;
+}
+
+LinkHeader::LinkHeader(const String& header)
+{
+ if (header.isNull()) {
+ m_isValid = false;
+ return;
+ }
+
+ if (header.is8Bit())
+ m_isValid = init(header.characters8(), header.length());
+ else
+ m_isValid = init(header.characters16(), header.length());
+}
+
+}

Powered by Google App Engine
This is Rietveld 408576698