mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[fix] refactor error handling
This commit is contained in:
parent
b79bd29d5e
commit
601dbcbfe9
@ -2,7 +2,6 @@ var http = require('http'),
|
||||
https = require('https'),
|
||||
url = require('url'),
|
||||
httpProxy = require('./http-proxy/'),
|
||||
events = require('eventemitter2'),
|
||||
proxy = exports;
|
||||
|
||||
/**
|
||||
@ -42,3 +41,4 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option
|
||||
|
||||
return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options));
|
||||
};
|
||||
|
||||
|
||||
@ -89,20 +89,20 @@ function ProxyServer(options, web, ws) {
|
||||
ProxyServer.prototype.listen = function(port) {
|
||||
var self = this,
|
||||
closure = function(req, res) { self.web(req, res); },
|
||||
server = options.ssl ?
|
||||
this._server = options.ssl ?
|
||||
https.createServer(this.options.ssl, closure) :
|
||||
http.createServer(closure);
|
||||
|
||||
if(options.ws) {
|
||||
server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
|
||||
this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
|
||||
}
|
||||
|
||||
server.listen(port);
|
||||
this._server.listen(port);
|
||||
|
||||
return server;
|
||||
return this;
|
||||
};
|
||||
|
||||
ProxyServer.prototype.before = function() {};
|
||||
ProxyServer.prototype.after = function() {};
|
||||
|
||||
require('util').inherits(ProxyServer, EE);
|
||||
require('util').inherits(ProxyServer, EE3);
|
||||
|
||||
@ -89,55 +89,32 @@ web_o = Object.keys(web_o).map(function(pass) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function stream(req, res, options) {
|
||||
function stream(req, res, server) {
|
||||
if(options.forward) {
|
||||
// If forward enable, so just pipe the request
|
||||
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
|
||||
var forwardReq = (server.options.forward.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(server.options.ssl || {}, server.options, req, 'forward')
|
||||
);
|
||||
req.pipe(forwardReq);
|
||||
return res.end();
|
||||
}
|
||||
|
||||
// Request initalization
|
||||
var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
var proxyReq = (server.options.target.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(server.options.ssl || {}, server.options, req)
|
||||
);
|
||||
|
||||
// Error Handler
|
||||
proxyReq.on('error', function(err){
|
||||
var ev = 'http-proxy:outgoing:web:';
|
||||
// If no error listeners, so throw the error.
|
||||
if (!options.ee.listeners(ev + 'error').length){
|
||||
throw err;
|
||||
}
|
||||
// Also emit the error events
|
||||
options.ee.emit(ev + 'error', err, req, res);
|
||||
server.emit('error', err);
|
||||
});
|
||||
|
||||
req.pipe(proxyReq);
|
||||
|
||||
proxyReq.on('response', function(proxyRes) {
|
||||
var ev = 'http-proxy:outgoing:web:';
|
||||
|
||||
options.ee.emit(ev + 'begin', req, res);
|
||||
|
||||
// When the previous request respond, we apply the
|
||||
// outgoing passes to the response
|
||||
web_o.some(function(pass) {
|
||||
var evnt = ev + pass.name.toLowerCase() + ':', val;
|
||||
|
||||
options.ee.emit(evnt + 'begin', req, res);
|
||||
// Call the pass with the proxy response
|
||||
// pass(ClientRequest, IncomingMessage, proxyResponse)
|
||||
val = pass(req, res, proxyRes);
|
||||
options.ee.emit(evnt + 'end');
|
||||
|
||||
return val;
|
||||
});
|
||||
|
||||
options.ee.emit(ev + 'end');
|
||||
|
||||
for(var i=0; i < web_o.length; i++) {
|
||||
if(web_o[i](req, res, proxyRes)) { break; }
|
||||
}
|
||||
|
||||
proxyRes.pipe(res);
|
||||
});
|
||||
|
||||
@ -96,24 +96,18 @@ var passes = exports;
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
function stream(req, socket, options, head) {
|
||||
function stream(req, socket, server, head) {
|
||||
common.setupSocket(socket);
|
||||
|
||||
if (head && head.length) socket.unshift(head);
|
||||
|
||||
|
||||
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
var proxyReq = (~['https:', 'wss:'].indexOf(server.options.target.protocol) ? https : http).request(
|
||||
common.setupOutgoing(server.options.ssl || {}, server.options, req)
|
||||
);
|
||||
// Error Handler
|
||||
proxyReq.on('error', function(err){
|
||||
var ev = 'http-proxy:outgoing:ws:';
|
||||
// If no error listeners, so throw the error.
|
||||
if (!options.ee.listeners(ev + 'error').length){
|
||||
throw err;
|
||||
}
|
||||
// Also emit the error events
|
||||
options.ee.emit(ev + 'error', err, req, socket, head);
|
||||
server.emit('error', err);
|
||||
});
|
||||
|
||||
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user