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

url: allow use of URL with http.request and https.request #10638

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ function ClientRequest(options, cb) {
if (!options.hostname) {
throw new Error('Unable to determine the domain name');
}
} else if (options instanceof url.URL) {
Copy link
Member

Choose a reason for hiding this comment

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

If I understand correctly this makes using the new URL api and using additional options(like agent) mutual exclusive?

Copy link
Member

Choose a reason for hiding this comment

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

True, but that's already the case if you pass the URL as a string or the result of url.parse. We can think later about a way to pass a URL object along with additional options but I don't think it should block this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I should have make this one a comment, not a change request :/ Sorry.

Copy link
Member

Choose a reason for hiding this comment

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

No worries!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, making those mutually exclusive is intentional. Attaching additional non-standard properties to the URL object is not something that we should promote. And as @targos points out, that is already the case when passing the URL as a string.

options = {
protocol: options.protocol,
host: options.host,
Copy link
Member

@joyeecheung joyeecheung Jan 6, 2017

Choose a reason for hiding this comment

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

Maybe a auth: `${options.username}:${options.password}` here(and in http.js too)?

EDIT: don't know why this comment appeared in another line, deleted that.

hostname: options.hostname,
port: options.port,
hash: options.hash,
search: options.search,
pathname: options.pathname,
path: `${options.pathname}${options.search}`,
href: options.href
};
Copy link
Member

Choose a reason for hiding this comment

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

I’m not sure how I feel about the code duplication here… could we make this a generic helper, possibly even hanging off the public url module (if that makes transitioning url.parse → url.URL easier)?

I would suggest adding it as the toJSON method for URL but I have looked at whatwg/url#137 and I’m not sure what to make of that other than that my idea would probably be silly/unrealistic/… :P

Copy link
Member

Choose a reason for hiding this comment

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

Also – could this blocked be just executed independently of options instanceof url.URL? It would mean using path: options.path || …, but other than that, it should just work, no?

(If you feel unsure about these ideas, I can PR them myself later. If you feel they are horrible, say so and I won’t. ;))

Copy link
Member Author

Choose a reason for hiding this comment

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

I can eliminate the duplication but having the instanceof and copying into a separate options is the most reliable and requires the fewest number of changes throughout the code.

Copy link
Member

@joyeecheung joyeecheung Jan 6, 2017

Choose a reason for hiding this comment

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

Those properties are enumerable (required by the spec) so a for-in loop with some ifs would do I think?

EDIT: util._extend doesn't apply because it only copies properties that are both enumerable and is own property. A helper function can loose the "isOwnProperty" bit but maybe not worth an additional abstraction anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd rather avoid doing the for-in loop in favor of keeping things more explicit and obvious. Also, as you point out, the auth vs username+password would demonstrates that the properties do not match up one-to-one.

} else {
options = util._extend({}, options);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ 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 = {
protocol: options.protocol,
host: options.host,
hostname: options.hostname,
port: options.port,
hash: options.hash,
search: options.search,
pathname: options.pathname,
path: `${options.pathname}${options.search}`,
href: options.href
};
} else {
options = util._extend({}, options);
}
Expand Down
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));
});