From 9faa924a29544cfd84c28cb1c45489f495e3806a Mon Sep 17 00:00:00 2001 From: indexzero Date: Mon, 21 Feb 2011 04:39:51 -0500 Subject: [PATCH] [api] First pass at removing pool and working with node v0.4.0 --- .gitignore | 1 + lib/node-http-proxy.js | 212 ++++++++++++++++------------------- test/forward-proxy-test.js | 5 +- test/helpers.js | 6 +- test/node-http-proxy-test.js | 5 +- 5 files changed, 106 insertions(+), 123 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..234f13a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test/config.json \ No newline at end of file diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 7f51bf5..7961996 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -27,15 +27,8 @@ var util = require('util'), http = require('http'), events = require('events'), - pool = require('pool'), ProxyTable = require('./proxy-table').ProxyTable, - min = 0, - max = 100; - -// Setup the PoolManager -var manager = pool.createPoolManager(); -manager.setMinClients(min); -manager.setMaxClients(max); + maxSockets = 100; exports.createServer = function () { var args, callback, port, host, forward, @@ -114,17 +107,13 @@ exports.createServer = function () { return server; }; -exports.setMin = function (value) { - min = value; - manager.setMinClients(min); +exports.setMaxSockets = function (value) { + maxSockets = value; }; -exports.setMax = function (value) { - max = value; - manager.setMaxClients(max); -}; +exports.ProxyTable = ProxyTable; -var HttpProxy = function (req, res, head) { +var HttpProxy = exports.HttpProxy = function (req, res, head) { this.events = {}; this.req = req; @@ -177,116 +166,116 @@ HttpProxy.prototype = { }, proxyRequest: function (port, server) { - var self = this, req = this.req, res = this.res; + var self = this, req = this.req, res = this.res, reverseProxy; + + // Create an error handler so we can use it temporarily + function error (obj) { + var fn = function (err) { + res.writeHead(500, {'Content-Type': 'text/plain'}); + + if (req.method !== 'HEAD') { + res.write('An error has occurred: ' + JSON.stringify(err)); + } + + // Response end may never come so removeListener here + obj.removeListener('error', fn); + res.end(); + }; + + return fn; + }; // Open new HTTP request to internal resource with will act as a reverse proxy pass - var p = manager.getPool(port, server); - - p.on('error', function (err) { - // Remark: We should probably do something here - // but this is a hot-fix because I don't think 'pool' - // should be emitting this event. - }); - - p.request(req.method, req.url, req.headers, function (reverse_proxy) { - // Create an error handler so we can use it temporarily - function error (obj) { - var fn = function (err) { - res.writeHead(500, {'Content-Type': 'text/plain'}); - - if (req.method !== 'HEAD') { - res.write('An error has occurred: ' + JSON.stringify(err)); - } - - // Response end may never come so removeListener here - obj.removeListener('error', fn); - res.end(); - }; + reverseProxy = http.request({ + host: server, + port: port, + method: req.method, + path: req.url, + headers: req.headers + }, function (response) { - return fn; - }; + // Process the reverse_proxy response when it's received. + if (response.headers.connection) { + if (req.headers.connection) response.headers.connection = req.headers.connection; + else response.headers.connection = 'close'; + } - // Add a listener for the connection timeout event - var reverseProxyError = error(reverse_proxy); - reverse_proxy.addListener('error', reverseProxyError); - - // Add a listener for the reverse_proxy response event - reverse_proxy.addListener('response', function (response) { - if (response.headers.connection) { - if (req.headers.connection) response.headers.connection = req.headers.connection; - else response.headers.connection = 'close'; + // Set the response headers of the client response + res.writeHead(response.statusCode, response.headers); + + // Status code = 304 + // No 'data' event and no 'end' + if (response.statusCode === 304) { + res.end(); + return; + } + + // Add event handler for the proxied response in chunks + response.addListener('data', function (chunk) { + if (req.method !== 'HEAD') { + res.write(chunk, 'binary'); + self.body += chunk; } - - // Set the response headers of the client response - res.writeHead(response.statusCode, response.headers); - - // Status code = 304 - // No 'data' event and no 'end' - if (response.statusCode === 304) { - res.end(); - return; - } - - // Add event handler for the proxied response in chunks - response.addListener('data', function (chunk) { - if (req.method !== 'HEAD') { - res.write(chunk, 'binary'); - self.body += chunk; - } - }); - - // Add event listener for end of proxied response - response.addListener('end', function () { - reverse_proxy.removeListener('error', reverseProxyError); - res.end(); - }); }); - // Chunk the client request body as chunks from the proxied request come in - req.addListener('data', function (chunk) { - reverse_proxy.write(chunk, 'binary'); - }) - - // At the end of the client request, we are going to stop the proxied request - req.addListener('end', function () { - reverse_proxy.end(); + // Add event listener for end of proxied response + response.addListener('end', function () { + reverseProxy.removeListener('error', reverseProxyError); + res.end(); }); - - self.unwatch(req); }); + + // Add a listener for the connection timeout event + var reverseProxyError = error(reverseProxy); + reverseProxy.addListener('error', reverseProxyError); + + // Chunk the client request body as chunks from the proxied request come in + req.addListener('data', function (chunk) { + reverseProxy.write(chunk, 'binary'); + }) + + // At the end of the client request, we are going to stop the proxied request + req.addListener('end', function () { + reverseProxy.end(); + }); + + self.unwatch(req); }, forwardRequest: function (port, server) { - var self = this, req = this.req; + var self = this, req = this.req, forwardProxy; // Open new HTTP request to internal resource with will act as a reverse proxy pass - var p = manager.getPool(port, server); - - p.on('error', function (err) { - // Remark: We should probably do something here - // but this is a hot-fix because I don't think 'pool' - // should be emitting this event. + forwardProxy = http.request({ + host: server, + port: port, + method: req.method, + path: req.url, + headers: req.headers + }, function (response) { + // + // Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy. + // Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning. + // }); - p.request(req.method, req.url, req.headers, function (forward_proxy) { - // Add a listener for the connection timeout event - forward_proxy.addListener('error', function (err) { - // Remark: Ignoring this error in the event - // forward target doesn't exist. - }); - - // Chunk the client request body as chunks from the proxied request come in - req.addListener('data', function (chunk) { - forward_proxy.write(chunk, 'binary'); - }) - - // At the end of the client request, we are going to stop the proxied request - req.addListener('end', function () { - forward_proxy.end(); - }); - - self.unwatch(req); + // Add a listener for the connection timeout event + forwardProxy.addListener('error', function (err) { + // Remark: Ignoring this error in the event + // forward target doesn't exist. }); + + // Chunk the client request body as chunks from the proxied request come in + req.addListener('data', function (chunk) { + forwardProxy.write(chunk, 'binary'); + }) + + // At the end of the client request, we are going to stop the proxied request + req.addListener('end', function () { + forwardProxy.end(); + }); + + self.unwatch(req); }, proxyWebSocketRequest: function (port, server, host) { @@ -477,7 +466,4 @@ HttpProxy.prototype = { }); }; } -}; - -exports.HttpProxy = HttpProxy; -exports.ProxyTable = ProxyTable; \ No newline at end of file +}; \ No newline at end of file diff --git a/test/forward-proxy-test.js b/test/forward-proxy-test.js index 1db9b42..0d8e8ba 100644 --- a/test/forward-proxy-test.js +++ b/test/forward-proxy-test.js @@ -11,10 +11,9 @@ var fs = require('fs'), path = require('path'), request = require('request'), assert = require('assert'), - helpers = require('./helpers'), - TestRunner = helpers.TestRunner; + helpers = require('./helpers'); -var runner = new TestRunner(), +var runner = new helpers.TestRunner(), assertProxiedWithTarget = helpers.assertProxiedWithTarget, assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget; diff --git a/test/helpers.js b/test/helpers.js index 6bca02c..fddf3b4 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -63,7 +63,7 @@ exports.assertProxiedWithNoTarget = function (runner, proxyPort, statusCode, cre return test; } -var TestRunner = function () { +var TestRunner = exports.TestRunner = function () { this.testServers = []; } @@ -159,6 +159,4 @@ TestRunner.prototype.closeServers = function () { }); return this.testServers; -}; - -exports.TestRunner = TestRunner; \ No newline at end of file +}; \ No newline at end of file diff --git a/test/node-http-proxy-test.js b/test/node-http-proxy-test.js index 43d5320..bbbcdec 100644 --- a/test/node-http-proxy-test.js +++ b/test/node-http-proxy-test.js @@ -28,10 +28,9 @@ var vows = require('vows'), util = require('util'), request = require('request'), assert = require('assert'), - helpers = require('./helpers'), - TestRunner = helpers.TestRunner; + helpers = require('./helpers'); -var runner = new TestRunner(), +var runner = new helpers.TestRunner(), assertProxiedWithTarget = helpers.assertProxiedWithTarget, assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;