Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/internal/querystring.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { ERR_INVALID_URI } = require('internal/errors').codes;

const hexTable = new Array(256);
for (var i = 0; i < 256; ++i)
hexTable[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
Expand All @@ -23,7 +25,72 @@ const isHexTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 256
];

function encodeStr(str, noEscapeTable, hexTable) {
const len = str.length;
if (len === 0)
return '';

var out = '';
var lastPos = 0;

for (var i = 0; i < len; i++) {
var c = str.charCodeAt(i);

// ASCII
if (c < 0x80) {
if (noEscapeTable[c] === 1)
continue;
if (lastPos < i)
out += str.slice(lastPos, i);
lastPos = i + 1;
out += hexTable[c];
continue;
}

if (lastPos < i)
out += str.slice(lastPos, i);

// Multi-byte characters ...
if (c < 0x800) {
lastPos = i + 1;
out += hexTable[0xC0 | (c >> 6)] +
hexTable[0x80 | (c & 0x3F)];
continue;
}
if (c < 0xD800 || c >= 0xE000) {
lastPos = i + 1;
out += hexTable[0xE0 | (c >> 12)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
continue;
}
// Surrogate pair
++i;

// This branch should never happen because all URLSearchParams entries
// should already be converted to USVString. But, included for
// completion's sake anyway.
if (i >= len)
throw new ERR_INVALID_URI();

var c2 = str.charCodeAt(i) & 0x3FF;

lastPos = i + 1;
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
out += hexTable[0xF0 | (c >> 18)] +
hexTable[0x80 | ((c >> 12) & 0x3F)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
}
if (lastPos === 0)
return str;
if (lastPos < len)
return out + str.slice(lastPos);
return out;
}

module.exports = {
encodeStr,
hexTable,
isHexTable
};
65 changes: 1 addition & 64 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const util = require('util');
const {
encodeStr,
hexTable,
isHexTable
} = require('internal/querystring');
Expand Down Expand Up @@ -826,70 +827,6 @@ const noEscape = [
const paramHexTable = hexTable.slice();
paramHexTable[0x20] = '+';

function encodeStr(str, noEscapeTable, hexTable) {
const len = str.length;
if (len === 0)
return '';

var out = '';
var lastPos = 0;

for (var i = 0; i < len; i++) {
var c = str.charCodeAt(i);

// ASCII
if (c < 0x80) {
if (noEscapeTable[c] === 1)
continue;
if (lastPos < i)
out += str.slice(lastPos, i);
lastPos = i + 1;
out += hexTable[c];
continue;
}

if (lastPos < i)
out += str.slice(lastPos, i);

// Multi-byte characters ...
if (c < 0x800) {
lastPos = i + 1;
out += hexTable[0xC0 | (c >> 6)] +
hexTable[0x80 | (c & 0x3F)];
continue;
}
if (c < 0xD800 || c >= 0xE000) {
lastPos = i + 1;
out += hexTable[0xE0 | (c >> 12)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
continue;
}
// Surrogate pair
++i;
var c2;
if (i < len)
c2 = str.charCodeAt(i) & 0x3FF;
else {
// This branch should never happen because all URLSearchParams entries
// should already be converted to USVString. But, included for
// completion's sake anyway.
c2 = 0;
}
lastPos = i + 1;
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
out += hexTable[0xF0 | (c >> 18)] +
hexTable[0x80 | ((c >> 12) & 0x3F)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
}
if (lastPos === 0)
return str;
if (lastPos < len)
return out + str.slice(lastPos);
return out;
}

// application/x-www-form-urlencoded serializer
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-serializer
function serializeParams(array) {
Expand Down
55 changes: 2 additions & 53 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
'use strict';

const { Buffer } = require('buffer');
const { ERR_INVALID_URI } = require('internal/errors').codes;
const {
encodeStr,
hexTable,
isHexTable
} = require('internal/querystring');
Expand Down Expand Up @@ -140,59 +140,8 @@ function qsEscape(str) {
else
str += '';
}
var out = '';
var lastPos = 0;

for (var i = 0; i < str.length; ++i) {
var c = str.charCodeAt(i);

// ASCII
if (c < 0x80) {
if (noEscape[c] === 1)
continue;
if (lastPos < i)
out += str.slice(lastPos, i);
lastPos = i + 1;
out += hexTable[c];
continue;
}

if (lastPos < i)
out += str.slice(lastPos, i);

// Multi-byte characters ...
if (c < 0x800) {
lastPos = i + 1;
out += hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)];
continue;
}
if (c < 0xD800 || c >= 0xE000) {
lastPos = i + 1;
out += hexTable[0xE0 | (c >> 12)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
continue;
}
// Surrogate pair
++i;

if (i >= str.length)
throw new ERR_INVALID_URI();

var c2 = str.charCodeAt(i) & 0x3FF;

lastPos = i + 1;
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
out += hexTable[0xF0 | (c >> 18)] +
hexTable[0x80 | ((c >> 12) & 0x3F)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
}
if (lastPos === 0)
return str;
if (lastPos < str.length)
return out + str.slice(lastPos);
return out;
return encodeStr(str, noEscape, hexTable);
}

function stringifyPrimitive(v) {
Expand Down