Merge branch 'agent' into caronte

This commit is contained in:
srossross 2013-09-17 11:51:17 -07:00
commit 4ee96ddd66
4 changed files with 76 additions and 9 deletions

55
examples/https.js Normal file
View File

@ -0,0 +1,55 @@
var http = require('http')
, https = require('https')
, caronte = require('caronte')
;
//
// Create your proxy server
//
var options = {target:'https://google.com',
agent: new https.Agent({rejectUnauthorized:false}),
};
var proxyServer = caronte.createProxyServer(options);
proxyServer.ee.on('*:error', function(err, req, res){
res.end('There was an error proxying your request');
});
console.log("Proxy server listening on port 8000");
proxyServer.listen(8000);
//
// Create your proxy server
//
var options2 = {target:'https://google.com',
headers: {'host':'google.com'},
};
var proxyServer2 = caronte.createProxyServer(options2);
proxyServer2.ee.on('*:error', function(err, req, res){
res.end('There was an error proxying your request');
});
console.log("Proxy server 2 listening on port 8001");
proxyServer2.listen(8001);
//
// Create your proxy server
//
var options3 = {target:'https://google.com',
xfwd:true};
var proxyServer3 = caronte.createProxyServer(options3);
proxyServer3.ee.on('*:error', function(err, req, res){
res.end('There was an error proxying your request');
});
console.log("Proxy server 3 listening on port 8002");
proxyServer3.listen(8002);

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,6 +28,7 @@ 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> ",
@ -41,14 +42,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,8 @@
var common = exports;
var common = exports
, http = require('http')
, https = require('https')
, extend = require('util')._extend
;
/**
* Copies the right headers from `options` and `req` to
@ -32,6 +36,19 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
function(e) { outgoing[e] = req[e]; }
);
if (options.headers){
extend(outgoing.headers, options.headers);
}
if (options.agent){
outgoing.agent = options.agent;
}
if (!outgoing.agent){
var Agent = (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? https.Agent : http.Agent);
outgoing.agent = new Agent(options.maxSock || 100);
}
outgoing.path = req.url;
return outgoing;