Merge pull request #482 from srossross/caronte

[merge] https & agent
This commit is contained in:
yawnt 2013-09-21 01:49:00 -07:00
commit 32dcb0449c
7 changed files with 58 additions and 19 deletions

View File

@ -107,10 +107,14 @@ server.listen(5050);
* **target**: url string to be parsed with the url module
* **forward**: url string to be parsed with the url module
* **agent**: object to be passed to http(s).request (see Node's [https agent](http://nodejs.org/api/https.html#https_class_https_agent) and [http agent](http://nodejs.org/api/http.html#http_class_http_agent) objects)
If you are using the `proxyServer.listen` method, the following options are also applicable:
* **ssl**: object to be passed to https.createServer()
* **ws**: true/false, if you want to proxy websockets
* **xfwd**: true/false, adds x-forward headers
* **maxSock**: maximum number of sockets
### Test
@ -146,3 +150,4 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
>THE SOFTWARE.

15
examples/https-secure.js Normal file
View File

@ -0,0 +1,15 @@
var caronte = require('caronte'),
https = require('https');
/*
* Create your proxy server pointing to a secure domain
* Enable ssl validation
*/
var options = {target : 'https://google.com',
agent : https.globalAgent,
headers: {host: 'google.com'}
};
var proxyServer = caronte.createProxyServer(options);
console.log("Proxy server listening on port 8000");
proxyServer.listen(8000);

10
examples/https.js Normal file
View File

@ -0,0 +1,10 @@
var caronte = require('caronte');
/*
* Create your proxy server pointing to a secure domain
*/
var options = {target:'https://google.com'};
var proxyServer = caronte.createProxyServer(options);
console.log("Proxy server listening on port 8000");
proxyServer.listen(8000);

View File

@ -3,11 +3,13 @@ var http = require('http'),
//
// Create your proxy server
//
console.log("Proxy server listening on port 8000");
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);
//
// Create your target server
//
console.log("Web server listening on port 9000");
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));

View File

@ -28,11 +28,10 @@ proxy.createProxyServer = function createProxyServer(options) {
" { ",
" target : <url string to be parsed with the url module> ",
" forward: <url string to be parsed with the url module> ",
" agent : <object to be passed to http(s).request> ",
" ssl : <object to be passed to https.createServer()> ",
" ws : <true/false, if you want to proxy websockets> ",
" xfwd : <true/false, adds x-forward headers> ",
" maxSock: <maximum number of sockets> ",
" agent : <http agent for pooled connections> ",
" } ",
" ",
"NOTE: `options.ws` and `options.ssl` are optional. ",
@ -41,14 +40,6 @@ proxy.createProxyServer = function createProxyServer(options) {
].join("\n"));
}
['target', 'forward'].forEach(function(key) {
if(!options[key]) return;
options[key] = url.parse(options[key]);
options[key].maxSockets = options.maxSock;
options[key].agent = options.agent || false // new (options.ssl ? https.Agent : http.Agent)(options[key].maxSockets || 100);
});
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
return {

View File

@ -1,4 +1,5 @@
var common = exports;
var common = exports,
extend = require('util')._extend;
/**
* Copies the right headers from `options` and `req` to
@ -24,7 +25,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.port = options[forward || 'target'].port ||
(~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? 443 : 80);
['host', 'hostname', 'socketPath', 'agent'].forEach(
['host', 'hostname', 'socketPath'].forEach(
function(e) { outgoing[e] = options[forward || 'target'][e]; }
);
@ -32,6 +33,11 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
function(e) { outgoing[e] = req[e]; }
);
if (options.headers){
extend(outgoing.headers, options.headers);
}
outgoing.agent = options.agent || false;
outgoing.path = req.url;
return outgoing;

View File

@ -7,18 +7,19 @@ describe('lib/caronte/common.js', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent : '?',
target: {
host : 'hey',
hostname : 'how',
socketPath: 'are',
port : 'you',
agent : '?'
}
},
headers: {'fizz': 'bang', 'overwritten':true},
},
{
method : 'i',
url : 'am',
headers : 'proxy'
headers : {'pro':'xy','overwritten':false}
});
expect(outgoing.host).to.eql('hey');
@ -29,18 +30,27 @@ describe('lib/caronte/common.js', function () {
expect(outgoing.method).to.eql('i');
expect(outgoing.path).to.eql('am');
expect(outgoing.headers).to.eql('proxy')
expect(outgoing.headers.pro).to.eql('xy');
expect(outgoing.headers.fizz).to.eql('bang');
expect(outgoing.headers.overwritten).to.eql(true);
});
it('should set the agent to false if none is given', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {target: {},}, {});
expect(outgoing.agent).to.eql(false);
});
it('set the port according to the protocol', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
{
agent : '?',
target: {
host : 'how',
hostname : 'are',
socketPath: 'you',
agent : '?',
protocol: 'https:'
}
},