diff --git a/routes/avatars.js b/routes/avatars.js index cd1d01b..301b862 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -36,6 +36,26 @@ module.exports = function(req, res) { res.end(http_status === 304 ? null : image); } + function handle_default(rid, http_status, img_status, userId) { + if (def && def !== "steve" && def !== "alex") { + 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 + }); + res.end(); + } else { + def = def || skins.default_skin(userId); + skins.resize_img("public/images/" + def + ".png", size, function(err, image) { + sendimage(rid, http_status, img_status, image); + }); + } + } + // Prevent app from crashing/freezing if (size < config.min_size || size > config.max_size) { // "Unprocessable Entity", valid request, but semantically erroneous: @@ -74,40 +94,18 @@ module.exports = function(req, res) { var matches = req.headers["if-none-match"] === '"' + etag + '"'; if (image) { var http_status = 200; - if (matches) { - http_status = 304; - } else if (err) { + if (err) { http_status = 503; } logging.debug(rid, "etag:", req.headers["if-none-match"]); logging.debug(rid, "matches:", matches); - sendimage(rid, http_status, status, image); + sendimage(rid, matches ? 304 : http_status, status, image); } else { - handle_default(rid, 200, status, userId); + handle_default(rid, matches ? 304 : 200, status, userId); } }); } catch(e) { logging.error(rid, "error:", e.stack); handle_default(rid, 500, -1, userId); } - - function handle_default(rid, http_status, img_status, userId) { - if (def && def !== "steve" && def !== "alex") { - 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 - }); - res.end(); - } else { - def = def || skins.default_skin(userId); - skins.resize_img("public/images/" + def + ".png", size, function(err, image) { - sendimage(rid, http_status, img_status, image); - }); - } - } }; \ No newline at end of file diff --git a/routes/capes.js b/routes/capes.js index 7c6c3b6..5f745a5 100644 --- a/routes/capes.js +++ b/routes/capes.js @@ -58,18 +58,23 @@ module.exports = function(req, res) { var matches = req.headers["if-none-match"] === '"' + etag + '"'; if (image) { var http_status = 200; - if (matches) { - http_status = 304; - } else if (err) { + 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, http_status, status, image); + 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"); diff --git a/routes/renders.js b/routes/renders.js index 03bdfe1..ccd808a 100644 --- a/routes/renders.js +++ b/routes/renders.js @@ -121,17 +121,15 @@ module.exports = function(req, res) { var matches = req.headers["if-none-match"] === '"' + etag + '"'; if (image) { var http_status = 200; - if (matches) { - http_status = 304; - } else if (err) { + if (err) { http_status = 503; } logging.debug(rid, "etag:", req.headers["if-none-match"]); logging.debug(rid, "matches:", matches); - sendimage(rid, http_status, status, image); + sendimage(rid, matches ? 304 : http_status, status, image); } else { logging.log(rid, "image not found, using default."); - handle_default(rid, 200, status, userId); + handle_default(rid, matches ? 304 : 200, status, userId); } }); } catch(e) { diff --git a/routes/skins.js b/routes/skins.js index d4b2cb7..9bc58e7 100644 --- a/routes/skins.js +++ b/routes/skins.js @@ -72,14 +72,12 @@ module.exports = function(req, res) { var matches = req.headers["if-none-match"] === '"' + etag + '"'; if (image) { var http_status = 200; - if (matches) { - http_status = 304; - } else if (err) { + if (err) { http_status = 503; } logging.debug(rid, "etag:", req.headers["if-none-match"]); logging.debug(rid, "matches:", matches); - sendimage(rid, http_status, image); + sendimage(rid, matches ? 304 : http_status, image); } else { handle_default(rid, 200, userId); }