mirror of
https://github.com/azures04/crafatar.git
synced 2026-05-06 11:00:39 +02:00
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:
@@ -13,40 +13,6 @@ var mojang_urls = [skins_url, capes_url];
|
||||
|
||||
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+
|
||||
// +userId+ is used for usernames, while +profile+ is used for UUIDs
|
||||
// callback: error, url, slim
|
||||
|
||||
@@ -63,19 +63,15 @@ module.exports = function(request, response, result) {
|
||||
headers["X-Storage-Type"] = human_status[result.status];
|
||||
}
|
||||
|
||||
if (result.body) {
|
||||
// use Mojang's image hash if available
|
||||
// use crc32 as a hash function otherwise
|
||||
var etag = result.hash && result.hash.substr(0, 10) || crc(result.body);
|
||||
headers.Etag = "\"" + etag + "\"";
|
||||
// use crc32 as a hash function for Etag
|
||||
var etag = "\"" + crc(result.body || "") + "\"";
|
||||
|
||||
// handle etag caching
|
||||
var incoming_etag = request.headers["if-none-match"];
|
||||
if (incoming_etag && incoming_etag === headers.Etag) {
|
||||
response.writeHead(304, headers);
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
// handle etag caching
|
||||
var incoming_etag = request.headers["if-none-match"];
|
||||
if (incoming_etag && incoming_etag === etag) {
|
||||
response.writeHead(304, headers);
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.redirect) {
|
||||
@@ -87,12 +83,16 @@ module.exports = function(request, response, result) {
|
||||
|
||||
if (result.status === -2) {
|
||||
response.writeHead(result.code || 422, headers);
|
||||
response.end(result.body);
|
||||
} else if (result.status === -1) {
|
||||
response.writeHead(500, headers);
|
||||
response.end(result.body);
|
||||
} else {
|
||||
response.writeHead(result.body ? 200 : 404, headers);
|
||||
response.end(result.body);
|
||||
if (result.body) {
|
||||
headers.Etag = etag;
|
||||
response.writeHead(200, headers);
|
||||
} else {
|
||||
response.writeHead(404, headers);
|
||||
}
|
||||
}
|
||||
|
||||
response.end(result.body);
|
||||
};
|
||||
Reference in New Issue
Block a user