From d4f0da898e5e8a2d6740e50a7fc34576435e1132 Mon Sep 17 00:00:00 2001 From: yawnt Date: Tue, 20 Aug 2013 16:09:37 +0200 Subject: [PATCH] [fix] some stuff start debugging proxystream --- .gitignore | 1 + lib/caronte.js | 8 ++++- lib/caronte/common.js | 4 +-- lib/caronte/index.js | 13 ++++---- lib/caronte/streams/proxy.js | 59 +++++++++++++++++++++++++++++++----- 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 1a07e33df..9ace78b32 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules *.swp cov +ttest.js diff --git a/lib/caronte.js b/lib/caronte.js index c05701c91..81a25e469 100644 --- a/lib/caronte.js +++ b/lib/caronte.js @@ -31,6 +31,7 @@ proxy.createProxyServer = function createProxyServer(options) { " ssl : ", " ws : ", " xfwd : ", + " maxSock: ", " } ", " ", "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; } }; diff --git a/lib/caronte/common.js b/lib/caronte/common.js index 7028bb171..59a1cf94b 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -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( diff --git a/lib/caronte/index.js b/lib/caronte/index.js index 88f7368a7..bf6160976 100644 --- a/lib/caronte/index.js +++ b/lib/caronte/index.js @@ -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'); }; }; } diff --git a/lib/caronte/streams/proxy.js b/lib/caronte/streams/proxy.js index dad221639..6d67218f0 100644 --- a/lib/caronte/streams/proxy.js +++ b/lib/caronte/streams/proxy.js @@ -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,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'); }; @@ -60,3 +102,4 @@ ProxyStream.prototype._read = function(size) { this.push(chunk); }; +module.exports = ProxyStream; \ No newline at end of file