always use crc32 for etag, much more reliable than mojang skin hash

had to make quite a few changes to tests to prevent them from failing
also, etag is now only sent with a 200 response, as defined in RFC7232
This commit is contained in:
jomo 2015-12-16 00:47:51 +01:00
parent caeb9a52fe
commit 1144b6755a
3 changed files with 81 additions and 162 deletions

View File

@ -13,40 +13,6 @@ var mojang_urls = [skins_url, capes_url];
var exp = {}; var exp = {};
// extracts the +type+ [SKIN|CAPE] URL
// from the nested & encoded +profile+ object
// returns the URL or null if not present
function extract_url(profile, type) {
var url = null;
if (profile && profile.properties) {
profile.properties.forEach(function(prop) {
if (prop.name === "textures") {
var json = new Buffer(prop.value, "base64").toString();
var props = JSON.parse(json);
url = Object.get(props, "textures." + type + ".url") || null;
}
});
}
return url;
}
// extracts the +type+ [SKIN|CAPE] URL
// from the nested & encoded +profile+ object
// returns the if the model is "slim"
function extract_model(profile) {
var slim = null;
if (profile && profile.properties) {
profile.properties.forEach(function(prop) {
if (prop.name === "textures") {
var json = new Buffer(prop.value, "base64").toString();
var props = JSON.parse(json);
slim = Object.get(props, "textures.SKIN.metadata.model");
}
});
}
return slim === "slim";
}
// helper method that calls `get_username_url` or `get_uuid_info` based on the +usedId+ // helper method that calls `get_username_url` or `get_uuid_info` based on the +usedId+
// +userId+ is used for usernames, while +profile+ is used for UUIDs // +userId+ is used for usernames, while +profile+ is used for UUIDs
// callback: error, url, slim // callback: error, url, slim

View File

@ -63,19 +63,15 @@ module.exports = function(request, response, result) {
headers["X-Storage-Type"] = human_status[result.status]; headers["X-Storage-Type"] = human_status[result.status];
} }
if (result.body) { // use crc32 as a hash function for Etag
// use Mojang's image hash if available var etag = "\"" + crc(result.body || "") + "\"";
// use crc32 as a hash function otherwise
var etag = result.hash && result.hash.substr(0, 10) || crc(result.body);
headers.Etag = "\"" + etag + "\"";
// handle etag caching // handle etag caching
var incoming_etag = request.headers["if-none-match"]; var incoming_etag = request.headers["if-none-match"];
if (incoming_etag && incoming_etag === headers.Etag) { if (incoming_etag && incoming_etag === etag) {
response.writeHead(304, headers); response.writeHead(304, headers);
response.end(); response.end();
return; return;
}
} }
if (result.redirect) { if (result.redirect) {
@ -87,12 +83,16 @@ module.exports = function(request, response, result) {
if (result.status === -2) { if (result.status === -2) {
response.writeHead(result.code || 422, headers); response.writeHead(result.code || 422, headers);
response.end(result.body);
} else if (result.status === -1) { } else if (result.status === -1) {
response.writeHead(500, headers); response.writeHead(500, headers);
response.end(result.body);
} else { } else {
response.writeHead(result.body ? 200 : 404, headers); if (result.body) {
response.end(result.body); headers.Etag = etag;
response.writeHead(200, headers);
} else {
response.writeHead(404, headers);
}
} }
response.end(result.body);
}; };

View File

@ -236,7 +236,6 @@ describe("Crafatar", function() {
assert.ifError(error); assert.ifError(error);
assert.ifError(body); assert.ifError(body);
assert.equal(res.statusCode, 304); assert.equal(res.statusCode, 304);
assert(res.headers.etag);
assert_headers(res); assert_headers(res);
callback(); callback();
}); });
@ -341,440 +340,396 @@ describe("Crafatar", function() {
var server_tests = { var server_tests = {
"avatar with existing username": { "avatar with existing username": {
url: "http://localhost:3000/avatars/jeb_?size=16", url: "http://localhost:3000/avatars/jeb_?size=16",
etag: '"a846b82963"', crc32: [1623808067]
crc32: 1623808067
}, },
"avatar with non-existent username": { "avatar with non-existent username": {
url: "http://localhost:3000/avatars/0?size=16", url: "http://localhost:3000/avatars/0?size=16",
etag: '"mhf_steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"avatar with non-existent username defaulting to mhf_alex": { "avatar with non-existent username defaulting to mhf_alex": {
url: "http://localhost:3000/avatars/0?size=16&default=mhf_alex", url: "http://localhost:3000/avatars/0?size=16&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"avatar with non-existent username defaulting to username": { "avatar with non-existent username defaulting to username": {
url: "http://localhost:3000/avatars/0?size=16&default=jeb_", url: "http://localhost:3000/avatars/0?size=16&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/avatars/jeb_?size=16" redirect: "/avatars/jeb_?size=16"
}, },
"avatar with non-existent username defaulting to uuid": { "avatar with non-existent username defaulting to uuid": {
url: "http://localhost:3000/avatars/0?size=16&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/avatars/0?size=16&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16" redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16"
}, },
"avatar with non-existent username defaulting to url": { "avatar with non-existent username defaulting to url": {
url: "http://localhost:3000/avatars/0?size=16&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/avatars/0?size=16&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"overlay avatar with existing username": { "overlay avatar with existing username": {
url: "http://localhost:3000/avatars/jeb_?size=16&overlay", url: "http://localhost:3000/avatars/jeb_?size=16&overlay",
etag: '"a846b82963"', crc32: [646871998]
crc32: 646871998
}, },
"overlay avatar with non-existent username": { "overlay avatar with non-existent username": {
url: "http://localhost:3000/avatars/0?size=16&overlay", url: "http://localhost:3000/avatars/0?size=16&overlay",
etag: '"mhf_steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"overlay avatar with non-existent username defaulting to mhf_alex": { "overlay avatar with non-existent username defaulting to mhf_alex": {
url: "http://localhost:3000/avatars/0?size=16&overlay&default=mhf_alex", url: "http://localhost:3000/avatars/0?size=16&overlay&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"overlay avatar with non-existent username defaulting to username": { "overlay avatar with non-existent username defaulting to username": {
url: "http://localhost:3000/avatars/0?size=16&overlay&default=jeb_", url: "http://localhost:3000/avatars/0?size=16&overlay&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/avatars/jeb_?size=16&overlay=" redirect: "/avatars/jeb_?size=16&overlay="
}, },
"overlay avatar with non-existent username defaulting to uuid": { "overlay avatar with non-existent username defaulting to uuid": {
url: "http://localhost:3000/avatars/0?size=16&overlay&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/avatars/0?size=16&overlay&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&overlay=" redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&overlay="
}, },
"overlay avatar with non-existent username defaulting to url": { "overlay avatar with non-existent username defaulting to url": {
url: "http://localhost:3000/avatars/0?size=16&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/avatars/0?size=16&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"avatar with existing uuid": { "avatar with existing uuid": {
url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16", url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16",
etag: '"a846b82963"', crc32: [1623808067]
crc32: 1623808067
}, },
"avatar with non-existent uuid": { "avatar with non-existent uuid": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16",
etag: '"mhf_steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"avatar with non-existent uuid defaulting to mhf_alex": { "avatar with non-existent uuid defaulting to mhf_alex": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=mhf_alex", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"avatar with non-existent uuid defaulting to username": { "avatar with non-existent uuid defaulting to username": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=jeb_", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/avatars/jeb_?size=16" redirect: "/avatars/jeb_?size=16"
}, },
"avatar with non-existent uuid defaulting to uuid": { "avatar with non-existent uuid defaulting to uuid": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16" redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16"
}, },
"avatar with non-existent uuid defaulting to url": { "avatar with non-existent uuid defaulting to url": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"overlay avatar with existing uuid": { "overlay avatar with existing uuid": {
url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&overlay", url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&overlay",
etag: '"a846b82963"', crc32: [646871998]
crc32: 646871998
}, },
"overlay avatar with non-existent uuid": { "overlay avatar with non-existent uuid": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&overlay", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&overlay",
etag: '"mhf_steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"overlay avatar with non-existent uuid defaulting to mhf_alex": { "overlay avatar with non-existent uuid defaulting to mhf_alex": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&overlay&default=mhf_alex", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&overlay&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"overlay avatar with non-existent uuid defaulting to username": { "overlay avatar with non-existent uuid defaulting to username": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=jeb_", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/avatars/jeb_?size=16" redirect: "/avatars/jeb_?size=16"
}, },
"overlay avatar with non-existent uuid defaulting to uuid": { "overlay avatar with non-existent uuid defaulting to uuid": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16" redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16"
}, },
"overlay avatar with non-existent uuid defaulting to url": { "overlay avatar with non-existent uuid defaulting to url": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"cape with existing username": { "cape with existing username": {
url: "http://localhost:3000/capes/jeb_", url: "http://localhost:3000/capes/jeb_",
etag: '"3f688e0e69"',
crc32: [989800403, 1901140141] crc32: [989800403, 1901140141]
}, },
"cape with non-existent username": { "cape with non-existent username": {
url: "http://localhost:3000/capes/0", url: "http://localhost:3000/capes/0",
crc32: 0 crc32: [0]
}, },
"cape with non-existent username defaulting to url": { "cape with non-existent username defaulting to url": {
url: "http://localhost:3000/capes/0?default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/capes/0?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"cape with existing uuid": { "cape with existing uuid": {
url: "http://localhost:3000/capes/853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/capes/853c80ef3c3749fdaa49938b674adae6",
etag: '"3f688e0e69"',
crc32: [989800403, 1901140141] crc32: [989800403, 1901140141]
}, },
"cape with non-existent uuid": { "cape with non-existent uuid": {
url: "http://localhost:3000/capes/00000000000000000000000000000000", url: "http://localhost:3000/capes/00000000000000000000000000000000",
crc32: 0 crc32: [0]
}, },
"cape with non-existent uuid defaulting to url": { "cape with non-existent uuid defaulting to url": {
url: "http://localhost:3000/capes/00000000000000000000000000000000?default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/capes/00000000000000000000000000000000?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"skin with existing username": { "skin with existing username": {
url: "http://localhost:3000/skins/jeb_", url: "http://localhost:3000/skins/jeb_",
etag: '"a846b82963"', crc32: [26500336]
crc32: 26500336
}, },
"skin with non-existent username": { "skin with non-existent username": {
url: "http://localhost:3000/skins/0", url: "http://localhost:3000/skins/0",
etag: '"mhf_steve"', crc32: [981937087]
crc32: 981937087
}, },
"skin with non-existent username defaulting to mhf_alex": { "skin with non-existent username defaulting to mhf_alex": {
url: "http://localhost:3000/skins/0?default=mhf_alex", url: "http://localhost:3000/skins/0?default=mhf_alex",
etag: '"mhf_alex"', crc32: [2298915739]
crc32: 2298915739
}, },
"skin with non-existent username defaulting to username": { "skin with non-existent username defaulting to username": {
url: "http://localhost:3000/skins/0?size=16&default=jeb_", url: "http://localhost:3000/skins/0?size=16&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/skins/jeb_?size=16" redirect: "/skins/jeb_?size=16"
}, },
"skin with non-existent username defaulting to uuid": { "skin with non-existent username defaulting to uuid": {
url: "http://localhost:3000/skins/0?size=16&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/skins/0?size=16&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/skins/853c80ef3c3749fdaa49938b674adae6?size=16" redirect: "/skins/853c80ef3c3749fdaa49938b674adae6?size=16"
}, },
"skin with non-existent username defaulting to url": { "skin with non-existent username defaulting to url": {
url: "http://localhost:3000/skins/0?default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/skins/0?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"skin with existing uuid": { "skin with existing uuid": {
url: "http://localhost:3000/skins/853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/skins/853c80ef3c3749fdaa49938b674adae6",
etag: '"a846b82963"', crc32: [26500336]
crc32: 26500336
}, },
"skin with non-existent uuid": { "skin with non-existent uuid": {
url: "http://localhost:3000/skins/00000000000000000000000000000000", url: "http://localhost:3000/skins/00000000000000000000000000000000",
etag: '"mhf_steve"', crc32: [981937087]
crc32: 981937087
}, },
"skin with non-existent uuid defaulting to mhf_alex": { "skin with non-existent uuid defaulting to mhf_alex": {
url: "http://localhost:3000/skins/00000000000000000000000000000000?default=mhf_alex", url: "http://localhost:3000/skins/00000000000000000000000000000000?default=mhf_alex",
etag: '"mhf_alex"', crc32: [2298915739]
crc32: 2298915739
}, },
"skin with non-existent uuid defaulting to username": { "skin with non-existent uuid defaulting to username": {
url: "http://localhost:3000/skins/00000000000000000000000000000000?size=16&default=jeb_", url: "http://localhost:3000/skins/00000000000000000000000000000000?size=16&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/skins/jeb_?size=16" redirect: "/skins/jeb_?size=16"
}, },
"skin with non-existent uuid defaulting to uuid": { "skin with non-existent uuid defaulting to uuid": {
url: "http://localhost:3000/skins/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/skins/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/skins/853c80ef3c3749fdaa49938b674adae6?size=16" redirect: "/skins/853c80ef3c3749fdaa49938b674adae6?size=16"
}, },
"skin with non-existent uuid defaulting to url": { "skin with non-existent uuid defaulting to url": {
url: "http://localhost:3000/skins/00000000000000000000000000000000?default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/skins/00000000000000000000000000000000?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"head render with existing username": { "head render with existing username": {
url: "http://localhost:3000/renders/head/jeb_?scale=2", url: "http://localhost:3000/renders/head/jeb_?scale=2",
etag: '"a846b82963"',
crc32: [3487896679, 3001090792] crc32: [3487896679, 3001090792]
}, },
"head render with non-existent username": { "head render with non-existent username": {
url: "http://localhost:3000/renders/head/0?scale=2", url: "http://localhost:3000/renders/head/0?scale=2",
etag: '"mhf_steve"',
crc32: [3257141069, 214248305] crc32: [3257141069, 214248305]
}, },
"head render with non-existent username defaulting to mhf_alex": { "head render with non-existent username defaulting to mhf_alex": {
url: "http://localhost:3000/renders/head/0?scale=2&default=mhf_alex", url: "http://localhost:3000/renders/head/0?scale=2&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [263450586, 3116770561] crc32: [263450586, 3116770561]
}, },
"head render with non-existent username defaulting to username": { "head render with non-existent username defaulting to username": {
url: "http://localhost:3000/avatars/0?scale=2&default=jeb_", url: "http://localhost:3000/avatars/0?scale=2&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/avatars/jeb_?scale=2" redirect: "/avatars/jeb_?scale=2"
}, },
"head render with non-existent username defaulting to uuid": { "head render with non-existent username defaulting to uuid": {
url: "http://localhost:3000/avatars/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/avatars/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?scale=2" redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?scale=2"
}, },
"head render with non-existent username defaulting to url": { "head render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/head/0?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/head/0?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"overlay head render with existing username": { "overlay head render with existing username": {
url: "http://localhost:3000/renders/head/jeb_?scale=2&overlay", url: "http://localhost:3000/renders/head/jeb_?scale=2&overlay",
etag: '"a846b82963"',
crc32: [762377383, 1726474987] crc32: [762377383, 1726474987]
}, },
"overlay head render with non-existent username": { "overlay head render with non-existent username": {
url: "http://localhost:3000/renders/head/0?scale=2&overlay", url: "http://localhost:3000/renders/head/0?scale=2&overlay",
etag: '"mhf_steve"',
crc32: [3257141069, 214248305] crc32: [3257141069, 214248305]
}, },
"overlay head render with non-existent username defaulting to mhf_alex": { "overlay head render with non-existent username defaulting to mhf_alex": {
url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=mhf_alex", url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [263450586, 3116770561] crc32: [263450586, 3116770561]
}, },
"overlay head render with non-existent username defaulting to username": { "overlay head render with non-existent username defaulting to username": {
url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=jeb_", url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/renders/head/jeb_?scale=2&overlay=" redirect: "/renders/head/jeb_?scale=2&overlay="
}, },
"overlay head render with non-existent username defaulting to uuid": { "overlay head render with non-existent username defaulting to uuid": {
url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay=" redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay="
}, },
"overlay head render with non-existent username defaulting to url": { "overlay head render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/head/0?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"head render with existing uuid": { "head render with existing uuid": {
url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2", url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2",
etag: '"a846b82963"',
crc32: [3487896679, 3001090792] crc32: [3487896679, 3001090792]
}, },
"head render with non-existent uuid": { "head render with non-existent uuid": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2",
etag: '"mhf_steve"',
crc32: [3257141069, 214248305] crc32: [3257141069, 214248305]
}, },
"head render with non-existent uuid defaulting to mhf_alex": { "head render with non-existent uuid defaulting to mhf_alex": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=mhf_alex", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [263450586, 3116770561] crc32: [263450586, 3116770561]
}, },
"head render with non-existent uuid defaulting to username": { "head render with non-existent uuid defaulting to username": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=jeb_", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/renders/head/jeb_?scale=2" redirect: "/renders/head/jeb_?scale=2"
}, },
"head render with non-existent uuid defaulting to uuid": { "head render with non-existent uuid defaulting to uuid": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2" redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2"
}, },
"head render with non-existent uuid defaulting to url": { "head render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"overlay head render with existing uuid": { "overlay head render with existing uuid": {
url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay", url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay",
etag: '"a846b82963"',
crc32: [762377383, 1726474987] crc32: [762377383, 1726474987]
}, },
"overlay head render with non-existent uuid": { "overlay head render with non-existent uuid": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay",
etag: '"mhf_steve"',
crc32: [3257141069, 214248305] crc32: [3257141069, 214248305]
}, },
"overlay head render with non-existent uuid defaulting to mhf_alex": { "overlay head render with non-existent uuid defaulting to mhf_alex": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=mhf_alex", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [263450586, 3116770561] crc32: [263450586, 3116770561]
}, },
"overlay head with non-existent uuid defaulting to username": { "overlay head with non-existent uuid defaulting to username": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=jeb_", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/renders/head/jeb_?scale=2&overlay=" redirect: "/renders/head/jeb_?scale=2&overlay="
}, },
"overlay head with non-existent uuid defaulting to uuid": { "overlay head with non-existent uuid defaulting to uuid": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay=" redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay="
}, },
"overlay head render with non-existent uuid defaulting to url": { "overlay head render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"body render with existing username": { "body render with existing username": {
url: "http://localhost:3000/renders/body/jeb_?scale=2", url: "http://localhost:3000/renders/body/jeb_?scale=2",
etag: '"a846b82963"',
crc32: [3127075871, 2595192206] crc32: [3127075871, 2595192206]
}, },
"body render with non-existent username": { "body render with non-existent username": {
url: "http://localhost:3000/renders/body/0?scale=2", url: "http://localhost:3000/renders/body/0?scale=2",
etag: '"mhf_steve"',
crc32: [1046655221, 1620063267] crc32: [1046655221, 1620063267]
}, },
"body render with non-existent username defaulting to mhf_alex": { "body render with non-existent username defaulting to mhf_alex": {
url: "http://localhost:3000/renders/body/0?scale=2&default=mhf_alex", url: "http://localhost:3000/renders/body/0?scale=2&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [549240598, 3952648540] crc32: [549240598, 3952648540]
}, },
"body render with non-existent username defaulting to username": { "body render with non-existent username defaulting to username": {
url: "http://localhost:3000/renders/body/0?scale=2&default=jeb_", url: "http://localhost:3000/renders/body/0?scale=2&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/renders/body/jeb_?scale=2" redirect: "/renders/body/jeb_?scale=2"
}, },
"body render with non-existent username defaulting to uuid": { "body render with non-existent username defaulting to uuid": {
url: "http://localhost:3000/renders/body/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/renders/body/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2" redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2"
}, },
"body render with non-existent username defaulting to url": { "body render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/body/0?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/body/0?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"overlay body render with existing username": { "overlay body render with existing username": {
url: "http://localhost:3000/renders/body/jeb_?scale=2&overlay", url: "http://localhost:3000/renders/body/jeb_?scale=2&overlay",
etag: '"a846b82963"',
crc32: [699892097, 2732138694] crc32: [699892097, 2732138694]
}, },
"overlay body render with non-existent username": { "overlay body render with non-existent username": {
url: "http://localhost:3000/renders/body/0?scale=2&overlay", url: "http://localhost:3000/renders/body/0?scale=2&overlay",
etag: '"mhf_steve"',
crc32: [1046655221, 1620063267] crc32: [1046655221, 1620063267]
}, },
"overlay body render with non-existent username defaulting to mhf_alex": { "overlay body render with non-existent username defaulting to mhf_alex": {
url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=mhf_alex", url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [549240598, 3952648540] crc32: [549240598, 3952648540]
}, },
"overlay body render with non-existent username defaulting to username": { "overlay body render with non-existent username defaulting to username": {
url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=jeb_", url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/renders/body/jeb_?scale=2&overlay=" redirect: "/renders/body/jeb_?scale=2&overlay="
}, },
"overlay body render with non-existent username defaulting to uuid": { "overlay body render with non-existent username defaulting to uuid": {
url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay=" redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay="
}, },
"overlay body render with non-existent username defaulting to url": { "overlay body render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/body/0?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"body render with existing uuid": { "body render with existing uuid": {
url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2", url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2",
etag: '"a846b82963"',
crc32: [3127075871, 2595192206] crc32: [3127075871, 2595192206]
}, },
"body render with non-existent uuid": { "body render with non-existent uuid": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2",
etag: '"mhf_steve"',
crc32: [1046655221, 1620063267] crc32: [1046655221, 1620063267]
}, },
"body render with non-existent uuid defaulting to mhf_alex": { "body render with non-existent uuid defaulting to mhf_alex": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=mhf_alex", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [549240598, 3952648540] crc32: [549240598, 3952648540]
}, },
"body render with non-existent uuid defaulting to username": { "body render with non-existent uuid defaulting to username": {
url: "http://localhost:3000/renders/body/0?scale=2&default=jeb_", url: "http://localhost:3000/renders/body/0?scale=2&default=jeb_",
crc32: 0, crc32: [0],
redirect: "/renders/body/jeb_?scale=2" redirect: "/renders/body/jeb_?scale=2"
}, },
"body render with non-existent uuid defaulting to uuid": { "body render with non-existent uuid defaulting to uuid": {
url: "http://localhost:3000/renders/body/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6", url: "http://localhost:3000/renders/body/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
crc32: 0, crc32: [0],
redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2" redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2"
}, },
"body render with non-existent uuid defaulting to url": { "body render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
"overlay body render with existing uuid": { "overlay body render with existing uuid": {
url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay", url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&overlay",
etag: '"a846b82963"',
crc32: [699892097, 2732138694] crc32: [699892097, 2732138694]
}, },
"overlay body render with non-existent uuid": { "overlay body render with non-existent uuid": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&overlay", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&overlay",
etag: '"mhf_steve"',
crc32: [1046655221, 1620063267] crc32: [1046655221, 1620063267]
}, },
"overlay body render with non-existent uuid defaulting to mhf_alex": { "overlay body render with non-existent uuid defaulting to mhf_alex": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&overlay&default=mhf_alex", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&overlay&default=mhf_alex",
etag: '"mhf_alex"',
crc32: [549240598, 3952648540] crc32: [549240598, 3952648540]
}, },
"overlay body render with non-existent uuid defaulting to url": { "overlay body render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&overlay&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
crc32: 0, crc32: [0],
redirect: "http://example.com/CaseSensitive" redirect: "http://example.com/CaseSensitive"
}, },
}; };
@ -787,32 +742,30 @@ describe("Crafatar", function() {
assert.ifError(error); assert.ifError(error);
assert_headers(res); assert_headers(res);
assert(res.headers["x-storage-type"]); assert(res.headers["x-storage-type"]);
assert.strictEqual(res.headers.etag, location.etag); var hash = crc(body);
var matches = false; var matches = false;
if (location.crc32 instanceof Array) { for (var c = 0; c < location.crc32.length; c++) {
for (var i = 0; i < location.crc32.length; i++) { if (location.crc32[c] === hash) {
if (location.crc32[i] === crc(body)) { matches = true;
matches = true; break;
break;
}
} }
} else {
matches = location.crc32 === crc(body);
} }
try { try {
assert.ok(matches); assert(matches);
} catch(e) { } catch(e) {
throw new Error(crc(body) + " != " + location.crc32 + " | " + body.toString("base64")); throw new Error(hash + " != " + location.crc32 + " | " + body.toString("base64"));
} }
assert.strictEqual(res.headers.location, location.redirect); assert.strictEqual(res.headers.location, location.redirect);
if (location.etag === undefined) { if (location.crc32[0] === 0) {
assert.strictEqual(res.statusCode, location.redirect ? 307 : 404); assert.strictEqual(res.statusCode, location.redirect ? 307 : 404);
assert.ifError(res.headers.etag); // etag must not be present on non-200
assert.strictEqual(res.headers["content-type"], "text/plain"); assert.strictEqual(res.headers["content-type"], "text/plain");
done(); done();
} else { } else {
assert(res.headers.etag);
assert.strictEqual(res.headers["content-type"], "image/png"); assert.strictEqual(res.headers["content-type"], "image/png");
assert.strictEqual(res.statusCode, 200); assert.strictEqual(res.statusCode, 200);
assert(res.headers.etag);
assert.strictEqual(res.headers.etag, '"' + hash + '"');
assert_cache(location.url, res.headers.etag, function() { assert_cache(location.url, res.headers.etag, function() {
done(); done();
}); });