diff --git a/lib/helpers.js b/lib/helpers.js index e55a872..985ca39 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -210,12 +210,7 @@ exp.id_valid = function(userId) { // decides whether to get a +type+ image for +userId+ from disk or to download it // callback: error, status, hash -// the status gives information about how the image was received -// -1: "error" -// 0: "none" - cached as null -// 1: "cached" - found on disk -// 2: "downloaded" - profile downloaded, skin downloaded from mojang servers -// 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin +// for status, see response.js exp.get_image_hash = function(rid, userId, type, callback) { cache.get_details(userId, function(err, cache_details) { var cached_hash = null; @@ -357,11 +352,11 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) { }; // handles requests for +userId+ capes -// callback: error, cape hash, image buffer +// callback: error, cape hash, status, image buffer exp.get_cape = function(rid, userId, callback) { exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) { if (!cape_hash) { - callback(err, null, null); + callback(err, null, null, null); return; } var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png"); @@ -369,14 +364,14 @@ exp.get_cape = function(rid, userId, callback) { if (exists) { logging.log(rid, "cape already exists, not downloading"); skins.open_skin(rid, capepath, function(skin_err, img) { - callback(skin_err, cape_hash, img); + callback(skin_err || err, cape_hash, status, img); }); } else { networking.save_texture(rid, cape_hash, capepath, function(net_err, response, img) { if (response && response.statusCode === 404) { - callback(net_err, cape_hash, null); + callback(net_err, cape_hash, status, null); } else { - callback(net_err, cape_hash, img); + callback(net_err, cape_hash, status, img); } }); } diff --git a/lib/response.js b/lib/response.js index 7172c46..0db3dcd 100644 --- a/lib/response.js +++ b/lib/response.js @@ -29,7 +29,7 @@ module.exports = function(request, response, result) { }); response.on("finish", function() { - logging.log(request.id, response.statusCode, "(" + human_status[result.status] + ")"); + logging.log(request.id, response.statusCode, "(" + (human_status[result.status] || "-") + ")"); }); response.on("error", function(err) { @@ -51,7 +51,8 @@ module.exports = function(request, response, result) { result.status = -1; } - if (result.status !== undefined) { + if (result.status !== undefined && result.status !== null) { + logging.debug("status: " + result.status); headers["X-Storage-Type"] = human_status[result.status]; } @@ -85,7 +86,7 @@ module.exports = function(request, response, result) { response.writeHead(500, headers); response.end(result.body); } else { - response.writeHead(200, headers); + response.writeHead(result.body ? 200 : 404, headers); response.end(result.body); } }; \ No newline at end of file diff --git a/lib/routes/capes.js b/lib/routes/capes.js index e72fd0e..415e272 100644 --- a/lib/routes/capes.js +++ b/lib/routes/capes.js @@ -1,52 +1,25 @@ var logging = require("../logging"); var helpers = require("../helpers"); -var config = require("../config"); var cache = require("../cache"); -var human_status = { - 0: "none", - 1: "cached", - 2: "downloaded", - 3: "checked", - "-1": "error" -}; - // GET cape request -module.exports = function(req, res) { - var start = new Date(); +module.exports = function(req, callback) { var userId = (req.url.pathname.split("/")[2] || "").split(".")[0]; - var etag = null; var rid = req.id; - 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 + '"' - }); - res.end(http_status === 304 ? null : image); - } - if (!helpers.id_valid(userId)) { - res.writeHead(422, { - "Content-Type": "text/plain", - "Response-Time": new Date() - start + callback({ + status: -2, + body: "Invalid userid" }); - res.end("Invalid ID"); return; } // strip dashes userId = userId.replace(/-/g, ""); - logging.debug(rid, "userid:", userId); try { - helpers.get_cape(rid, userId, function(err, status, image, hash) { - logging.log(rid, "storage type:", human_status[status]); + helpers.get_cape(rid, userId, function(err, hash, status, image) { if (err) { logging.error(rid, err); if (err.code === "ENOENT") { @@ -54,38 +27,18 @@ module.exports = function(req, res) { cache.remove_hash(rid, userId); } } - etag = hash && hash.substr(0, 32) || "none"; - var matches = req.headers["if-none-match"] === '"' + etag + '"'; - if (image) { - var http_status = 200; - if (err) { - http_status = 503; - } - logging.debug(rid, "etag:", req.headers["if-none-match"]); - logging.debug(rid, "matches:", matches); - logging.log(rid, "status:", http_status); - sendimage(rid, matches ? 304 : http_status, status, image); - } else if (matches) { - res.writeHead(304, { - "Etag": '"' + etag + '"', - "Response-Time": new Date() - start - }); - res.end(); - } else { - res.writeHead(404, { - "Content-Type": "text/plain", - "Etag": '"' + etag + '"', - "Response-Time": new Date() - start - }); - res.end("404 not found"); - } + callback({ + status: status, + body: image, + type: image ? "image/png" : undefined, + hash: hash, + err: err + }); }); } catch(e) { - logging.error(rid, "error:" + e.stack); - res.writeHead(500, { - "Content-Type": "text/plain", - "Response-Time": new Date() - start + callback({ + status: -1, + err: e }); - res.end("500 server error"); } }; \ No newline at end of file diff --git a/lib/server.js b/lib/server.js index aa70842..37e7f20 100644 --- a/lib/server.js +++ b/lib/server.js @@ -90,7 +90,9 @@ function requestHandler(req, res) { }); break; case "capes": - routes.capes(req, res); + routes.capes(req, function(result) { + response(req, res, result); + }); break; default: asset_request(req, res);