diff --git a/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java b/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java index bc95e7a..aa94c6a 100644 --- a/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java +++ b/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java @@ -66,13 +66,13 @@ public class MinecraftCapesAPI implements JsonAPILoader.IJsonAPI { } String capeBase64 = result.textures.cape; - String hash = HttpTextureUtil.getHash(capeBase64); + byte[] capeBytes = Base64.decodeBase64(capeBase64); + String hash = HttpTextureUtil.getHash(capeBytes); File cacheFile = HttpTextureUtil.getCacheFile(hash); String fakeUrl = HttpTextureUtil.getBase64FakeUrl(hash); //Save base64 image to cache file try { - byte[] capeBytes = Base64.decodeBase64(capeBase64); FileUtils.writeByteArrayToFile(cacheFile, capeBytes); CustomSkinLoader.logger.info("Saved base64 image to " + cacheFile); } catch (Exception e) { diff --git a/Common/source/customskinloader/utils/HttpRequestUtil.java b/Common/source/customskinloader/utils/HttpRequestUtil.java index a555ca9..0e615fb 100644 --- a/Common/source/customskinloader/utils/HttpRequestUtil.java +++ b/Common/source/customskinloader/utils/HttpRequestUtil.java @@ -92,13 +92,14 @@ public class HttpRequestUtil { public static HttpResponce makeHttpRequest(HttpRequest request, int redirectTime) { try { - if(request.url.contains("{ABORT}")){ - CustomSkinLoader.logger.info("ABORT tag found in url, request has been aborted."); - return new HttpResponce(); + //No url provided, try to load from special cache file + if (request.url == null || request.url.isEmpty()) { + CustomSkinLoader.logger.debug("Try to read cache '" + request.cacheFile + "'."); + return loadFromCache(request, new HttpResponce()); } CustomSkinLoader.logger.debug("Try to request '" + request.url + (request.userAgent == null ? "'." : "' with user agent '" + request.userAgent + "'.")); //Check Cache - if (StringUtils.isNotEmpty(request.payload) || CustomSkinLoader.config.forceDisableCache){ + if (StringUtils.isNotEmpty(request.payload) || CustomSkinLoader.config.forceDisableCache) { request.cacheTime = -1;//No Cache } File cacheInfoFile = null; @@ -108,32 +109,38 @@ public class HttpRequestUtil { request.cacheFile = new File(CACHE_DIR, hash); cacheInfoFile = new File(CACHE_DIR, hash + ".json"); } - if (request.cacheTime == 0 && request.cacheFile.isFile()) + //noinspection BulkFileAttributesRead + if (request.cacheTime == 0 && request.cacheFile.isFile()) { return loadFromCache(request, new HttpResponce()); + } if (cacheInfoFile != null && cacheInfoFile.isFile()) { String json = FileUtils.readFileToString(cacheInfoFile, "UTF-8"); - if (StringUtils.isNotEmpty(json)) + if (StringUtils.isNotEmpty(json)) { cacheInfo = CustomSkinLoader.GSON.fromJson(json, CacheInfo.class); - if (cacheInfo == null) + } + if (cacheInfo == null) { cacheInfo = new CacheInfo(); - if (cacheInfo.expire >= TimeUtil.getCurrentUnixTimestamp()) + } + if (cacheInfo.expire >= TimeUtil.getCurrentUnixTimestamp()) { return loadFromCache(request, new HttpResponce(), cacheInfo.expire); + } } //Init Connection URL rawUrl = new URL(request.url); URI uri = new URI( - rawUrl.getProtocol(), - rawUrl.getUserInfo(), - rawUrl.getHost(), - rawUrl.getPort(), - rawUrl.getPath(), - rawUrl.getQuery(), - rawUrl.getRef() + rawUrl.getProtocol(), + rawUrl.getUserInfo(), + rawUrl.getHost(), + rawUrl.getPort(), + rawUrl.getPath(), + rawUrl.getQuery(), + rawUrl.getRef() ); String url = uri.toASCIIString(); - if (!url.equalsIgnoreCase(request.url)) + if (!url.equalsIgnoreCase(request.url)) { CustomSkinLoader.logger.debug("Encoded URL: " + url); + } HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection(); c.setReadTimeout(1000 * 10); c.setConnectTimeout(1000 * 10); @@ -142,13 +149,16 @@ public class HttpRequestUtil { c.setInstanceFollowRedirects(true); //Make Connection - if (cacheInfo.lastModified >= 0) + if (cacheInfo.lastModified >= 0) { c.setIfModifiedSince(cacheInfo.lastModified); - if (cacheInfo.etag != null) + } + if (cacheInfo.etag != null) { c.setRequestProperty("If-None-Match", cacheInfo.etag); + } c.setRequestProperty("Accept-Encoding", "gzip"); - if (request.userAgent != null) + if (request.userAgent != null) { c.setRequestProperty("User-Agent", request.userAgent); + } if (StringUtils.isNotEmpty(request.payload)) { CustomSkinLoader.logger.info("Payload: " + request.payload); c.setRequestProperty("Content-Type", "application/json"); @@ -183,10 +193,12 @@ public class HttpRequestUtil { } responce.success = true; CustomSkinLoader.logger.debug("Successfully request (Response Code: " + res + " , Content Length: " + c.getContentLength() + ")"); - if (responce.responceCode == HttpURLConnection.HTTP_NOT_MODIFIED) + if (responce.responceCode == HttpURLConnection.HTTP_NOT_MODIFIED) { return loadFromCache(request, responce); - if (responce.responceCode == HttpURLConnection.HTTP_NO_CONTENT) + } + if (responce.responceCode == HttpURLConnection.HTTP_NO_CONTENT) { request.cacheTime = 3600; + } //Load Content InputStream is = "gzip".equals(c.getContentEncoding()) ? new GZIPInputStream(c.getInputStream()) : c.getInputStream(); @@ -205,16 +217,18 @@ public class HttpRequestUtil { cacheInfo.expire = getExpire(c, request.cacheTime); FileUtils.write(cacheInfoFile, CustomSkinLoader.GSON.toJson(cacheInfo), "UTF-8"); } + //noinspection BulkFileAttributesRead CustomSkinLoader.logger.debug("Saved to cache (Length: " + request.cacheFile.length() + " , Path: '" + request.cacheFile.getAbsolutePath() + "' , Expire: " + cacheInfo.expire + ")"); } - if (!request.loadContent) + if (!request.loadContent) { return responce; + } responce.content = new String(bytes, StandardCharsets.UTF_8); CustomSkinLoader.logger.debug("Content: " + responce.content); return responce; } catch (Exception e) { - CustomSkinLoader.logger.debug("Failed to request " + request.url + " (Exception: " + e.toString() + ")"); + CustomSkinLoader.logger.debug("Failed to request " + request.url + " (Exception: " + e + ")"); return loadFromCache(request, new HttpResponce()); } } @@ -228,13 +242,15 @@ public class HttpRequestUtil { } private static HttpResponce loadFromCache(HttpRequest request, HttpResponce responce, long expireTime) { - if (request.cacheFile == null || !request.cacheFile.isFile()) + if (request.cacheFile == null || !request.cacheFile.isFile()) { return responce; + } CustomSkinLoader.logger.debug("Cache file found (Length: " + request.cacheFile.length() + " , Path: '" + request.cacheFile.getAbsolutePath() + "' , Expire: " + expireTime + ")"); responce.fromCache = true; responce.success = true; - if (!request.loadContent) + if (!request.loadContent) { return responce; + } CustomSkinLoader.logger.info("Try to load from cache '" + request.cacheFile.getAbsolutePath() + "'."); try { @@ -242,7 +258,7 @@ public class HttpRequestUtil { CustomSkinLoader.logger.debug("Successfully load from cache"); CustomSkinLoader.logger.debug("Content: " + responce.content); } catch (IOException e) { - CustomSkinLoader.logger.debug("Failed to load from cache (Exception: " + e.toString() + ")"); + CustomSkinLoader.logger.debug("Failed to load from cache (Exception: " + e + ")"); responce.success = false; } return responce; diff --git a/Common/source/customskinloader/utils/HttpTextureUtil.java b/Common/source/customskinloader/utils/HttpTextureUtil.java index e3ee4e2..a51add5 100644 --- a/Common/source/customskinloader/utils/HttpTextureUtil.java +++ b/Common/source/customskinloader/utils/HttpTextureUtil.java @@ -67,7 +67,7 @@ public class HttpTextureUtil { if (fakeUrl.startsWith(BASE64_MARK)) { fakeUrl = fakeUrl.replace(BASE64_MARK, ""); info.hash = fakeUrl; - info.cacheFile = HttpRequestUtil.getCacheFile(info.hash); + info.cacheFile = getCacheFile(info.hash); return info; } return info; @@ -93,8 +93,8 @@ public class HttpTextureUtil { return DigestUtils.sha1Hex(size + url + lastModified); } - public static String getHash(String base64) { - return DigestUtils.sha1Hex(base64); + public static String getHash(byte[] bytes) { + return DigestUtils.sha1Hex(bytes); } public static File getCacheFile(String hash) {