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: refactor to use http internals #16395

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 27 additions & 17 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ require('internal/util').assertCrypto();

const tls = require('tls');
const url = require('url');
const http = require('http');
const util = require('util');
const { Agent: HttpAgent } = require('_http_agent');
const {
Server: HttpServer,
_connectionListener
} = require('_http_server');
const { ClientRequest } = require('_http_client');
const { inherits } = util;
const debug = util.debuglog('https');
const { urlToOptions, searchParamsSymbol } = require('internal/url');
Expand All @@ -52,7 +57,7 @@ function Server(opts, requestListener) {
opts.ALPNProtocols = ['http/1.1'];
}

tls.Server.call(this, opts, http._connectionListener);
tls.Server.call(this, opts, _connectionListener);

this.httpAllowHalfOpen = false;

Expand All @@ -69,13 +74,12 @@ function Server(opts, requestListener) {
this.keepAliveTimeout = 5000;
}
inherits(Server, tls.Server);
exports.Server = Server;

Server.prototype.setTimeout = http.Server.prototype.setTimeout;
Server.prototype.setTimeout = HttpServer.prototype.setTimeout;

exports.createServer = function createServer(opts, requestListener) {
function createServer(opts, requestListener) {
return new Server(opts, requestListener);
};
}


// HTTPS agents.
Expand Down Expand Up @@ -130,7 +134,7 @@ function Agent(options) {
if (!(this instanceof Agent))
return new Agent(options);

http.Agent.call(this, options);
HttpAgent.call(this, options);
this.defaultPort = 443;
this.protocol = 'https:';
this.maxCachedSessions = this.options.maxCachedSessions;
Expand All @@ -142,11 +146,11 @@ function Agent(options) {
list: []
};
}
inherits(Agent, http.Agent);
inherits(Agent, HttpAgent);
Agent.prototype.createConnection = createConnection;

Agent.prototype.getName = function getName(options) {
var name = http.Agent.prototype.getName.call(this, options);
var name = HttpAgent.prototype.getName.call(this, options);

name += ':';
if (options.ca)
Expand Down Expand Up @@ -220,10 +224,7 @@ Agent.prototype._evictSession = function _evictSession(key) {

const globalAgent = new Agent();

exports.globalAgent = globalAgent;
exports.Agent = Agent;

exports.request = function request(options, cb) {
function request(options, cb) {
if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
Expand All @@ -237,11 +238,20 @@ exports.request = function request(options, cb) {
options = util._extend({}, options);
}
options._defaultAgent = globalAgent;
return http.request(options, cb);
};
return new ClientRequest(options, cb);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this PR break existing code that relies on monkey patching?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, definitely a good concern. I'm not immediately aware of modules monkeypatching the client side code but it is definitely something we need to look for before landing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

If anyone is monkey patching http.request, then http.ClientRequest and _http_client.ClientRequest also need to be monkey patched since they're all exposed and equivalent. I've seen userland code using all three.

That https.request currently calls http.request is an implementation detail (i.e. semver-patch). Users should not be relying on that.

Choose a reason for hiding this comment

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

Just found this commit after noticing our tests for https break. Turns out our instrumentation relied on this implementation detail. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to be breaking GCP's Stackdriver Trace as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

revert for v8.x in #16660

}

exports.get = function get(options, cb) {
var req = exports.request(options, cb);
function get(options, cb) {
const req = request(options, cb);
req.end();
return req;
}

module.exports = {
Agent,
globalAgent,
Server,
createServer,
get,
request
};