From 76128da67e7cd6c7b2a1bee58b9bf7b65300e13f Mon Sep 17 00:00:00 2001 From: MrMicky Date: Thu, 28 Mar 2024 22:02:59 +0100 Subject: [PATCH] Correctly handle HTTP redirects & async SkinsRestorer requests Closes #40 --- .github/workflows/build.yml | 13 +++++++----- build.gradle | 2 +- .../azlink/common/http/client/HttpClient.java | 15 +++++++++----- .../integrations/BaseSkinsRestorer.java | 20 ++++++++++--------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8e2302e..7dfe1d3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,10 +16,10 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup JDK ${{ matrix.java-version }} - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: distribution: temurin java-version: ${{ matrix.java-version }} @@ -34,19 +34,22 @@ jobs: run: ./gradlew build - name: Upload AzLink.jar - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AzLink path: universal/build/libs/AzLink-*.jar + overwrite: true - name: Upload AzLink-Legacy.jar - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AzLink-Legacy path: universal-legacy/build/libs/AzLink-Legacy-*.jar + overwrite: true - name: Upload AzLink-Fabric.jar - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: AzLink-Fabric path: fabric/build/libs/AzLink-Fabric-*.jar + overwrite: true diff --git a/build.gradle b/build.gradle index 3580bd7..1548e81 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ allprojects { group 'com.azuriom' - version '1.3.4' + version '1.3.5' } subprojects { diff --git a/common/src/main/java/com/azuriom/azlink/common/http/client/HttpClient.java b/common/src/main/java/com/azuriom/azlink/common/http/client/HttpClient.java index 784ad24..0c0f39b 100644 --- a/common/src/main/java/com/azuriom/azlink/common/http/client/HttpClient.java +++ b/common/src/main/java/com/azuriom/azlink/common/http/client/HttpClient.java @@ -101,11 +101,16 @@ private T rawRequest(RequestMethod method, String endpoint, String body, Cla } } - if (conn.getResponseCode() >= 400) { - int status = conn.getResponseCode(); - String message = conn.getResponseMessage(); + int status = conn.getResponseCode(); - throw new IOException("Invalid response code (" + status + " - " + message + ")"); + if (status >= 400) { + throw new IOException("Unexpected HTTP error " + status); + } + + if (status >= 300) { + String dest = conn.getHeaderField("Location"); + + throw new IOException("Unexpected redirect status - " + status + ": " + dest); } if (clazz == null || clazz == Void.class) { @@ -131,7 +136,7 @@ private HttpURLConnection prepareConnection(RequestMethod method, String endpoin HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); - conn.setInstanceFollowRedirects(true); + conn.setInstanceFollowRedirects(false); // POST requests are redirected as GET requests conn.setConnectTimeout(CONNECT_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); conn.setRequestMethod(method.name()); diff --git a/common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java b/common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java index b3ca3c0..0ee8d78 100644 --- a/common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java +++ b/common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java @@ -26,14 +26,16 @@ protected void handleJoin(String playerName, P player) { return; } - try { - String url = baseUrl + "/api/skin-api/skins/" + playerName; - SkinsRestorer skins = SkinsRestorerProvider.get(); - MineSkinResponse res = skins.getMineSkinAPI().genSkin(url, null); - - skins.getSkinApplier(this.playerClass).applySkin(player, res.getProperty()); - } catch (DataRequestException | MineSkinException ex) { - this.plugin.getLogger().warn("Unable to apply skin for " + playerName + ": " + ex.getMessage()); - } + this.plugin.getScheduler().executeAsync(() -> { + try { + String url = baseUrl + "/api/skin-api/skins/" + playerName; + SkinsRestorer skins = SkinsRestorerProvider.get(); + MineSkinResponse res = skins.getMineSkinAPI().genSkin(url, null); + + skins.getSkinApplier(this.playerClass).applySkin(player, res.getProperty()); + } catch (DataRequestException | MineSkinException e) { + this.plugin.getLogger().warn("Unable to apply skin for " + playerName + ": " + e.getMessage()); + } + }); } }