diff options
author | Mark Cavage <mark.cavage@joyent.com> | 2011-04-25 16:04:07 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2011-04-25 16:52:31 -0700 |
commit | a2328dc73c425c4d6f58cb8d97ece92adafcb96b (patch) | |
tree | ce943654b04a8d18a1f7c9ca4ad88f64e55238b4 /lib/http.js | |
parent | 0b3ecc05a681c74d35334cdd269f68f4cd473281 (diff) | |
download | node-a2328dc73c425c4d6f58cb8d97ece92adafcb96b.tar.gz |
Add support for Unix Domain Sockets to HTTP
fixes #979.
Diffstat (limited to 'lib/http.js')
-rw-r--r-- | lib/http.js | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/lib/http.js b/lib/http.js index a4447289a..9339dd684 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1118,10 +1118,12 @@ function Agent(options) { this.options = options; this.host = options.host; this.port = options.port || this.defaultPort; + this.socketPath = options.socketPath; this.queue = []; this.sockets = []; this.maxSockets = Agent.defaultMaxSockets; + } util.inherits(Agent, EventEmitter); exports.Agent = Agent; @@ -1161,7 +1163,7 @@ Agent.prototype._establishNewConnection = function() { // Grab a new "socket". Depending on the implementation of _getConnection // this could either be a raw TCP socket or a TLS stream. - var socket = this._getConnection(this.host, this.port, function() { + var socket = this._getConnection(self, function() { socket._httpConnecting = false; self.emit('connect'); // mostly for the shim. debug('Agent _getConnection callback'); @@ -1342,9 +1344,18 @@ Agent.prototype._establishNewConnection = function() { // Sub-classes can overwrite this method with e.g. something that supplies // TLS streams. -Agent.prototype._getConnection = function(host, port, cb) { +Agent.prototype._getConnection = function(options, cb) { debug('Agent connected!'); - var c = net.createConnection(port, host); + + var c; + + if (options.host) { + c = net.createConnection(options.port, options.host); + } else if (options.socketPath) { + c = net.createConnection(options.socketPath); + } else { + c = net.createConnection(options.port); + } c.on('connect', cb); return c; }; @@ -1404,14 +1415,41 @@ Agent.prototype._cycle = function() { // to remove it? var agents = {}; +// Backwards compatible with legacy getAgent(host, port); +function getAgent(options) { + var agent; + var host; + var id; + var port; + + var _opts = {}; + + if (options instanceof String) { + port = arguments[1] || 80; + id = options + ':' + port; + _opts.host = options; + _opts.port = port; + } else if (options instanceof Object) { + if (options.port || options.host) { + host = options.host || 'localhost'; + port = options.port || 80; + id = host + port; + _opts.host = host; + _opts.port = port; + } else if (options.socketPath) { + id = options.socketPath; + _opts.socketPath = options.socketPath; + } else { + throw new TypeError('Invalid options specification to getAgent'); + } + } else { + throw new TypeError('Invalid argument to getAgent'); + } -function getAgent(host, port) { - port = port || 80; - var id = host + ':' + port; - var agent = agents[id]; + agent = agents[id]; if (!agent) { - agent = agents[id] = new Agent({ host: host, port: port }); + agent = agents[id] = new Agent(_opts); } return agent; @@ -1429,7 +1467,7 @@ exports._requestFromAgent = function(options, cb) { exports.request = function(options, cb) { if (options.agent === undefined) { - options.agent = getAgent(options.host, options.port); + options.agent = getAgent(options); } else if (options.agent === false) { options.agent = new Agent(options); } |