mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[api] First pass at removing pool and working with node v0.4.0
This commit is contained in:
parent
34cba38c29
commit
9faa924a29
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
test/config.json
|
||||||
@ -27,15 +27,8 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
events = require('events'),
|
events = require('events'),
|
||||||
pool = require('pool'),
|
|
||||||
ProxyTable = require('./proxy-table').ProxyTable,
|
ProxyTable = require('./proxy-table').ProxyTable,
|
||||||
min = 0,
|
maxSockets = 100;
|
||||||
max = 100;
|
|
||||||
|
|
||||||
// Setup the PoolManager
|
|
||||||
var manager = pool.createPoolManager();
|
|
||||||
manager.setMinClients(min);
|
|
||||||
manager.setMaxClients(max);
|
|
||||||
|
|
||||||
exports.createServer = function () {
|
exports.createServer = function () {
|
||||||
var args, callback, port, host, forward,
|
var args, callback, port, host, forward,
|
||||||
@ -114,17 +107,13 @@ exports.createServer = function () {
|
|||||||
return server;
|
return server;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.setMin = function (value) {
|
exports.setMaxSockets = function (value) {
|
||||||
min = value;
|
maxSockets = value;
|
||||||
manager.setMinClients(min);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.setMax = function (value) {
|
exports.ProxyTable = ProxyTable;
|
||||||
max = value;
|
|
||||||
manager.setMaxClients(max);
|
|
||||||
};
|
|
||||||
|
|
||||||
var HttpProxy = function (req, res, head) {
|
var HttpProxy = exports.HttpProxy = function (req, res, head) {
|
||||||
this.events = {};
|
this.events = {};
|
||||||
this.req = req;
|
this.req = req;
|
||||||
|
|
||||||
@ -177,18 +166,8 @@ HttpProxy.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
proxyRequest: function (port, server) {
|
proxyRequest: function (port, server) {
|
||||||
var self = this, req = this.req, res = this.res;
|
var self = this, req = this.req, res = this.res, reverseProxy;
|
||||||
|
|
||||||
// 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
|
// Create an error handler so we can use it temporarily
|
||||||
function error (obj) {
|
function error (obj) {
|
||||||
var fn = function (err) {
|
var fn = function (err) {
|
||||||
@ -206,12 +185,16 @@ HttpProxy.prototype = {
|
|||||||
return fn;
|
return fn;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add a listener for the connection timeout event
|
// Open new HTTP request to internal resource with will act as a reverse proxy pass
|
||||||
var reverseProxyError = error(reverse_proxy);
|
reverseProxy = http.request({
|
||||||
reverse_proxy.addListener('error', reverseProxyError);
|
host: server,
|
||||||
|
port: port,
|
||||||
|
method: req.method,
|
||||||
|
path: req.url,
|
||||||
|
headers: req.headers
|
||||||
|
}, function (response) {
|
||||||
|
|
||||||
// Add a listener for the reverse_proxy response event
|
// Process the reverse_proxy response when it's received.
|
||||||
reverse_proxy.addListener('response', function (response) {
|
|
||||||
if (response.headers.connection) {
|
if (response.headers.connection) {
|
||||||
if (req.headers.connection) response.headers.connection = req.headers.connection;
|
if (req.headers.connection) response.headers.connection = req.headers.connection;
|
||||||
else response.headers.connection = 'close';
|
else response.headers.connection = 'close';
|
||||||
@ -237,56 +220,62 @@ HttpProxy.prototype = {
|
|||||||
|
|
||||||
// Add event listener for end of proxied response
|
// Add event listener for end of proxied response
|
||||||
response.addListener('end', function () {
|
response.addListener('end', function () {
|
||||||
reverse_proxy.removeListener('error', reverseProxyError);
|
reverseProxy.removeListener('error', reverseProxyError);
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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
|
// Chunk the client request body as chunks from the proxied request come in
|
||||||
req.addListener('data', function (chunk) {
|
req.addListener('data', function (chunk) {
|
||||||
reverse_proxy.write(chunk, 'binary');
|
reverseProxy.write(chunk, 'binary');
|
||||||
})
|
})
|
||||||
|
|
||||||
// At the end of the client request, we are going to stop the proxied request
|
// At the end of the client request, we are going to stop the proxied request
|
||||||
req.addListener('end', function () {
|
req.addListener('end', function () {
|
||||||
reverse_proxy.end();
|
reverseProxy.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
self.unwatch(req);
|
self.unwatch(req);
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
forwardRequest: function (port, server) {
|
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
|
// Open new HTTP request to internal resource with will act as a reverse proxy pass
|
||||||
var p = manager.getPool(port, server);
|
forwardProxy = http.request({
|
||||||
|
host: server,
|
||||||
p.on('error', function (err) {
|
port: port,
|
||||||
// Remark: We should probably do something here
|
method: req.method,
|
||||||
// but this is a hot-fix because I don't think 'pool'
|
path: req.url,
|
||||||
// should be emitting this event.
|
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
|
// Add a listener for the connection timeout event
|
||||||
forward_proxy.addListener('error', function (err) {
|
forwardProxy.addListener('error', function (err) {
|
||||||
// Remark: Ignoring this error in the event
|
// Remark: Ignoring this error in the event
|
||||||
// forward target doesn't exist.
|
// forward target doesn't exist.
|
||||||
});
|
});
|
||||||
|
|
||||||
// Chunk the client request body as chunks from the proxied request come in
|
// Chunk the client request body as chunks from the proxied request come in
|
||||||
req.addListener('data', function (chunk) {
|
req.addListener('data', function (chunk) {
|
||||||
forward_proxy.write(chunk, 'binary');
|
forwardProxy.write(chunk, 'binary');
|
||||||
})
|
})
|
||||||
|
|
||||||
// At the end of the client request, we are going to stop the proxied request
|
// At the end of the client request, we are going to stop the proxied request
|
||||||
req.addListener('end', function () {
|
req.addListener('end', function () {
|
||||||
forward_proxy.end();
|
forwardProxy.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
self.unwatch(req);
|
self.unwatch(req);
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
proxyWebSocketRequest: function (port, server, host) {
|
proxyWebSocketRequest: function (port, server, host) {
|
||||||
@ -478,6 +467,3 @@ HttpProxy.prototype = {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.HttpProxy = HttpProxy;
|
|
||||||
exports.ProxyTable = ProxyTable;
|
|
||||||
@ -11,10 +11,9 @@ var fs = require('fs'),
|
|||||||
path = require('path'),
|
path = require('path'),
|
||||||
request = require('request'),
|
request = require('request'),
|
||||||
assert = require('assert'),
|
assert = require('assert'),
|
||||||
helpers = require('./helpers'),
|
helpers = require('./helpers');
|
||||||
TestRunner = helpers.TestRunner;
|
|
||||||
|
|
||||||
var runner = new TestRunner(),
|
var runner = new helpers.TestRunner(),
|
||||||
assertProxiedWithTarget = helpers.assertProxiedWithTarget,
|
assertProxiedWithTarget = helpers.assertProxiedWithTarget,
|
||||||
assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;
|
assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;
|
||||||
|
|
||||||
|
|||||||
@ -63,7 +63,7 @@ exports.assertProxiedWithNoTarget = function (runner, proxyPort, statusCode, cre
|
|||||||
return test;
|
return test;
|
||||||
}
|
}
|
||||||
|
|
||||||
var TestRunner = function () {
|
var TestRunner = exports.TestRunner = function () {
|
||||||
this.testServers = [];
|
this.testServers = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,5 +160,3 @@ TestRunner.prototype.closeServers = function () {
|
|||||||
|
|
||||||
return this.testServers;
|
return this.testServers;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.TestRunner = TestRunner;
|
|
||||||
@ -28,10 +28,9 @@ var vows = require('vows'),
|
|||||||
util = require('util'),
|
util = require('util'),
|
||||||
request = require('request'),
|
request = require('request'),
|
||||||
assert = require('assert'),
|
assert = require('assert'),
|
||||||
helpers = require('./helpers'),
|
helpers = require('./helpers');
|
||||||
TestRunner = helpers.TestRunner;
|
|
||||||
|
|
||||||
var runner = new TestRunner(),
|
var runner = new helpers.TestRunner(),
|
||||||
assertProxiedWithTarget = helpers.assertProxiedWithTarget,
|
assertProxiedWithTarget = helpers.assertProxiedWithTarget,
|
||||||
assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;
|
assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user