GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — url: skip plain prefixes in percent-escape checks · nodejs/node@1540dfd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/url
    • url-searchparams-percent-scan.js
  • lib/internal
    • url.js
  • test/parallel
    • test-whatwg-url-searchparams-percent-scan.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/url/url-searchparams-percent-scan.js‎

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('node:assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e5],
8+
length: [32, 1024],
9+
position: ['start', 'end'],
10+
encoding: ['valid', 'invalid', 'unicode', 'plain'],
11+
});
12+
13+
function main({ n, length, position, encoding }) {
14+
const prefix = 'a'.repeat(length);
15+
const [encoded, decoded] = {
16+
valid: ['%20', ' '],
17+
invalid: ['%zz', '%zz'],
18+
unicode: ['%E2%82%AC', '\u20ac'],
19+
plain: ['text', 'text'],
20+
}[encoding];
21+
const input = position === 'start' ? encoded + prefix : prefix + encoded;
22+
const expected = position === 'start' ? decoded + prefix : prefix + decoded;
23+
const query = `key=${input}`;
24+
for (let index = 0; index < 1000; index++) {
25+
new URLSearchParams(query);
26+
}
27+
28+
let result;
29+
bench.start();
30+
for (let index = 0; index < n; index++) {
31+
result = new URLSearchParams(query);
32+
}
33+
bench.end(n);
34+
assert.deepStrictEqual([...result], [['key', expected]]);
35+
}
Collapse file

‎lib/internal/url.js‎

Copy file name to clipboardExpand all lines: lib/internal/url.js
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,11 +1321,11 @@ function unescapeFormComponent(s) {
13211321
}
13221322
}
13231323

1324-
function hasPercentHex(s) {
1324+
function hasPercentHex(s, start) {
13251325
const end = s.length - 2;
1326-
for (let i = 0; i < end; i++) {
1327-
if (StringPrototypeCharCodeAt(s, i) === 37 && // '%'
1328-
isHexTable[StringPrototypeCharCodeAt(s, i + 1)] === 1 &&
1326+
for (let i = start; i !== -1 && i < end;
1327+
i = StringPrototypeIndexOf(s, '%', i + 1)) {
1328+
if (isHexTable[StringPrototypeCharCodeAt(s, i + 1)] === 1 &&
13291329
isHexTable[StringPrototypeCharCodeAt(s, i + 2)] === 1) {
13301330
return true;
13311331
}
@@ -1347,7 +1347,7 @@ function decodeFormComponent(qs, start, end) {
13471347
// Only percent-decode when a complete %HH sequence exists. A lone '%' or
13481348
// a '%' followed by a non-hex character must be left intact so later
13491349
// serialization can encode the raw bytes.
1350-
if (pct === -1 || !hasPercentHex(replaced)) {
1350+
if (pct === -1 || !hasPercentHex(replaced, pct)) {
13511351
return replaced;
13521352
}
13531353
return unescapeFormComponent(replaced);
Collapse file

‎test/parallel/test-whatwg-url-searchparams-percent-scan.js‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('node:assert');
5+
6+
const cases = [
7+
['%41', 'A'],
8+
['%zz', '%zz'],
9+
['%zz%41', '%zzA'],
10+
['%', '%'],
11+
['%A', '%A'],
12+
['%C3%A9', '\u00e9'],
13+
['%F0%9F%98%80', '\ud83d\ude00'],
14+
['%FF', '\ufffd'],
15+
['%E2%82', '\ufffd'],
16+
];
17+
18+
for (const prefix of ['', 'k'.repeat(32), 'k'.repeat(1024), 'k+'.repeat(64)]) {
19+
const decodedPrefix = prefix.replaceAll('+', ' ');
20+
for (const [encoded, decoded] of cases) {
21+
for (const position of ['start', 'end']) {
22+
const input = position === 'start' ? encoded + prefix : prefix + encoded;
23+
const expected = position === 'start' ?
24+
decoded + decodedPrefix : decodedPrefix + decoded;
25+
const query = `${input}=${input}`;
26+
assert.deepStrictEqual([...new URLSearchParams(query)], [[expected, expected]]);
27+
const url = new URL(`https://example.org/?${query}`);
28+
assert.deepStrictEqual([...url.searchParams], [[expected, expected]]);
29+
}
30+
}
31+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.