Skip to content

Commit 49519f1

Browse files
committed
http: Reuse more http/https Agent code
1 parent 40e9265 commit 49519f1

9 files changed

+128
-86
lines changed

lib/_http_agent.js

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var url = require('url');
2424
var util = require('util');
2525
var EventEmitter = require('events').EventEmitter;
2626
var ClientRequest = require('_http_client').ClientRequest;
27+
var debug = util.debuglog('http');
2728

2829
// New Agent code.
2930

@@ -44,7 +45,12 @@ function Agent(options) {
4445
EventEmitter.call(this);
4546

4647
var self = this;
48+
49+
self.defaultPort = 80;
50+
self.protocol = 'http:';
51+
4752
self.options = util._extend({}, options);
53+
4854
// don't confuse net and make it think that we're connecting to a pipe
4955
self.options.path = null;
5056
self.requests = {};
@@ -54,11 +60,9 @@ function Agent(options) {
5460
self.keepAlive = self.options.keepAlive || false;
5561
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
5662

57-
self.on('free', function(socket, host, port, localAddress) {
58-
var name = host + ':' + port;
59-
if (localAddress) {
60-
name += ':' + localAddress;
61-
}
63+
self.on('free', function(socket, options) {
64+
var name = self.getName(options);
65+
debug('agent.on(free)', name);
6266

6367
if (!socket.destroyed &&
6468
self.requests[name] && self.requests[name].length) {
@@ -103,18 +107,38 @@ exports.Agent = Agent;
103107
Agent.defaultMaxSockets = Infinity;
104108

105109
Agent.prototype.createConnection = net.createConnection;
106-
Agent.prototype.defaultPort = 80;
107-
Agent.prototype.protocol = 'http:';
108-
Agent.prototype.addRequest = function(req, host, port, localAddress) {
109-
var name = host + ':' + port;
110-
if (localAddress) {
111-
name += ':' + localAddress;
112-
}
110+
111+
// Get the key for a given set of request options
112+
Agent.prototype.getName = function(options) {
113+
var name = '';
114+
115+
if (options.host)
116+
name += options.host;
117+
else
118+
name += 'localhost';
119+
120+
name += ':';
121+
if (options.port)
122+
name += options.port;
123+
name += ':';
124+
if (options.localAddress)
125+
name += options.localAddress;
126+
name += ':';
127+
return name;
128+
};
129+
130+
Agent.prototype.addRequest = function(req, options) {
131+
var host = options.host;
132+
var port = options.port;
133+
var localAddress = options.localAddress;
134+
135+
var name = this.getName(options);
113136
if (!this.sockets[name]) {
114137
this.sockets[name] = [];
115138
}
116139

117140
if (this.freeSockets[name] && this.freeSockets[name].length) {
141+
debug('have free socket');
118142
// we have a free socket, so use that.
119143
var socket = this.freeSockets[name].shift();
120144

@@ -125,9 +149,11 @@ Agent.prototype.addRequest = function(req, host, port, localAddress) {
125149
socket.ref();
126150
req.onSocket(socket);
127151
} else if (this.sockets[name].length < this.maxSockets) {
152+
debug('call onSocket');
128153
// If we are under maxSockets create a new one.
129-
req.onSocket(this.createSocket(name, host, port, localAddress, req));
154+
req.onSocket(this.createSocket(req, options));
130155
} else {
156+
debug('wait for socket');
131157
// We are over limit so we'll add it to the queue.
132158
if (!this.requests[name]) {
133159
this.requests[name] = [];
@@ -136,45 +162,49 @@ Agent.prototype.addRequest = function(req, host, port, localAddress) {
136162
}
137163
};
138164

139-
Agent.prototype.createSocket = function(name, host, port, localAddress, req) {
165+
Agent.prototype.createSocket = function(req, options) {
140166
var self = this;
141-
var options = util._extend({}, self.options);
142-
options.port = port;
143-
options.host = host;
144-
options.localAddress = localAddress;
167+
options = util._extend({}, options);
168+
options = util._extend(options, self.options);
145169

146-
options.servername = host;
170+
options.servername = options.host;
147171
if (req) {
148172
var hostHeader = req.getHeader('host');
149173
if (hostHeader) {
150174
options.servername = hostHeader.replace(/:.*$/, '');
151175
}
152176
}
153177

178+
var name = self.getName(options);
179+
180+
debug('createConnection', name, options);
154181
var s = self.createConnection(options);
155182
if (!self.sockets[name]) {
156183
self.sockets[name] = [];
157184
}
158185
this.sockets[name].push(s);
186+
debug('sockets', name, this.sockets[name].length);
159187

160188
function onFree() {
161-
self.emit('free', s, host, port, localAddress);
189+
self.emit('free', s, options);
162190
}
163191
s.on('free', onFree);
164192

165193
function onClose(err) {
194+
debug('CLIENT socket onClose');
166195
// This is the only place where sockets get removed from the Agent.
167196
// If you want to remove a socket from the pool, just close it.
168197
// All socket errors end in a close event anyway.
169-
self.removeSocket(s, name, host, port, localAddress);
198+
self.removeSocket(s, options);
170199
}
171200
s.on('close', onClose);
172201

173202
function onRemove() {
174203
// We need this function for cases like HTTP 'upgrade'
175-
// (defined by WebSockets) where we need to remove a socket from the pool
176-
// because it'll be locked up indefinitely
177-
self.removeSocket(s, name, host, port, localAddress);
204+
// (defined by WebSockets) where we need to remove a socket from the
205+
// pool because it'll be locked up indefinitely
206+
debug('CLIENT socket onRemove');
207+
self.removeSocket(s, options);
178208
s.removeListener('close', onClose);
179209
s.removeListener('free', onFree);
180210
s.removeListener('agentRemove', onRemove);
@@ -183,7 +213,9 @@ Agent.prototype.createSocket = function(name, host, port, localAddress, req) {
183213
return s;
184214
};
185215

186-
Agent.prototype.removeSocket = function(s, name, host, port, localAddress) {
216+
Agent.prototype.removeSocket = function(s, options) {
217+
var name = this.getName(options);
218+
debug('removeSocket', name);
187219
if (this.sockets[name]) {
188220
var index = this.sockets[name].indexOf(s);
189221
if (index !== -1) {
@@ -195,9 +227,10 @@ Agent.prototype.removeSocket = function(s, name, host, port, localAddress) {
195227
}
196228
}
197229
if (this.requests[name] && this.requests[name].length) {
230+
debug('removeSocket, have a request, make a socket');
198231
var req = this.requests[name][0];
199-
// If we have pending requests and a socket gets closed a new one
200-
this.createSocket(name, host, port, localAddress, req).emit('free');
232+
// If we have pending requests and a socket gets closed make a new one
233+
this.createSocket(req, options).emit('free');
201234
}
202235
};
203236

@@ -216,6 +249,10 @@ Agent.prototype.request = function(options, cb) {
216249
if (typeof options === 'string') {
217250
options = url.parse(options);
218251
}
252+
// don't try to do dns lookups of foo.com:8080, just foo.com
253+
if (options.hostname) {
254+
options.host = options.hostname;
255+
}
219256

220257
if (options && options.path && / /.test(options.path)) {
221258
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
@@ -229,11 +266,16 @@ Agent.prototype.request = function(options, cb) {
229266
throw new Error('Protocol:' + options.protocol + ' not supported.');
230267
}
231268

232-
options = util._extend({ agent: this, keepAlive: false }, options);
269+
options = util._extend({
270+
agent: this,
271+
keepAlive: this.keepAlive
272+
}, options);
233273

274+
// if it's false, then make a new one, just like this one.
234275
if (options.agent === false)
235-
options.agent = new Agent(options);
276+
options.agent = new this.constructor(options);
236277

278+
debug('agent.request', options);
237279
return new ClientRequest(options, cb);
238280
};
239281

lib/_http_client.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,26 @@ function ClientRequest(options, cb) {
9999
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
100100
self._renderHeaders());
101101
}
102+
102103
if (self.socketPath) {
103104
self._last = true;
104105
self.shouldKeepAlive = false;
105-
if (options.createConnection) {
106-
self.onSocket(options.createConnection(self.socketPath));
107-
} else {
108-
self.onSocket(net.createConnection(self.socketPath));
109-
}
106+
var conn = self.agent.createConnection({ path: self.socketPath });
107+
self.onSocket(conn);
110108
} else if (self.agent) {
111109
// If there is an agent we should default to Connection:keep-alive.
112110
self._last = false;
113111
self.shouldKeepAlive = true;
114-
self.agent.addRequest(self, host, port, options.localAddress);
112+
self.agent.addRequest(self, options);
115113
} else {
116114
// No agent, default to Connection:close.
117115
self._last = true;
118116
self.shouldKeepAlive = false;
119117
if (options.createConnection) {
120-
options.port = port;
121-
options.host = host;
122118
var conn = options.createConnection(options);
123119
} else {
124-
var conn = net.createConnection({
125-
port: port,
126-
host: host,
127-
localAddress: options.localAddress
128-
});
120+
debug('CLIENT use net.createConnection', options);
121+
var conn = net.createConnection(options);
129122
}
130123
self.onSocket(conn);
131124
}
@@ -134,8 +127,8 @@ function ClientRequest(options, cb) {
134127
self._flush();
135128
self = null;
136129
});
137-
138130
}
131+
139132
util.inherits(ClientRequest, OutgoingMessage);
140133

141134
exports.ClientRequest = ClientRequest;

lib/https.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var http = require('http');
2424
var util = require('util');
2525
var url = require('url');
2626
var inherits = require('util').inherits;
27+
var debug = util.debuglog('https');
2728

2829
function Server(opts, requestListener) {
2930
if (!(this instanceof Server)) return new Server(opts, requestListener);
@@ -77,56 +78,58 @@ function createConnection(port, host, options) {
7778
options.host = host;
7879
}
7980

81+
debug('createConnection', options);
8082
return tls.connect(options);
8183
}
8284

8385

8486
function Agent(options) {
8587
http.Agent.call(this, options);
88+
this.defaultPort = 443;
89+
this.protocol = 'https:';
8690
}
8791
inherits(Agent, http.Agent);
88-
Agent.prototype.defaultPort = 443;
89-
Agent.prototype.protocol = 'https:';
9092
Agent.prototype.createConnection = createConnection;
9193

94+
Agent.prototype.getName = function(options) {
95+
var name = http.Agent.prototype.getName.call(this, options);
96+
97+
name += ':';
98+
if (options.ca)
99+
name += options.ca;
100+
101+
name += ':';
102+
if (options.cert)
103+
name += options.cert;
104+
105+
name += ':';
106+
if (options.ciphers)
107+
name += options.ciphers;
108+
109+
name += ':';
110+
if (options.key)
111+
name += options.key;
112+
113+
name += ':';
114+
if (options.pfx)
115+
name += options.pfx;
116+
117+
name += ':';
118+
if (options.rejectUnauthorized !== undefined)
119+
name += options.rejectUnauthorized;
120+
121+
return name;
122+
};
123+
92124
var globalAgent = new Agent();
93125

94126
exports.globalAgent = globalAgent;
95127
exports.Agent = Agent;
96128

97129
exports.request = function(options, cb) {
98-
if (typeof options === 'string') {
99-
options = url.parse(options);
100-
}
101-
102-
if (options.protocol && options.protocol !== 'https:') {
103-
throw new Error('Protocol:' + options.protocol + ' not supported.');
104-
}
105-
106-
options = util._extend({
107-
createConnection: createConnection,
108-
defaultPort: 443
109-
}, options);
110-
111-
if (typeof options.agent === 'undefined') {
112-
if (typeof options.ca === 'undefined' &&
113-
typeof options.cert === 'undefined' &&
114-
typeof options.ciphers === 'undefined' &&
115-
typeof options.key === 'undefined' &&
116-
typeof options.passphrase === 'undefined' &&
117-
typeof options.pfx === 'undefined' &&
118-
typeof options.rejectUnauthorized === 'undefined') {
119-
options.agent = globalAgent;
120-
} else {
121-
options.agent = new Agent(options);
122-
}
123-
}
124-
125-
return new http.ClientRequest(options, cb);
130+
return globalAgent.request(options, cb);
126131
};
127132

128133
exports.get = function(options, cb) {
129-
var req = exports.request(options, cb);
130-
req.end();
131-
return req;
134+
return globalAgent.get(options, cb);
132135
};

test/simple/test-http-agent-destroyed-socket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var requestOptions = {
4343

4444
var request1 = http.get(requestOptions, function(response) {
4545
// assert request2 is queued in the agent
46-
var key = 'localhost:' + common.PORT;
46+
var key = agent.getName(requestOptions);
4747
assert(agent.requests[key].length === 1);
4848
console.log('got response1');
4949
request1.socket.on('close', function() {

test/simple/test-http-client-agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var common = require('../common');
2323
var assert = require('assert');
2424
var http = require('http');
2525

26-
var name = 'localhost:' + common.PORT;
26+
var name = http.globalAgent.getName({ port: common.PORT });
2727
var max = 3;
2828
var count = 0;
2929

0 commit comments

Comments
 (0)