[fix] some stuff start debugging proxystream

This commit is contained in:
yawnt 2013-08-20 16:09:37 +02:00
parent 9ab8749a9b
commit d4f0da898e
5 changed files with 68 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules
*.swp
cov
ttest.js

View File

@ -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. ",
@ -42,6 +43,9 @@ 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 {
@ -49,12 +53,14 @@ proxy.createProxyServer = function createProxyServer(options) {
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;
}
};

View File

@ -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(

View File

@ -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) {
@ -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');
};
};
}

View File

@ -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(); });
}
@ -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) {
@ -36,15 +37,56 @@ 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');
@ -60,3 +102,4 @@ ProxyStream.prototype._read = function(size) {
this.push(chunk);
};
module.exports = ProxyStream;