diff --git a/lib/_http_client.js b/lib/_http_client.js index 201593e61da5ac..02d6e7ed374a98 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -22,6 +22,9 @@ function ClientRequest(options, cb) { if (typeof options === 'string') { options = url.parse(options); + if (!options.hostname) { + throw new Error('Unable to determine the domain name'); + } } else { options = util._extend({}, options); } diff --git a/lib/https.js b/lib/https.js index edf0aa4432f82d..90b6346bd95f20 100644 --- a/lib/https.js +++ b/lib/https.js @@ -163,6 +163,9 @@ exports.Agent = Agent; exports.request = function(options, cb) { if (typeof options === 'string') { options = url.parse(options); + if (!options.hostname) { + throw new Error('Unable to determine the domain name'); + } } else { options = util._extend({}, options); } diff --git a/test/parallel/test-http-invalid-urls.js b/test/parallel/test-http-invalid-urls.js new file mode 100644 index 00000000000000..678e8eceeb2d46 --- /dev/null +++ b/test/parallel/test-http-invalid-urls.js @@ -0,0 +1,19 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const http = require('http'); +const https = require('https'); +const error = 'Unable to determine the domain name'; + +function test(host) { + ['get', 'request'].forEach((method) => { + [http, https].forEach((module) => { + assert.throws(() => module[method](host, () => { + throw new Error(`${module}.${method} should not connect to ${host}`); + }), error); + }); + }); +} + +['www.nodejs.org', 'localhost', '127.0.0.1', 'http://:80/'].forEach(test);