Skip to content

Commit

Permalink
[fix] some stuff start debugging proxystream
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Aug 20, 2013
1 parent 9ab8749 commit d4f0da8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
*.swp
cov
ttest.js
8 changes: 7 additions & 1 deletion lib/caronte.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ proxy.createProxyServer = function createProxyServer(options) {
" 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> ",
" } ",
" ",
"NOTE: `options.ws` and `options.ssl` are optional. ",
Expand All @@ -42,19 +43,24 @@ proxy.createProxyServer = function createProxyServer(options) {
['target', 'forward'].forEach(function(key) {
if(!options[key]) return;
options[key] = url.parse(options[key]);

options[key].maxSockets = options.maxSock;
options[key].agent = new (options.ssl ? https.Agent : http.Agent)(options[key]);
});

return {
__proto__: new events.EventEmitter2({ wildcard: true, delimiter: ':' }),
web : caronte.createWebProxy(options),
ws : caronte.createWsProxy(options),
listen : function listen(port) {
var server = options.ssl ? http.createServer(this.web) : https.createServer(options.ssl, this.web);
var server = options.ssl ? https.createServer(options.ssl, this.web) : http.createServer(this.web);

if(options.ws) {
server.on('upgrade', this.ws);
}

server.listen(port);

return server;
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/caronte/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var common = exports;
*/

common.setupOutgoing = function(outgoing, options, req) {
['host', 'hostname', 'port', 'socketPath', 'agent'].forEach(
function(e) { outgoing[e] = options[e]; }
['host', 'hostname', 'port', 'socketPath'/*, 'agent'*/].forEach(
function(e) { outgoing[e] = options.target[e]; }
);

['method', 'path', 'headers'].forEach(
Expand Down
13 changes: 7 additions & 6 deletions lib/caronte/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ caronte.createWsProxy = createRightProxy('ws');
*/

function createRightProxy(type) {
passes = type === 'ws' ? ws : web;
var passes = (type === 'ws') ? ws : web;

return function(options) {

passes = Object.keys(passes).map(function(pass) {
Expand All @@ -33,17 +34,17 @@ function createRightProxy(type) {
var self = this,
ev = 'caronte:' + type + ':';

self.emit(ev + 'begin', req, res);
//self.emit(ev + 'begin', req, res);

passes.forEach(function(pass) {
var event = ev + pass.name.toLowerCase();
var evnt = ev + pass.name.toLowerCase();

self.emit(event + 'begin', req, res);
//self.emit(evnt + 'begin', req, res);
pass(req, res, options, self);
self.emit(event + 'end');
//self.emit(evnt + 'end');
});

self.emit(ev + 'end');
//self.emit(ev + 'end');
};
};
}
Expand Down
59 changes: 51 additions & 8 deletions lib/caronte/streams/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var Duplex = require('stream').Duplex,
https = require('https');

function ProxyStream(options, res, instance) {
Duplex.call(this);

this.options = options;
this.res = res;
this.instance = instance;

var self = this;

Duplex.call(this);

this.once('pipe', function(pipe) { self.onPipe(pipe); });
this.once('finish', function() { self.onFinish(); });
}
Expand All @@ -23,11 +23,12 @@ ProxyStream.prototype.onPipe = function(req) {

var self = this;

this.proxyReq = (options.ssl ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req)
this.proxyReq = (self.options.ssl ? https : http).request(
common.setupOutgoing(self.options.ssl || {}, self.options, req)
);

//console.log(common.setupOutgoing(self.options.ssl || {}, self.options, req));
this.proxyReq.once('response', function(proxyRes) {
console.log(proxyRes);
self.onResponse(proxyRes);
});
this.proxyReq.on('error', function(e) {
Expand All @@ -36,16 +37,57 @@ ProxyStream.prototype.onPipe = function(req) {
};

ProxyStream.prototype.onFinish = function() {

this.proxyReq.end();
};

ProxyStream.prototype.onResponse = function(proxyRes) {
this.proxyRes = proxyRes;

// rewrite
if(req.httpVersion === '1.0') {
res.headers.connection = req.headers.connection || 'close';
}
else if(!res.headers.connection) {
res.headers.connection = req.headers.connection || 'keep-alive';
}

if(req.httpVersion === '1.0' || (req.method === 'DELETE' && !req.headers['content-length'])) {
delete res.headers['transfer-encoding'];
}

if(~[301,302].indexOf(res.statusCode) && typeof res.headers.location !== 'undefined') {
var location = url.parse(res.headers.location);
if (
location.host === req.headers.host &&
(
source.https && !target.https ||
target.https && !source.https
)
) {
res.headers.location = res.headers.location.replace(/^https\:/, 'http:');
}
}

self.emit('proxyResponse', req, response, res);

Object.keys(res.headers).forEach(function (key) {
response.setHeader(key, res.headers[key]);
});
response.writeHead(response.statusCode);

res.on('readable', function() {
self.read(0);
});

res.on('end', function() {
self.push(null);
});
self.emit('readable');
};

ProxyStream.prototype.onError = function(e) {
if(this.instance.emit('error', this.req, this.res, e)) return;

if(this.instance.emit('proxyError', this.req, this.res, e)) return;
this.res.writeHead(500, { 'Content-Type': 'text/plain' });
this.res.end('Internal Server Error');
};
Expand All @@ -60,3 +102,4 @@ ProxyStream.prototype._read = function(size) {
this.push(chunk);
};

module.exports = ProxyStream;

0 comments on commit d4f0da8

Please sign in to comment.