diff --git a/lib/http-proxy/index.js b/lib/http-proxy/index.js index 5c6e7e0..83e1993 100644 --- a/lib/http-proxy/index.js +++ b/lib/http-proxy/index.js @@ -35,10 +35,17 @@ function createRightProxy(type) { }); return function(req, res /*, [head], [opts] */) { - var self = this, + var passes = this.passes || passes, args = [].slice.call(arguments), cntr = args.length - 1, - head; + head, cbl; + + /* optional args parse begin */ + if(typeof args[cntr] === 'function') { + cbl = args[cntr]; + + cntr--; + } if( !(args[cntr] instanceof Buffer) && @@ -56,6 +63,8 @@ function createRightProxy(type) { head = args[cntr]; } + /* optional args parse end */ + ['target', 'forward'].forEach(function(e) { if (typeof options[e] === 'string') options[e] = parse_url(options[e]); @@ -71,7 +80,7 @@ function createRightProxy(type) { * refer to the connection socket * pass(req, socket, options, head) */ - if(passes[i](req, res, this, head)) { // passes can return a truthy value to halt the loop + if(passes[i](req, res, cbl ? this : false, head, cbl)) { // passes can return a truthy value to halt the loop break; } } @@ -84,6 +93,10 @@ function ProxyServer(options, web, ws) { this.web = web; this.ws = ws; this.options = options; + + this.passes = Object.keys(passes).map(function(pass) { + return passes[pass]; + }); } ProxyServer.prototype.listen = function(port) { @@ -102,7 +115,25 @@ ProxyServer.prototype.listen = function(port) { return this; }; -ProxyServer.prototype.before = function() {}; -ProxyServer.prototype.after = function() {}; +ProxyServer.prototype.before = function(passName, callback) { + var i = false; + this.passes.forEach(function(v, idx) { + if(v.name === passName) i = idx; + }) + + if(!i) throw new Error('No such pass'); + + this.passes.splice(i, 0, callback); +}; +ProxyServer.prototype.after = function(passName, callback) { + var i = false; + this.passes.forEach(function(v, idx) { + if(v.name === passName) i = idx; + }) + + if(!i) throw new Error('No such pass'); + + this.passes.splice(i++, 0, callback); +}; require('util').inherits(ProxyServer, EE3); diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 13d1c01..b8f936d 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -89,8 +89,8 @@ web_o = Object.keys(web_o).map(function(pass) { * @api private */ - function stream(req, res, server) { - if(options.forward) { + function stream(req, res, server, _, clb) { + if(server.options.forward) { // If forward enable, so just pipe the request var forwardReq = (server.options.forward.protocol === 'https:' ? https : http).request( common.setupOutgoing(server.options.ssl || {}, server.options, req, 'forward') @@ -106,7 +106,12 @@ web_o = Object.keys(web_o).map(function(pass) { // Error Handler proxyReq.on('error', function(err){ - server.emit('error', err); + if(server) { + server.emit('error', err); + } + else {  + clb(err); + } }); req.pipe(proxyReq); diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 47ed638..66c75d5 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -96,7 +96,7 @@ var passes = exports; * * @api private */ - function stream(req, socket, server, head) { + function stream(req, socket, server, head, clb) { common.setupSocket(socket); if (head && head.length) socket.unshift(head); @@ -107,7 +107,12 @@ var passes = exports; ); // Error Handler proxyReq.on('error', function(err){ - server.emit('error', err); + if(server) { + server.emit('error', err); + } + else { + clb(err); + } }); proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {