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

Make http.request correctly parse {host: "hostname:port"} #2271

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,19 @@ function ClientRequest(options, cb) {
const defaultPort = options.defaultPort ||
self.agent && self.agent.defaultPort;

var port = options.port = options.port || defaultPort || 80;
var host = options.host = options.hostname || options.host || 'localhost';
const parsedHost = {host: options.host};
if (options.host) {
url.Url.prototype.parseHost.call(parsedHost);

// unwrap brackets from ipv6 ip addresses
const hostname = parsedHost.hostname;
if (hostname[0] === '[' && hostname[hostname.length - 1] === ']') {
parsedHost.hostname = hostname.substr(1, hostname.length - 2);
}
}

const port = options.port = options.port || parsedHost.port || defaultPort || 80;
const host = options.host = options.hostname || parsedHost.hostname || 'localhost';

if (options.setHost === undefined) {
var setHost = true;
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-http-request-host-port-ipv6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

if (!common.hasIPv6) {
console.log('1..0 # Skipped: no IPv6 support');
return;
}

var connected = false;

const server = http.createServer(function(req, res) {
connected = true;
res.writeHead(204);
res.end();
});

server.listen(common.PORT, '::1', function() {
http.get({
host: '[::1]:' + common.PORT
}, function(res) {
res.resume();
server.close();
}).on('error', function(e) {
throw e;
});
});

process.on('exit', function() {
assert(connected, 'http.request should correctly parse ' +
'{host: "hostname:port"} and connect to the specified host');
});
29 changes: 29 additions & 0 deletions test/parallel/test-http-request-host-port.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

var connected = false;

const server = http.createServer(function(req, res) {
connected = true;
res.writeHead(204);
res.end();
});

server.listen(common.PORT, function() {
http.get({
host: 'localhost:' + common.PORT
}, function(res) {
res.resume();
server.close();
}).on('error', function(e) {
throw e;
});
});

process.on('exit', function() {
assert(connected, 'http.request should correctly parse ' +
'{host: "hostname:port"} and connect to the specified host');
});