Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[fix] refactor error handling
  • Loading branch information
yawnt committed Oct 9, 2013
1 parent b79bd29 commit 601dbcb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/http-proxy.js
Expand Up @@ -2,7 +2,6 @@ var http = require('http'),
https = require('https'),
url = require('url'),
httpProxy = require('./http-proxy/'),
events = require('eventemitter2'),
proxy = exports;

/**
Expand Down Expand Up @@ -42,3 +41,4 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option

return new ProxyServer(options, httpProxy.createWebProxy(options), httpProxy.createWsProxy(options));
};

10 changes: 5 additions & 5 deletions lib/http-proxy/index.js
Expand Up @@ -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);
41 changes: 9 additions & 32 deletions lib/http-proxy/passes/web-incoming.js
Expand Up @@ -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);
});
Expand Down
14 changes: 4 additions & 10 deletions lib/http-proxy/passes/ws-incoming.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 601dbcb

Please sign in to comment.