Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d5c6479

Browse filesBrowse files
committed
[fix] Handle the case where the port is specified but empty
1 parent 4f2ae67 commit d5c6479
Copy full SHA for d5c6479

File tree

2 files changed

+32
-3
lines changed
Filter options

2 files changed

+32
-3
lines changed

‎index.js

Copy file name to clipboardExpand all lines: index.js
+10-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var rules = [
3939
['/', 'pathname'], // Extract from the back.
4040
['@', 'auth', 1], // Extract from the front.
4141
[NaN, 'host', undefined, 1, 1], // Set left over value.
42-
[/:(\d+)$/, 'port', undefined, 1], // RegExp the back.
42+
[/:(\d*)$/, 'port', undefined, 1], // RegExp the back.
4343
[NaN, 'hostname', undefined, 1, 1] // Set left over.
4444
];
4545

@@ -524,6 +524,7 @@ function toString(stringify) {
524524

525525
var query
526526
, url = this
527+
, host = url.host
527528
, protocol = url.protocol;
528529

529530
if (protocol && protocol.charAt(protocol.length - 1) !== ':') protocol += ':';
@@ -542,7 +543,7 @@ function toString(stringify) {
542543
} else if (
543544
url.protocol !== 'file:' &&
544545
isSpecial(url.protocol) &&
545-
!url.host &&
546+
!host &&
546547
url.pathname !== '/'
547548
) {
548549
//
@@ -552,7 +553,13 @@ function toString(stringify) {
552553
result += '@';
553554
}
554555

555-
result += url.host + url.pathname;
556+
//
557+
// Trailing colon is removed from `url.host` when it is parsed. If it still
558+
// ends with a colon, then add back the trailing colon that was removed. This
559+
// prevents an invalid URL from being transformed into a valid one.
560+
//
561+
if (host[host.length - 1] === ':') host += ':';
562+
result += host + url.pathname;
556563

557564
query = 'object' === typeof url.query ? stringify(url.query) : url.query;
558565
if (query) result += '?' !== query.charAt(0) ? '?'+ query : query;

‎test/test.js

Copy file name to clipboardExpand all lines: test/test.js
+22
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,28 @@ describe('url-parse', function () {
442442
assume(parsed.href).equals('sip:alice@atlanta.com');
443443
});
444444

445+
it('handles the case where the port is specified but empty', function () {
446+
var parsed = parse('http://example.com:');
447+
448+
assume(parsed.protocol).equals('http:');
449+
assume(parsed.port).equals('');
450+
assume(parsed.host).equals('example.com');
451+
assume(parsed.hostname).equals('example.com');
452+
assume(parsed.pathname).equals('/');
453+
assume(parsed.origin).equals('http://example.com');
454+
assume(parsed.href).equals('http://example.com/');
455+
456+
parsed = parse('http://example.com::');
457+
458+
assume(parsed.protocol).equals('http:');
459+
assume(parsed.port).equals('');
460+
assume(parsed.host).equals('example.com:');
461+
assume(parsed.hostname).equals('example.com:');
462+
assume(parsed.pathname).equals('/');
463+
assume(parsed.origin).equals('http://example.com:');
464+
assume(parsed.href).equals('http://example.com::/');
465+
});
466+
445467
describe('origin', function () {
446468
it('generates an origin property', function () {
447469
var url = 'http://google.com:80/pathname'

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.