Skip to content

Commit

Permalink
Small cleanup, made HttpUtil internal only
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Jun 10, 2023
1 parent c004a59 commit 488fe17
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 69 deletions.
9 changes: 1 addition & 8 deletions src/main/java/dev/booky/stackdeobf/http/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

public final class HttpUtil {
final class HttpUtil {

private static final Map<Executor, HttpClient> HTTP = new WeakHashMap<>();

Expand All @@ -26,13 +26,6 @@ private static HttpClient getHttpClient(Executor executor) {
}
}

public static CompletableFuture<byte[]> getAsync(VerifiableUrl url, Executor executor) {
return getAsync(url.getUrl(), executor).thenApply(bytes -> {
url.verifyHash(bytes);
return bytes;
});
}

static CompletableFuture<byte[]> getAsync(URI url, Executor executor) {
HttpRequest request = HttpRequest.newBuilder(url).build();
return getAsyncRaw(request, executor).thenApply(resp -> {
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/dev/booky/stackdeobf/http/VerifiableUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ public static CompletableFuture<VerifiableUrl> resolveByMd5Header(URI url, Execu
});
}

void verifyHash(byte[] bytes) throws FailedUrlVerificationException {
HashCode hashCode = this.hashType.hash(bytes);
CompatUtil.LOGGER.info("Verifying {} hash {} for {}...",
this.hashType, hashCode, this.uri);

if (!hashCode.equals(this.hashCode)) {
throw new FailedUrlVerificationException(this.hashType + " hash " + hashCode + " doesn't match "
+ this.hashCode + " for " + this.uri);
}
public CompletableFuture<byte[]> get(Executor executor) {
return HttpUtil.getAsync(this.uri, executor).thenApply(bytes -> {
HashCode hashCode = this.hashType.hash(bytes);
CompatUtil.LOGGER.info("Verifying {} hash {} for {}...",
this.hashType, hashCode, this.uri);

if (!hashCode.equals(this.hashCode)) {
throw new FailedUrlVerificationException(this.hashType + " hash " + hashCode + " doesn't match "
+ this.hashCode + " for " + this.uri);
}
return bytes;
});
}

URI getUrl() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.booky.stackdeobf.mappings.providers;
// Created by booky10 in StackDeobfuscator (22:08 23.03.23)

import dev.booky.stackdeobf.http.HttpUtil;
import dev.booky.stackdeobf.http.VerifiableUrl;
import dev.booky.stackdeobf.util.CompatUtil;
import dev.booky.stackdeobf.util.MavenArtifactInfo;
Expand Down Expand Up @@ -65,16 +64,16 @@ protected CompletableFuture<Void> downloadMappings0(Path cacheDir, Executor exec
return this.artifactInfo.buildVerifiableUrl(build, "jar", this.hashType, executor)
.thenCompose(url -> {
CompatUtil.LOGGER.info("Downloading {} mappings jar for build {}...", this.name, build);

return HttpUtil.getAsync(url, executor).thenAccept(mappingJarBytes -> {
byte[] mappingBytes = this.extractPackagedMappings(mappingJarBytes);
try (OutputStream fileOutput = Files.newOutputStream(this.path);
GZIPOutputStream gzipOutput = new GZIPOutputStream(fileOutput)) {
gzipOutput.write(mappingBytes);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
});
return url.get(executor);
})
.thenAccept(mappingJarBytes -> {
byte[] mappingBytes = this.extractPackagedMappings(mappingJarBytes);
try (OutputStream fileOutput = Files.newOutputStream(this.path);
GZIPOutputStream gzipOutput = new GZIPOutputStream(fileOutput)) {
gzipOutput.write(mappingBytes);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
});
});
}
Expand Down Expand Up @@ -104,7 +103,7 @@ private CompletableFuture<String> fetchLatestVersion(Path cacheDir, String mcVer

return this.artifactInfo.buildVerifiableMetaUrl(this.hashType, executor).thenCompose(metaUrl -> {
CompatUtil.LOGGER.info("Fetching latest {} build...", this.name);
return HttpUtil.getAsync(metaUrl, executor).thenApply(resp -> {
return metaUrl.get(executor).thenApply(resp -> {
try (InputStream input = new ByteArrayInputStream(resp)) {
Document document;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package dev.booky.stackdeobf.mappings.providers;
// Created by booky10 in StackDeobfuscator (20:56 30.03.23)

import dev.booky.stackdeobf.http.HttpUtil;
import dev.booky.stackdeobf.util.CompatUtil;
import dev.booky.stackdeobf.http.VerifiableUrl;
import dev.booky.stackdeobf.util.CompatUtil;
import dev.booky.stackdeobf.util.MavenArtifactInfo;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingVisitor;
Expand Down Expand Up @@ -47,17 +46,18 @@ protected CompletableFuture<Void> downloadMappings0(Path cacheDir, Executor exec
}

return MAPPINGS_ARTIFACT.buildVerifiableUrl(CompatUtil.VERSION_ID, "jar", HASH_TYPE, executor)
.thenCompose(source -> {
.thenCompose(url -> {
CompatUtil.LOGGER.info("Downloading intermediary mappings for {}...", CompatUtil.VERSION_ID);
return HttpUtil.getAsync(source, executor).thenAccept(jarBytes -> {
byte[] mappingBytes = this.extractPackagedMappings(jarBytes);
try (OutputStream fileOutput = Files.newOutputStream(this.path);
GZIPOutputStream gzipOutput = new GZIPOutputStream(fileOutput)) {
gzipOutput.write(mappingBytes);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
});
return url.get(executor);
})
.thenAccept(jarBytes -> {
byte[] mappingBytes = this.extractPackagedMappings(jarBytes);
try (OutputStream fileOutput = Files.newOutputStream(this.path);
GZIPOutputStream gzipOutput = new GZIPOutputStream(fileOutput)) {
gzipOutput.write(mappingBytes);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.common.base.Preconditions;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import dev.booky.stackdeobf.http.HttpUtil;
import dev.booky.stackdeobf.http.VerifiableUrl;
import dev.booky.stackdeobf.util.CompatUtil;
import net.fabricmc.api.EnvType;
Expand Down Expand Up @@ -75,7 +74,7 @@ protected CompletableFuture<Void> downloadMappings0(Path cacheDir, Executor exec

return intermediaryFuture
.thenCompose($ -> this.fetchMojangMappingSource(CompatUtil.VERSION_ID, executor))
.thenCompose(source -> HttpUtil.getAsync(source, executor))
.thenCompose(url -> url.get(executor))
.thenAccept(mappingBytes -> {
try (OutputStream fileOutput = Files.newOutputStream(this.path);
GZIPOutputStream gzipOutput = new GZIPOutputStream(fileOutput)) {
Expand All @@ -94,7 +93,7 @@ private CompletableFuture<VerifiableUrl> fetchMojangMappingSource(String mcVersi
// but only if the http method is "HEAD"

return VerifiableUrl.resolveByMd5Header(manifestUri, executor)
.thenCompose(url -> HttpUtil.getAsync(url, executor))
.thenCompose(url -> url.get(executor))
.thenCompose(manifestResp -> {
JsonObject manifestObj;
try (ByteArrayInputStream input = new ByteArrayInputStream(manifestResp);
Expand All @@ -114,7 +113,7 @@ private CompletableFuture<VerifiableUrl> fetchMojangMappingSource(String mcVersi
String infoSha1 = elementObj.get("sha1").getAsString();
VerifiableUrl infoUrl = new VerifiableUrl(rawInfoUrl, VerifiableUrl.HashType.SHA1, infoSha1);

return HttpUtil.getAsync(infoUrl, executor).thenApply(infoResp -> {
return infoUrl.get(executor).thenApply(infoResp -> {
JsonObject infoObj;
try (ByteArrayInputStream input = new ByteArrayInputStream(infoResp);
Reader reader = new InputStreamReader(input)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Created by booky10 in StackDeobfuscator (22:06 23.03.23)

import com.google.common.base.Preconditions;
import dev.booky.stackdeobf.http.HttpUtil;
import dev.booky.stackdeobf.http.VerifiableUrl;
import dev.booky.stackdeobf.util.CompatUtil;
import dev.booky.stackdeobf.util.MavenArtifactInfo;
Expand Down Expand Up @@ -71,16 +70,16 @@ protected CompletableFuture<Void> downloadMappings0(Path cacheDir, Executor exec
.thenCompose($ -> HASHED_MAPPINGS_ARTIFACT.buildVerifiableUrl(CompatUtil.VERSION_ID, "jar", this.hashType, executor))
.thenCompose(hashedUrl -> {
CompatUtil.LOGGER.info("Downloading hashed {} mappings for {}...", this.name, CompatUtil.VERSION_ID);

return HttpUtil.getAsync(hashedUrl, executor).thenAccept(jarBytes -> {
byte[] mappingBytes = this.extractPackagedMappings(jarBytes);
try (OutputStream fileOutput = Files.newOutputStream(this.hashedPath);
GZIPOutputStream gzipOutput = new GZIPOutputStream(fileOutput)) {
gzipOutput.write(mappingBytes);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
});
return hashedUrl.get(executor);
})
.thenAccept(jarBytes -> {
byte[] mappingBytes = this.extractPackagedMappings(jarBytes);
try (OutputStream fileOutput = Files.newOutputStream(this.hashedPath);
GZIPOutputStream gzipOutput = new GZIPOutputStream(fileOutput)) {
gzipOutput.write(mappingBytes);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
});
}

Expand All @@ -92,19 +91,21 @@ protected CompletableFuture<Void> parseMappings0(Executor executor) {
return future; // 1.19.2+
}

return future.thenCompose($ -> this.intermediary.parseMappings0(executor)).thenRun(() -> {
MemoryMappingTree mappings = new MemoryMappingTree();

try (InputStream fileInput = Files.newInputStream(this.hashedPath);
GZIPInputStream gzipInput = new GZIPInputStream(fileInput);
Reader reader = new InputStreamReader(gzipInput)) {
MappingReader.read(reader, MappingFormat.TINY_2, mappings);
} catch (IOException exception) {
throw new RuntimeException(exception);
}

this.hashedMappings = mappings;
});
return future
.thenCompose($ -> this.intermediary.parseMappings0(executor))
.thenRun(() -> {
MemoryMappingTree mappings = new MemoryMappingTree();

try (InputStream fileInput = Files.newInputStream(this.hashedPath);
GZIPInputStream gzipInput = new GZIPInputStream(fileInput);
Reader reader = new InputStreamReader(gzipInput)) {
MappingReader.read(reader, MappingFormat.TINY_2, mappings);
} catch (IOException exception) {
throw new RuntimeException(exception);
}

this.hashedMappings = mappings;
});
}

@Override
Expand Down

0 comments on commit 488fe17

Please sign in to comment.