Add more safeguards for socket errors

This commit is contained in:
Calvin Montgomery 2015-01-06 10:54:14 -05:00
parent cd22570c40
commit 414cbfdc5d
3 changed files with 37 additions and 11 deletions

View File

@ -34,6 +34,12 @@ const CONTENT_TYPES = {
var urlRetrieve = function (transport, options, callback) { var urlRetrieve = function (transport, options, callback) {
var req = transport.request(options, function (res) { var req = transport.request(options, function (res) {
res.on("error", function (err) {
Logger.errlog.log("HTTP response " + options.host + options.path + " failed: "+
err);
callback(503, "");
});
var buffer = ""; var buffer = "";
res.setEncoding("utf-8"); res.setEncoding("utf-8");
res.on("data", function (chunk) { res.on("data", function (chunk) {
@ -979,18 +985,16 @@ function vimeoWorkaround(id, cb) {
} }
}; };
http.get(options, function (res) { urlRetrieve(http, options, function (status, buffer) {
res.setEncoding("utf-8"); if (status !== 200) {
var buffer = ""; setImmediate(function () {
cb({});
res.on("data", function (data) {
buffer += data;
}); });
return;
}
res.on("end", function () {
parse(buffer); parse(buffer);
}); });
});
}; };
inner(); inner();

View File

@ -217,7 +217,15 @@ module.exports = {
if (id in srv.servers) { if (id in srv.servers) {
io.attach(srv.servers[id]); io.attach(srv.servers[id]);
} else { } else {
io.attach(require("http").createServer().listen(bind.port, bind.ip)); var server = require("http").createServer().listen(bind.port, bind.ip);
server.on("clientError", function (err, socket) {
Logger.errlog.log("clientError on " + id + " - " + err);
try {
socket.destroy();
} catch (e) {
}
});
io.attach(server);
} }
bound[id] = null; bound[id] = null;

View File

@ -85,8 +85,22 @@ var Server = function () {
if (bind.https && Config.get("https.enabled")) { if (bind.https && Config.get("https.enabled")) {
self.servers[id] = https.createServer(opts, self.express) self.servers[id] = https.createServer(opts, self.express)
.listen(bind.port, bind.ip); .listen(bind.port, bind.ip);
self.servers[id].on("clientError", function (err, socket) {
Logger.errlog.log("clientError on " + id + " - " + err);
try {
socket.destroy();
} catch (e) {
}
});
} else if (bind.http) { } else if (bind.http) {
self.servers[id] = self.express.listen(bind.port, bind.ip); self.servers[id] = self.express.listen(bind.port, bind.ip);
self.servers[id].on("clientError", function (err, socket) {
Logger.errlog.log("clientError on " + id + " - " + err);
try {
socket.destroy();
} catch (e) {
}
});
} }
}); });