Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/loader/LinkHeader.h" | 6 #include "core/loader/LinkHeader.h" |
| 7 | 7 |
| 8 #include "platform/ParsingUtilities.h" | 8 #include "platform/ParsingUtilities.h" |
| 9 | 9 |
| 10 namespace blink { | 10 namespace blink { |
| 11 | 11 |
| 12 // LWSP definition in https://www.ietf.org/rfc/rfc0822.txt | 12 // LWSP definition in https://www.ietf.org/rfc/rfc0822.txt |
| 13 template <typename CharType> | 13 template <typename CharType> |
| 14 static bool isWhitespace(CharType chr) | 14 static bool isWhitespace(CharType chr) |
| 15 { | 15 { |
| 16 return (chr == ' ') || (chr == '\t'); | 16 return (chr == ' ') || (chr == '\t'); |
| 17 } | 17 } |
| 18 | 18 |
| 19 template <typename CharType> | 19 template <typename CharType> |
| 20 static bool isValidURLChar(CharType chr) | 20 static bool isValidURLChar(CharType chr) |
| 21 { | 21 { |
| 22 return !isWhitespace(chr) && chr != '>'; | 22 return !isWhitespace(chr) && chr != '>'; |
| 23 } | 23 } |
| 24 | 24 |
| 25 template <typename CharType> | 25 template <typename CharType> |
| 26 static bool isValidParameterNameChar(CharType chr) | 26 static bool isValidParameterNameChar(CharType chr) |
| 27 { | 27 { |
| 28 return !isWhitespace(chr) && chr != '='; | 28 // TODO(yoav): We need to move this function to a central location and possi bly rewrite as a lookup table. https://crbug.com/527324 |
| 29 | |
| 30 // Alpha-numeric is a valid char. | |
| 31 // This is likely the common case - bailing early. | |
| 32 if (isASCIIAlphanumeric(chr)) | |
| 33 return true; | |
| 34 // A separator or CTL or '%', '*' or '\'' means the char is not valid. | |
| 35 // So any of: |{}[]/\:;<=>?@,()*'"% | |
|
Mike West
2015/09/02 12:30:52
TODO link to the bug you created?
Yoav Weiss
2015/09/02 12:32:33
I did :D (a few lines up)
| |
| 36 if (chr <= ' ' || chr > '|' || chr == '{' || chr == ']' || chr == '[' | |
| 37 || chr == '/' || chr == '\\' || (chr <= '@' && chr >= ':') || chr == ',' | |
| 38 || (chr >= '(' && chr <= '*') || chr == '\'' || chr == '"' || chr == '%' ) { | |
| 39 return false; | |
| 40 } | |
| 41 return true; | |
| 29 } | 42 } |
| 30 | 43 |
| 31 template <typename CharType> | 44 template <typename CharType> |
| 32 static bool isValidParameterValueEnd(CharType chr) | 45 static bool isValidParameterValueEnd(CharType chr) |
| 33 { | 46 { |
| 34 return chr == ';' || chr == ','; | 47 return chr == ';' || chr == ','; |
| 35 } | 48 } |
| 36 | 49 |
| 37 template <typename CharType> | 50 template <typename CharType> |
| 38 static bool isValidParameterValueChar(CharType chr) | 51 static bool isValidParameterValueChar(CharType chr) |
| 39 { | 52 { |
| 40 return !isWhitespace(chr) && !isValidParameterValueEnd(chr); | 53 return !isWhitespace(chr) && !isValidParameterValueEnd(chr); |
| 41 } | 54 } |
| 42 | 55 |
| 56 // Verify that the parameter is a link-extension which according to spec doesn't have to have a value. | |
| 43 static bool isExtensionParameter(LinkHeader::LinkParameterName name) | 57 static bool isExtensionParameter(LinkHeader::LinkParameterName name) |
| 44 { | 58 { |
| 45 return name > LinkHeader::LinkParameterAnchor; | 59 return name >= LinkHeader::LinkParameterUnknown; |
| 46 } | 60 } |
| 47 | 61 |
| 48 // Before: | 62 // Before: |
| 49 // | 63 // |
| 50 // <cat.jpg>; rel=preload | 64 // <cat.jpg>; rel=preload |
| 51 // ^ ^ | 65 // ^ ^ |
| 52 // position end | 66 // position end |
| 53 // | 67 // |
| 54 // After (if successful: otherwise the method returns false) | 68 // After (if successful: otherwise the method returns false) |
| 55 // | 69 // |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 return false; | 123 return false; |
| 110 } | 124 } |
| 111 skipWhile<CharType, isWhitespace>(position, end); | 125 skipWhile<CharType, isWhitespace>(position, end); |
| 112 if (validFieldEnd(position, end)) | 126 if (validFieldEnd(position, end)) |
| 113 return false; | 127 return false; |
| 114 return true; | 128 return true; |
| 115 } | 129 } |
| 116 | 130 |
| 117 static LinkHeader::LinkParameterName paramterNameFromString(String name) | 131 static LinkHeader::LinkParameterName paramterNameFromString(String name) |
| 118 { | 132 { |
| 119 // FIXME: Add support for more header parameters as neccessary. | |
| 120 if (equalIgnoringCase(name, "rel")) | 133 if (equalIgnoringCase(name, "rel")) |
| 121 return LinkHeader::LinkParameterRel; | 134 return LinkHeader::LinkParameterRel; |
| 122 if (equalIgnoringCase(name, "anchor")) | 135 if (equalIgnoringCase(name, "anchor")) |
| 123 return LinkHeader::LinkParameterAnchor; | 136 return LinkHeader::LinkParameterAnchor; |
| 124 if (equalIgnoringCase(name, "crossorigin")) | 137 if (equalIgnoringCase(name, "crossorigin")) |
| 125 return LinkHeader::LinkParameterCrossOrigin; | 138 return LinkHeader::LinkParameterCrossOrigin; |
| 139 if (equalIgnoringCase(name, "title")) | |
| 140 return LinkHeader::LinkParameterTitle; | |
| 141 if (equalIgnoringCase(name, "media")) | |
| 142 return LinkHeader::LinkParameterMedia; | |
| 143 if (equalIgnoringCase(name, "type")) | |
| 144 return LinkHeader::LinkParameterType; | |
| 145 if (equalIgnoringCase(name, "rev")) | |
| 146 return LinkHeader::LinkParameterRev; | |
| 147 if (equalIgnoringCase(name, "hreflang")) | |
| 148 return LinkHeader::LinkParameterHreflang; | |
| 126 return LinkHeader::LinkParameterUnknown; | 149 return LinkHeader::LinkParameterUnknown; |
| 127 } | 150 } |
| 128 | 151 |
| 129 // Before: | 152 // Before: |
| 130 // | 153 // |
| 131 // <cat.jpg>; rel=preload | 154 // <cat.jpg>; rel=preload |
| 132 // ^ ^ | 155 // ^ ^ |
| 133 // position end | 156 // position end |
| 134 // | 157 // |
| 135 // After (if successful: otherwise the method returns false) | 158 // After (if successful: otherwise the method returns false) |
| 136 // | 159 // |
| 137 // <cat.jpg>; rel=preload | 160 // <cat.jpg>; rel=preload |
| 138 // ^ ^ | 161 // ^ ^ |
| 139 // position end | 162 // position end |
| 140 template <typename CharType> | 163 template <typename CharType> |
| 141 static bool parseParameterName(CharType*& position, CharType* end, LinkHeader::L inkParameterName& name) | 164 static bool parseParameterName(CharType*& position, CharType* end, LinkHeader::L inkParameterName& name) |
| 142 { | 165 { |
| 143 CharType* nameStart = position; | 166 CharType* nameStart = position; |
| 144 skipWhile<CharType, isValidParameterNameChar>(position, end); | 167 skipWhile<CharType, isValidParameterNameChar>(position, end); |
| 145 CharType* nameEnd = position; | 168 CharType* nameEnd = position; |
| 146 skipWhile<CharType, isWhitespace>(position, end); | 169 skipWhile<CharType, isWhitespace>(position, end); |
| 147 bool hasEqual = skipExactly<CharType>(position, end, '='); | 170 bool hasEqual = skipExactly<CharType>(position, end, '='); |
| 148 skipWhile<CharType, isWhitespace>(position, end); | 171 skipWhile<CharType, isWhitespace>(position, end); |
| 149 name = paramterNameFromString(String(nameStart, nameEnd - nameStart)); | 172 name = paramterNameFromString(String(nameStart, nameEnd - nameStart)); |
| 150 return hasEqual || isExtensionParameter(name); | 173 if (hasEqual) |
| 174 return true; | |
| 175 bool validParameterValueEnd = (position == end) || isValidParameterValueEnd( *position); | |
| 176 return validParameterValueEnd && isExtensionParameter(name); | |
| 151 } | 177 } |
| 152 | 178 |
| 153 // Before: | 179 // Before: |
| 154 // | 180 // |
| 155 // <cat.jpg>; rel="preload"; type="image/jpeg"; | 181 // <cat.jpg>; rel="preload"; type="image/jpeg"; |
| 156 // ^ ^ | 182 // ^ ^ |
| 157 // position end | 183 // position end |
| 158 // | 184 // |
| 159 // After (if the parameter starts with a quote, otherwise the method returns fal se) | 185 // After (if the parameter starts with a quote, otherwise the method returns fal se) |
| 160 // | 186 // |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 template <typename CharType> | 311 template <typename CharType> |
| 286 void LinkHeaderSet::init(CharType* headerValue, unsigned len) | 312 void LinkHeaderSet::init(CharType* headerValue, unsigned len) |
| 287 { | 313 { |
| 288 CharType* position = headerValue; | 314 CharType* position = headerValue; |
| 289 CharType* end = headerValue + len; | 315 CharType* end = headerValue + len; |
| 290 while (position < end) | 316 while (position < end) |
| 291 m_headerSet.append(LinkHeader(position, end)); | 317 m_headerSet.append(LinkHeader(position, end)); |
| 292 } | 318 } |
| 293 | 319 |
| 294 } | 320 } |
| OLD | NEW |