1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
|