Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: improve parsing speed and code style #419

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 15 additions & 6 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ const unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims);
// Allowed by RFCs, but cause of XSS attacks. Always escape these.
const autoEscape = ['\''].concat(unwise);


const slashPattern = /\\/g;

const userAtHostPattern = /^\/\/[^@\/]+@[^@\/]+/;

const colonPattern = /%3A/i;

const pathnameEscapePattern = /[?#]/g;

// Characters that are never ever allowed in a hostname.
// Note that any invalid chars are also handled, but these
// are the ones that are *expected* to be seen, so we fast-path them.
Expand Down Expand Up @@ -97,9 +106,9 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
var queryIndex = url.indexOf('?'),
splitter =
(queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#',
uSplit = url.split(splitter),
slashRegex = /\\/g;
uSplit[0] = uSplit[0].replace(slashRegex, '/');
uSplit = url.split(splitter);

uSplit[0] = uSplit[0].replace(slashPattern, '/');
url = uSplit.join(splitter);

var rest = url;
Expand Down Expand Up @@ -142,7 +151,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
// user@server is *always* interpreted as a hostname, and url
// resolution will treat //foo/bar as host=foo,path=bar because that's
// how the browser resolves relative URLs.
if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
if (slashesDenoteHost || proto || rest.match(userAtHostPattern)) {
var slashes = rest.substr(0, 2) === '//';
if (slashes && !(proto && hostlessProtocol[proto])) {
rest = rest.substr(2);
Expand Down Expand Up @@ -362,7 +371,7 @@ Url.prototype.format = function() {
var auth = this.auth || '';
if (auth) {
auth = encodeURIComponent(auth);
auth = auth.replace(/%3A/i, ':');
auth = auth.replace(colonPattern, ':');
auth += '@';
}

Expand Down Expand Up @@ -406,7 +415,7 @@ Url.prototype.format = function() {
if (hash && hash.charAt(0) !== '#') hash = '#' + hash;
if (search && search.charAt(0) !== '?') search = '?' + search;

pathname = pathname.replace(/[?#]/g, function(match) {
pathname = pathname.replace(pathnameEscapePattern, function(match) {
return encodeURIComponent(match);
});
search = search.replace('#', '%23');
Expand Down