Skip to content

Commit 8eaf86f

Browse files
committed
Download Utils 0.4 - Remove Dependency on Log Utils (#16)
1 parent 337f013 commit 8eaf86f

File tree

7 files changed

+226
-363
lines changed

7 files changed

+226
-363
lines changed

download-utils/build.gradle

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
alias libs.plugins.gradleutils
88
alias libs.plugins.gitversion
99
alias libs.plugins.changelog
10+
alias libs.plugins.multi.release
1011
}
1112

1213
gradleutils.displayName = 'Download Utils'
@@ -23,33 +24,26 @@ java {
2324

2425
dependencies {
2526
compileOnly libs.nulls
26-
27-
implementation projects.logUtils
2827
}
2928

30-
final java11 = configurations.detachedConfiguration(
31-
dependencies.create(projects.downloadUtils.java11) {
32-
transitive = false
33-
}
34-
)
35-
3629
tasks.named('jar', Jar) {
37-
dependsOn java11.buildDependencies
38-
3930
manifest {
4031
attributes([
41-
'Automatic-Module-Name': 'net.minecraftforge.utils.download',
42-
'Multi-Release' : 'true'
32+
'Automatic-Module-Name': 'net.minecraftforge.utils.download'
4333
])
4434

4535
gradleutils.manifestDefaults(it, 'net/minecraftforge/util/download/')
4636
}
4737

48-
into('META-INF/versions/11') {
49-
from(provider { zipTree(java11.singleFile) }) {
50-
exclude 'META-INF/**'
51-
}
52-
}
38+
archiveClassifier = 'java8'
39+
}
40+
41+
multiRelease.register {
42+
add(JavaLanguageVersion.of(11), projects.downloadUtils.downloadUtilsJ11)
43+
}
44+
45+
tasks.named('multiReleaseJar', Jar) {
46+
archiveClassifier = ''
5347
}
5448

5549
license {
@@ -68,7 +62,7 @@ publishing {
6862
}
6963

7064
publications.register('mavenJava', MavenPublication).configure {
71-
from components.java
65+
from multiRelease.component
7266

7367
changelog.publish(it)
7468
gradleutils.promote(it)

download-utils/java11/build.gradle renamed to download-utils/download-utils-j11/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ java.toolchain.languageVersion = JavaLanguageVersion.of(11)
1010

1111
dependencies {
1212
compileOnly libs.nulls
13-
14-
implementation projects.logUtils
1513
}
1614

1715
license {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.util.download;
6+
7+
import java.io.FileNotFoundException;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.net.HttpURLConnection;
11+
import java.net.URI;
12+
import java.net.URISyntaxException;
13+
import java.net.http.HttpClient;
14+
import java.net.http.HttpRequest;
15+
import java.net.http.HttpResponse;
16+
import java.time.Duration;
17+
import java.util.ArrayList;
18+
19+
final class DownloadUtilsImpl {
20+
private DownloadUtilsImpl() { }
21+
22+
private static final Duration TIMEOUT = Duration.ofSeconds(5);
23+
private static final int MAX_REDIRECTS = 3;
24+
25+
private static final HttpClient CLIENT = HttpClient
26+
.newBuilder()
27+
.connectTimeout(TIMEOUT)
28+
.followRedirects(HttpClient.Redirect.NEVER)
29+
.build();
30+
31+
private static final HttpRequest.Builder REQUEST_BUILDER = HttpRequest
32+
.newBuilder()
33+
.timeout(TIMEOUT)
34+
.header("User-Agent", "MinecraftForge-Utils")
35+
.header("Accept", "application/json");
36+
37+
static InputStream connect(String address) throws IOException, InterruptedException {
38+
URI uri;
39+
try {
40+
uri = new URI(address);
41+
} catch (URISyntaxException e) {
42+
throw new IOException(e);
43+
}
44+
45+
// if not http, then try a URLConnection. we might be trying to make a file or jar connection.
46+
// see jdk.internal.net.http.HttpRequestBuilderImpl#checkUri
47+
var scheme = uri.getScheme();
48+
if (scheme == null || !(scheme.equalsIgnoreCase("https") || scheme.equalsIgnoreCase("http"))) {
49+
return uri.toURL().openStream();
50+
}
51+
52+
var redirections = new ArrayList<String>();
53+
HttpResponse<InputStream> con;
54+
for (int redirects = 0; ; redirects++) {
55+
con = CLIENT.send(
56+
REQUEST_BUILDER.uri(uri).build(),
57+
info -> HttpResponse.BodySubscribers.ofInputStream()
58+
);
59+
60+
int res = con.statusCode();
61+
if (res == HttpURLConnection.HTTP_MOVED_PERM || res == HttpURLConnection.HTTP_MOVED_TEMP) {
62+
var header = con.headers().firstValue("Location");
63+
if (header.isEmpty())
64+
throw new IOException(String.format(
65+
"No location header found in redirect response: %s -- previous redirections: [%s]",
66+
uri, String.join(", ", redirections)
67+
));
68+
69+
var location = header.get();
70+
redirections.add(location);
71+
72+
if (redirects == MAX_REDIRECTS - 1) {
73+
throw new IOException(String.format(
74+
"Too many redirects: %s -- redirections: [%s]",
75+
address, String.join(", ", redirections)
76+
));
77+
} else {
78+
uri = uri.resolve(location);
79+
}
80+
} else if (res == HttpURLConnection.HTTP_NOT_FOUND) {
81+
throw new FileNotFoundException("Returned 404: " + address);
82+
} else {
83+
break;
84+
}
85+
}
86+
87+
return con.body();
88+
}
89+
90+
static byte[] readInputStream(InputStream stream) throws IOException {
91+
return stream.readAllBytes();
92+
}
93+
}

download-utils/java11/src/main/java/net/minecraftforge/util/download/DownloadUtils.java

Lines changed: 0 additions & 219 deletions
This file was deleted.

0 commit comments

Comments
 (0)