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

https: allow url and options to be passed to https.request #22003

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
29 changes: 18 additions & 11 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,11 @@ Agent.prototype._evictSession = function _evictSession(key) {
const globalAgent = new Agent();

let urlWarningEmitted = false;
function request(options, cb) {
if (typeof options === 'string') {
const urlStr = options;
function request(...args) {
let options = {};

if (typeof args[0] === 'string') {
const urlStr = args.shift();
try {
options = urlToOptions(new URL(urlStr));
} catch (err) {
Expand All @@ -273,19 +275,24 @@ function request(options, cb) {
'DeprecationWarning', 'DEP0109');
}
}
} else if (options && options[searchParamsSymbol] &&
options[searchParamsSymbol][searchParamsSymbol]) {
} else if (args[0] && args[0][searchParamsSymbol] &&
args[0][searchParamsSymbol][searchParamsSymbol]) {
// url.URL instance
options = urlToOptions(options);
} else {
options = util._extend({}, options);
options = urlToOptions(args.shift());
}

if (args[0] && typeof args[0] !== 'function') {
options = util._extend(options, args.shift());
}

options._defaultAgent = globalAgent;
return new ClientRequest(options, cb);
args.unshift(options);

return new ClientRequest(...args);
Copy link
Contributor

@mscdex mscdex Jul 29, 2018

Choose a reason for hiding this comment

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

Last I checked, this was still significantly slower in V8 I think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in Node 8.3? See https://github.com/davidmarkclements/v8-perf

Here’s the takeaway: if we want to write performant code around processing function inputs as an array (which in my experience seems fairly common), then in Node 8.3 and up we should use the spread operator.

Responding to your original suggestions:

Making HttpsClientRequest have no additional methods and checking this.constructor === HttpsClientRequest

The issue is that without additional methods, it never would be called... unless we duplicated the entire constructor.

Allow passing a 4th option to ClientRequest that if not undefined, must be a private symbol indicating an https client request

This will impact the ability to add future arguments. And we are currently dealing with three arguments, all optional...

Make the few changes needed to the original code in https to support both url and options

I went this way for now.

The other option:

Maybe we should move them under a private Symbol?

This is clean and would work. The symbols would need to be exported by lib/_http_client.js

Copy link
Member

Choose a reason for hiding this comment

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

This is clean and would work. The symbols would need to be exported by lib/_http_client.js

I would prefer it. Unfortunately there is a lot of monkey patching around core methods, and I would prefer this would be as private as possible. I would not block this for it.

If you feel strongly about going _, then ok.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mcollina I'm not following.... in particular, I'm not sure what 'it' refers to.

Should I go back to the previous approach of subclassing ClientRequest for https, and introducing two new methods, this time with the method names being Symbols? My thoughts are that that approach is more understandable and maintainable.

Or would you prefer the current approach which duplicates a bit of code, but doesn't introduce any new methods. My thoughts are that approach is more private.

Copy link
Member

Choose a reason for hiding this comment

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

I was actually commenting on some old code. The current approach is fine. LGTM

Copy link
Contributor

@mscdex mscdex Jul 29, 2018

Choose a reason for hiding this comment

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

Making HttpsClientRequest have no additional methods and checking this.constructor === HttpsClientRequest

The issue is that without additional methods, it never would be called... unless we duplicated the entire constructor.

@rubys I'm not sure what you mean, HttpsClientRequest would extend from ClientRequest and be empty itself (no explicit constructor or additional methods). Then inside ClientRequest, checking this.constructor like:

var defaultAgent = options._defaultAgent ||
                   (this.constructor === https[ClientRequestSym]
                    ? https.globalAgent : Agent.globalAgent);

Copy link
Member Author

Choose a reason for hiding this comment

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

@McDex Fair point, but making that work would involve a circular require.

Copy link
Contributor

Choose a reason for hiding this comment

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

@rubys Not if it's lazy loaded.

}

function get(options, cb) {
const req = request(options, cb);
function get(input, options, cb) {
const req = request(input, options, cb);
req.end();
return req;
}
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-https-request-arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const https = require('https');
const fixtures = require('../common/fixtures');

if (!common.hasCrypto)
common.skip('missing crypto');

const options = {
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
ca: fixtures.readKey('ca1-cert.pem')
};

// Test providing both a url and options, with the options partially
// replacing address and port portions of the URL provided.
{
const server = https.createServer(
options,
common.mustCall((req, res) => {
assert.strictEqual(req.url, '/testpath');
res.end();
server.close();
})
);

server.listen(
0,
common.mustCall(() => {
https.get(
'https://example.com/testpath',

{
hostname: 'localhost',
port: server.address().port,
rejectUnauthorized: false
},

common.mustCall((res) => {
res.resume();
})
);
})
);
}