2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const net = require('net');
|
|
|
|
const util = require('util');
|
2015-09-16 22:45:29 +00:00
|
|
|
const EventEmitter = require('events');
|
2015-01-21 16:36:59 +00:00
|
|
|
const debug = util.debuglog('http');
|
2018-02-11 21:35:59 +00:00
|
|
|
const { async_id_symbol } = require('internal/async_hooks').symbols;
|
2013-04-11 21:47:15 +00:00
|
|
|
|
|
|
|
// New Agent code.
|
|
|
|
|
|
|
|
// The largest departure from the previous implementation is that
|
|
|
|
// an Agent instance holds connections for a variable number of host:ports.
|
|
|
|
// Surprisingly, this is still API compatible as far as third parties are
|
|
|
|
// concerned. The only code that really notices the difference is the
|
|
|
|
// request object.
|
|
|
|
|
|
|
|
// Another departure is that all code related to HTTP parsing is in
|
|
|
|
// ClientRequest.onSocket(). The Agent is now *strictly*
|
|
|
|
// concerned with managing a connection pool.
|
|
|
|
|
|
|
|
function Agent(options) {
|
2013-05-15 22:10:53 +00:00
|
|
|
if (!(this instanceof Agent))
|
|
|
|
return new Agent(options);
|
|
|
|
|
2013-04-11 21:47:15 +00:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2017-10-25 06:34:43 +00:00
|
|
|
this.defaultPort = 80;
|
|
|
|
this.protocol = 'http:';
|
2013-05-23 01:44:24 +00:00
|
|
|
|
2017-10-25 06:34:43 +00:00
|
|
|
this.options = util._extend({}, options);
|
2013-05-23 01:44:24 +00:00
|
|
|
|
2013-05-21 21:02:18 +00:00
|
|
|
// don't confuse net and make it think that we're connecting to a pipe
|
2017-10-25 06:34:43 +00:00
|
|
|
this.options.path = null;
|
|
|
|
this.requests = {};
|
|
|
|
this.sockets = {};
|
|
|
|
this.freeSockets = {};
|
|
|
|
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
|
|
|
|
this.keepAlive = this.options.keepAlive || false;
|
|
|
|
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
|
|
|
|
this.maxFreeSockets = this.options.maxFreeSockets || 256;
|
|
|
|
|
|
|
|
this.on('free', (socket, options) => {
|
|
|
|
var name = this.getName(options);
|
2013-05-23 01:44:24 +00:00
|
|
|
debug('agent.on(free)', name);
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2016-01-12 04:41:01 +00:00
|
|
|
if (socket.writable &&
|
2017-10-25 06:34:43 +00:00
|
|
|
this.requests[name] && this.requests[name].length) {
|
|
|
|
this.requests[name].shift().onSocket(socket);
|
|
|
|
if (this.requests[name].length === 0) {
|
2013-04-11 21:47:15 +00:00
|
|
|
// don't leak
|
2017-10-25 06:34:43 +00:00
|
|
|
delete this.requests[name];
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-05-15 22:10:53 +00:00
|
|
|
// If there are no pending requests, then put it in
|
|
|
|
// the freeSockets pool, but only if we're allowed to do so.
|
|
|
|
var req = socket._httpMessage;
|
|
|
|
if (req &&
|
|
|
|
req.shouldKeepAlive &&
|
2016-01-12 04:41:01 +00:00
|
|
|
socket.writable &&
|
2017-10-25 06:34:43 +00:00
|
|
|
this.keepAlive) {
|
|
|
|
var freeSockets = this.freeSockets[name];
|
2013-08-05 03:54:52 +00:00
|
|
|
var freeLen = freeSockets ? freeSockets.length : 0;
|
|
|
|
var count = freeLen;
|
2017-10-25 06:34:43 +00:00
|
|
|
if (this.sockets[name])
|
|
|
|
count += this.sockets[name].length;
|
2013-05-15 22:10:53 +00:00
|
|
|
|
2017-10-25 06:34:43 +00:00
|
|
|
if (count > this.maxSockets || freeLen >= this.maxFreeSockets) {
|
2013-05-15 22:10:53 +00:00
|
|
|
socket.destroy();
|
2017-10-25 06:34:43 +00:00
|
|
|
} else if (this.keepSocketAlive(socket)) {
|
2013-05-15 22:10:53 +00:00
|
|
|
freeSockets = freeSockets || [];
|
2017-10-25 06:34:43 +00:00
|
|
|
this.freeSockets[name] = freeSockets;
|
2017-03-10 13:17:42 +00:00
|
|
|
socket[async_id_symbol] = -1;
|
2013-05-15 22:10:53 +00:00
|
|
|
socket._httpMessage = null;
|
2017-10-25 06:34:43 +00:00
|
|
|
this.removeSocket(socket, options);
|
2013-05-15 22:10:53 +00:00
|
|
|
freeSockets.push(socket);
|
2017-05-12 20:53:15 +00:00
|
|
|
} else {
|
|
|
|
// Implementation doesn't want to keep socket alive
|
|
|
|
socket.destroy();
|
2013-05-15 22:10:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
socket.destroy();
|
|
|
|
}
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-05-15 21:24:08 +00:00
|
|
|
|
2013-04-11 21:47:15 +00:00
|
|
|
util.inherits(Agent, EventEmitter);
|
|
|
|
|
2013-05-15 22:10:53 +00:00
|
|
|
Agent.defaultMaxSockets = Infinity;
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2013-05-21 21:02:18 +00:00
|
|
|
Agent.prototype.createConnection = net.createConnection;
|
2013-05-23 01:44:24 +00:00
|
|
|
|
|
|
|
// Get the key for a given set of request options
|
2016-10-12 11:03:13 +00:00
|
|
|
Agent.prototype.getName = function getName(options) {
|
2015-09-11 21:18:01 +00:00
|
|
|
var name = options.host || 'localhost';
|
2013-05-23 01:44:24 +00:00
|
|
|
|
|
|
|
name += ':';
|
|
|
|
if (options.port)
|
|
|
|
name += options.port;
|
2015-09-11 21:18:01 +00:00
|
|
|
|
2013-05-23 01:44:24 +00:00
|
|
|
name += ':';
|
|
|
|
if (options.localAddress)
|
|
|
|
name += options.localAddress;
|
2015-09-11 21:18:01 +00:00
|
|
|
|
2016-05-09 14:35:20 +00:00
|
|
|
// Pacify parallel/test-http-agent-getname by only appending
|
|
|
|
// the ':' when options.family is set.
|
|
|
|
if (options.family === 4 || options.family === 6)
|
2017-11-10 07:06:21 +00:00
|
|
|
name += `:${options.family}`;
|
2016-05-09 14:35:20 +00:00
|
|
|
|
2017-05-25 07:32:04 +00:00
|
|
|
if (options.socketPath)
|
2017-11-10 07:06:21 +00:00
|
|
|
name += `:${options.socketPath}`;
|
2017-05-25 07:32:04 +00:00
|
|
|
|
2013-05-23 01:44:24 +00:00
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2018-03-25 14:27:38 +00:00
|
|
|
Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
|
|
|
|
localAddress/* legacy */) {
|
2016-02-11 00:59:25 +00:00
|
|
|
// Legacy API: addRequest(req, host, port, localAddress)
|
2013-08-07 17:23:45 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = {
|
|
|
|
host: options,
|
2017-04-24 06:19:30 +00:00
|
|
|
port,
|
|
|
|
localAddress
|
2013-08-07 17:23:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-14 15:56:02 +00:00
|
|
|
options = util._extend({}, options);
|
2017-02-14 01:58:56 +00:00
|
|
|
util._extend(options, this.options);
|
2017-05-25 07:32:04 +00:00
|
|
|
if (options.socketPath)
|
|
|
|
options.path = options.socketPath;
|
2016-03-14 15:56:02 +00:00
|
|
|
|
2017-08-10 13:11:19 +00:00
|
|
|
if (!options.servername)
|
|
|
|
options.servername = calculateServerName(options, req);
|
2016-09-18 11:34:48 +00:00
|
|
|
|
2013-05-23 01:44:24 +00:00
|
|
|
var name = this.getName(options);
|
2013-04-11 21:47:15 +00:00
|
|
|
if (!this.sockets[name]) {
|
|
|
|
this.sockets[name] = [];
|
|
|
|
}
|
2013-05-15 22:10:53 +00:00
|
|
|
|
2013-08-05 03:54:52 +00:00
|
|
|
var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
|
|
|
|
var sockLen = freeLen + this.sockets[name].length;
|
|
|
|
|
|
|
|
if (freeLen) {
|
2013-05-15 22:10:53 +00:00
|
|
|
// we have a free socket, so use that.
|
|
|
|
var socket = this.freeSockets[name].shift();
|
2017-07-21 23:10:56 +00:00
|
|
|
// Guard against an uninitialized or user supplied Socket.
|
|
|
|
if (socket._handle && typeof socket._handle.asyncReset === 'function') {
|
|
|
|
// Assign the handle a new asyncId and run any init() hooks.
|
|
|
|
socket._handle.asyncReset();
|
|
|
|
socket[async_id_symbol] = socket._handle.getAsyncId();
|
|
|
|
}
|
2013-05-15 22:10:53 +00:00
|
|
|
|
|
|
|
// don't leak
|
|
|
|
if (!this.freeSockets[name].length)
|
|
|
|
delete this.freeSockets[name];
|
|
|
|
|
2017-05-12 20:53:15 +00:00
|
|
|
this.reuseSocket(socket, req);
|
2013-05-15 22:10:53 +00:00
|
|
|
req.onSocket(socket);
|
2013-08-05 03:54:52 +00:00
|
|
|
this.sockets[name].push(socket);
|
|
|
|
} else if (sockLen < this.maxSockets) {
|
|
|
|
debug('call onSocket', sockLen, freeLen);
|
2013-04-11 21:47:15 +00:00
|
|
|
// If we are under maxSockets create a new one.
|
2017-07-03 03:52:41 +00:00
|
|
|
this.createSocket(req, options, handleSocketCreation(req, true));
|
2013-04-11 21:47:15 +00:00
|
|
|
} else {
|
2013-05-23 01:44:24 +00:00
|
|
|
debug('wait for socket');
|
2013-04-11 21:47:15 +00:00
|
|
|
// We are over limit so we'll add it to the queue.
|
|
|
|
if (!this.requests[name]) {
|
|
|
|
this.requests[name] = [];
|
|
|
|
}
|
|
|
|
this.requests[name].push(req);
|
|
|
|
}
|
|
|
|
};
|
2013-05-15 21:24:08 +00:00
|
|
|
|
2016-10-12 11:03:13 +00:00
|
|
|
Agent.prototype.createSocket = function createSocket(req, options, cb) {
|
2013-05-23 01:44:24 +00:00
|
|
|
options = util._extend({}, options);
|
2017-10-25 06:34:43 +00:00
|
|
|
util._extend(options, this.options);
|
2017-05-25 07:32:04 +00:00
|
|
|
if (options.socketPath)
|
|
|
|
options.path = options.socketPath;
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2017-08-10 13:11:19 +00:00
|
|
|
if (!options.servername)
|
|
|
|
options.servername = calculateServerName(options, req);
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2017-10-25 06:34:43 +00:00
|
|
|
var name = this.getName(options);
|
2015-07-23 04:18:38 +00:00
|
|
|
options._agentKey = name;
|
2013-05-23 01:44:24 +00:00
|
|
|
|
|
|
|
debug('createConnection', name, options);
|
2013-11-03 22:32:03 +00:00
|
|
|
options.encoding = null;
|
2016-01-12 04:41:01 +00:00
|
|
|
var called = false;
|
2017-07-03 03:52:41 +00:00
|
|
|
|
2017-10-25 06:34:43 +00:00
|
|
|
const oncreate = (err, s) => {
|
2016-01-12 04:41:01 +00:00
|
|
|
if (called)
|
|
|
|
return;
|
|
|
|
called = true;
|
|
|
|
if (err)
|
|
|
|
return cb(err);
|
2017-10-25 06:34:43 +00:00
|
|
|
if (!this.sockets[name]) {
|
|
|
|
this.sockets[name] = [];
|
2016-01-12 04:41:01 +00:00
|
|
|
}
|
2017-10-25 06:34:43 +00:00
|
|
|
this.sockets[name].push(s);
|
|
|
|
debug('sockets', name, this.sockets[name].length);
|
|
|
|
installListeners(this, s, options);
|
2016-01-12 04:41:01 +00:00
|
|
|
cb(null, s);
|
2017-10-25 06:34:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const newSocket = this.createConnection(options, oncreate);
|
|
|
|
if (newSocket)
|
|
|
|
oncreate(null, newSocket);
|
2013-04-11 21:47:15 +00:00
|
|
|
};
|
2013-05-15 21:24:08 +00:00
|
|
|
|
2017-08-10 13:11:19 +00:00
|
|
|
function calculateServerName(options, req) {
|
|
|
|
let servername = options.host;
|
|
|
|
const hostHeader = req.getHeader('host');
|
|
|
|
if (hostHeader) {
|
|
|
|
// abc => abc
|
|
|
|
// abc:123 => abc
|
|
|
|
// [::1] => ::1
|
|
|
|
// [::1]:123 => ::1
|
|
|
|
if (hostHeader.startsWith('[')) {
|
|
|
|
const index = hostHeader.indexOf(']');
|
|
|
|
if (index === -1) {
|
|
|
|
// Leading '[', but no ']'. Need to do something...
|
|
|
|
servername = hostHeader;
|
|
|
|
} else {
|
|
|
|
servername = hostHeader.substr(1, index - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
servername = hostHeader.split(':', 1)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return servername;
|
|
|
|
}
|
|
|
|
|
2016-12-05 01:51:51 +00:00
|
|
|
function installListeners(agent, s, options) {
|
|
|
|
function onFree() {
|
|
|
|
debug('CLIENT socket onFree');
|
|
|
|
agent.emit('free', s, options);
|
|
|
|
}
|
|
|
|
s.on('free', onFree);
|
|
|
|
|
|
|
|
function onClose(err) {
|
|
|
|
debug('CLIENT socket onClose');
|
|
|
|
// This is the only place where sockets get removed from the Agent.
|
|
|
|
// If you want to remove a socket from the pool, just close it.
|
|
|
|
// All socket errors end in a close event anyway.
|
|
|
|
agent.removeSocket(s, options);
|
|
|
|
}
|
|
|
|
s.on('close', onClose);
|
|
|
|
|
|
|
|
function onRemove() {
|
|
|
|
// We need this function for cases like HTTP 'upgrade'
|
|
|
|
// (defined by WebSockets) where we need to remove a socket from the
|
|
|
|
// pool because it'll be locked up indefinitely
|
|
|
|
debug('CLIENT socket onRemove');
|
|
|
|
agent.removeSocket(s, options);
|
|
|
|
s.removeListener('close', onClose);
|
|
|
|
s.removeListener('free', onFree);
|
|
|
|
s.removeListener('agentRemove', onRemove);
|
|
|
|
}
|
|
|
|
s.on('agentRemove', onRemove);
|
|
|
|
}
|
|
|
|
|
2016-10-12 11:03:13 +00:00
|
|
|
Agent.prototype.removeSocket = function removeSocket(s, options) {
|
2013-05-23 01:44:24 +00:00
|
|
|
var name = this.getName(options);
|
2016-01-12 04:41:01 +00:00
|
|
|
debug('removeSocket', name, 'writable:', s.writable);
|
2013-11-06 11:58:03 +00:00
|
|
|
var sets = [this.sockets];
|
|
|
|
|
|
|
|
// If the socket was destroyed, remove it from the free buffers too.
|
2016-01-12 04:41:01 +00:00
|
|
|
if (!s.writable)
|
2013-11-06 11:58:03 +00:00
|
|
|
sets.push(this.freeSockets);
|
|
|
|
|
2014-08-14 03:15:24 +00:00
|
|
|
for (var sk = 0; sk < sets.length; sk++) {
|
|
|
|
var sockets = sets[sk];
|
|
|
|
|
2013-11-06 11:58:03 +00:00
|
|
|
if (sockets[name]) {
|
|
|
|
var index = sockets[name].indexOf(s);
|
|
|
|
if (index !== -1) {
|
|
|
|
sockets[name].splice(index, 1);
|
|
|
|
// Don't leak
|
|
|
|
if (sockets[name].length === 0)
|
|
|
|
delete sockets[name];
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-14 03:15:24 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 21:47:15 +00:00
|
|
|
if (this.requests[name] && this.requests[name].length) {
|
2013-05-23 01:44:24 +00:00
|
|
|
debug('removeSocket, have a request, make a socket');
|
2013-04-11 21:47:15 +00:00
|
|
|
var req = this.requests[name][0];
|
2013-05-23 01:44:24 +00:00
|
|
|
// If we have pending requests and a socket gets closed make a new one
|
2017-07-03 03:52:41 +00:00
|
|
|
this.createSocket(req, options, handleSocketCreation(req, false));
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-12 20:53:15 +00:00
|
|
|
Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) {
|
|
|
|
socket.setKeepAlive(true, this.keepAliveMsecs);
|
|
|
|
socket.unref();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
|
|
|
|
debug('have free socket');
|
|
|
|
socket.ref();
|
|
|
|
};
|
|
|
|
|
2016-10-12 11:03:13 +00:00
|
|
|
Agent.prototype.destroy = function destroy() {
|
2013-05-15 22:10:53 +00:00
|
|
|
var sets = [this.freeSockets, this.sockets];
|
2014-08-14 03:15:24 +00:00
|
|
|
for (var s = 0; s < sets.length; s++) {
|
|
|
|
var set = sets[s];
|
|
|
|
var keys = Object.keys(set);
|
|
|
|
for (var v = 0; v < keys.length; v++) {
|
|
|
|
var setName = set[keys[v]];
|
|
|
|
for (var n = 0; n < setName.length; n++) {
|
|
|
|
setName[n].destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-15 22:10:53 +00:00
|
|
|
};
|
|
|
|
|
2017-07-03 03:52:41 +00:00
|
|
|
function handleSocketCreation(request, informRequest) {
|
|
|
|
return function handleSocketCreation_Inner(err, socket) {
|
|
|
|
if (err) {
|
2018-03-05 11:20:48 +00:00
|
|
|
process.nextTick(emitErrorNT, request, err);
|
2017-07-03 03:52:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (informRequest)
|
|
|
|
request.onSocket(socket);
|
|
|
|
else
|
|
|
|
socket.emit('free');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-05 11:20:48 +00:00
|
|
|
function emitErrorNT(emitter, err) {
|
|
|
|
emitter.emit('error', err);
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:09:32 +00:00
|
|
|
module.exports = {
|
|
|
|
Agent,
|
|
|
|
globalAgent: new Agent()
|
|
|
|
};
|