url: allow use of URL with http.request and https.request by jasnell · Pull Request #10638 · nodejs/node · GitHub
Skip to content
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
4 changes: 3 additions & 1 deletion lib/_http_client.js
3 changes: 3 additions & 0 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const http = require('http');
const util = require('util');
const inherits = util.inherits;
const debug = util.debuglog('https');
const urlToOptions = require('internal/url').urlToOptions;

function Server(opts, requestListener) {
if (!(this instanceof Server)) return new Server(opts, requestListener);
Expand Down Expand Up @@ -192,6 +193,8 @@ exports.request = function request(options, cb) {
if (!options.hostname) {
throw new Error('Unable to determine the domain name');
}
} else if (options instanceof url.URL) {
options = urlToOptions(options);
} else {
options = util._extend({}, options);
}
Expand Down
24 changes: 24 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,32 @@ function domainToUnicode(domain) {
return binding.domainToUnicode(String(domain));
}

// Utility function that converts a URL object into an ordinary
// options object as expected by the http.request and https.request
// APIs.
function urlToOptions(url) {
var options = {
protocol: url.protocol,
host: url.host,
hostname: url.hostname,
hash: url.hash,
search: url.search,
pathname: url.pathname,
path: `${url.pathname}${url.search}`,
href: url.href
};
if (url.port !== '') {
options.port = Number(url.port);
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't support new URL("http://0:0@hostname/")

}
return options;
}

exports.URL = URL;
exports.originFor = originFor;
exports.domainToASCII = domainToASCII;
exports.domainToUnicode = domainToUnicode;
exports.encodeAuth = encodeAuth;
exports.urlToOptions = urlToOptions;
9 changes: 7 additions & 2 deletions test/parallel/test-http-client-get-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const common = require('../common');
const assert = require('assert');
const http = require('http');
const url = require('url');
const URL = url.URL;

var server = http.createServer(common.mustCall(function(req, res) {
assert.equal('GET', req.method);
Expand All @@ -10,8 +12,11 @@ var server = http.createServer(common.mustCall(function(req, res) {
res.write('hello\n');
res.end();
server.close();
}));
}, 3));

server.listen(0, function() {
http.get(`http://127.0.0.1:${this.address().port}/foo?bar`);
const u = `http://127.0.0.1:${this.address().port}/foo?bar`;
http.get(u);
http.get(url.parse(u));
http.get(new URL(u));
});
9 changes: 7 additions & 2 deletions test/parallel/test-https-client-get-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ if (!common.hasCrypto) {
const https = require('https');

const fs = require('fs');
const url = require('url');
const URL = url.URL;

var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
Expand All @@ -25,8 +27,11 @@ var server = https.createServer(options, common.mustCall(function(req, res) {
res.write('hello\n');
res.end();
server.close();
}));
}, 3));

server.listen(0, function() {
https.get(`https://127.0.0.1:${this.address().port}/foo?bar`);
const u = `https://127.0.0.1:${this.address().port}/foo?bar`;
https.get(u);
https.get(url.parse(u));
https.get(new URL(u));
});
17 changes: 17 additions & 0 deletions test/parallel/test-whatwg-url-properties.js