Skip to content

Commit 6f34fb4

Browse files
author
Terry Chen
committed
net: preserve port 80 in SocketAddress.parse
Fixes: #62906 Signed-off-by: Terry Chen <[email protected]>
1 parent 80e0f14 commit 6f34fb4

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

lib/internal/socketaddress.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ const { URLParse } = require('internal/url');
4242
const kHandle = Symbol('kHandle');
4343
const kDetail = Symbol('kDetail');
4444

45+
function getSocketAddressPort(input, port) {
46+
if (port !== '') {
47+
return port;
48+
}
49+
// The HTTP parser drops an explicit ":80" port. SocketAddress input is not
50+
// an HTTP URL, so parse with a scheme that has no default port.
51+
return URLParse(`socket://${input}`).port;
52+
}
53+
4554
class SocketAddress {
4655
static isSocketAddress(value) {
4756
return value?.[kHandle] !== undefined;
@@ -157,14 +166,15 @@ class SocketAddress {
157166
hostname: address,
158167
port,
159168
} = URLParse(`http://${input}`);
169+
const addressPort = getSocketAddressPort(input, port) | 0;
160170
if (address.startsWith('[') && address.endsWith(']')) {
161171
return new SocketAddress({
162172
address: address.slice(1, -1),
163-
port: port | 0,
173+
port: addressPort,
164174
family: 'ipv6',
165175
});
166176
}
167-
return new SocketAddress({ address, port: port | 0 });
177+
return new SocketAddress({ address, port: addressPort });
168178
} catch {
169179
// Ignore errors here. Return undefined if the input cannot
170180
// be successfully parsed or is not a proper socket address.

test/parallel/test-socketaddress.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ describe('net.SocketAddress...', () => {
141141
it('SocketAddress.parse() works as expected', () => {
142142
const good = [
143143
{ input: '1.2.3.4', address: '1.2.3.4', port: 0, family: 'ipv4' },
144+
{ input: '1.2.3.4:80', address: '1.2.3.4', port: 80, family: 'ipv4' },
145+
{ input: '1.2.3.4:080', address: '1.2.3.4', port: 80, family: 'ipv4' },
144146
{ input: '192.168.257:1', address: '192.168.1.1', port: 1, family: 'ipv4' },
145147
{ input: '256', address: '0.0.1.0', port: 0, family: 'ipv4' },
148+
{ input: '999999999:80', address: '59.154.201.255', port: 80, family: 'ipv4' },
146149
{ input: '999999999:12', address: '59.154.201.255', port: 12, family: 'ipv4' },
147150
{ input: '0xffffffff', address: '255.255.255.255', port: 0, family: 'ipv4' },
148151
{ input: '0x.0x.0', address: '0.0.0.0', port: 0, family: 'ipv4' },
149152
{ input: '[1:0::]', address: '1::', port: 0, family: 'ipv6' },
153+
{ input: '[1::8]:80', address: '1::8', port: 80, family: 'ipv6' },
150154
{ input: '[1::8]:123', address: '1::8', port: 123, family: 'ipv6' },
151155
];
152156

0 commit comments

Comments
 (0)