mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[fix] callback as optional error handler
This commit is contained in:
parent
601dbcbfe9
commit
c7924e01f9
@ -35,10 +35,17 @@ function createRightProxy(type) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return function(req, res /*, [head], [opts] */) {
|
return function(req, res /*, [head], [opts] */) {
|
||||||
var self = this,
|
var passes = this.passes || passes,
|
||||||
args = [].slice.call(arguments),
|
args = [].slice.call(arguments),
|
||||||
cntr = args.length - 1,
|
cntr = args.length - 1,
|
||||||
head;
|
head, cbl;
|
||||||
|
|
||||||
|
/* optional args parse begin */
|
||||||
|
if(typeof args[cntr] === 'function') {
|
||||||
|
cbl = args[cntr];
|
||||||
|
|
||||||
|
cntr--;
|
||||||
|
}
|
||||||
|
|
||||||
if(
|
if(
|
||||||
!(args[cntr] instanceof Buffer) &&
|
!(args[cntr] instanceof Buffer) &&
|
||||||
@ -56,6 +63,8 @@ function createRightProxy(type) {
|
|||||||
head = args[cntr];
|
head = args[cntr];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* optional args parse end */
|
||||||
|
|
||||||
['target', 'forward'].forEach(function(e) {
|
['target', 'forward'].forEach(function(e) {
|
||||||
if (typeof options[e] === 'string')
|
if (typeof options[e] === 'string')
|
||||||
options[e] = parse_url(options[e]);
|
options[e] = parse_url(options[e]);
|
||||||
@ -71,7 +80,7 @@ function createRightProxy(type) {
|
|||||||
* refer to the connection socket
|
* refer to the connection socket
|
||||||
* pass(req, socket, options, head)
|
* 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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,6 +93,10 @@ function ProxyServer(options, web, ws) {
|
|||||||
this.web = web;
|
this.web = web;
|
||||||
this.ws = ws;
|
this.ws = ws;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
|
||||||
|
this.passes = Object.keys(passes).map(function(pass) {
|
||||||
|
return passes[pass];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ProxyServer.prototype.listen = function(port) {
|
ProxyServer.prototype.listen = function(port) {
|
||||||
@ -102,7 +115,25 @@ ProxyServer.prototype.listen = function(port) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
ProxyServer.prototype.before = function() {};
|
ProxyServer.prototype.before = function(passName, callback) {
|
||||||
ProxyServer.prototype.after = function() {};
|
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);
|
require('util').inherits(ProxyServer, EE3);
|
||||||
|
|||||||
@ -89,8 +89,8 @@ web_o = Object.keys(web_o).map(function(pass) {
|
|||||||
* @api private
|
* @api private
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function stream(req, res, server) {
|
function stream(req, res, server, _, clb) {
|
||||||
if(options.forward) {
|
if(server.options.forward) {
|
||||||
// If forward enable, so just pipe the request
|
// If forward enable, so just pipe the request
|
||||||
var forwardReq = (server.options.forward.protocol === 'https:' ? https : http).request(
|
var forwardReq = (server.options.forward.protocol === 'https:' ? https : http).request(
|
||||||
common.setupOutgoing(server.options.ssl || {}, server.options, req, 'forward')
|
common.setupOutgoing(server.options.ssl || {}, server.options, req, 'forward')
|
||||||
@ -106,7 +106,12 @@ web_o = Object.keys(web_o).map(function(pass) {
|
|||||||
|
|
||||||
// Error Handler
|
// Error Handler
|
||||||
proxyReq.on('error', function(err){
|
proxyReq.on('error', function(err){
|
||||||
server.emit('error', err);
|
if(server) {
|
||||||
|
server.emit('error', err);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
clb(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
req.pipe(proxyReq);
|
req.pipe(proxyReq);
|
||||||
|
|||||||
@ -96,7 +96,7 @@ var passes = exports;
|
|||||||
*
|
*
|
||||||
* @api private
|
* @api private
|
||||||
*/
|
*/
|
||||||
function stream(req, socket, server, head) {
|
function stream(req, socket, server, head, clb) {
|
||||||
common.setupSocket(socket);
|
common.setupSocket(socket);
|
||||||
|
|
||||||
if (head && head.length) socket.unshift(head);
|
if (head && head.length) socket.unshift(head);
|
||||||
@ -107,7 +107,12 @@ var passes = exports;
|
|||||||
);
|
);
|
||||||
// Error Handler
|
// Error Handler
|
||||||
proxyReq.on('error', function(err){
|
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) {
|
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user