Skip to content

Commit

Permalink
Add location tag to errors and also close sockets on errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
axiak committed Feb 5, 2012
1 parent df7712d commit 8b58440
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports.createProxyServer = function (opts) {
try {
return wrapped(req, res);
} catch (error) {
emitter.emit('error', error);
emitter.emit('error', error, 'request/response wrapper');
}
};
};
Expand Down Expand Up @@ -151,7 +151,8 @@ module.exports.createProxyServer = function (opts) {
}

proxy_response.on('error', function (error) {
emitter.emit('error', error);
response.end();
emitter.emit('error', error, 'proxyResponse');
});

proxy_response.on('data', function (chunk) {
Expand All @@ -175,7 +176,7 @@ module.exports.createProxyServer = function (opts) {
var encoding = recompress ? responseEncoding : undefined;
var writeOutput = function (error, b) {
if (error) {
emitter.emit('error', error);
emitter.emit('error', error, 'recompressing');
}
response.end(b);
};
Expand All @@ -193,7 +194,7 @@ module.exports.createProxyServer = function (opts) {

var setupIntercept = function (error, newBuffer) {
if (error) {
emitter.emit('error', error);
emitter.emit('error', error, 'decompressing');
}
emitOrRun('interceptResponseContent', function () { writeResponse(newBuffer); },
newBuffer, proxy_response, is_ssl, charset, writeResponse);
Expand Down Expand Up @@ -228,7 +229,10 @@ module.exports.createProxyServer = function (opts) {
}
});

proxy_request.on('error', function (error) { emitter.emit('error', error); });
proxy_request.on('error', function (error) {
response.end();
emitter.emit('error', error, 'proxyRequest', request_info);
});

request.on('data', function (chunk) { proxy_request.write(chunk, 'binary'); });
request.on('end', function (chunk) { proxy_request.end(); });
Expand Down Expand Up @@ -257,8 +261,8 @@ module.exports.createProxyServer = function (opts) {
/* Start http server */
var httpServer = http.createServer(serverDefinition(false));
httpServer.listen(main_port, hostname);
httpServer.on('clientError', function (error) { emitter.emit('clientError', error); });
httpServer.on('error', function (error) { emitter.emit('error', error); });
httpServer.on('clientError', function (error) { emitter.emit('clientError', error, 'proxyClient'); });
httpServer.on('error', function (error) { emitter.emit('error', error, 'proxyServer'); });


/* Parse the ssl options and create sslRouting, sslExact, and sslServers */
Expand Down Expand Up @@ -299,8 +303,8 @@ module.exports.createProxyServer = function (opts) {

var httpsServer = https.createServer(sslOptions, serverDefinition(true));
httpsServer.listen(socketPath);
httpsServer.on('clientError', function (error) { emitter.emit('clientError', error); });
httpsServer.on('error', function (error) { emitter.emit('error', error); });
httpsServer.on('clientError', function (error) { emitter.emit('clientError', error, 'httpsClient'); });
httpsServer.on('error', function (error) { emitter.emit('error', error, 'httpsServer'); });
httpsServers.push(httpsServer);
}

Expand Down Expand Up @@ -336,15 +340,18 @@ module.exports.createProxyServer = function (opts) {
socket.write('HTTP/1.0 200 Connection established\r\n\r\n');
}
} catch (error) {
emitter.emit('error', error);
emitter.emit('error', error, 'httpsSocket');
}
});

clientSocket.on('data', function (data) {
try {
socket.write(data);
} catch (error) {
emitter.emit('error', error);
try {
clientSocket.end();
} catch (error) {}
emitter.emit('error', error, 'httpsSocketData');
}
});

Expand All @@ -354,7 +361,10 @@ module.exports.createProxyServer = function (opts) {
try {
clientSocket.write(data);
} catch (error) {
emitter.emit('error', error);
try {
socket.end();
} catch (error) {}
emitter.emit('error', error, 'httpsClientSocketData');
}
});
socket.on('end', function () { clientSocket.end(); });
Expand Down Expand Up @@ -397,7 +407,7 @@ module.exports.createProxyServer = function (opts) {
server.listen(opts['transSslPort'], hostname);
}, 1000);
} else {
emitter.emit('error', e);
emitter.emit('error', e, 'transSslError');
}
});

Expand Down

0 comments on commit 8b58440

Please sign in to comment.