diff --git a/lib/caronte/index.js b/lib/caronte/index.js index 0b9f544..88f7368 100644 --- a/lib/caronte/index.js +++ b/lib/caronte/index.js @@ -39,7 +39,7 @@ function createRightProxy(type) { var event = ev + pass.name.toLowerCase(); self.emit(event + 'begin', req, res); - pass(req, res, options); + pass(req, res, options, self); self.emit(event + 'end'); }); diff --git a/lib/caronte/passes/web.js b/lib/caronte/passes/web.js index ea9e2d9..97c69af 100644 --- a/lib/caronte/passes/web.js +++ b/lib/caronte/passes/web.js @@ -79,17 +79,18 @@ function XHeaders(req, res, options) { * @param {ClientRequest} Req Request object * @param {IncomingMessage} Res Response object * @param {Object} Options Config object passed to the proxy + * @param {Object} Instance Proxy object that emits events * * @api private */ -function stream(req, res, options) { +function stream(req, res, options, instance) { if(options.forward) { - req.pipe(new ForwardStream(options.forward)); + req.pipe(new ForwardStream(options, instance)); } if(options.target) { - return req.pipe(new ProxyStream(res, options)).pipe(res); + return req.pipe(new ProxyStream(options, res, instance)).pipe(res); } res.end(); diff --git a/lib/caronte/streams/forward.js b/lib/caronte/streams/forward.js index f1f85c7..a3fe955 100644 --- a/lib/caronte/streams/forward.js +++ b/lib/caronte/streams/forward.js @@ -20,9 +20,12 @@ module.exports = ForwardStream; * @api private */ -function ForwardStream() { +function ForwardStream(options) { var self = this; + self.options = options; + self.res = res; + Writable.call(this); this.once('pipe', function(pipe) { self.onPipe(pipe) }); @@ -48,10 +51,12 @@ ForwardStream.prototype.onPipe = function(request) { this.forwardReq = (options.ssl ? https : http).request( common.setupOutgoing(options.ssl || {}, options, request) ); + + this.forwardReq.on('error', function() {}); /** Fire and forget */ }; /** - * Closes forwarded request when `pipe` is finished + * Closes forwarded request when `pipe` is finished. * * Examples: * diff --git a/lib/caronte/streams/proxy.js b/lib/caronte/streams/proxy.js index 5112b43..dad2216 100644 --- a/lib/caronte/streams/proxy.js +++ b/lib/caronte/streams/proxy.js @@ -3,7 +3,11 @@ var Duplex = require('stream').Duplex, http = require('http'), https = require('https'); -function ProxyStream() { +function ProxyStream(options, res, instance) { + this.options = options; + this.res = res; + this.instance = instance; + var self = this; Duplex.call(this); @@ -14,26 +18,37 @@ function ProxyStream() { require('util').inherits(ProxyStream, Duplex); -ProxyStream.prototype.onPipe = function(request) { +ProxyStream.prototype.onPipe = function(req) { + this.req = req; + var self = this; this.proxyReq = (options.ssl ? https : http).request( - common.setupOutgoing(options.ssl || {}, options, request) + common.setupOutgoing(options.ssl || {}, options, req) ); - this.proxyReq.once('response', function(response) { - self.onResponse(response); + this.proxyReq.once('response', function(proxyRes) { + self.onResponse(proxyRes); }); - this.proxyReq.on('error', function() {}); // XXX TODO: add error handling -} + this.proxyReq.on('error', function(e) { + self.onError(e); + }); +}; ProxyStream.prototype.onFinish = function() { -} +}; ProxyStream.prototype.onResponse = function(proxyRes) { this.proxyRes = proxyRes; -} +}; + +ProxyStream.prototype.onError = function(e) { + if(this.instance.emit('error', this.req, this.res, e)) return; + + this.res.writeHead(500, { 'Content-Type': 'text/plain' }); + this.res.end('Internal Server Error'); +}; ProxyStream.prototype._write = function(chunk, encoding, callback) { this.proxyReq.write(chunk, encoding, callback);