Skip to content

Commit

Permalink
Correctly handle HTTP redirects & async SkinsRestorer requests
Browse files Browse the repository at this point in the history
Closes #40
  • Loading branch information
MrMicky-FR committed Mar 28, 2024
1 parent 0b02fac commit 76128da
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group 'com.azuriom'
version '1.3.4'
version '1.3.5'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,16 @@ private <T> 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) {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}
}

0 comments on commit 76128da

Please sign in to comment.