prepend request IDs instead of UUIDs, fixes #77

also cleaned up a few other things I noticed when looking over the files
This commit is contained in:
jomo
2015-01-31 17:10:49 +01:00
parent a2fc680b11
commit e6152d5898
11 changed files with 214 additions and 225 deletions

View File

@@ -21,6 +21,7 @@ module.exports = function(req, res) {
var def = req.url.query.default;
var helm = req.url.query.hasOwnProperty("helm");
var etag = null;
var rid = req.id;
// Prevent app from crashing/freezing
if (size < config.min_size || size > config.max_size) {
@@ -43,14 +44,16 @@ module.exports = function(req, res) {
// strip dashes
uuid = uuid.replace(/-/g, "");
logging.log(rid + "uuid: " + uuid);
try {
helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) {
logging.log(uuid + " - " + human_status[status]);
helpers.get_avatar(rid, uuid, helm, size, function(err, status, image, hash) {
logging.log(rid + "storage type: " + human_status[status]);
if (err) {
logging.error(uuid + " " + err);
logging.error(rid + err);
if (err.code == "ENOENT") {
cache.remove_hash(uuid);
// no such file
cache.remove_hash(rid, uuid);
}
}
etag = image && hash && hash.substr(0, 32) || "none";
@@ -62,25 +65,26 @@ module.exports = function(req, res) {
} else if (err) {
http_status = 503;
}
logging.debug(uuid + " etag: " + req.headers["if-none-match"]);
logging.debug(uuid + " matches: " + matches);
sendimage(http_status, status, image, uuid);
logging.debug(rid + "etag: " + req.headers["if-none-match"]);
logging.debug(rid + "matches: " + matches);
sendimage(rid, http_status, status, image);
} else {
handle_default(404, status, uuid);
handle_default(rid, 404, status, uuid);
}
});
} catch(e) {
logging.error(uuid + " error: " + e);
handle_default(500, status, uuid);
logging.error(rid + "error: " + e.stack);
handle_default(rid, 500, -1, uuid);
}
function handle_default(http_status, img_status, uuid) {
function handle_default(rid, http_status, img_status, uuid) {
if (def && def != "steve" && def != "alex") {
logging.log(uuid + " status: 301");
logging.log(rid + "status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Location": def
});
@@ -88,18 +92,19 @@ module.exports = function(req, res) {
} else {
def = def || skins.default_skin(uuid);
skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
sendimage(http_status, img_status, image, uuid);
sendimage(rid, http_status, img_status, image);
});
}
}
function sendimage(http_status, img_status, image, uuid) {
logging.log(uuid + " status: " + http_status);
function sendimage(rid, http_status, img_status, image) {
logging.log(rid + "status: " + http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});

View File

@@ -15,6 +15,7 @@ module.exports = function(req, res) {
var start = new Date();
var uuid = (req.url.pathname.split("/")[2] || "").split(".")[0];
var etag = null;
var rid = req.id;
if (!helpers.uuid_valid(uuid)) {
res.writeHead(422, {
@@ -27,12 +28,17 @@ module.exports = function(req, res) {
// strip dashes
uuid = uuid.replace(/-/g, "");
logging.log(rid + "uuid: " + uuid);
try {
helpers.get_cape(uuid, function(err, status, image, hash) {
logging.log(uuid + " - " + human_status[status]);
helpers.get_cape(rid, uuid, function(err, status, image, hash) {
logging.log(rid + "storage type: " + human_status[status]);
if (err) {
logging.error(uuid + " " + err);
logging.error(rid + err);
if (err.code == "ENOENT") {
// no such file
cache.remove_hash(rid, uuid);
}
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
@@ -43,10 +49,10 @@ module.exports = function(req, res) {
} else if (err) {
http_status = 503;
}
logging.debug("Etag: " + req.headers["if-none-match"]);
logging.debug("matches: " + matches);
logging.log("status: " + http_status);
sendimage(http_status, status, image);
logging.debug(rid + "etag: " + req.headers["if-none-match"]);
logging.debug(rid + "matches: " + matches);
logging.log(rid + "status: " + http_status);
sendimage(rid, http_status, status, image);
} else {
res.writeHead(404, {
"Content-Type": "text/plain",
@@ -56,8 +62,7 @@ module.exports = function(req, res) {
}
});
} catch(e) {
logging.error(uuid + " error:");
logging.error(e);
logging.error(rid + "error:" + e.stack);
res.writeHead(500, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
@@ -65,12 +70,13 @@ module.exports = function(req, res) {
res.end("500 server error");
}
function sendimage(http_status, img_status, image) {
function sendimage(rid, http_status, img_status, image) {
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});

View File

@@ -21,6 +21,7 @@ var human_status = {
module.exports = function(req, res) {
var start = new Date();
var raw_type = (req.url.path_list[2] || "");
var rid = req.id;
// validate type
if (raw_type !== "body" && raw_type !== "head") {
@@ -57,12 +58,17 @@ module.exports = function(req, res) {
// strip dashes
uuid = uuid.replace(/-/g, "");
logging.log(rid + "uuid: " + uuid);
try {
helpers.get_render(uuid, scale, helm, body, function(err, status, hash, image) {
logging.log(uuid + " - " + human_status[status]);
helpers.get_render(rid, uuid, scale, helm, body, function(err, status, hash, image) {
logging.log(rid + "storage type: " + human_status[status]);
if (err) {
logging.error(uuid + " " + err);
logging.error(rid + err);
if (err.code == "ENOENT") {
// no such file
cache.remove_hash(rid, uuid);
}
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
@@ -73,29 +79,30 @@ module.exports = function(req, res) {
} else if (err) {
http_status = 503;
}
logging.debug(uuid + " etag: " + req.headers["if-none-match"]);
logging.debug(uuid + " matches: " + matches);
sendimage(http_status, status, image, uuid);
logging.debug(rid + "etag: " + req.headers["if-none-match"]);
logging.debug(rid + "matches: " + matches);
sendimage(rid, http_status, status, image);
} else {
logging.log(uuid + " image not found, using default.");
handle_default(404, status, uuid);
logging.log(rid + "image not found, using default.");
handle_default(rid, 404, status, uuid);
}
});
} catch(e) {
logging.error(uuid + " error: " + e);
handle_default(500, status, uuid);
logging.error(rid + "error: " + e.stack);
handle_default(rid, 500, -1, uuid);
}
// default alex/steve images can be rendered, but
// custom images will not be
function handle_default(http_status, img_status, uuid) {
function handle_default(rid, http_status, img_status, uuid) {
if (def && def != "steve" && def != "alex") {
logging.log(uuid + " status: 301");
logging.log(rid + "status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Location": def
});
@@ -105,26 +112,27 @@ module.exports = function(req, res) {
fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
if (err) {
// errored while loading the default image, continuing with null image
logging.error(uuid + "error loading default render image: " + err);
logging.error(rid + "error loading default render image: " + err);
}
// we render the default skins, but not custom images
renders.draw_model(uuid, buf, scale, helm, body, function(err, def_img) {
renders.draw_model(rid, buf, scale, helm, body, function(err, def_img) {
if (err) {
logging.log(uuid + "error while rendering default image: " + err);
logging.log(rid + "error while rendering default image: " + err);
}
sendimage(http_status, img_status, def_img, uuid);
sendimage(rid, http_status, img_status, def_img);
});
});
}
}
function sendimage(http_status, img_status, image, uuid) {
logging.log(uuid + " status: " + http_status);
function sendimage(rid, http_status, img_status, image) {
logging.log(rid + "status: " + http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});

View File

@@ -11,6 +11,7 @@ module.exports = function(req, res) {
var uuid = (req.url.path_list[2] || "").split(".")[0];
var def = req.url.query.default;
var etag = null;
var rid = req.id;
if (!helpers.uuid_valid(uuid)) {
res.writeHead(422, {
@@ -23,12 +24,12 @@ module.exports = function(req, res) {
// strip dashes
uuid = uuid.replace(/-/g, "");
logging.log(rid + "uuid: " + uuid);
try {
helpers.get_skin(uuid, function(err, hash, image) {
logging.log(uuid);
helpers.get_skin(rid, uuid, function(err, hash, image) {
if (err) {
logging.error(uuid + " " + err);
logging.error(rid + err);
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
@@ -39,25 +40,26 @@ module.exports = function(req, res) {
} else if (err) {
http_status = 503;
}
logging.debug(uuid + " etag: " + req.headers["if-none-match"]);
logging.debug(uuid + " matches: " + matches);
sendimage(http_status, image, uuid);
logging.debug(rid + "etag: " + req.headers["if-none-match"]);
logging.debug(rid + "matches: " + matches);
sendimage(rid, http_status, image);
} else {
handle_default(404, uuid);
handle_default(rid, 404, uuid);
}
});
} catch(e) {
logging.error(uuid + " error: " + e);
handle_default(500, uuid);
logging.error(rid + "error: " + e.stack);
handle_default(rid, 500, uuid);
}
function handle_default(http_status, uuid) {
function handle_default(rid, http_status, uuid) {
if (def && def != "steve" && def != "alex") {
logging.log(uuid + " status: 301");
logging.log(rid + "status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": "downloaded",
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Location": def
});
@@ -66,19 +68,20 @@ module.exports = function(req, res) {
def = def || skins.default_skin(uuid);
lwip.open("public/images/" + def + "_skin.png", function(err, image) {
image.toBuffer("png", function(err, buffer) {
sendimage(http_status, buffer, uuid);
sendimage(rid, http_status, buffer);
});
});
}
}
function sendimage(http_status, image, uuid) {
logging.log(uuid + " status: " + http_status);
function sendimage(rid, http_status, image) {
logging.log(rid + "status: " + http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": "downloaded",
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});