Skip to content

Commit

Permalink
[fix] callback as optional error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Oct 9, 2013
1 parent 601dbcb commit c7924e0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
41 changes: 36 additions & 5 deletions lib/http-proxy/index.js
Expand Up @@ -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) &&
Expand All @@ -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]);
Expand All @@ -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;
}
}
Expand All @@ -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) {
Expand All @@ -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);
11 changes: 8 additions & 3 deletions lib/http-proxy/passes/web-incoming.js
Expand Up @@ -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')
Expand All @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions lib/http-proxy/passes/ws-incoming.js
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit c7924e0

Please sign in to comment.