Skip to content

Commit

Permalink
https: refactor to use http internals
Browse files Browse the repository at this point in the history
Rather than using `http`, use `_http_client`, etc. directly.

Also moving all the exports to the bottom, in line with most of the rest
of the codebase.

PR-URL: #16395
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bengl authored and gibfahn committed Oct 31, 2017
1 parent a763fcd commit 754df71
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ require('internal/util').assertCrypto();

const tls = require('tls');
const url = require('url');
const http = require('http');
const util = require('util');
const inherits = util.inherits;
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 @@ -51,7 +56,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 @@ -68,13 +73,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 @@ -129,7 +133,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 @@ -141,11 +145,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 @@ -219,10 +223,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 @@ -236,11 +237,20 @@ exports.request = function request(options, cb) {
options = util._extend({}, options);
}
options._defaultAgent = globalAgent;
return http.request(options, cb);
};
return new ClientRequest(options, cb);
}

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
};

0 comments on commit 754df71

Please sign in to comment.