Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
http/https: pass request to .createConnection()
Browse files Browse the repository at this point in the history
It's useful for passing some additional options of request object to the
underlying API
  • Loading branch information
indutny committed Jul 19, 2012
1 parent b0950cb commit 2b3ba3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
19 changes: 13 additions & 6 deletions lib/http.js
Expand Up @@ -1067,7 +1067,7 @@ Agent.prototype.addRequest = function(req, host, port, localAddress) {
}
if (this.sockets[name].length < this.maxSockets) {
// If we are under maxSockets create a new one.
req.onSocket(this.createSocket(name, host, port, localAddress));
req.onSocket(this.createSocket(name, host, port, localAddress, req));
} else {
// We are over limit so we'll add it to the queue.
if (!this.requests[name]) {
Expand All @@ -1076,13 +1076,13 @@ Agent.prototype.addRequest = function(req, host, port, localAddress) {
this.requests[name].push(req);
}
};
Agent.prototype.createSocket = function(name, host, port, localAddress) {
Agent.prototype.createSocket = function(name, host, port, localAddress, req) {
var self = this;
var options = util._extend({}, self.options);
options.port = port;
options.host = host;
options.localAddress = localAddress;
var s = self.createConnection(options);
var s = self.createConnection.call(req, options);
if (!self.sockets[name]) {
self.sockets[name] = [];
}
Expand Down Expand Up @@ -1123,7 +1123,13 @@ Agent.prototype.removeSocket = function(s, name, host, port, localAddress) {
}
if (this.requests[name] && this.requests[name].length) {
// If we have pending requests and a socket gets closed a new one
this.createSocket(name, host, port, localAddress).emit('free');
this.createSocket(
name,
host,
port,
localAddress,
this.requests[name][0]
).emit('free');
}
};

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

this.options = options;
self.agent = options.agent === undefined ? globalAgent : options.agent;

var defaultPort = options.defaultPort || 80;
Expand Down Expand Up @@ -1194,7 +1201,7 @@ function ClientRequest(options, cb) {
self._last = true;
self.shouldKeepAlive = false;
if (options.createConnection) {
self.onSocket(options.createConnection(self.socketPath));
self.onSocket(options.createConnection.call(self, self.socketPath));
} else {
self.onSocket(net.createConnection(self.socketPath));
}
Expand All @@ -1210,7 +1217,7 @@ function ClientRequest(options, cb) {
if (options.createConnection) {
options.port = port;
options.host = host;
var conn = options.createConnection(options);
var conn = options.createConnection.call(self, options);
} else {
var conn = net.createConnection({
port: port,
Expand Down
11 changes: 6 additions & 5 deletions lib/https.js
Expand Up @@ -21,7 +21,8 @@

var tls = require('tls');
var http = require('http');
var inherits = require('util').inherits;
var util = require('util');
var inherits = util.inherits;

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

function createConnection(/* [port, host, options] */) {
var options = {};
var options = util._extend({}, this.options);

if (typeof arguments[0] === 'object') {
options = arguments[0];
options = util._extend(options, arguments[0]);
} else if (typeof arguments[1] === 'object') {
options = arguments[1];
options = util._extend(options, arguments[1]);
options.port = arguments[0];
} else if (typeof arguments[2] === 'object') {
options = arguments[2];
options = util._extend(options, arguments[2]);
options.port = arguments[0];
options.host = arguments[1];
} else {
Expand Down

0 comments on commit 2b3ba3f

Please sign in to comment.