Skip to content

Commit f1622f8

Browse files
committed
Make http.request correctly parse {host: "hostname:port"}
The issue: http.request() treats host as an alternate form of hostname, which it sort of is, except that host is (normally) allowed to contain a port number. Before this change, if host contains a port number and there isn't a hostname field to override it, http.request() attempts a DNS lookup on the entire host field, including the port number. This always fails. The fix: If a host field is present, use the url lib to parse it and use the parsed hostname nad port values as fallbacks if options.hostname and options.port are unset. Includes tests for localhost:12346 and [::1]:12346.
1 parent bc733f7 commit f1622f8

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

lib/_http_client.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,19 @@ function ClientRequest(options, cb) {
5757
const defaultPort = options.defaultPort ||
5858
self.agent && self.agent.defaultPort;
5959

60-
var port = options.port = options.port || defaultPort || 80;
61-
var host = options.host = options.hostname || options.host || 'localhost';
60+
const parsedHost = {host: options.host};
61+
if (options.host) {
62+
url.Url.prototype.parseHost.call(parsedHost);
63+
64+
// unwrap brackets from ipv6 ip addresses
65+
const hostname = parsedHost.hostname;
66+
if (hostname[0] === '[' && hostname[hostname.length - 1] === ']') {
67+
parsedHost.hostname = hostname.substr(1, hostname.length - 2);
68+
}
69+
}
70+
71+
const port = options.port = options.port || parsedHost.port || defaultPort || 80;
72+
const host = options.host = options.hostname || parsedHost.hostname || 'localhost';
6273

6374
if (options.setHost === undefined) {
6475
var setHost = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
if (!common.hasIPv6) {
8+
console.log('1..0 # Skipped: no IPv6 support');
9+
return;
10+
}
11+
12+
var connected = false;
13+
14+
const server = http.createServer(function(req, res) {
15+
connected = true;
16+
res.writeHead(204);
17+
res.end();
18+
});
19+
20+
server.listen(common.PORT, '::1', function() {
21+
http.get({
22+
host: '[::1]:' + common.PORT
23+
}, function(res) {
24+
res.resume();
25+
server.close();
26+
}).on('error', function(e) {
27+
throw e;
28+
});
29+
});
30+
31+
process.on('exit', function() {
32+
assert(connected, 'http.request should correctly parse ' +
33+
'{host: "hostname:port"} and connect to the specified host');
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
var connected = false;
8+
9+
const server = http.createServer(function(req, res) {
10+
connected = true;
11+
res.writeHead(204);
12+
res.end();
13+
});
14+
15+
server.listen(common.PORT, function() {
16+
http.get({
17+
host: 'localhost:' + common.PORT
18+
}, function(res) {
19+
res.resume();
20+
server.close();
21+
}).on('error', function(e) {
22+
throw e;
23+
});
24+
});
25+
26+
process.on('exit', function() {
27+
assert(connected, 'http.request should correctly parse ' +
28+
'{host: "hostname:port"} and connect to the specified host');
29+
});

0 commit comments

Comments
 (0)