Skip to content

Commit ce7a01f

Browse files
committed
[fix] Improve handling of empty port
Make `Url.prototype.toString()` add a trailing colon to the host if the hostname ends with a port number and the parsed port is empty. Refs: d5c64791ef49
1 parent 0071490 commit ce7a01f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var required = require('requires-port')
55
, controlOrWhitespace = /^[\x00-\x20\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/
66
, CRHTLF = /[\n\r\t]/g
77
, slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//
8+
, port = /:\d+$/
89
, protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\\/]+)?([\S\s]*)/i
910
, windowsDriveLetter = /^[a-zA-Z]:/;
1011

@@ -452,7 +453,7 @@ function set(part, value, fn) {
452453
case 'host':
453454
url[part] = value;
454455

455-
if (/:\d+$/.test(value)) {
456+
if (port.test(value)) {
456457
value = value.split(':');
457458
url.port = value.pop();
458459
url.hostname = value.join(':');
@@ -560,7 +561,10 @@ function toString(stringify) {
560561
// ends with a colon, then add back the trailing colon that was removed. This
561562
// prevents an invalid URL from being transformed into a valid one.
562563
//
563-
if (host[host.length - 1] === ':') host += ':';
564+
if (host[host.length - 1] === ':' || (port.test(url.hostname) && !url.port)) {
565+
host += ':';
566+
}
567+
564568
result += host + url.pathname;
565569

566570
query = 'object' === typeof url.query ? stringify(url.query) : url.query;

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,26 @@ describe('url-parse', function () {
468468
assume(parsed.pathname).equals('/');
469469
assume(parsed.origin).equals('http://example.com:');
470470
assume(parsed.href).equals('http://example.com::/');
471+
472+
parsed = parse('http://example.com:8080:');
473+
474+
assume(parsed.protocol).equals('http:');
475+
assume(parsed.port).equals('');
476+
assume(parsed.host).equals('example.com:8080');
477+
assume(parsed.hostname).equals('example.com:8080');
478+
assume(parsed.pathname).equals('/');
479+
assume(parsed.origin).equals('http://example.com:8080');
480+
assume(parsed.href).equals('http://example.com:8080:/');
481+
482+
parsed = parse('http://example.com:8000:8080');
483+
484+
assume(parsed.protocol).equals('http:');
485+
assume(parsed.port).equals('8080');
486+
assume(parsed.host).equals('example.com:8000:8080');
487+
assume(parsed.hostname).equals('example.com:8000');
488+
assume(parsed.pathname).equals('/');
489+
assume(parsed.origin).equals('http://example.com:8000:8080');
490+
assume(parsed.href).equals('http://example.com:8000:8080/');
471491
});
472492

473493
describe('origin', function () {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy