node/lib/_http_agent.js

287 lines
8.8 KiB
JavaScript
Raw Normal View History

2013-04-11 21:47:15 +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.
var net = require('net');
2013-05-21 21:02:18 +00:00
var url = require('url');
2013-04-11 21:47:15 +00:00
var util = require('util');
var EventEmitter = require('events').EventEmitter;
2013-05-21 21:02:18 +00:00
var ClientRequest = require('_http_client').ClientRequest;
2013-05-23 01:44:24 +00:00
var debug = util.debuglog('http');
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) {
if (!(this instanceof Agent))
return new Agent(options);
2013-04-11 21:47:15 +00:00
EventEmitter.call(this);
var self = this;
2013-05-23 01:44:24 +00:00
self.defaultPort = 80;
self.protocol = 'http:';
2013-05-21 21:02:18 +00:00
self.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
self.options.path = null;
2013-04-11 21:47:15 +00:00
self.requests = {};
self.sockets = {};
self.freeSockets = {};
self.keepAliveMsecs = self.options.keepAliveMsecs || 1000;
self.keepAlive = self.options.keepAlive || false;
2013-04-11 21:47:15 +00:00
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
2013-05-23 01:44:24 +00:00
self.on('free', function(socket, options) {
var name = self.getName(options);
debug('agent.on(free)', name);
2013-04-11 21:47:15 +00:00
if (!socket.destroyed &&
self.requests[name] && self.requests[name].length) {
self.requests[name].shift().onSocket(socket);
if (self.requests[name].length === 0) {
// don't leak
delete self.requests[name];
}
} else {
// 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 &&
!socket.destroyed &&
self.options.keepAlive) {
var freeSockets = self.freeSockets[name];
var count = freeSockets ? freeSockets.length : 0;
if (self.sockets[name])
count += self.sockets[name].length;
if (count > self.maxSockets) {
socket.destroy();
} else {
freeSockets = freeSockets || [];
self.freeSockets[name] = freeSockets;
socket.setKeepAlive(true, self.keepAliveMsecs);
socket.unref();
socket._httpMessage = null;
freeSockets.push(socket);
}
} 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);
exports.Agent = Agent;
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
Agent.prototype.getName = function(options) {
var name = '';
if (options.host)
name += options.host;
else
name += 'localhost';
name += ':';
if (options.port)
name += options.port;
name += ':';
if (options.localAddress)
name += options.localAddress;
name += ':';
return name;
};
Agent.prototype.addRequest = function(req, options) {
var host = options.host;
var port = options.port;
var localAddress = options.localAddress;
var name = this.getName(options);
2013-04-11 21:47:15 +00:00
if (!this.sockets[name]) {
this.sockets[name] = [];
}
if (this.freeSockets[name] && this.freeSockets[name].length) {
2013-05-23 01:44:24 +00:00
debug('have free socket');
// we have a free socket, so use that.
var socket = this.freeSockets[name].shift();
// don't leak
if (!this.freeSockets[name].length)
delete this.freeSockets[name];
socket.ref();
req.onSocket(socket);
} else if (this.sockets[name].length < this.maxSockets) {
2013-05-23 01:44:24 +00:00
debug('call onSocket');
2013-04-11 21:47:15 +00:00
// If we are under maxSockets create a new one.
2013-05-23 01:44:24 +00:00
req.onSocket(this.createSocket(req, options));
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
2013-05-23 01:44:24 +00:00
Agent.prototype.createSocket = function(req, options) {
2013-04-11 21:47:15 +00:00
var self = this;
2013-05-23 01:44:24 +00:00
options = util._extend({}, options);
options = util._extend(options, self.options);
2013-04-11 21:47:15 +00:00
2013-05-23 01:44:24 +00:00
options.servername = options.host;
2013-04-11 21:47:15 +00:00
if (req) {
var hostHeader = req.getHeader('host');
if (hostHeader) {
options.servername = hostHeader.replace(/:.*$/, '');
}
}
2013-05-23 01:44:24 +00:00
var name = self.getName(options);
debug('createConnection', name, options);
2013-04-11 21:47:15 +00:00
var s = self.createConnection(options);
if (!self.sockets[name]) {
self.sockets[name] = [];
}
this.sockets[name].push(s);
2013-05-23 01:44:24 +00:00
debug('sockets', name, this.sockets[name].length);
2013-05-15 21:24:08 +00:00
function onFree() {
2013-05-23 01:44:24 +00:00
self.emit('free', s, options);
2013-04-11 21:47:15 +00:00
}
s.on('free', onFree);
2013-05-15 21:24:08 +00:00
function onClose(err) {
2013-05-23 01:44:24 +00:00
debug('CLIENT socket onClose');
2013-04-11 21:47:15 +00:00
// 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.
2013-05-23 01:44:24 +00:00
self.removeSocket(s, options);
2013-04-11 21:47:15 +00:00
}
s.on('close', onClose);
2013-05-15 21:24:08 +00:00
function onRemove() {
2013-04-11 21:47:15 +00:00
// We need this function for cases like HTTP 'upgrade'
2013-05-23 01:44:24 +00:00
// (defined by WebSockets) where we need to remove a socket from the
// pool because it'll be locked up indefinitely
debug('CLIENT socket onRemove');
self.removeSocket(s, options);
2013-04-11 21:47:15 +00:00
s.removeListener('close', onClose);
s.removeListener('free', onFree);
s.removeListener('agentRemove', onRemove);
}
s.on('agentRemove', onRemove);
return s;
};
2013-05-15 21:24:08 +00:00
2013-05-23 01:44:24 +00:00
Agent.prototype.removeSocket = function(s, options) {
var name = this.getName(options);
debug('removeSocket', name);
2013-04-11 21:47:15 +00:00
if (this.sockets[name]) {
var index = this.sockets[name].indexOf(s);
if (index !== -1) {
this.sockets[name].splice(index, 1);
if (this.sockets[name].length === 0) {
// don't leak
delete this.sockets[name];
}
}
}
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
this.createSocket(req, options).emit('free');
2013-04-11 21:47:15 +00:00
}
};
Agent.prototype.destroy = function() {
var sets = [this.freeSockets, this.sockets];
sets.forEach(function(set) {
Object.keys(set).forEach(function(name) {
set[name].forEach(function(socket) {
socket.destroy();
});
});
});
};
2013-05-21 21:02:18 +00:00
Agent.prototype.request = function(options, cb) {
if (util.isString(options)) {
2013-05-21 21:02:18 +00:00
options = url.parse(options);
}
2013-05-23 01:44:24 +00:00
// don't try to do dns lookups of foo.com:8080, just foo.com
if (options.hostname) {
options.host = options.hostname;
}
2013-05-21 21:02:18 +00:00
if (options && options.path && / /.test(options.path)) {
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
// with an additional rule for ignoring percentage-escaped characters
// but that's a) hard to capture in a regular expression that performs
// well, and b) possibly too restrictive for real-world usage. That's
// why it only scans for spaces because those are guaranteed to create
// an invalid request.
throw new TypeError('Request path contains unescaped characters.');
} else if (options.protocol && options.protocol !== this.protocol) {
throw new Error('Protocol:' + options.protocol + ' not supported.');
}
2013-05-23 01:44:24 +00:00
options = util._extend({
agent: this,
keepAlive: this.keepAlive
}, options);
2013-05-21 21:02:18 +00:00
2013-05-23 01:44:24 +00:00
// if it's false, then make a new one, just like this one.
2013-05-21 21:02:18 +00:00
if (options.agent === false)
2013-05-23 01:44:24 +00:00
options.agent = new this.constructor(options);
2013-05-21 21:02:18 +00:00
2013-05-23 01:44:24 +00:00
debug('agent.request', options);
2013-05-21 21:02:18 +00:00
return new ClientRequest(options, cb);
};
Agent.prototype.get = function(options, cb) {
var req = this.request(options, cb);
req.end();
return req;
};