Skip to content

Commit 53716eb

Browse files
committed
http/https: pass request to .createConnection()
It's useful for passing some additional options of request object to the underlying API
1 parent 1fa0bca commit 53716eb

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/http.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ Agent.prototype.addRequest = function(req, host, port, localAddress) {
10671067
}
10681068
if (this.sockets[name].length < this.maxSockets) {
10691069
// If we are under maxSockets create a new one.
1070-
req.onSocket(this.createSocket(name, host, port, localAddress));
1070+
req.onSocket(this.createSocket(name, host, port, localAddress, req));
10711071
} else {
10721072
// We are over limit so we'll add it to the queue.
10731073
if (!this.requests[name]) {
@@ -1076,13 +1076,13 @@ Agent.prototype.addRequest = function(req, host, port, localAddress) {
10761076
this.requests[name].push(req);
10771077
}
10781078
};
1079-
Agent.prototype.createSocket = function(name, host, port, localAddress) {
1079+
Agent.prototype.createSocket = function(name, host, port, localAddress, req) {
10801080
var self = this;
10811081
var options = util._extend({}, self.options);
10821082
options.port = port;
10831083
options.host = host;
10841084
options.localAddress = localAddress;
1085-
var s = self.createConnection(options);
1085+
var s = self.createConnection.call(req, options);
10861086
if (!self.sockets[name]) {
10871087
self.sockets[name] = [];
10881088
}
@@ -1123,7 +1123,13 @@ Agent.prototype.removeSocket = function(s, name, host, port, localAddress) {
11231123
}
11241124
if (this.requests[name] && this.requests[name].length) {
11251125
// If we have pending requests and a socket gets closed a new one
1126-
this.createSocket(name, host, port, localAddress).emit('free');
1126+
this.createSocket(
1127+
name,
1128+
host,
1129+
port,
1130+
localAddress,
1131+
this.requests[name][0]
1132+
).emit('free');
11271133
}
11281134
};
11291135

@@ -1135,6 +1141,7 @@ function ClientRequest(options, cb) {
11351141
var self = this;
11361142
OutgoingMessage.call(self);
11371143

1144+
this.options = options;
11381145
self.agent = options.agent === undefined ? globalAgent : options.agent;
11391146

11401147
var defaultPort = options.defaultPort || 80;
@@ -1194,7 +1201,7 @@ function ClientRequest(options, cb) {
11941201
self._last = true;
11951202
self.shouldKeepAlive = false;
11961203
if (options.createConnection) {
1197-
self.onSocket(options.createConnection(self.socketPath));
1204+
self.onSocket(options.createConnection.call(self, self.socketPath));
11981205
} else {
11991206
self.onSocket(net.createConnection(self.socketPath));
12001207
}
@@ -1210,7 +1217,7 @@ function ClientRequest(options, cb) {
12101217
if (options.createConnection) {
12111218
options.port = port;
12121219
options.host = host;
1213-
var conn = options.createConnection(options);
1220+
var conn = options.createConnection.call(self, options);
12141221
} else {
12151222
var conn = net.createConnection({
12161223
port: port,

lib/https.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
var tls = require('tls');
2323
var http = require('http');
24-
var inherits = require('util').inherits;
24+
var util = require('util');
25+
var inherits = util.inherits;
2526

2627
function Server(opts, requestListener) {
2728
if (!(this instanceof Server)) return new Server(opts, requestListener);
@@ -52,15 +53,15 @@ exports.createServer = function(opts, requestListener) {
5253
// HTTPS agents.
5354

5455
function createConnection(/* [port, host, options] */) {
55-
var options = {};
56+
var options = util._extend({}, this.options);
5657

5758
if (typeof arguments[0] === 'object') {
58-
options = arguments[0];
59+
options = util._extend(options, arguments[0]);
5960
} else if (typeof arguments[1] === 'object') {
60-
options = arguments[1];
61+
options = util._extend(options, arguments[1]);
6162
options.port = arguments[0];
6263
} else if (typeof arguments[2] === 'object') {
63-
options = arguments[2];
64+
options = util._extend(options, arguments[2]);
6465
options.port = arguments[0];
6566
options.host = arguments[1];
6667
} else {

0 commit comments

Comments
 (0)