-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.js
425 lines (353 loc) · 12.4 KB
/
parser.js
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* jshint esversion: 8 */
/* jshint node: true */
"use strict";
const { domainToASCII } = require("node:url");
const { FAILURE, Regex } = require("./constants");
const {
is_ascii_hex,
is_ascii_digit,
utf8_percent_encode,
} = require("./string");
const { is_c0_control_percent_encoded } = require("./encoding");
/**
* @function ends_with_a_number
* @param {string} input
* @returns {boolean}
*/
function ends_with_a_number(input) {
// Let parts be the result of strictly splitting input on U+002E (.).
const last_point = input.lastIndexOf(".");
// Let last be the last item in parts.
let last = input.substring(last_point + 1, input.length);
// If the last item in parts is the empty string, then:
if (last_point === input.length - 1) {
let previous_point = input.lastIndexOf(".", last_point - 1);
// If part's size is 1, then return false.
if (previous_point === last_point) {
return false;
}
// Remove the last item from parts.
last = input.substring(previous_point + 1, last_point - 1);
}
// If parsing last as an IPv4 number does not return failure, then return true.
if (parse_ipv4_number(last) !== FAILURE) {
return true;
}
// If last is non-empty and contains only ASCII digits, then return true.
return /^[0-9]+$/u.test(last);
}
/**
* @function parse_ipv6
* @description [Specification]{@link https://url.spec.whatwg.org/#concept-ipv6-parser}
* @param {string} input
* @returns {number[]|number}
*/
function parse_ipv6(input) {
let address = [0, 0, 0, 0, 0, 0, 0, 0];
let piece_index = 0;
let compress = null;
let pointer = 0;
// If c is U+003A (:), then:
if (input[pointer] === ":") {
// If remaining does not start with U+003A (:), validation error, return failure.
if (input[pointer + 1] !== ":") {
return FAILURE;
}
// Increase pointer by 2.
pointer += 2;
// Increase pieceIndex by 1 and then set compress to pieceIndex.
piece_index += 1;
compress = piece_index;
}
// While c is not the EOF code point:
while (pointer < input.length) {
// If pieceIndex is 8, validation error, return failure.
if (piece_index === 8) {
return FAILURE;
}
// If c is U+003A (:), then:
if (input[pointer] === ":") {
// If compress is non-null, validation error, return failure.
if (compress !== null) {
return FAILURE;
}
// Increase pointer and pieceIndex by 1, set compress to pieceIndex, and then continue.
pointer += 1;
piece_index += 1;
compress = piece_index;
continue;
}
// Let value and length be 0.
let value = 0;
let length = 0;
// While length is less than 4 and c is an ASCII hex digit,
// set value to value × 0x10 + c interpreted as hexadecimal number, and increase pointer and length by 1.
while (length < 4 && is_ascii_hex(input.codePointAt(pointer))) {
value = value * 0x10 + parseInt(input[pointer], 16);
pointer++;
length++;
}
// If c is U+002E (.), then:
if (input[pointer] === ".") {
// If length is 0, validation error, return failure.
if (length === 0) {
return FAILURE;
}
// Decrease pointer by length.
pointer -= length;
// If pieceIndex is greater than 6, validation error, return failure.
if (piece_index > 6) {
return FAILURE;
}
// Let numbersSeen be 0.
let numbers_seen = 0;
// While c is not the EOF code point:
while (input[pointer] !== undefined) {
// Let ipv4Piece be null.
/** @type {number|null} */
let ipv4_piece = null;
// If numbersSeen is greater than 0, then:
if (numbers_seen > 0) {
// If c is a U+002E (.) and numbersSeen is less than 4, then increase pointer by 1.
if (input[pointer] === "." && numbers_seen < 4) {
pointer++;
}
// Otherwise, validation error, return failure.
else {
return FAILURE;
}
}
// If c is not an ASCII digit, validation error, return failure.
if (!is_ascii_digit(input.codePointAt(pointer))) {
return FAILURE;
}
// While c is an ASCII digit:
while (is_ascii_digit(input.codePointAt(pointer))) {
// Let number be c interpreted as decimal number.
let number = parseInt(input[pointer]);
// If ipv4Piece is null, then set ipv4Piece to number.
if (ipv4_piece === null) {
ipv4_piece = number;
}
// Otherwise, if ipv4Piece is 0, validation error, return failure.
else if (ipv4_piece === 0) {
return FAILURE;
}
// Otherwise, set ipv4Piece to ipv4Piece × 10 + number.
else {
ipv4_piece = ipv4_piece * 10 + number;
}
// If ipv4Piece is greater than 255, validation error, return failure.
if (ipv4_piece > 255) {
return FAILURE;
}
pointer++;
}
// Set address[pieceIndex] to address[pieceIndex] × 0x100 + ipv4Piece.
address[piece_index] = address[piece_index] * 0x100 + ipv4_piece;
// Increase numbersSeen by 1.
numbers_seen++;
// If numbersSeen is 2 or 4, then increase pieceIndex by 1.
if (numbers_seen === 2 || numbers_seen === 4) {
piece_index++;
}
}
// If numbersSeen is not 4, validation error, return failure.
if (numbers_seen !== 4) {
return FAILURE;
}
break;
}
// Otherwise, if c is U+003A (:):
else if (input[pointer] === ":") {
// Increase pointer by 1.
pointer++;
// If c is the EOF code point, validation error, return failure.
if (input[pointer] === undefined) {
return FAILURE;
}
}
// Otherwise, if c is not the EOF code point, validation error, return failure.
else if (input[pointer] !== undefined) {
return FAILURE;
}
// Set address[pieceIndex] to value.
address[piece_index] = value;
// Increase pieceIndex by 1.
piece_index++;
}
// If compress is non-null, then:
if (compress !== null) {
// Let swaps be pieceIndex − compress.
let swaps = piece_index - compress;
// Set pieceIndex to 7.
piece_index = 7;
// While pieceIndex is not 0 and swaps is greater than 0, swap address[pieceIndex] with address[compress + swaps − 1],
// and then decrease both pieceIndex and swaps by 1.
while (piece_index !== 0 && swaps > 0) {
let temp = address[compress + swaps - 1];
address[compress + swaps - 1] = address[piece_index];
address[piece_index] = temp;
piece_index--;
swaps--;
}
}
// Otherwise, if compress is null and pieceIndex is not 8, validation error, return failure.
else if (piece_index !== 8) {
return FAILURE;
}
// Return address
return address;
}
/**
* @function parse_ipv4
* @description [Specification]{@link https://url.spec.whatwg.org/#concept-ipv4-parser}
* @param {string} input
* @returns {string|number}
*/
function parse_ipv4(input) {
// Let parts be the result of strictly splitting input on U+002E (.).
let parts = input.split(".");
// If the last item in parts is the empty string, then:
// If parts’s size is greater than 1, then remove the last item from parts.
if (parts.length > 1 && parts[parts.length - 1] === "") {
parts.pop();
}
// If parts’s size is greater than 4, validation error, return failure.
if (parts.length > 4) {
return FAILURE;
}
let numbers = [];
// For each part of parts:
for (let i = 0; i < parts.length; i++) {
// Let result be the result of parsing part.
let result = parse_ipv4_number(parts[i]);
// If result is failure, validation error, return failure.
// If any item in numbers is greater than 255, validation error.
// If any but the last item in numbers is greater than 255, then return failure.
// Note: FAILURE is bigger than 255. Therefore, no need to check if result equals FAILURE
if (result > 255) {
return FAILURE;
}
// Append result[0] to numbers.
numbers.push(result);
}
// If the last item in numbers is greater than or equal to 256(5 − numbers’s size), validation error, return failure.
if (numbers[numbers.length - 1] >= 256 ** (5 - numbers.length)) {
return FAILURE;
}
// Let ipv4 be the last item in numbers.
// Remove the last item from numbers.
let ipv4 = numbers.pop();
// Let counter be 0.
for (let i = 0; i < numbers.length; i++) {
// Increment ipv4 by n × 256^(3 − counter).
ipv4 += numbers[i] * 256 ** (3 - i);
}
return ipv4;
}
/**
* @function parse_ipv4_number
* @description [Specification]{@link https://url.spec.whatwg.org/#ipv4-number-parser}
* @param {string} input Non-empty (length > 0) input string
* @returns {number|number}
*/
function parse_ipv4_number(input) {
// Let R be 10.
let R = 10;
// If input contains at least two code points.
if (input.length >= 2) {
let first_character = input.codePointAt(0);
let second_character = input.codePointAt(1);
// If the first two code points are either "0X" or "0x", then:
if (
first_character === 48 &&
(second_character === 120 || second_character === 88)
) {
// Remove the first two code points from input.
input = input.slice(2);
// Set R to 16.
R = 16;
}
// Otherwise, if the first code point is U+0030 (0), then:
else if (first_character === 48) {
// Remove the first code point from input.
input = input.slice(1);
// Set R to 8.
R = 8;
}
}
// If input is the empty string, then return (0, true).
if (input === "") {
return 0;
}
let regex = /[^0-7]/u;
if (R === 10) {
regex = /[^0-9]/u;
} else if (R === 16) {
regex = /[^0-9A-Fa-f]/u;
}
// If input contains a code point that is not a radix-R digit, then return failure.
if (regex.test(input)) {
return FAILURE;
}
// Let output be the mathematical integer value that is represented by input in radix-R notation, using ASCII
// hex digits for digits with values 0 through 15.
return parseInt(input, R);
}
/**
* @function parse_opaque_host
* @description [Specification]{@link https://url.spec.whatwg.org/#concept-opaque-host-parser}
*
* A forbidden host code point is U+0000 NULL, U+0009 TAB, U+000A LF, U+000D CR, U+0020 SPACE, U+0023 (#), U+002F (/),
* U+003A (:), U+003C (<), U+003E (>), U+003F (?), U+0040 (@), U+005B ([), U+005C (\), U+005D (]), U+005E (^), or U+007C (|).
*
* @param {string} buffer
* @returns {string|number}
*/
function parse_opaque_host(buffer) {
// If input contains a forbidden host code point, validation error, return failure.
if (buffer.search(Regex.FORBIDDEN_HOST_CODE_POINTS) !== -1) {
return FAILURE;
}
// Return the result of running UTF-8 percent-encode on input using the C0 control percent-encode set.
return utf8_percent_encode(buffer, is_c0_control_percent_encoded);
}
/**
* @function parse_host
* @description [Official Specification]{@link https://url.spec.whatwg.org/#host-parsing}
* @param {string} buffer
* @param {boolean=} [is_not_url_special=false] is_not_url_special
* @returns {string|number|number[]}
*/
function parse_host(buffer, is_not_url_special = false) {
// If input starts with U+005B ([), then:
if (buffer[0] === "[") {
// If input does not end with U+005D (]), validation error, return failure.
if (buffer[buffer.length - 1] !== "]") {
return FAILURE;
}
// Return the result of IPv6 parsing input with its leading U+005B ([) and trailing U+005D (]) removed.
return parse_ipv6(buffer.substring(1, buffer.length - 1));
}
// If isNotSpecial is true, then return the result of opaque-host parsing input.
if (is_not_url_special) {
return parse_opaque_host(buffer);
}
const ascii_domain = domainToASCII(buffer);
// If asciiDomain is failure, validation error, return failure.
// domainToASCII returns empty string on failure.
if (ascii_domain === "") {
return FAILURE;
}
// If asciiDomain contains a forbidden domain code point, validation error, return failure.
if (ascii_domain.search(Regex.FORBIDDEN_DOMAIN_CODE_POINTS) !== -1) {
return FAILURE;
}
// If asciiDomain ends in a number, then return the result of IPv4 parsing asciiDomain.
if (ends_with_a_number(ascii_domain)) {
return parse_ipv4(ascii_domain);
}
return ascii_domain;
}
module.exports = { parse_host };