使用 WebURL 代替 URI (#6883)

This commit is contained in:
Glavo authored and GitHub committed 2026-09-20 20:38:59 +08:00
1 parent 2f5594d8c1
commit 77b42b6f99
54 files changed
+905 -637

No files matched your search

@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.game;
import com.jfoenix.controls.JFXButton;
import javafx.stage.Stage;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.auth.*;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorDownloadException;
@@ -39,8 +40,8 @@ import org.jackhuang.hmcl.setting.LauncherVisibility;
import org.jackhuang.hmcl.task.*;
import org.jackhuang.hmcl.ui.*;
import org.jackhuang.hmcl.ui.construct.DialogCloseEvent;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.PromptDialogPane;
import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane;
import org.jackhuang.hmcl.util.*;
@@ -57,7 +58,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.nio.file.AccessDeniedException;
import java.nio.file.Path;
import java.time.Instant;
@@ -356,26 +356,26 @@ public final class LauncherHelper {
message = i18n("launch.failed.download_library", ((LibraryDownloadException) ex).getLibrary().name()) + "\n";
if (ex.getCause() instanceof ResponseCodeException rce) {
int responseCode = rce.getResponseCode();
String uri = rce.getUri();
String url = rce.getUrl();
if (responseCode == 404)
message += i18n("download.code.404", uri);
message += i18n("download.code.404", url);
else
message += i18n("download.failed", uri, responseCode);
message += i18n("download.failed", url, responseCode);
} else {
message += StringUtils.getStackTrace(ex.getCause());
}
} else if (ex instanceof DownloadException) {
URI uri = ((DownloadException) ex).getUri();
} else if (ex instanceof DownloadException de) {
WebURL url = de.getUrl();
if (ex.getCause() instanceof SocketTimeoutException) {
message = i18n("install.failed.downloading.timeout", uri);
message = i18n("install.failed.downloading.timeout", url);
} else if (ex.getCause() instanceof ResponseCodeException responseCodeException) {
if (I18n.hasKey("download.code." + responseCodeException.getResponseCode())) {
message = i18n("download.code." + responseCodeException.getResponseCode(), uri);
message = i18n("download.code." + responseCodeException.getResponseCode(), url);
} else {
message = i18n("install.failed.downloading.detail", uri) + "\n" + StringUtils.getStackTrace(ex.getCause());
message = i18n("install.failed.downloading.detail", url) + "\n" + StringUtils.getStackTrace(ex.getCause());
}
} else {
message = i18n("install.failed.downloading.detail", uri) + "\n" + StringUtils.getStackTrace(ex.getCause());
message = i18n("install.failed.downloading.detail", url) + "\n" + StringUtils.getStackTrace(ex.getCause());
}
} else if (ex instanceof GameAssetIndexDownloadTask.GameAssetIndexMalformedException) {
message = i18n("assets.index.malformed");
@@ -385,11 +385,11 @@ public final class LauncherHelper {
message = i18n("account.failed.character_deleted");
} else if (ex instanceof ResponseCodeException rce) {
int responseCode = rce.getResponseCode();
String uri = rce.getUri();
String url = rce.getUrl();
if (responseCode == 404)
message = i18n("download.code.404", uri);
message = i18n("download.code.404", url);
else
message = i18n("download.failed", uri, responseCode);
message = i18n("download.failed", url, responseCode);
} else if (ex instanceof CommandTooLongException) {
message = i18n("launch.failed.command_too_long");
} else if (ex instanceof ExecutionPolicyLimitException) {
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.setting;
import javafx.beans.InvalidationListener;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.*;
import org.jackhuang.hmcl.task.DownloadException;
import org.jackhuang.hmcl.task.FetchTask;
@@ -29,7 +30,6 @@ import org.jackhuang.hmcl.util.io.ResponseCodeException;
import javax.net.ssl.SSLHandshakeException;
import java.io.FileNotFoundException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.nio.file.AccessDeniedException;
import java.util.List;
import java.util.concurrent.CancellationException;
@@ -104,30 +104,30 @@ public final class DownloadProviders {
}
public static String localizeErrorMessage(Throwable exception) {
if (exception instanceof DownloadException) {
URI uri = ((DownloadException) exception).getUri();
if (exception instanceof DownloadException de) {
WebURL url = de.getUrl();
if (exception.getCause() instanceof SocketTimeoutException) {
return i18n("install.failed.downloading.timeout", uri);
return i18n("install.failed.downloading.timeout", url);
} else if (exception.getCause() instanceof ResponseCodeException) {
ResponseCodeException responseCodeException = (ResponseCodeException) exception.getCause();
if (I18n.hasKey("download.code." + responseCodeException.getResponseCode())) {
return i18n("download.code." + responseCodeException.getResponseCode(), uri);
return i18n("download.code." + responseCodeException.getResponseCode(), url);
} else {
return i18n("install.failed.downloading.detail", uri) + "\n" + StringUtils.getStackTrace(exception.getCause());
return i18n("install.failed.downloading.detail", url) + "\n" + StringUtils.getStackTrace(exception.getCause());
}
} else if (exception.getCause() instanceof FileNotFoundException) {
return i18n("download.code.404", uri);
return i18n("download.code.404", url);
} else if (exception.getCause() instanceof AccessDeniedException) {
return i18n("install.failed.downloading.detail", uri) + "\n" + i18n("exception.access_denied", ((AccessDeniedException) exception.getCause()).getFile());
return i18n("install.failed.downloading.detail", url) + "\n" + i18n("exception.access_denied", ((AccessDeniedException) exception.getCause()).getFile());
} else if (exception.getCause() instanceof ArtifactMalformedException) {
return i18n("install.failed.downloading.detail", uri) + "\n" + i18n("exception.artifact_malformed");
return i18n("install.failed.downloading.detail", url) + "\n" + i18n("exception.artifact_malformed");
} else if (exception.getCause() instanceof SSLHandshakeException && !(exception.getCause().getMessage() != null && exception.getCause().getMessage().contains("Remote host terminated"))) {
if (exception.getCause().getMessage() != null && (exception.getCause().getMessage().contains("No name matching") || exception.getCause().getMessage().contains("No subject alternative DNS name matching"))) {
return i18n("install.failed.downloading.detail", uri) + "\n" + i18n("exception.dns.pollution");
return i18n("install.failed.downloading.detail", url) + "\n" + i18n("exception.dns.pollution");
}
return i18n("install.failed.downloading.detail", uri) + "\n" + i18n("exception.ssl_handshake");
return i18n("install.failed.downloading.detail", url) + "\n" + i18n("exception.ssl_handshake");
} else {
return i18n("install.failed.downloading.detail", uri) + "\n" + StringUtils.getStackTrace(exception.getCause());
return i18n("install.failed.downloading.detail", url) + "\n" + StringUtils.getStackTrace(exception.getCause());
}
} else if (exception instanceof ArtifactMalformedException) {
return i18n("exception.artifact_malformed");
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.terracotta;
import kala.compress.archivers.tar.TarArchiveEntry;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.ArtifactMalformedException;
import org.jackhuang.hmcl.task.FetchTask;
import org.jackhuang.hmcl.task.FileDownloadTask;
@@ -29,13 +30,12 @@ import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.UrlResponseInfo;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jetbrains.annotations.Nullable;
import org.jackhuang.hmcl.util.tree.TarFileTree;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.DigestInputStream;
@@ -50,13 +50,16 @@ import static org.jackhuang.hmcl.util.logging.Logger.LOG;
public final class TerracottaBundle {
private final Path root;
private final List<URI> links;
/// Candidate bundle URLs in attempt order.
private final List<WebURL> links;
private final FileDownloadTask.IntegrityCheck hash;
private final Map<String, FileDownloadTask.IntegrityCheck> files;
public TerracottaBundle(Path root, List<URI> links, FileDownloadTask.IntegrityCheck hash, Map<String, FileDownloadTask.IntegrityCheck> files) {
/// Creates a bundle using the supplied download candidates and archive/file integrity metadata.
/// The candidate list and file map are retained without copying.
public TerracottaBundle(Path root, List<WebURL> links, FileDownloadTask.IntegrityCheck hash, Map<String, FileDownloadTask.IntegrityCheck> files) {
this.root = root;
this.links = links;
this.hash = hash;
@@ -22,6 +22,7 @@ import javafx.application.Platform;
import javafx.beans.property.ReadOnlyDoubleWrapper;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.setting.Accounts;
import org.jackhuang.hmcl.task.DownloadException;
@@ -42,7 +43,6 @@ import org.jackhuang.hmcl.util.platform.ManagedProcess;
import org.jackhuang.hmcl.util.platform.SystemUtils;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@@ -288,7 +288,7 @@ public final class TerracottaManager {
public static Task<String> exportLogs() {
if (STATE_V.get() instanceof TerracottaState.PortSpecific portSpecific) {
return new GetTask(URI.create(String.format("http://127.0.0.1:%d/log?fetch=true", portSpecific.port)))
return new GetTask(WebURL.parse(String.format("http://127.0.0.1:%d/log?fetch=true", portSpecific.port)))
.setSignificance(Task.TaskSignificance.MINOR);
}
return Task.completed(null);
@@ -297,7 +297,7 @@ public final class TerracottaManager {
public static TerracottaState.Waiting setWaiting() {
TerracottaState state = STATE_V.get();
if (state instanceof TerracottaState.PortSpecific portSpecific) {
new GetTask(URI.create(String.format("http://127.0.0.1:%d/state/ide", portSpecific.port)))
new GetTask(WebURL.parse(String.format("http://127.0.0.1:%d/state/ide", portSpecific.port)))
.setSignificance(Task.TaskSignificance.MINOR)
.start();
return new TerracottaState.Waiting(-1, -1, null);
@@ -317,7 +317,7 @@ public final class TerracottaManager {
.thenComposeAsync(nodes -> {
List<Pair<String, String>> query = new ArrayList<>(nodes.size() + 1);
query.add(pair("player", getPlayerName()));
for (URI node : nodes) {
for (WebURL node : nodes) {
query.add(pair("public_nodes", node.toString()));
}
return new GetTask(NetworkUtils.withQuery(
@@ -338,7 +338,7 @@ public final class TerracottaManager {
ArrayList<Pair<String, String>> query = new ArrayList<>(nodes.size() + 2);
query.add(pair("room", room));
query.add(pair("player", getPlayerName()));
for (URI node : nodes) {
for (WebURL node : nodes) {
query.add(pair("public_nodes", node.toString()));
}
return new GetTask(NetworkUtils.withQuery("http://127.0.0.1:%d/state/guesting".formatted(portSpecific.port), query))
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.terracotta;
import com.google.gson.annotations.SerializedName;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.terracotta.provider.AbstractTerracottaProvider;
@@ -35,10 +36,10 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jackhuang.hmcl.util.versioning.VersionNumber;
import org.jackhuang.hmcl.util.versioning.VersionRange;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -90,8 +91,8 @@ public final class TerracottaMetadata {
}
Stream<String> stream = downloads.stream(), streamCN = downloadsCN.stream();
List<URI> links = (LocaleUtils.IS_CHINA_MAINLAND ? Stream.concat(streamCN, stream) : Stream.concat(stream, streamCN))
.map(link -> URI.create(options.replace(link)))
@Unmodifiable List<WebURL> links = (LocaleUtils.IS_CHINA_MAINLAND ? Stream.concat(streamCN, stream) : Stream.concat(stream, streamCN))
.map(link -> WebURL.parse(options.replace(link)))
.toList();
Map<String, FileDownloadTask.IntegrityCheck> files = pkg.files.entrySet().stream().collect(Collectors.toUnmodifiableMap(
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.terracotta;
import com.google.gson.JsonParseException;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.JsonSerializable;
import org.jackhuang.hmcl.util.gson.JsonUtils;
@@ -26,9 +27,8 @@ import org.jackhuang.hmcl.util.gson.Validation;
import org.jackhuang.hmcl.util.i18n.LocaleUtils;
import org.jackhuang.hmcl.util.io.HttpRequest;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
@@ -43,17 +43,19 @@ public final class TerracottaNodeList {
public void validate() throws JsonParseException, TolerableValidationException {
Validation.requireNonNull(url, "TerracottaNode.url cannot be null");
try {
new URI(url);
} catch (URISyntaxException e) {
WebURL.parse(url);
} catch (IllegalArgumentException e) {
throw new JsonParseException("Invalid URL: " + url, e);
}
}
}
private static volatile List<URI> list;
/// Cached node URLs, or `null` before the first fetch completes.
private static volatile @Nullable @Unmodifiable List<WebURL> list;
public static List<URI> fetch() {
List<URI> list = TerracottaNodeList.list;
/// Returns the cached unmodifiable node URLs, fetching once and caching an empty list on failure.
public static @Unmodifiable List<WebURL> fetch() {
@Nullable @Unmodifiable List<WebURL> list = TerracottaNodeList.list;
if (list != null)
return list;
@@ -84,7 +86,7 @@ public final class TerracottaNodeList {
return StringUtils.isBlank(node.region) || LocaleUtils.IS_CHINA_MAINLAND == "CN".equalsIgnoreCase(node.region);
})
.map(it -> URI.create(it.url()))
.map(it -> WebURL.parse(it.url()))
.toList();
LOG.info("Terracotta node list: " + list);
}
@@ -26,6 +26,7 @@ import org.glavo.monetfx.Brightness;
import org.glavo.monetfx.ColorRole;
import org.glavo.monetfx.ColorStyle;
import org.glavo.monetfx.Contrast;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.setting.BackgroundType;
import org.jackhuang.hmcl.setting.LauncherSettings;
@@ -36,8 +37,8 @@ import org.jackhuang.hmcl.util.MathUtils;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.i18n.LocalizedText;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.ContentEncoding;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.IOUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.NotNullByDefault;
@@ -48,7 +49,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
@@ -1497,28 +1497,28 @@ public final class ThemePackManager {
List<Path> temporaryFiles,
ResolvedBackground background) throws IOException {
String url = requireNonBlank(background.networkImageUrl(), "background.url");
URI uri = NetworkUtils.toURI(url);
if (!NetworkUtils.isHttpUri(uri)) {
WebURL webUrl = WebURL.parse(url);
if (!NetworkUtils.isHttpUri(webUrl)) {
throw new IOException("Theme background URL must be HTTP or HTTPS: " + url);
}
String entryName = "assets/wallpapers/" + networkBackgroundAssetName(uri);
String entryName = "assets/wallpapers/" + networkBackgroundAssetName(webUrl);
if (background.networkImageCachePolicy() == NetworkBackgroundImageCachePolicy.ENABLED) {
try {
@Nullable Path cachedFile = new CacheFileTask(uri).run();
@Nullable Path cachedFile = new CacheFileTask(webUrl).run();
if (cachedFile != null && Files.isRegularFile(cachedFile)) {
assets.add(new ThemePackAsset(cachedFile, entryName));
return new ThemeBackground.Image(entryName);
}
} catch (Exception e) {
LOG.warning("Failed to cache theme background, falling back to direct download: " + uri, e);
LOG.warning("Failed to cache theme background, falling back to direct download: " + webUrl, e);
}
}
Path temporaryFile = Files.createTempFile("hmcl-theme-background-", ".tmp");
boolean success = false;
try {
downloadNetworkBackground(uri, temporaryFile);
downloadNetworkBackground(webUrl, temporaryFile);
assets.add(new ThemePackAsset(temporaryFile, entryName));
temporaryFiles.add(temporaryFile);
success = true;
@@ -1535,12 +1535,12 @@ public final class ThemePackManager {
}
/// Downloads one network background into a temporary file without installing it into the persistent image cache.
private static void downloadNetworkBackground(URI uri, Path outputFile) throws IOException {
HttpURLConnection connection = NetworkUtils.resolveConnection(NetworkUtils.createHttpConnection(uri));
private static void downloadNetworkBackground(WebURL url, Path outputFile) throws IOException {
HttpURLConnection connection = NetworkUtils.resolveConnection(NetworkUtils.createHttpConnection(url));
try {
int responseCode = connection.getResponseCode();
if (responseCode / 100 != 2) {
throw new IOException("Failed to download theme background: HTTP " + responseCode + " " + uri);
throw new IOException("Failed to download theme background: HTTP " + responseCode + " " + url);
}
ContentEncoding contentEncoding = ContentEncoding.fromConnection(connection);
@@ -1554,8 +1554,8 @@ public final class ThemePackManager {
}
/// Returns a safe theme-pack asset file name for a downloaded network background.
private static String networkBackgroundAssetName(URI uri) {
String path = Objects.toString(uri.getPath(), "");
private static String networkBackgroundAssetName(WebURL url) {
String path = Objects.toString(url.getPath(), "");
@Nullable Path fileNamePath = path.isBlank() ? null : Path.of(path).getFileName();
String fileName = fileNamePath != null ? fileNamePath.toString() : "";
String sanitized = sanitizePathSegment(fileName);
@@ -606,20 +606,20 @@ public final class Controllers {
}
}
public static void openUriOrCopy(@Nullable URI uri) {
if (uri == null) return;
openUriOrCopy(uri.toString());
public static void openUriOrCopy(@Nullable URI url) {
if (url == null) return;
openUriOrCopy(url.toString());
}
public static void openUriOrCopy(@Nullable String uri) {
if (uri == null) return;
public static void openUriOrCopy(@Nullable String url) {
if (url == null) return;
var dialog = new MessageDialogPane.Builder(
i18n("web.open_in_browser", uri),
i18n("web.open_in_browser", url),
i18n("message.confirm"),
MessageDialogPane.MessageType.QUESTION
)
.addAction(i18n("button.copy"), () -> FXUtils.copyText(uri))
.yesOrNo(() -> FXUtils.openLink(uri), null)
.addAction(i18n("button.copy"), () -> FXUtils.copyText(url))
.yesOrNo(() -> FXUtils.openLink(url), null)
.build();
dialog(dialog);
}
@@ -613,21 +613,21 @@ public final class FXUtils {
if (link == null)
return;
String uri = NetworkUtils.encodeLocation(link);
String url = NetworkUtils.encodeLocation(link);
thread(() -> {
try {
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
Runtime.getRuntime().exec(new String[]{"rundll32.exe", "url.dll,FileProtocolHandler", uri});
Runtime.getRuntime().exec(new String[]{"rundll32.exe", "url.dll,FileProtocolHandler", url});
return;
} else if (OperatingSystem.CURRENT_OS == OperatingSystem.MACOS) {
Runtime.getRuntime().exec(new String[]{"open", uri});
Runtime.getRuntime().exec(new String[]{"open", url});
return;
} else {
for (String browser : linuxBrowsers) {
Path path = SystemUtils.which(browser);
if (path != null) {
try {
Runtime.getRuntime().exec(new String[]{path.toString(), uri});
Runtime.getRuntime().exec(new String[]{path.toString(), url});
return;
} catch (Throwable ignored) {
}
@@ -640,7 +640,7 @@ public final class FXUtils {
}
try {
java.awt.Desktop.getDesktop().browse(new URI(uri));
java.awt.Desktop.getDesktop().browse(new URI(url));
} catch (Throwable e) {
LOG.warning("Failed to open link: " + link, e);
}
@@ -1001,8 +1001,9 @@ public final class FXUtils {
.setSignificance(Task.TaskSignificance.MINOR);
}
public static Task<Image> getRemoteImageTask(List<URI> uris, int requestedWidth, int requestedHeight, boolean preserveRatio, boolean smooth) {
return new CacheFileTask(uris)
/// Creates a task that caches an image from HTTP(S) candidates and loads it with the requested sizing options.
public static Task<Image> getRemoteImageTask(List<WebURL> urls, int requestedWidth, int requestedHeight, boolean preserveRatio, boolean smooth) {
return new CacheFileTask(urls)
.setSignificance(Task.TaskSignificance.MINOR)
.thenApplyAsync(file -> loadImage(file, requestedWidth, requestedHeight, preserveRatio, smooth))
.setSignificance(Task.TaskSignificance.MINOR);
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.ui.download;
import javafx.scene.Node;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.*;
import org.jackhuang.hmcl.download.game.GameAssetIndexDownloadTask;
import org.jackhuang.hmcl.download.game.LibraryDownloadException;
@@ -39,7 +40,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.util.ArrayList;
import java.util.concurrent.CancellationException;
import java.util.zip.ZipException;
@@ -137,27 +137,27 @@ public final class UpdateInstallerWizardProvider implements WizardProvider {
String message = i18n("launch.failed.download_library", ((LibraryDownloadException) exception).getLibrary().name()) + "\n";
if (exception.getCause() instanceof ResponseCodeException rce) {
int responseCode = rce.getResponseCode();
String uri = rce.getUri();
String url = rce.getUrl();
if (responseCode == 404)
message += i18n("download.code.404", uri);
message += i18n("download.code.404", url);
else
message += i18n("download.failed", uri, responseCode);
message += i18n("download.failed", url, responseCode);
} else {
message += StringUtils.getStackTrace(exception.getCause());
}
Controllers.dialog(message, i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
} else if (exception instanceof DownloadException) {
URI uri = ((DownloadException) exception).getUri();
} else if (exception instanceof DownloadException de) {
WebURL url = de.getUrl();
if (exception.getCause() instanceof SocketTimeoutException) {
Controllers.dialog(i18n("install.failed.downloading.timeout", uri), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
Controllers.dialog(i18n("install.failed.downloading.timeout", url), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
} else if (exception.getCause() instanceof ResponseCodeException responseCodeException) {
if (I18n.hasKey("download.code." + responseCodeException.getResponseCode())) {
Controllers.dialog(i18n("download.code." + responseCodeException.getResponseCode(), uri), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
Controllers.dialog(i18n("download.code." + responseCodeException.getResponseCode(), url), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
} else {
Controllers.dialog(i18n("install.failed.downloading.detail", uri) + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
Controllers.dialog(i18n("install.failed.downloading.detail", url) + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
}
} else {
Controllers.dialog(i18n("install.failed.downloading.detail", uri) + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
Controllers.dialog(i18n("install.failed.downloading.detail", url) + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageDialogPane.MessageType.ERROR, next);
}
} else if (exception instanceof UnsupportedInstallationException unsupportedInstallationException) {
switch (unsupportedInstallationException.getReason()) {
@@ -37,11 +37,12 @@ import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.game.*;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.addon.RemoteAddon;
import org.jackhuang.hmcl.addon.RemoteAddonRepository;
import org.jackhuang.hmcl.addon.repository.ModrinthRemoteAddonRepository;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.setting.DownloadProviders;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
@@ -57,7 +58,6 @@ import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
@@ -252,8 +252,8 @@ public class DownloadListPage extends Control implements DecoratorPage {
iconLoader = new RemoteImageLoader(control.downloadProvider) {
@Override
protected @NotNull Task<Image> createLoadTask(@NotNull List<URI> uris) {
return FXUtils.getRemoteImageTask(uris, 80, 80, true, true);
protected @NotNull Task<Image> createLoadTask(@NotNull List<WebURL> urls) {
return FXUtils.getRemoteImageTask(urls, 80, 80, true, true);
}
};
@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.ui.instances;
import com.jfoenix.controls.JFXButton;
import javafx.application.Platform;
import javafx.stage.FileChooser;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.addon.RemoteAddon;
import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorAccount;
@@ -49,9 +50,9 @@ import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
@@ -81,7 +82,7 @@ public final class Instances {
public static void downloadModpackImpl(DownloadProvider downloadProvider, HMCLGameRepository repository, GameInstanceID instanceId, RemoteAddon mod, RemoteAddon.Version file) {
Path modpack;
List<URI> downloadURLs;
@Unmodifiable List<WebURL> downloadURLs;
try {
downloadURLs = downloadProvider.injectURLWithCandidates(file.file().url());
modpack = Files.createTempFile("modpack", ".zip");
@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.util;
import javafx.beans.value.WritableValue;
import javafx.scene.image.Image;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
@@ -27,7 +28,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.util.*;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
@@ -35,9 +35,12 @@ import static org.jackhuang.hmcl.util.logging.Logger.LOG;
/// @author Glavo
public abstract class RemoteImageLoader {
private final DownloadProvider downloadProvider;
private final Map<URI, WeakReference<Image>> cache = new HashMap<>();
private final Map<URI, List<WeakReference<WritableValue<Image>>>> pendingRequests = new HashMap<>();
private final WeakHashMap<WritableValue<Image>, URI> reverseLookup = new WeakHashMap<>();
/// Loaded images indexed by normalized URL; image references do not prevent garbage collection.
private final Map<WebURL, WeakReference<@Nullable Image>> cache = new HashMap<>();
/// Image targets awaiting each URL's active load task.
private final Map<WebURL, List<WeakReference<WritableValue<Image>>>> pendingRequests = new HashMap<>();
/// Current requested URL for each target, used to discard obsolete load completions.
private final WeakHashMap<WritableValue<Image>, WebURL> reverseLookup = new WeakHashMap<>();
public RemoteImageLoader(DownloadProvider downloadProvider) {
this.downloadProvider = downloadProvider;
@@ -47,59 +50,61 @@ public abstract class RemoteImageLoader {
return null;
}
protected abstract @NotNull Task<Image> createLoadTask(@NotNull List<URI> uris);
/// Creates a task that loads an image from candidate URLs in attempt order.
protected abstract @NotNull Task<Image> createLoadTask(@NotNull List<WebURL> urls);
/// Loads an image or assigns the placeholder when the URL is absent or invalid.
@FXThread
public void load(@NotNull WritableValue<Image> writableValue, String url) {
URI uri = NetworkUtils.toURIOrNull(url);
if (uri == null) {
public void load(@NotNull WritableValue<Image> writableValue, @Nullable String url) {
@Nullable WebURL webUrl = NetworkUtils.toWebURLOrNull(url);
if (webUrl == null) {
reverseLookup.remove(writableValue);
writableValue.setValue(getPlaceholder());
return;
}
WeakReference<Image> reference = cache.get(uri);
@Nullable WeakReference<@Nullable Image> reference = cache.get(webUrl);
if (reference != null) {
Image image = reference.get();
@Nullable Image image = reference.get();
if (image != null) {
reverseLookup.remove(writableValue);
writableValue.setValue(image);
return;
}
cache.remove(uri);
cache.remove(webUrl);
}
writableValue.setValue(getPlaceholder());
{
List<WeakReference<WritableValue<Image>>> list = pendingRequests.get(uri);
@Nullable List<WeakReference<WritableValue<Image>>> list = pendingRequests.get(webUrl);
if (list != null) {
list.add(new WeakReference<>(writableValue));
reverseLookup.put(writableValue, uri);
reverseLookup.put(writableValue, webUrl);
return;
} else {
list = new ArrayList<>(1);
list.add(new WeakReference<>(writableValue));
pendingRequests.put(uri, list);
reverseLookup.put(writableValue, uri);
pendingRequests.put(webUrl, list);
reverseLookup.put(writableValue, webUrl);
}
}
createLoadTask(downloadProvider.injectURLWithCandidates(url)).whenComplete(Schedulers.javafx(), (result, exception) -> {
Image image;
@Nullable Image image;
if (exception == null) {
image = result;
} else {
LOG.warning("Failed to load image from " + uri, exception);
LOG.warning("Failed to load image from " + webUrl, exception);
image = getPlaceholder();
}
cache.put(uri, new WeakReference<>(image));
List<WeakReference<WritableValue<Image>>> list = pendingRequests.remove(uri);
cache.put(webUrl, new WeakReference<>(image));
@Nullable List<WeakReference<WritableValue<Image>>> list = pendingRequests.remove(webUrl);
if (list != null) {
for (WeakReference<WritableValue<Image>> ref : list) {
WritableValue<Image> target = ref.get();
if (target != null && uri.equals(reverseLookup.get(target))) {
@Nullable WritableValue<Image> target = ref.get();
if (target != null && webUrl.equals(reverseLookup.get(target))) {
reverseLookup.remove(target);
target.setValue(image);
}
@@ -17,45 +17,65 @@
*/
package org.jackhuang.hmcl.util.url.data;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Base64;
/**
* @author Glavo
*/
public final class DataUri {
/// Parses a data URL's media type, charset, and percent-decoded payload.
/// Query content is included in the payload; the fragment is excluded.
/// Percent escapes are decoded as UTF-8 before the declared charset is applied by the read methods.
///
/// @author Glavo
public final class DataURL {
/// Scheme identifying inline data URLs.
public static final String SCHEME = "data";
private static IllegalArgumentException invalidUri(URI uri) {
return new IllegalArgumentException("Invalid data URI: " + uri);
/// Creates a parsing failure for a data URL without a metadata/payload separator.
private static IllegalArgumentException invalidUri(WebURL url) {
return new IllegalArgumentException("Invalid data URL: " + url);
}
public static boolean isDataUri(URI uri) {
return uri != null && SCHEME.equals(uri.getScheme());
/// Returns whether the URL uses the data scheme, or `false` for `null`.
public static boolean isDataUri(@Nullable WebURL url) {
return url != null && SCHEME.equals(url.getScheme());
}
/// Trimmed media type and parameters, excluding the Base64 marker.
private final @NotNull String mediaType;
/// Declared charset, or UTF-8 when absent or unrecognized.
private final @NotNull Charset charset;
/// Whether the metadata ends with the case-sensitive Base64 marker.
private final boolean base64;
/// Percent-decoded payload before optional Base64 decoding.
private final @NotNull String rawData;
public DataUri(URI uri) {
if (!uri.getScheme().equals(SCHEME)) {
throw new IllegalArgumentException("URI scheme must be " + SCHEME);
/// Parses metadata and payload without decoding Base64 content.
///
/// @param url the data URL
/// @throws IllegalArgumentException if the scheme is not data or the decoded content has no comma
public DataURL(WebURL url) {
if (!SCHEME.equals(url.getScheme())) {
throw new IllegalArgumentException("URL scheme must be " + SCHEME);
}
String schemeSpecificPart = uri.getSchemeSpecificPart();
if (schemeSpecificPart == null)
throw invalidUri(uri);
String schemeSpecificPart = url.getPath();
@Nullable String authority = url.getAuthority();
if (authority != null)
schemeSpecificPart = "//" + authority + schemeSpecificPart;
// The query delimiter and content belong to the data, while the fragment does not.
@Nullable String query = url.getQuery();
if (query != null)
schemeSpecificPart += "?" + query;
int comma = schemeSpecificPart.indexOf(',');
if (comma < 0)
throw invalidUri(uri);
throw invalidUri(url);
String mediaType = schemeSpecificPart.substring(0, comma);
boolean base64 = mediaType.endsWith(";base64");
@@ -68,22 +88,29 @@ public final class DataUri {
this.rawData = schemeSpecificPart.substring(comma + 1);
}
/// Returns the trimmed media type and parameters without the Base64 marker; may be empty.
public @NotNull String getMediaType() {
return mediaType;
}
/// Returns the declared charset, or UTF-8 when absent or unrecognized.
public @NotNull Charset getCharset() {
return charset;
}
/// Returns whether the payload is marked for Base64 decoding.
public boolean isBase64() {
return base64;
}
/// Returns the percent-decoded payload before optional Base64 decoding, preserving literal plus signs.
public @NotNull String getRawData() {
return rawData;
}
/// Returns a new byte array by decoding Base64 or encoding the text payload with [#getCharset()].
///
/// @throws IOException if a Base64 payload cannot be decoded
public byte[] readBytes() throws IOException {
if (base64) {
try {
@@ -96,6 +123,9 @@ public final class DataUri {
}
}
/// Returns the text payload, decoding Base64 bytes with [#getCharset()] when marked as Base64.
///
/// @throws IOException if a Base64 payload cannot be decoded
public String readString() throws IOException {
if (base64) {
try {
@@ -27,20 +27,20 @@ import java.net.URLConnection;
* @author Glavo
*/
public final class DataURLConnection extends URLConnection {
private final DataUri dataUri;
private final DataURL dataUrl;
private byte[] data;
private InputStream inputStream;
DataURLConnection(URL url, DataUri dataUri) {
DataURLConnection(URL url, DataURL dataUrl) {
super(url);
this.dataUri = dataUri;
this.dataUrl = dataUrl;
}
@Override
public void connect() throws IOException {
if (!connected) {
connected = true;
data = dataUri.readBytes();
data = dataUrl.readBytes();
inputStream = new ByteArrayInputStream(this.data);
}
}
@@ -53,7 +53,7 @@ public final class DataURLConnection extends URLConnection {
@Override
public String getContentType() {
return dataUri.getMediaType();
return dataUrl.getMediaType();
}
@Override
@@ -17,23 +17,26 @@
*/
package org.jackhuang.hmcl.util.url.data;
import org.glavo.url.WebURL;
import java.io.IOException;
import java.net.*;
/**
* @author Glavo
*/
/// Opens data URL connections using [WebURL] to parse the address.
///
/// @author Glavo
public final class DataURLHandle extends URLStreamHandler {
/// Creates a connection without decoding its Base64 payload.
///
/// @throws MalformedURLException if the address cannot be parsed as a data URL
@Override
protected URLConnection openConnection(URL u) throws IOException {
try {
URI uri = u.toURI();
if (!DataUri.isDataUri(uri))
throw new MalformedURLException("URI is not a data URI: " + u);
return new DataURLConnection(u, new DataUri(uri));
} catch (URISyntaxException e) {
throw new MalformedURLException(e.getMessage());
return new DataURLConnection(u, new DataURL(WebURL.of(u)));
} catch (IllegalArgumentException e) {
MalformedURLException exception = new MalformedURLException(e.getMessage());
exception.initCause(e);
throw exception;
}
}
}
@@ -0,0 +1,110 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <[email protected]> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.url.data;
import org.glavo.url.WebURL;
import org.jetbrains.annotations.NotNullByDefault;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.*;
/// Tests data URL parsing and decoding through both WebURL and the Java URL handler.
@NotNullByDefault
public final class DataURLTest {
/// Parses a data URL and reads its text payload.
private static String readString(String url) throws IOException {
return new DataURL(WebURL.parse(url)).readString();
}
/// Parses a data URL and reads its byte payload.
private static byte[] readBytes(String url) throws IOException {
return new DataURL(WebURL.parse(url)).readBytes();
}
/// Ensures percent-encoded and Base64 text payloads are decoded.
@Test
public void testReadString() throws IOException {
assertEquals("Hello, World!", readString("data:,Hello%2C%20World%21"));
assertEquals("Hello, World!", readString("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="));
assertEquals("<h1>Hello, World!</h1>", readString("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E"));
assertEquals("<script>alert('hi');</script>", readString("data:text/html,%3Cscript%3Ealert%28%27hi%27%29%3B%3C%2Fscript%3E"));
}
/// Ensures queries remain part of the payload, fragments are excluded, and escapes are decoded once.
@Test
public void testPayloadDelimiters() throws IOException {
assertEquals("Hello?name=a+b&path=/#end", readString("data:,Hello?name=a+b&path=%2F%23end#ignored"));
assertEquals("Hello?", readString("data:,Hello?#ignored"));
assertEquals("?a+b#c%23", readString("data:,%3Fa%2Bb%23c%2523#ignored"));
assertEquals("a,b", readString("data:,a,b"));
assertEquals("", readString("data:,"));
}
/// Ensures WebURL normalization accepts uppercase schemes and unescaped spaces.
@Test
public void testNormalizedInput() throws IOException {
assertEquals("Hello, World!", readString("DATA:text/plain,Hello, World!"));
assertTrue(DataURL.isDataUri(WebURL.parse("DATA:,value")));
assertFalse(DataURL.isDataUri(WebURL.parse("https://example.com/")));
assertFalse(DataURL.isDataUri(null));
}
/// Ensures payload bytes, Base64 escapes, charset metadata, and literal plus signs are retained.
@Test
public void testReadBytesAndMetadata() throws IOException {
assertArrayEquals("Hello + World!".getBytes(UTF_8), readBytes("data:,Hello%20+%20World!"));
assertArrayEquals(new byte[]{0, (byte) 0xFF, (byte) 0x80}, readBytes("data:application/octet-stream;base64,AP+A"));
assertEquals("Hello", readString("data:;base64,SGVsbG8%3D#ignored"));
DataURL data = new DataURL(WebURL.parse("data:text/plain;charset=ISO-8859-1;base64,Y2Fm6Q=="));
assertEquals("text/plain;charset=ISO-8859-1", data.getMediaType());
assertEquals("ISO-8859-1", data.getCharset().name());
assertTrue(data.isBase64());
assertEquals("Y2Fm6Q==", data.getRawData());
assertEquals("caf\u00E9", data.readString());
}
/// Ensures invalid schemes, missing separators, and invalid Base64 are reported at the appropriate stage.
@Test
public void testInvalidData() {
assertThrows(IllegalArgumentException.class, () -> new DataURL(WebURL.parse("https://example.com/")));
assertThrows(IllegalArgumentException.class, () -> new DataURL(WebURL.parse("data:text/plain")));
assertThrows(IOException.class, () -> readBytes("data:;base64,invalid!"));
assertThrows(IOException.class, () -> readString("data:;base64,invalid!"));
}
/// Ensures Java URL connections use the WebURL parser and expose decoded payload bytes.
@Test
public void testURLHandler() throws IOException {
URLConnection connection = new URL(null, "data:text/plain,Hello world?name=a+b%23c#ignored", new DataURLHandle())
.openConnection();
assertEquals("text/plain", connection.getContentType());
try (var input = connection.getInputStream()) {
assertEquals("Hello world?name=a+b#c", new String(input.readAllBytes(), UTF_8));
}
assertThrows(MalformedURLException.class,
() -> new URL(null, "data:text/plain", new DataURLHandle()).openConnection());
}
}
@@ -1,44 +0,0 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <[email protected]> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.url.data;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URI;
import static org.junit.jupiter.api.Assertions.assertEquals;
public final class DataUriTest {
private static String readString(String uri) throws IOException {
return new DataUri(URI.create(uri)).readString();
}
private static byte[] readBytes(String uri) throws IOException {
return new DataUri(URI.create(uri)).readBytes();
}
@Test
public void testReadString() throws IOException {
assertEquals("Hello, World!", readString("data:,Hello%2C%20World%21"));
assertEquals("Hello, World!", readString("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="));
assertEquals("<h1>Hello, World!</h1>", readString("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E"));
assertEquals("<script>alert('hi');</script>", readString("data:text/html,%3Cscript%3Ealert%28%27hi%27%29%3B%3C%2Fscript%3E"));
}
}
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.addon.repository;
import com.google.gson.reflect.TypeToken;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.addon.AddonLoader;
import org.jackhuang.hmcl.addon.RemoteAddon;
import org.jackhuang.hmcl.addon.RemoteAddonRepository;
@@ -29,10 +30,10 @@ import org.jackhuang.hmcl.util.io.*;
import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
@@ -154,8 +155,8 @@ public final class CurseForgeRemoteAddonRepository implements RemoteAddonReposit
@Nullable Response<List<CurseAddon>> response = null;
@Nullable IOException exception = null;
List<URI> candidates = downloadProvider.injectURLWithCandidates(NetworkUtils.withQuery(PREFIX + "/v1/mods/search", query));
for (URI candidate : candidates) {
@Unmodifiable List<WebURL> candidates = downloadProvider.injectURLWithCandidates(NetworkUtils.withQuery(PREFIX + "/v1/mods/search", query));
for (WebURL candidate : candidates) {
LOG.info("Fetching " + candidate);
try {
response = withApiKey(HttpRequest.GET(candidate.toString()))
@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.addon.repository;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.addon.AddonLoader;
import org.jackhuang.hmcl.addon.RemoteAddon;
import org.jackhuang.hmcl.addon.RemoteAddonRepository;
@@ -39,7 +40,6 @@ import org.jetbrains.annotations.Unmodifiable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.time.Instant;
@@ -182,9 +182,9 @@ public final class ModrinthRemoteAddonRepository implements RemoteAddonRepositor
pair("index", convertSortType(sort))
);
List<URI> candidates = downloadProvider.injectURLWithCandidates(NetworkUtils.withQuery(PREFIX + "/v2/search", query));
@Unmodifiable List<WebURL> candidates = downloadProvider.injectURLWithCandidates(NetworkUtils.withQuery(PREFIX + "/v2/search", query));
IOException exception = null;
for (URI candidate : candidates) {
for (WebURL candidate : candidates) {
try {
LOG.info("Fetching " + candidate);
Response<ProjectSearchResult> response = HttpRequest.GET(candidate.toString())
@@ -246,10 +246,10 @@ public final class ModrinthRemoteAddonRepository implements RemoteAddonRepositor
SEMAPHORE.acquireUninterruptibly();
try {
id = StringUtils.removePrefix(id, "local-");
List<URI> candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/project/" + id);
@Unmodifiable List<WebURL> candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/project/" + id);
IOException exception = null;
for (URI candidate : candidates) {
for (WebURL candidate : candidates) {
try {
Project project = HttpRequest.GET(candidate.toString()).getJson(Project.class);
return project.toAddon();
@@ -303,10 +303,10 @@ public final class ModrinthRemoteAddonRepository implements RemoteAddonRepositor
try {
id = StringUtils.removePrefix(id, "local-");
List<URI> candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/project/" + id + "/version?include_changelog=false");
@Unmodifiable List<WebURL> candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/project/" + id + "/version?include_changelog=false");
IOException exception = null;
for (URI candidate : candidates) {
for (WebURL candidate : candidates) {
try {
List<ProjectVersion> versions = HttpRequest.GET(candidate.toString())
.getJson(listTypeOf(ProjectVersion.class));
@@ -334,15 +334,15 @@ public final class ModrinthRemoteAddonRepository implements RemoteAddonRepositor
public String getAddonChangelog(DownloadProvider downloadProvider, String addonId, String versionId) throws IOException {
SEMAPHORE.acquireUninterruptibly();
try {
List<URI> candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/version/" + versionId);
@Unmodifiable List<WebURL> candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/version/" + versionId);
IOException exception = null;
for (URI uri : candidates) {
for (WebURL url : candidates) {
try {
ProjectVersion version = HttpRequest.GET(uri.toString()).getJson(ProjectVersion.class);
ProjectVersion version = HttpRequest.GET(url.toString()).getJson(ProjectVersion.class);
return version.changelog();
} catch (IOException e) {
IOException wrapper = new IOException("Failed to get addon changelog: " + uri, e);
IOException wrapper = new IOException("Failed to get addon changelog: " + url, e);
if (candidates.size() == 1) {
exception = wrapper;
} else {
@@ -39,12 +39,12 @@ public final class AuthlibInjectorDnD {
private AuthlibInjectorDnD() {}
public static Optional<String> parseUrlFromDragboard(Dragboard dragboard) {
String uri = dragboard.getString();
if (uri == null) return Optional.empty();
String url = dragboard.getString();
if (url == null) return Optional.empty();
String[] uriElements = uri.split(":");
if (uriElements.length == 3 && SCHEME.equals(uriElements[0]) && PATH_YGGDRASIL_SERVER.equals(uriElements[1])) {
return Optional.of(decodeURL(uriElements[2]));
String[] urlElements = url.split(":");
if (urlElements.length == 3 && SCHEME.equals(urlElements[0]) && PATH_YGGDRASIL_SERVER.equals(urlElements[1])) {
return Optional.of(decodeURL(urlElements[2]));
}
return Optional.empty();
}
@@ -17,11 +17,11 @@
*/
package org.jackhuang.hmcl.auth.authlibinjector;
import org.glavo.url.WebURL;
import org.glavo.uuid.UUIDs;
import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilProvider;
import java.net.URI;
import java.util.UUID;
public class AuthlibInjectorProvider implements YggdrasilProvider {
@@ -33,33 +33,33 @@ public class AuthlibInjectorProvider implements YggdrasilProvider {
}
@Override
public URI getAuthenticationURL() throws AuthenticationException {
return URI.create(apiRoot + "authserver/authenticate");
public WebURL getAuthenticationURL() throws AuthenticationException {
return WebURL.parse(apiRoot + "authserver/authenticate");
}
@Override
public URI getRefreshmentURL() throws AuthenticationException {
return URI.create(apiRoot + "authserver/refresh");
public WebURL getRefreshmentURL() throws AuthenticationException {
return WebURL.parse(apiRoot + "authserver/refresh");
}
@Override
public URI getValidationURL() throws AuthenticationException {
return URI.create(apiRoot + "authserver/validate");
public WebURL getValidationURL() throws AuthenticationException {
return WebURL.parse(apiRoot + "authserver/validate");
}
@Override
public URI getInvalidationURL() throws AuthenticationException {
return URI.create(apiRoot + "authserver/invalidate");
public WebURL getInvalidationURL() throws AuthenticationException {
return WebURL.parse(apiRoot + "authserver/invalidate");
}
@Override
public URI getSkinUploadURL(UUID uuid) throws UnsupportedOperationException {
return URI.create(apiRoot + "api/user/profile/" + UUIDs.toCompactString(uuid) + "/skin");
public WebURL getSkinUploadURL(UUID uuid) throws UnsupportedOperationException {
return WebURL.parse(apiRoot + "api/user/profile/" + UUIDs.toCompactString(uuid) + "/skin");
}
@Override
public URI getProfilePropertiesURL(UUID uuid) throws AuthenticationException {
return URI.create(apiRoot + "sessionserver/session/minecraft/profile/" + UUIDs.toCompactString(uuid));
public WebURL getProfilePropertiesURL(UUID uuid) throws AuthenticationException {
return WebURL.parse(apiRoot + "sessionserver/session/minecraft/profile/" + UUIDs.toCompactString(uuid));
}
@Override
@@ -22,6 +22,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import org.glavo.url.WebURL;
import org.glavo.uuid.UUIDs;
import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.OAuth;
@@ -39,7 +40,6 @@ import org.jackhuang.hmcl.util.javafx.ObservableOptionalCache;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
@@ -290,13 +290,13 @@ public class MicrosoftService {
String response = NetworkUtils.readFullyAsString(con);
if (StringUtils.isBlank(response)) {
if (con.getResponseCode() / 100 != 2)
throw new ResponseCodeException(con.getURL().toURI(), con.getResponseCode());
throw new ResponseCodeException(WebURL.of(con.getURL()), con.getResponseCode());
} else {
MinecraftErrorResponse profileResponse = GSON.fromJson(response, MinecraftErrorResponse.class);
if (StringUtils.isNotBlank(profileResponse.errorMessage) || con.getResponseCode() / 100 != 2)
throw new AuthenticationException("Failed to upload skin, response code: " + con.getResponseCode() + ", response: " + response);
}
} catch (IOException | JsonParseException | URISyntaxException e) {
} catch (IOException | JsonParseException e) {
throw new AuthenticationException(e);
}
}
@@ -306,7 +306,7 @@ public class MicrosoftService {
if (payload == null)
return NetworkUtils.doGet(url);
else
return NetworkUtils.doPost(NetworkUtils.toURI(url), payload instanceof String ? (String) payload : GSON.toJson(payload), "application/json");
return NetworkUtils.doPost(WebURL.parse(url), payload instanceof String ? (String) payload : GSON.toJson(payload), "application/json");
} catch (IOException e) {
throw new ServerDisconnectException(e);
}
@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.auth.offline;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import javafx.scene.image.Image;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.auth.yggdrasil.TextureModel;
import org.jackhuang.hmcl.task.FetchTask;
import org.jackhuang.hmcl.task.GetTask;
@@ -185,8 +186,8 @@ public record Skin(Type type, String cslApi, TextureModel textureModel, String l
private static class FetchBytesTask extends FetchTask<InputStream> {
public FetchBytesTask(String uri) {
super(List.of(NetworkUtils.toURI(uri)));
public FetchBytesTask(String url) {
super(List.of(WebURL.parse(url)));
}
@Override
@@ -17,42 +17,45 @@
*/
package org.jackhuang.hmcl.auth.yggdrasil;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.auth.AuthenticationException;
import java.net.URI;
import java.util.UUID;
/**
* @see <a href="http://wiki.vg">http://wiki.vg</a>
*/
/// Supplies the endpoints used by [YggdrasilService] for authentication and profile operations.
///
/// @see <a href="http://wiki.vg">http://wiki.vg</a>
public interface YggdrasilProvider {
URI getAuthenticationURL() throws AuthenticationException;
/// Returns the endpoint for authenticating credentials.
WebURL getAuthenticationURL() throws AuthenticationException;
URI getRefreshmentURL() throws AuthenticationException;
/// Returns the endpoint for refreshing an access token.
WebURL getRefreshmentURL() throws AuthenticationException;
URI getValidationURL() throws AuthenticationException;
/// Returns the endpoint for validating an access token.
WebURL getValidationURL() throws AuthenticationException;
URI getInvalidationURL() throws AuthenticationException;
/// Returns the endpoint for invalidating an access token.
WebURL getInvalidationURL() throws AuthenticationException;
/**
* URL to upload skin.
*
* Headers:
* Authentication: Bearer &lt;access token&gt;
*
* Payload:
* The payload for this API consists of multipart form data. There are two parts (order does not matter b/c of boundary):
* model: Empty string for the default model and "slim" for the slim model
* file: Raw image file data
*
* @see <a href="https://wiki.vg/Mojang_API#Upload_Skin">https://wiki.vg/Mojang_API#Upload_Skin</a>
* @return url to upload skin
* @throws AuthenticationException if url cannot be generated. e.g. some parameter or query is malformed.
* @throws UnsupportedOperationException if the Yggdrasil provider does not support third-party skin uploading.
*/
URI getSkinUploadURL(UUID uuid) throws AuthenticationException, UnsupportedOperationException;
/// URL to upload skin.
///
/// Headers:
/// Authentication: Bearer &lt;access token&gt;
///
/// Payload:
/// The payload for this API consists of multipart form data. There are two parts (order does not matter b/c of boundary):
/// model: Empty string for the default model and "slim" for the slim model
/// file: Raw image file data
///
/// @see <a href="https://wiki.vg/Mojang_API#Upload_Skin">https://wiki.vg/Mojang_API#Upload_Skin</a>
/// @return url to upload skin
/// @throws AuthenticationException if url cannot be generated. e.g. some parameter or query is malformed.
/// @throws UnsupportedOperationException if the Yggdrasil provider does not support third-party skin uploading.
WebURL getSkinUploadURL(UUID uuid) throws AuthenticationException, UnsupportedOperationException;
URI getProfilePropertiesURL(UUID uuid) throws AuthenticationException;
/// Returns the endpoint for reading the profile properties of the given player UUID.
WebURL getProfilePropertiesURL(UUID uuid) throws AuthenticationException;
}
@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.auth.yggdrasil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import org.glavo.url.WebURL;
import org.glavo.uuid.UUIDs;
import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.ServerDisconnectException;
@@ -30,11 +31,11 @@ import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.HttpMultipartRequest;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.javafx.ObservableOptionalCache;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
@@ -227,12 +228,13 @@ public class YggdrasilService {
}
}
private static String request(URI uri, Object payload) throws AuthenticationException {
/// Performs a GET when the payload is null, or posts the payload as JSON otherwise.
private static String request(WebURL url, @Nullable Object payload) throws AuthenticationException {
try {
if (payload == null)
return NetworkUtils.doGet(uri);
return NetworkUtils.doGet(url);
else
return NetworkUtils.doPost(uri, payload instanceof String ? (String) payload : GSON.toJson(payload), "application/json");
return NetworkUtils.doPost(url, payload instanceof String ? (String) payload : GSON.toJson(payload), "application/json");
} catch (IOException e) {
throw new ServerDisconnectException(e);
}
@@ -17,10 +17,11 @@
*/
package org.jackhuang.hmcl.download;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.game.GameComponentType;
import org.jackhuang.hmcl.task.Task;
import org.jetbrains.annotations.Unmodifiable;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
@@ -65,10 +66,11 @@ public final class AutoDownloadProvider implements DownloadProvider {
return fileProviders.get(0);
}
private static List<URI> getAll(
/// Collects candidates in provider order, removing duplicates by normalized URL equality.
private static @Unmodifiable List<WebURL> getAll(
List<DownloadProvider> providers,
Function<DownloadProvider, List<URI>> function) {
LinkedHashSet<URI> result = new LinkedHashSet<>();
Function<DownloadProvider, List<WebURL>> function) {
LinkedHashSet<WebURL> result = new LinkedHashSet<>();
for (DownloadProvider provider : providers) {
result.addAll(function.apply(provider));
}
@@ -76,7 +78,7 @@ public final class AutoDownloadProvider implements DownloadProvider {
}
@Override
public List<URI> getVersionListURLs() {
public @Unmodifiable List<WebURL> getVersionListURLs() {
return getAll(versionListProviders, DownloadProvider::getVersionListURLs);
}
@@ -86,17 +88,17 @@ public final class AutoDownloadProvider implements DownloadProvider {
}
@Override
public List<URI> getAssetObjectCandidates(String assetObjectLocation) {
public @Unmodifiable List<WebURL> getAssetObjectCandidates(String assetObjectLocation) {
return getAll(fileProviders, provider -> provider.getAssetObjectCandidates(assetObjectLocation));
}
@Override
public List<URI> injectURLWithCandidates(String baseURL) {
public @Unmodifiable List<WebURL> injectURLWithCandidates(String baseURL) {
return getAll(fileProviders, provider -> provider.injectURLWithCandidates(baseURL));
}
@Override
public List<URI> injectURLsWithCandidates(List<String> urls) {
public @Unmodifiable List<WebURL> injectURLsWithCandidates(List<String> urls) {
return getAll(fileProviders, provider -> provider.injectURLsWithCandidates(urls));
}
@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.download;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.cleanroom.CleanroomVersionList;
import org.jackhuang.hmcl.download.fabric.FabricAPIVersionList;
import org.jackhuang.hmcl.download.fabric.FabricVersionList;
@@ -31,9 +32,8 @@ import org.jackhuang.hmcl.download.quilt.QuiltAPIVersionList;
import org.jackhuang.hmcl.download.quilt.QuiltVersionList;
import org.jackhuang.hmcl.game.GameComponentType;
import org.jackhuang.hmcl.util.Pair;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.Unmodifiable;
import java.net.URI;
import java.util.List;
import static org.jackhuang.hmcl.util.Pair.pair;
@@ -111,13 +111,13 @@ public final class BMCLAPIDownloadProvider implements DownloadProvider {
}
@Override
public List<URI> getVersionListURLs() {
return List.of(URI.create(apiRoot + "/mc/game/version_manifest.json"));
public @Unmodifiable List<WebURL> getVersionListURLs() {
return List.of(WebURL.parse(apiRoot + "/mc/game/version_manifest.json"));
}
@Override
public List<URI> getAssetObjectCandidates(String assetObjectLocation) {
return List.of(NetworkUtils.toURI(apiRoot + "/assets/" + assetObjectLocation));
public @Unmodifiable List<WebURL> getAssetObjectCandidates(String assetObjectLocation) {
return List.of(WebURL.parse(apiRoot + "/assets/" + assetObjectLocation));
}
@Override
@@ -153,20 +153,20 @@ public final class BMCLAPIDownloadProvider implements DownloadProvider {
}
@Override
public List<URI> injectURLWithCandidates(String baseURL) {
public @Unmodifiable List<WebURL> injectURLWithCandidates(String baseURL) {
String injected = injectURL(replacement, baseURL);
if (injected.equals(baseURL)) {
String fallbackInjected = injectURL(fallbackReplacement, baseURL);
if (fallbackInjected.equals(baseURL)) {
return List.of(NetworkUtils.toURI(baseURL));
return List.of(WebURL.parse(baseURL));
} else {
return List.of(
NetworkUtils.toURI(baseURL),
NetworkUtils.toURI(fallbackInjected)
WebURL.parse(baseURL),
WebURL.parse(fallbackInjected)
);
}
} else {
return List.of(NetworkUtils.toURI(injected));
return List.of(WebURL.parse(injected));
}
}
@@ -17,10 +17,10 @@
*/
package org.jackhuang.hmcl.download;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.game.GameComponentType;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.Unmodifiable;
import java.net.URI;
import java.util.LinkedHashSet;
import java.util.List;
@@ -29,18 +29,11 @@ import java.util.List;
/// @author huangyuhui
public interface DownloadProvider {
List<URI> getVersionListURLs();
/// Returns unmodifiable candidate URLs for the Minecraft version manifest, in attempt order.
@Unmodifiable List<WebURL> getVersionListURLs();
List<URI> getAssetObjectCandidates(String assetObjectLocation);
/// Inject into original URL provided by Mojang and Forge.
///
/// Since there are many provided URLs that are written in JSONs and are unmodifiable,
/// this method provides a way to change them.
///
/// @param baseURL original URL provided by Mojang and Forge.
/// @return the URL that is equivalent to `baseURL``, but belongs to your own service provider.
String injectURL(String baseURL);
/// Returns unmodifiable candidate URLs for an asset's relative object location, in attempt order.
@Unmodifiable List<WebURL> getAssetObjectCandidates(String assetObjectLocation);
/// Inject into original URL provided by Mojang and Forge.
///
@@ -49,12 +42,20 @@ public interface DownloadProvider {
///
/// @param baseURL original URL provided by Mojang and Forge.
/// @return the URL that is equivalent to `baseURL`, but belongs to your own service provider.
default List<URI> injectURLWithCandidates(String baseURL) {
return List.of(NetworkUtils.toURI(injectURL(baseURL)));
String injectURL(String baseURL);
/// Returns unmodifiable download candidates for an original URL, in attempt order.
/// The default implementation parses the result of [#injectURL(String)].
///
/// @param baseURL original URL provided by Mojang and Forge.
/// @return the candidate URLs
default @Unmodifiable List<WebURL> injectURLWithCandidates(String baseURL) {
return List.of(WebURL.parse(injectURL(baseURL)));
}
default List<URI> injectURLsWithCandidates(List<String> urls) {
LinkedHashSet<URI> result = new LinkedHashSet<>();
/// Returns unmodifiable candidates for all URLs, preserving first occurrence order and removing duplicates.
default @Unmodifiable List<WebURL> injectURLsWithCandidates(List<String> urls) {
LinkedHashSet<WebURL> result = new LinkedHashSet<>();
for (String url : urls) {
result.addAll(injectURLWithCandidates(url));
}
@@ -17,10 +17,11 @@
*/
package org.jackhuang.hmcl.download;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.game.GameComponentType;
import org.jackhuang.hmcl.task.Task;
import org.jetbrains.annotations.Unmodifiable;
import java.net.URI;
import java.util.List;
import java.util.Objects;
@@ -44,12 +45,12 @@ public final class DownloadProviderWrapper implements DownloadProvider {
}
@Override
public List<URI> getAssetObjectCandidates(String assetObjectLocation) {
public @Unmodifiable List<WebURL> getAssetObjectCandidates(String assetObjectLocation) {
return getProvider().getAssetObjectCandidates(assetObjectLocation);
}
@Override
public List<URI> getVersionListURLs() {
public @Unmodifiable List<WebURL> getVersionListURLs() {
return getProvider().getVersionListURLs();
}
@@ -59,12 +60,12 @@ public final class DownloadProviderWrapper implements DownloadProvider {
}
@Override
public List<URI> injectURLWithCandidates(String baseURL) {
public @Unmodifiable List<WebURL> injectURLWithCandidates(String baseURL) {
return getProvider().injectURLWithCandidates(baseURL);
}
@Override
public List<URI> injectURLsWithCandidates(List<String> urls) {
public @Unmodifiable List<WebURL> injectURLsWithCandidates(List<String> urls) {
return getProvider().injectURLsWithCandidates(urls);
}
@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.download;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.cleanroom.CleanroomVersionList;
import org.jackhuang.hmcl.download.fabric.FabricAPIVersionList;
import org.jackhuang.hmcl.download.fabric.FabricVersionList;
@@ -30,9 +31,8 @@ import org.jackhuang.hmcl.download.optifine.OptiFineBMCLVersionList;
import org.jackhuang.hmcl.download.quilt.QuiltAPIVersionList;
import org.jackhuang.hmcl.download.quilt.QuiltVersionList;
import org.jackhuang.hmcl.game.GameComponentType;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.Unmodifiable;
import java.net.URI;
import java.util.List;
/**
@@ -72,13 +72,13 @@ public class MojangDownloadProvider implements DownloadProvider {
}
@Override
public List<URI> getVersionListURLs() {
return List.of(URI.create("https://piston-meta.mojang.com/mc/game/version_manifest.json"));
public @Unmodifiable List<WebURL> getVersionListURLs() {
return List.of(WebURL.parse("https://piston-meta.mojang.com/mc/game/version_manifest.json"));
}
@Override
public List<URI> getAssetObjectCandidates(String assetObjectLocation) {
return List.of(NetworkUtils.toURI("https://resources.download.minecraft.net/" + assetObjectLocation));
public @Unmodifiable List<WebURL> getAssetObjectCandidates(String assetObjectLocation) {
return List.of(WebURL.parse("https://resources.download.minecraft.net/" + assetObjectLocation));
}
@Override
@@ -17,12 +17,14 @@
*/
package org.jackhuang.hmcl.download.forge;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.ArtifactMalformedException;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.download.forge.ForgeNewInstallProfile.Processor;
import org.jackhuang.hmcl.download.game.GameLibrariesTask;
import org.jackhuang.hmcl.download.game.GameInstanceJsonDownloadTask;
import org.jackhuang.hmcl.download.game.GameLibrariesTask;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.java.JavaRuntime;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.DigestUtils;
@@ -34,15 +36,14 @@ import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.CommandBuilder;
import org.jackhuang.hmcl.java.JavaRuntime;
import org.jackhuang.hmcl.util.platform.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -371,7 +372,7 @@ public class ForgeNewInstallTask extends Task<GameInstancePatch> {
throw new Exception("client_mappings download info not found");
}
List<URI> mappingsUrl = dependencyManager.getDownloadProvider()
@Unmodifiable List<WebURL> mappingsUrl = dependencyManager.getDownloadProvider()
.injectURLWithCandidates(mappings.getUrl());
var mappingsTask = new FileDownloadTask(
mappingsUrl,
@@ -17,14 +17,14 @@
*/
package org.jackhuang.hmcl.download.forge;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.ComponentVersionList;
import org.jackhuang.hmcl.download.DownloadProvider;
import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber;
import java.net.URI;
import java.time.Instant;
import java.util.Collections;
import java.util.Map;
@@ -96,5 +96,6 @@ public final class ForgeVersionList extends ComponentVersionList<ForgeRemoteVers
});
}
public static final URI FORGE_LIST = URI.create("https://hmcl.glavo.site/metadata/forge/");
/// Base URL for Forge version metadata maintained for the launcher.
public static final WebURL FORGE_LIST = WebURL.parse("https://hmcl.glavo.site/metadata/forge/");
}
@@ -18,15 +18,16 @@
package org.jackhuang.hmcl.download.game;
import com.google.gson.JsonParseException;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jetbrains.annotations.Unmodifiable;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@@ -100,9 +101,9 @@ public final class GameAssetDownloadTask extends Task<Void> {
LOG.warning("Unable to calc hash value of file " + file, e);
}
if (download) {
List<URI> uris = dependencyManager.getDownloadProvider().getAssetObjectCandidates(assetObject.getLocation());
@Unmodifiable List<WebURL> urls = dependencyManager.getDownloadProvider().getAssetObjectCandidates(assetObject.getLocation());
var task = new FileDownloadTask(uris, file, new FileDownloadTask.IntegrityCheck("SHA-1", assetObject.hash()));
var task = new FileDownloadTask(urls, file, new FileDownloadTask.IntegrityCheck("SHA-1", assetObject.hash()));
task.setName(assetObject.hash());
task.setCandidate(dependencyManager.getCacheRepository().getCommonDirectory()
.resolve("assets").resolve("objects").resolve(assetObject.getLocation()));
@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.download.game;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.task.FileDownloadTask;
@@ -27,10 +28,10 @@ import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
import org.jackhuang.hmcl.util.versioning.VersionNumber;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -152,9 +153,9 @@ public final class GameLibrariesTask extends Task<Void> {
for (FMLLib fmlLib : fmlLibs) {
Path file = libDir.resolve(fmlLib.name);
if (shouldDownloadFMLLib(fmlLib, file)) {
List<URI> uris = dependencyManager.getDownloadProvider()
@Unmodifiable List<WebURL> urls = dependencyManager.getDownloadProvider()
.injectURLWithCandidates(fmlLib.downloadUrl());
dependencies.add(new FileDownloadTask(uris, file)
dependencies.add(new FileDownloadTask(urls, file)
.withCounter("hmcl.install.libraries"));
}
}
@@ -17,19 +17,20 @@
*/
package org.jackhuang.hmcl.download.game;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.download.DefaultCacheRepository;
import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.task.DownloadException;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jetbrains.annotations.Unmodifiable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -115,8 +116,8 @@ public class LibraryDownloadTask extends Task<Void> {
}
List<URI> uris = dependencyManager.getDownloadProvider().injectURLWithCandidates(url);
task = new FileDownloadTask(uris, jar,
@Unmodifiable List<WebURL> urls = dependencyManager.getDownloadProvider().injectURLWithCandidates(url);
task = new FileDownloadTask(urls, jar,
library.getDownload().getSha1() != null ? new IntegrityCheck("SHA-1", library.getDownload().getSha1()) : null);
task.setCacheRepository(cacheRepository);
task.setCaching(true);
@@ -17,13 +17,15 @@
*/
package org.jackhuang.hmcl.download.neoforge;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.ArtifactMalformedException;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.download.forge.ForgeNewInstallProfile;
import org.jackhuang.hmcl.download.forge.ForgeNewInstallProfile.Processor;
import org.jackhuang.hmcl.download.game.GameLibrariesTask;
import org.jackhuang.hmcl.download.forge.ForgeNewInstallProfile;
import org.jackhuang.hmcl.download.game.GameInstanceJsonDownloadTask;
import org.jackhuang.hmcl.download.game.GameLibrariesTask;
import org.jackhuang.hmcl.game.*;
import org.jackhuang.hmcl.java.JavaRuntime;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.DigestUtils;
@@ -34,15 +36,14 @@ import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.CommandBuilder;
import org.jackhuang.hmcl.java.JavaRuntime;
import org.jackhuang.hmcl.util.platform.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -355,7 +356,7 @@ public class NeoForgeOldInstallTask extends Task<GameInstancePatch> {
throw new Exception("client_mappings download info not found");
}
List<URI> mappingsUri = dependencyManager.getDownloadProvider()
@Unmodifiable List<WebURL> mappingsUri = dependencyManager.getDownloadProvider()
.injectURLWithCandidates(mappings.getUrl());
var mappingsTask = new FileDownloadTask(
mappingsUri,
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.modpack.curse;
import com.google.gson.JsonParseException;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.download.GameBuilder;
import org.jackhuang.hmcl.game.*;
@@ -32,7 +33,6 @@ import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
@@ -183,7 +183,7 @@ public final class CurseInstallTask extends Task<Void> {
dependents.add(new ModpackInstallTask<>(zipFile, run, modpack.getEncoding(), Collections.singletonList(manifest.overrides()), any -> true, config).withStage("hmcl.modpack"));
dependents.add(new MinecraftInstanceTask<>(zipFile, modpack.getEncoding(), Collections.singletonList(manifest.overrides()), manifest, CurseModpackProvider.INSTANCE, manifest.name(), manifest.version(), repository.getLayout().getModpackConfigurationFile(instanceId)).withStage("hmcl.modpack"));
@Nullable URI iconUri = NetworkUtils.toURIOrNull(iconUrl);
@Nullable WebURL iconUri = NetworkUtils.toWebURLOrNull(iconUrl);
if (iconUri != null) {
String ext = FileUtils.getExtension(StringUtils.substringAfter(iconUri.getPath(), '/')).toLowerCase(Locale.ROOT);
if (Modpack.SUPPORTED_ICON_EXTS.contains(ext)) {
@@ -18,11 +18,12 @@
package org.jackhuang.hmcl.modpack.mcbbs;
import com.google.gson.JsonParseException;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.addon.mod.ModManager;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.game.DefaultGameInstance;
import org.jackhuang.hmcl.addon.mod.ModManager;
import org.jackhuang.hmcl.modpack.ModpackConfiguration;
import org.jackhuang.hmcl.modpack.ModpackCompletionException;
import org.jackhuang.hmcl.modpack.ModpackConfiguration;
import org.jackhuang.hmcl.modpack.curse.CurseMetaMod;
import org.jackhuang.hmcl.task.*;
import org.jackhuang.hmcl.util.DigestUtils;
@@ -218,7 +219,7 @@ public class McbbsModpackCompletionTask extends CompletableFutureTask<Void> {
McbbsModpackManifest.CurseFile file = (McbbsModpackManifest.CurseFile) rawFile;
if (StringUtils.isBlank(file.getFileName())) {
try {
return file.withFileName(NetworkUtils.detectFileName(NetworkUtils.toURI(file.getUrl())));
return file.withFileName(NetworkUtils.detectFileName(WebURL.parse(file.getUrl())));
} catch (IOException e) {
try {
String result = NetworkUtils.doGet(String.format("https://cursemeta.dries007.net/%d/%d.json", file.getProjectID(), file.getFileID()));
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.modpack.modrinth;
import com.google.gson.JsonParseException;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.download.GameBuilder;
import org.jackhuang.hmcl.game.*;
@@ -31,7 +32,6 @@ import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
@@ -181,7 +181,7 @@ public class ModrinthInstallTask extends Task<Void> {
dependents.add(new ModpackInstallTask<>(zipFile, run, modpack.getEncoding(), subDirectories, any -> true, config).withStage("hmcl.modpack"));
dependents.add(new MinecraftInstanceTask<>(zipFile, modpack.getEncoding(), subDirectories, manifest, ModrinthModpackProvider.INSTANCE, manifest.getName(), manifest.getVersionId(), repository.getLayout().getModpackConfigurationFile(instanceId)).withStage("hmcl.modpack"));
@Nullable URI iconUri = NetworkUtils.toURIOrNull(iconUrl);
@Nullable WebURL iconUri = NetworkUtils.toWebURLOrNull(iconUrl);
if (iconUri != null) {
String ext = FileUtils.getExtension(StringUtils.substringAfter(iconUri.getPath(), '/')).toLowerCase(Locale.ROOT);
if (Modpack.SUPPORTED_ICON_EXTS.contains(ext)) {
@@ -17,10 +17,10 @@
*/
package org.jackhuang.hmcl.modpack.multimc;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.game.GameComponentType;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jetbrains.annotations.Nullable;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -100,7 +100,8 @@ public final class MultiMCComponents {
return PAIRS;
}
public static URI getMetaURL(String componentID, String version, String mcVersion) {
/// Returns the component metadata URL, selecting the known LWJGL or Minecraft version fallback when absent.
public static WebURL getMetaURL(String componentID, @Nullable String version, String mcVersion) {
if (version == null) {
switch (componentID) {
case "org.lwjgl": {
@@ -119,6 +120,6 @@ public final class MultiMCComponents {
}
}
return NetworkUtils.toURI(String.format("https://meta.multimc.org/v1/%s/%s.json", componentID, version));
return WebURL.parse(String.format("https://meta.multimc.org/v1/%s/%s.json", componentID, version));
}
}
@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.task;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
@@ -26,7 +27,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
@@ -44,52 +44,52 @@ public final class CacheFileTask extends FetchTask<Path> {
/// Expected SHA-1 checksum, or `null` when the remote cache policy determines reuse.
private final @Nullable String expectedSha1;
/// Creates a task for one URI string using remote cache metadata.
/// Creates a task for one URL string using remote cache metadata.
///
/// @param uri the HTTP or HTTPS URI string
public CacheFileTask(@NotNull String uri) {
this(NetworkUtils.toURI(uri));
/// @param url the HTTP or HTTPS URL string
public CacheFileTask(@NotNull String url) {
this(WebURL.parse(url));
}
/// Creates a task for one URI using remote cache metadata.
/// Creates a task for one URL using remote cache metadata.
///
/// @param uri the HTTP or HTTPS URI
public CacheFileTask(@NotNull URI uri) {
this(List.of(uri));
/// @param url the HTTP or HTTPS URL
public CacheFileTask(@NotNull WebURL url) {
this(List.of(url));
}
/// Creates a task for candidate URIs using remote cache metadata.
/// Creates a task for candidate URLs using remote cache metadata.
///
/// @param uris candidate download URIs in attempt order
public CacheFileTask(@NotNull List<@NotNull URI> uris) {
super(uris);
/// @param urls candidate download URLs in attempt order
public CacheFileTask(@NotNull List<@NotNull WebURL> urls) {
super(urls);
this.expectedSha1 = null;
validateUris(uris);
setName(uris.get(0).toString());
validateUris(urls);
setName(urls.get(0).toString());
}
/// Creates a task that returns content cached under a verified SHA-1 checksum.
///
/// @param uris candidate download URIs in attempt order
/// @param urls candidate download URLs in attempt order
/// @param expectedSha1 the expected SHA-1 checksum
public CacheFileTask(
@NotNull List<@NotNull URI> uris,
@NotNull List<@NotNull WebURL> urls,
@NotNull String expectedSha1) {
super(uris);
super(urls);
if (!DigestUtils.isSha1Digest(expectedSha1)) {
throw new IllegalArgumentException("Invalid SHA-1 checksum: " + expectedSha1);
}
this.expectedSha1 = expectedSha1.toLowerCase(Locale.ROOT);
validateUris(uris);
setName(uris.get(0).toString());
validateUris(urls);
setName(urls.get(0).toString());
}
/// Verifies that all candidate URIs use HTTP or HTTPS.
/// Verifies that all candidate URLs use HTTP or HTTPS.
///
/// @param uris the candidate URIs
private static void validateUris(@NotNull List<@NotNull URI> uris) {
if (!uris.stream().allMatch(NetworkUtils::isHttpUri)) {
throw new IllegalArgumentException(uris.toString());
/// @param urls the candidate URLs
private static void validateUris(@NotNull List<@NotNull WebURL> urls) {
if (!urls.stream().allMatch(NetworkUtils::isHttpUri)) {
throw new IllegalArgumentException(urls.toString());
}
}
@@ -110,13 +110,13 @@ public final class CacheFileTask extends FetchTask<Path> {
}
// Check cache
for (URI uri : uris) {
for (WebURL url : urls) {
try {
setResult(repository.getCachedRemoteFile(uri, true));
LOG.info("Using cached file for " + NetworkUtils.dropQuery(uri));
setResult(repository.getCachedRemoteFile(url, true));
LOG.info("Using cached file for " + NetworkUtils.dropQuery(url));
return EnumCheckETag.CACHED;
} catch (CacheRepository.CacheExpiredException e) {
LOG.info("Cache expired for " + NetworkUtils.dropQuery(uri));
LOG.info("Cache expired for " + NetworkUtils.dropQuery(url));
} catch (IOException ignored) {
}
}
@@ -17,23 +17,27 @@
*/
package org.jackhuang.hmcl.task;
import org.glavo.url.WebURL;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.net.URI;
import static java.util.Objects.requireNonNull;
/// Reports failure to download a candidate URL, retaining the underlying cause.
public class DownloadException extends IOException {
private final URI uri;
/// Candidate URL whose download failed.
private final WebURL url;
public DownloadException(URI uri, @NotNull Throwable cause) {
super("Unable to download " + uri + ", " + cause.getMessage(), requireNonNull(cause));
this.uri = uri;
/// Creates a failure for the candidate URL and a non-null cause.
public DownloadException(WebURL url, @NotNull Throwable cause) {
super("Unable to download " + url + ", " + cause.getMessage(), requireNonNull(cause));
this.url = url;
}
public URI getUri() {
return uri;
/// Returns the candidate URL before any redirects.
public WebURL getUrl() {
return url;
}
}
@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.task;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.event.Event;
import org.jackhuang.hmcl.event.EventBus;
import org.jackhuang.hmcl.event.EventManager;
@@ -24,10 +25,10 @@ import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.io.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URLConnection;
import java.nio.file.Path;
import java.util.*;
@@ -42,20 +43,27 @@ import java.util.regex.Pattern;
import static org.jackhuang.hmcl.util.Lang.threadPool;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
/// Downloads candidate URLs in order, with retries, HTTP caching, and supported range resumption.
public abstract class FetchTask<T> extends Task<T> {
protected static final int DEFAULT_RETRY = 5;
protected final List<URI> uris;
/// Immutable snapshot of candidate URLs in attempt order.
protected final @Unmodifiable List<WebURL> urls;
protected int retry = DEFAULT_RETRY;
protected CacheRepository repository = CacheRepository.getInstance();
public FetchTask(@NotNull List<@NotNull URI> uris) {
Objects.requireNonNull(uris);
/// Creates a download task with a snapshot of the candidate URLs.
///
/// @param urls nonempty candidate URLs, with no null elements
/// @throws IllegalArgumentException if no candidates are supplied
/// @throws NullPointerException if the list or any element is null
public FetchTask(@NotNull List<@NotNull WebURL> urls) {
Objects.requireNonNull(urls);
this.uris = List.copyOf(uris);
this.urls = List.copyOf(urls);
if (this.uris.isEmpty())
if (this.urls.isEmpty())
throw new IllegalArgumentException("At least one URL is required");
setExecutor(DOWNLOAD_EXECUTOR);
@@ -72,7 +80,11 @@ public abstract class FetchTask<T> extends Task<T> {
this.repository = repository;
}
protected void beforeDownload(URI uri) throws IOException {
/// Invoked before each download attempt, after any cache lookup.
///
/// @param url the candidate URL before redirects
/// @throws IOException if the attempt cannot be prepared
protected void beforeDownload(WebURL url) throws IOException {
}
protected abstract void useCachedResult(Path cachedFile) throws IOException;
@@ -96,17 +108,17 @@ public abstract class FetchTask<T> extends Task<T> {
}
}
ArrayList<DownloadException> exceptions = null;
@Nullable ArrayList<DownloadException> exceptions = null;
if (SEMAPHORE != null)
SEMAPHORE.acquire();
try {
for (URI uri : uris) {
for (WebURL url : urls) {
try {
if (NetworkUtils.isHttpUri(uri))
downloadHttp(uri, checkETag);
if (NetworkUtils.isHttpUri(url))
downloadHttp(url, checkETag);
else
downloadNotHttp(uri);
downloadNotHttp(url);
return;
} catch (DownloadException e) {
if (exceptions == null)
@@ -155,18 +167,20 @@ public abstract class FetchTask<T> extends Task<T> {
if (strongETag == null && StringUtils.isBlank(lastModified))
return null;
return new HttpResumeContext(response.uri(), contentLength, strongETag, lastModified);
return new HttpResumeContext(response.url(), contentLength, strongETag, lastModified);
}
private final URI uri;
/// Final response URL whose partial content is retained for resumption.
private final WebURL url;
private final long contentLength;
private final @Nullable String strongETag;
private final @Nullable String lastModified;
long countUncompressed;
private HttpResumeContext(URI uri, long contentLength, @Nullable String strongETag, @Nullable String lastModified) {
this.uri = uri;
/// Records the response URL, total byte length, and available validators for resumption.
private HttpResumeContext(WebURL url, long contentLength, @Nullable String strongETag, @Nullable String lastModified) {
this.url = url;
this.contentLength = contentLength;
this.strongETag = strongETag;
this.lastModified = lastModified;
@@ -197,7 +211,7 @@ public abstract class FetchTask<T> extends Task<T> {
if (!strongETag.equals(eTag))
return false;
} else {
if (!uri.equals(response.uri()))
if (!url.equals(response.url()))
return false;
String lastModified = response.headers().firstValue("last-modified").orElse("");
@@ -279,22 +293,23 @@ public abstract class FetchTask<T> extends Task<T> {
}
}
private void downloadHttp(URI uri, boolean checkETag) throws DownloadException, InterruptedException {
/// Downloads an HTTP candidate, following HTTP(S) redirects and retrying recoverable failures.
private void downloadHttp(WebURL url, boolean checkETag) throws DownloadException, InterruptedException {
if (checkETag) {
// Handle cache
try {
Path cache = repository.getCachedRemoteFile(uri, true);
Path cache = repository.getCachedRemoteFile(url, true);
useCachedResult(cache);
LOG.info("Using cached file for " + NetworkUtils.dropQuery(uri));
LOG.info("Using cached file for " + NetworkUtils.dropQuery(url));
return;
} catch (IOException ignored) {
}
}
Context context = null;
HttpResumeContext resumeContext = null;
@Nullable Context context = null;
@Nullable HttpResumeContext resumeContext = null;
ArrayList<Exception> exceptions = null;
@Nullable ArrayList<Exception> exceptions = null;
// If loading the cache fails, the cache should not be loaded again.
boolean useCachedResult = true;
@@ -304,24 +319,24 @@ public abstract class FetchTask<T> extends Task<T> {
throw new InterruptedException();
}
List<URI> redirects = null;
@Nullable List<WebURL> redirects = null;
try {
beforeDownload(uri);
beforeDownload(url);
updateProgress(0);
HttpURLConnection connection = null;
@Nullable HttpURLConnection connection = null;
UrlResponseInfo responseInfo;
String bmclapiHash;
@Nullable String bmclapiHash;
int responseCode;
URI currentURI = uri;
WebURL currentURI = url;
LinkedHashMap<String, String> headers = new LinkedHashMap<>();
headers.put("accept-encoding", "gzip");
boolean resumeRequested = resumeContext != null && resumeContext.hasPartialContent();
if (useCachedResult && checkETag && !resumeRequested)
headers.putAll(repository.injectConnection(uri));
headers.putAll(repository.injectConnection(url));
if (resumeRequested) {
headers.put("range", "bytes=" + resumeContext.countUncompressed + "-");
headers.put("if-range", resumeContext.ifRange());
@@ -340,7 +355,7 @@ public abstract class FetchTask<T> extends Task<T> {
Optional<Path> cache = repository.checkExistentFile(null, "SHA-1", bmclapiHash);
if (cache.isPresent()) {
useCachedResult(cache.get());
LOG.info("Using cached file for " + NetworkUtils.dropQuery(uri));
LOG.info("Using cached file for " + NetworkUtils.dropQuery(url));
return;
}
}
@@ -352,15 +367,15 @@ public abstract class FetchTask<T> extends Task<T> {
throw new IOException("Too much redirects");
}
String location = connection.getHeaderField("Location");
@Nullable String location = connection.getHeaderField("Location");
if (StringUtils.isBlank(location))
throw new IOException("Redirected to an empty location");
URI target = currentURI.resolve(NetworkUtils.encodeLocation(location));
WebURL target = currentURI.resolve(location);
redirects.add(target);
if (!NetworkUtils.isHttpUri(target))
throw new IOException("Redirected to not http URI: " + target);
throw new IOException("Redirected to non-HTTP URL: " + target);
currentURI = target;
} else {
@@ -375,7 +390,7 @@ public abstract class FetchTask<T> extends Task<T> {
}
} while (true);
InputStream inputStream = null;
@Nullable InputStream inputStream = null;
boolean responseBodyConsumed = false;
try {
if (resumeRequested && responseCode == 416) {
@@ -389,14 +404,14 @@ public abstract class FetchTask<T> extends Task<T> {
if (useCachedResult && responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
// Handle cache
try {
Path cache = repository.getCachedRemoteFile(responseInfo.uri(), false);
Path cache = repository.getCachedRemoteFile(responseInfo.url(), false);
useCachedResult(cache);
LOG.info("Using cached file for " + NetworkUtils.dropQuery(uri));
LOG.info("Using cached file for " + NetworkUtils.dropQuery(url));
return;
} catch (CacheRepository.CacheExpiredException e) {
LOG.info("Cache expired for " + NetworkUtils.dropQuery(uri));
LOG.info("Cache expired for " + NetworkUtils.dropQuery(url));
} catch (IOException e) {
LOG.warning("Unable to use cached file, redownload " + NetworkUtils.dropQuery(uri), e);
LOG.warning("Unable to use cached file, redownload " + NetworkUtils.dropQuery(url), e);
repository.removeRemoteEntry(currentURI);
useCachedResult = false;
// Now we must reconnect the server since 304 may result in empty content,
@@ -405,9 +420,9 @@ public abstract class FetchTask<T> extends Task<T> {
continue;
}
} else if (responseCode / 100 == 4) {
throw new FileNotFoundException(uri.toString());
throw new FileNotFoundException(url.toString());
} else if (responseCode / 100 != 2) {
throw new ResponseCodeException(uri, responseCode);
throw new ResponseCodeException(url, responseCode);
}
long contentLength = responseInfo.headers().firstValueAsLong("content-length").orElse(-1L);
@@ -419,7 +434,7 @@ public abstract class FetchTask<T> extends Task<T> {
} else if (resumeRequested) {
if (resumeContext.canResume(responseCode, responseInfo)) {
// Resume download
LOG.info("Resuming " + resumeContext.uri + " from " + resumeContext.countUncompressed);
LOG.info("Resuming " + resumeContext.url + " from " + resumeContext.countUncompressed);
} else {
// Failed to resume download, so we will retry from the beginning
resumeContext = null;
@@ -469,15 +484,15 @@ public abstract class FetchTask<T> extends Task<T> {
} catch (InterruptedException e) {
throw e;
} catch (FileNotFoundException ex) {
LOG.warning("Failed to download " + uri + ", not found" + (redirects == null ? "" : ", redirects: " + redirects), ex);
throw toDownloadException(uri, ex, exceptions); // we will not try this URL again
LOG.warning("Failed to download " + url + ", not found" + (redirects == null ? "" : ", redirects: " + redirects), ex);
throw toDownloadException(url, ex, exceptions); // we will not try this URL again
} catch (Exception ex) {
if (exceptions == null)
exceptions = new ArrayList<>();
exceptions.add(ex);
LOG.warning("Failed to download " + uri + ", repeat times: " + retryTime + (redirects == null ? "" : ", redirects: " + redirects), ex);
LOG.warning("Failed to download " + url + ", repeat times: " + retryTime + (redirects == null ? "" : ", redirects: " + redirects), ex);
if (retryTime < retryLimit - 1) {
// Wait for a while before retrying
@@ -490,12 +505,12 @@ public abstract class FetchTask<T> extends Task<T> {
try {
context.close();
} catch (IOException e) {
LOG.warning("Failed to close context for " + NetworkUtils.dropQuery(uri), e);
LOG.warning("Failed to close context for " + NetworkUtils.dropQuery(url), e);
}
}
}
throw toDownloadException(uri, null, exceptions);
throw toDownloadException(url, null, exceptions);
}
/// Disconnects an HTTP URL connection whose response body will not be consumed.
@@ -511,18 +526,19 @@ public abstract class FetchTask<T> extends Task<T> {
}
}
private void downloadNotHttp(URI uri) throws DownloadException, InterruptedException {
ArrayList<Exception> exceptions = null;
/// Downloads a non-HTTP candidate through its installed URL handler, retrying I/O failures.
private void downloadNotHttp(WebURL url) throws DownloadException, InterruptedException {
@Nullable ArrayList<Exception> exceptions = null;
for (int retryTime = 0; retryTime < retry; retryTime++) {
if (isCancelled()) {
throw new InterruptedException();
}
try {
beforeDownload(uri);
beforeDownload(url);
updateProgress(0);
URLConnection conn = NetworkUtils.createConnection(uri);
URLConnection conn = NetworkUtils.createConnection(url);
try (Context context = getContext()) {
download(context,
null, conn.getInputStream(),
@@ -533,24 +549,25 @@ public abstract class FetchTask<T> extends Task<T> {
} catch (InterruptedException e) {
throw e;
} catch (FileNotFoundException ex) {
LOG.warning("Failed to download " + uri + ", not found", ex);
LOG.warning("Failed to download " + url + ", not found", ex);
throw toDownloadException(uri, ex, exceptions); // we will not try this URL again
throw toDownloadException(url, ex, exceptions); // we will not try this URL again
} catch (Exception ex) {
if (exceptions == null)
exceptions = new ArrayList<>();
exceptions.add(ex);
LOG.warning("Failed to download " + uri + ", repeat times: " + retryTime, ex);
LOG.warning("Failed to download " + url + ", repeat times: " + retryTime, ex);
}
}
throw toDownloadException(uri, null, exceptions);
throw toDownloadException(url, null, exceptions);
}
private static DownloadException toDownloadException(URI uri, @Nullable Exception last, @Nullable ArrayList<Exception> exceptions) {
/// Wraps the final failure and attaches preceding failures as suppressed exceptions.
private static DownloadException toDownloadException(WebURL url, @Nullable Exception last, @Nullable ArrayList<Exception> exceptions) {
if (exceptions == null || exceptions.isEmpty()) {
return new DownloadException(uri, last != null
return new DownloadException(url, last != null
? last
: new IOException("No exceptions"));
} else {
@@ -560,7 +577,7 @@ public abstract class FetchTask<T> extends Task<T> {
for (Exception e : exceptions) {
last.addSuppressed(e);
}
return new DownloadException(uri, last);
return new DownloadException(url, last);
}
}
@@ -17,17 +17,16 @@
*/
package org.jackhuang.hmcl.task;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.io.UrlResponseInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileSystem;
@@ -41,11 +40,9 @@ import java.util.*;
import static java.util.Objects.requireNonNull;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
/**
* A task that can download a file online.
*
* @author huangyuhui
*/
/// A task that can download a file online.
///
/// @author huangyuhui
public class FileDownloadTask extends FetchTask<Void> {
public record IntegrityCheck(String algorithm, String checksum) {
@@ -66,64 +63,61 @@ public class FileDownloadTask extends FetchTask<Void> {
}
private final Path file;
private final IntegrityCheck integrityCheck;
/// Optional expected checksum for downloaded and cached content.
private final @Nullable IntegrityCheck integrityCheck;
private boolean caching;
private Path candidate;
private final ArrayList<IntegrityCheckHandler> integrityCheckHandlers = new ArrayList<>();
/**
* @param uri the URI of remote file.
* @param path the location that download to.
*/
public FileDownloadTask(String uri, Path path) {
this(List.of(NetworkUtils.toURI(uri)), path, null);
/// Creates a download task for an absolute URL string.
///
/// @param url the URL of remote file.
/// @param path the location that download to.
public FileDownloadTask(String url, Path path) {
this(List.of(WebURL.parse(url)), path, null);
}
/**
* @param uri the URI of remote file.
* @param path the location that download to.
* @param integrityCheck the integrity check to perform, null if no integrity check is to be performed
*/
public FileDownloadTask(String uri, Path path, IntegrityCheck integrityCheck) {
this(List.of(NetworkUtils.toURI(uri)), path, integrityCheck);
/// Creates a download task with an optional integrity check for an absolute URL string.
///
/// @param url the URL of remote file.
/// @param path the location that download to.
/// @param integrityCheck the integrity check to perform, null if no integrity check is to be performed
public FileDownloadTask(String url, Path path, @Nullable IntegrityCheck integrityCheck) {
this(List.of(WebURL.parse(url)), path, integrityCheck);
}
/**
* @param uri the URI of remote file.
* @param path the location that download to.
*/
public FileDownloadTask(URI uri, Path path) {
this(uri, path, null);
/// Creates a download task for one URL.
///
/// @param url the URL of remote file.
/// @param path the location that download to.
public FileDownloadTask(WebURL url, Path path) {
this(url, path, null);
}
/**
* @param uri the URI of remote file.
* @param path the location that download to.
* @param integrityCheck the integrity check to perform, null if no integrity check is to be performed
*/
public FileDownloadTask(URI uri, Path path, IntegrityCheck integrityCheck) {
this(List.of(uri), path, integrityCheck);
/// Creates a download task with an optional integrity check for one URL.
///
/// @param url the URL of remote file.
/// @param path the location that download to.
/// @param integrityCheck the integrity check to perform, null if no integrity check is to be performed
public FileDownloadTask(WebURL url, Path path, @Nullable IntegrityCheck integrityCheck) {
this(List.of(url), path, integrityCheck);
}
/**
* Constructor.
*
* @param uris uris of remote file, will be attempted in order.
* @param file the location that download to.
*/
public FileDownloadTask(List<URI> uris, Path file) {
this(uris, file, null);
/// Creates a download task with a snapshot of nonempty candidate URLs.
///
/// @param urls candidate URLs of the remote file, attempted in order
/// @param file the location that download to.
public FileDownloadTask(List<WebURL> urls, Path file) {
this(urls, file, null);
}
/**
* Constructor.
*
* @param uris uris of remote file, will be attempted in order.
* @param path the location that download to.
* @param integrityCheck the integrity check to perform, null if no integrity check is to be performed
*/
public FileDownloadTask(List<URI> uris, Path path, IntegrityCheck integrityCheck) {
super(uris);
/// Creates a download task with a snapshot of nonempty candidates and an optional integrity check.
///
/// @param urls candidate URLs of the remote file, attempted in order
/// @param path the location that download to.
/// @param integrityCheck the integrity check to perform, null if no integrity check is to be performed
public FileDownloadTask(List<WebURL> urls, Path path, @Nullable IntegrityCheck integrityCheck) {
super(urls);
this.file = path;
this.integrityCheck = integrityCheck;
@@ -155,7 +149,7 @@ public class FileDownloadTask extends FetchTask<Void> {
if (cache.isPresent()) {
try {
FileUtils.copyFile(cache.get(), file);
LOG.trace("Successfully verified file " + file + " from " + uris.get(0));
LOG.trace("Successfully verified file " + file + " from " + urls.get(0));
return EnumCheckETag.CACHED;
} catch (IOException e) {
LOG.warning("Failed to copy cache files", e);
@@ -168,8 +162,8 @@ public class FileDownloadTask extends FetchTask<Void> {
}
@Override
protected void beforeDownload(URI uri) {
LOG.trace("Downloading " + uri + " to " + file);
protected void beforeDownload(WebURL url) {
LOG.trace("Downloading " + url + " to " + file);
}
@Override
@@ -290,13 +284,11 @@ public class FileDownloadTask extends FetchTask<Void> {
}
public interface IntegrityCheckHandler {
/**
* Check whether the file is corrupted or not.
*
* @param filePath the file locates in (maybe in temp directory)
* @param destinationPath for real file name
* @throws IOException if the file is corrupted
*/
/// Check whether the file is corrupted or not.
///
/// @param filePath the file locates in (maybe in temp directory)
/// @param destinationPath for real file name
/// @throws IOException if the file is corrupted
void checkIntegrity(Path filePath, Path destinationPath) throws IOException;
}
@@ -18,6 +18,7 @@
package org.jackhuang.hmcl.task;
import com.google.gson.reflect.TypeToken;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.io.UrlResponseInfo;
@@ -25,28 +26,28 @@ import org.jetbrains.annotations.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
/**
* @author huangyuhui
*/
/// Downloads text, decoding HTTP responses using their declared charset or UTF-8.
public final class GetTask extends FetchTask<String> {
public GetTask(String uri) {
this(NetworkUtils.toURI(uri));
/// Creates a text download task for an absolute URL string.
public GetTask(String url) {
this(WebURL.parse(url));
}
public GetTask(URI url) {
/// Creates a text download task for one URL.
public GetTask(WebURL url) {
this(List.of(url));
setName(url.toString());
}
public GetTask(List<URI> url) {
/// Creates a text download task with a snapshot of nonempty candidate URLs in attempt order.
public GetTask(List<WebURL> url) {
super(url);
setName(url.get(0).toString());
}
@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.util;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.util.function.ExceptionalSupplier;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
@@ -26,12 +27,12 @@ import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.io.UrlResponseInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpRequest;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
@@ -59,7 +60,8 @@ public class CacheRepository {
private Path cacheDirectory;
private Path indexFile;
private FileTime indexFileLastModified;
private LinkedHashMap<URI, ETagItem> index;
/// Remote entries keyed by URLs without queries or fragments; initialized by [#changeDirectory(Path)].
private @Nullable LinkedHashMap<WebURL, ETagItem> index;
protected final ReadWriteLock lock = new ReentrantReadWriteLock();
public void changeDirectory(Path commonDir) {
@@ -167,11 +169,16 @@ public class CacheRepository {
return cache;
}
public Path getCachedRemoteFile(URI uri, boolean checkExpires) throws IOException {
/// Returns an existing cache file for the URL, ignoring its query and fragment.
///
/// @param url the remote URL
/// @param checkExpires whether to reject expired entries
/// @throws IOException if the entry is missing, invalid, unreadable, or expired when checked
public Path getCachedRemoteFile(WebURL url, boolean checkExpires) throws IOException {
lock.readLock().lock();
ETagItem eTagItem;
@Nullable ETagItem eTagItem;
try {
eTagItem = index.get(NetworkUtils.dropQuery(uri));
eTagItem = index.get(NetworkUtils.dropQuery(url));
} finally {
lock.readLock().unlock();
}
@@ -189,26 +196,28 @@ public class CacheRepository {
return file;
}
public void removeRemoteEntry(URI uri) {
/// Removes the URL's index entry, ignoring its query and fragment; the cache file is retained.
public void removeRemoteEntry(WebURL url) {
lock.writeLock().lock();
try {
index.remove(NetworkUtils.dropQuery(uri));
index.remove(NetworkUtils.dropQuery(url));
} finally {
lock.writeLock().unlock();
}
}
public @NotNull Map<String, String> injectConnection(URI uri) {
/// Returns immutable conditional request headers for the URL's cached ETag, or an empty map.
public @NotNull @Unmodifiable Map<String, String> injectConnection(WebURL url) {
try {
uri = NetworkUtils.dropQuery(uri);
url = NetworkUtils.dropQuery(url);
} catch (IllegalArgumentException e) {
return Map.of();
}
ETagItem eTagItem;
@Nullable ETagItem eTagItem;
lock.readLock().lock();
try {
eTagItem = index.get(uri);
eTagItem = index.get(url);
} finally {
lock.readLock().unlock();
}
@@ -220,17 +229,18 @@ public class CacheRepository {
return Map.of();
}
public void injectConnection(URI uri, HttpRequest.Builder requestBuilder) {
/// Adds a conditional request header if the URL has a cached ETag.
public void injectConnection(WebURL url, HttpRequest.Builder requestBuilder) {
try {
uri = NetworkUtils.dropQuery(uri);
url = NetworkUtils.dropQuery(url);
} catch (IllegalArgumentException e) {
return;
}
ETagItem eTagItem;
@Nullable ETagItem eTagItem;
lock.readLock().lock();
try {
eTagItem = index.get(uri);
eTagItem = index.get(url);
} finally {
lock.readLock().unlock();
}
@@ -268,7 +278,7 @@ public class CacheRepository {
private Path cacheData(UrlResponseInfo info, ExceptionalSupplier<CacheResult, IOException> cacheSupplier) throws IOException {
String eTag = info.headers().firstValue("etag").orElse(null);
if (StringUtils.isBlank(eTag)) return null;
URI uri = NetworkUtils.dropQuery(info.uri());
WebURL url = NetworkUtils.dropQuery(info.url());
long expires = 0L;
expires:
@@ -298,7 +308,7 @@ public class CacheRepository {
String lastModified = info.headers().firstValue("last-modified").orElse(null);
CacheResult cacheResult = cacheSupplier.get();
ETagItem eTagItem = new ETagItem(uri.toString(),
ETagItem eTagItem = new ETagItem(url.toString(),
eTag,
cacheResult.hash,
Files.getLastModifiedTime(cacheResult.cachedFile).toMillis(),
@@ -306,7 +316,7 @@ public class CacheRepository {
expires);
lock.writeLock().lock();
try {
index.compute(uri, updateEntity(eTagItem, true));
index.compute(url, updateEntity(eTagItem, true));
saveETagIndex();
} finally {
lock.writeLock().unlock();
@@ -324,7 +334,8 @@ public class CacheRepository {
}
}
private BiFunction<URI, ETagItem, ETagItem> updateEntity(ETagItem newItem, boolean force) {
/// Creates a merge operation that selects an entry and removes replaced content when its hash differs.
private BiFunction<WebURL, @Nullable ETagItem, ETagItem> updateEntity(ETagItem newItem, boolean force) {
return (key, oldItem) -> {
if (oldItem == null) {
return newItem;
@@ -344,13 +355,14 @@ public class CacheRepository {
};
}
/// Merges persisted indexes using normalized URL keys and the entry replacement policy.
@SafeVarargs
private LinkedHashMap<URI, ETagItem> joinETagIndexes(Collection<ETagItem>... indexes) {
var eTags = new LinkedHashMap<URI, ETagItem>();
for (Collection<ETagItem> eTagItems : indexes) {
private LinkedHashMap<WebURL, ETagItem> joinETagIndexes(@Nullable Collection<ETagItem>... indexes) {
var eTags = new LinkedHashMap<WebURL, ETagItem>();
for (@Nullable Collection<ETagItem> eTagItems : indexes) {
if (eTagItems != null) {
for (ETagItem eTag : eTagItems) {
eTags.compute(NetworkUtils.toURI(eTag.url), updateEntity(eTag, false));
eTags.compute(WebURL.parse(eTag.url), updateEntity(eTag, false));
}
}
}
@@ -407,7 +419,7 @@ public class CacheRepository {
}
public int compareTo(ETagItem other) {
if (!url.equals(other.url) && !NetworkUtils.toURI(url).equals(NetworkUtils.toURI(other.url)))
if (!url.equals(other.url) && !WebURL.parse(url).equals(WebURL.parse(other.url)))
throw new IllegalArgumentException();
ZonedDateTime thisTime = Lang.ignoringException(() -> ZonedDateTime.parse(remoteLastModified, DateTimeFormatter.RFC_1123_DATE_TIME), null);
@@ -21,7 +21,6 @@ import org.glavo.url.WebURL;
import org.jackhuang.hmcl.addon.repository.CurseForgeRemoteAddonRepository;
import org.jackhuang.hmcl.util.Pair;
import org.jackhuang.hmcl.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
@@ -54,8 +53,20 @@ public final class NetworkUtils {
private NetworkUtils() {
}
/// Resolves the URL host and returns whether it is a loopback address.
/// Returns `false` when the host is absent or cannot be resolved.
public static boolean isLoopbackAddress(WebURL url) {
return isLoopbackHost(url.getHost());
}
/// Resolves the host of a JDK URI, including addresses supplied by a proxy selector.
/// Returns `false` when the host is absent or cannot be resolved.
public static boolean isLoopbackAddress(URI uri) {
String host = uri.getHost();
return isLoopbackHost(uri.getHost());
}
/// Returns whether the host resolves to a loopback address.
private static boolean isLoopbackHost(@Nullable String host) {
if (StringUtils.isBlank(host))
return false;
@@ -67,8 +78,9 @@ public final class NetworkUtils {
}
}
public static boolean isHttpUri(URI uri) {
return "http".equals(uri.getScheme()) || "https".equals(uri.getScheme());
/// Returns whether the URL uses HTTP or HTTPS.
public static boolean isHttpUri(WebURL url) {
return "http".equals(url.getScheme()) || "https".equals(url.getScheme());
}
public static String addHttpsIfMissing(String url) {
@@ -123,12 +135,19 @@ public final class NetworkUtils {
return sb.toString();
}
public static List<URI> withQuery(List<URI> list, Map<String, String> params) {
return list.stream().map(uri -> URI.create(withQuery(uri.toString(), params))).collect(Collectors.toList());
/// Returns URLs with their queries replaced by the form-encoded non-null parameters.
/// Fragments and existing path escapes are preserved. If no non-null parameters are supplied,
/// returns a copy of the list with the URLs unchanged.
public static List<WebURL> withQuery(List<WebURL> list, Map<String, @Nullable String> params) {
String query = withQuery("", params);
if (query.isEmpty())
return new ArrayList<>(list);
return list.stream().map(uri -> WebURL.newBuilder(uri).setRawQuery(query).build()).collect(Collectors.toList());
}
public static List<Pair<String, String>> parseQuery(URI uri) {
return parseQuery(uri.getRawQuery());
/// Parses the URL query as form-encoded name/value pairs.
public static List<Pair<String, String>> parseQuery(WebURL url) {
return parseQuery(url.getRawQuery());
}
public static List<Pair<String, String>> parseQuery(String queryParameterString) {
@@ -152,16 +171,13 @@ public final class NetworkUtils {
return result;
}
public static URI dropQuery(URI u) {
/// Returns the URL without its query or fragment, preserving encoded path delimiters.
public static WebURL dropQuery(WebURL u) {
if (u.getRawQuery() == null && u.getRawFragment() == null) {
return u;
}
try {
return new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), u.getPath(), null, null);
} catch (URISyntaxException e) {
throw new AssertionError("Unreachable", e);
}
return WebURL.newBuilder(u).setRawQuery(null).setRawFragment(null).build();
}
private static final List<Pair<String, String>> API_KEYS;
@@ -221,10 +237,6 @@ public final class NetworkUtils {
return connection;
}
public static URLConnection createConnection(URI uri) throws IOException {
return createConnection(WebURL.of(uri));
}
public static HttpURLConnection createHttpConnection(WebURL url) throws IOException {
return (HttpURLConnection) createConnection(url);
}
@@ -233,10 +245,6 @@ public final class NetworkUtils {
return (HttpURLConnection) createConnection(WebURL.parse(url));
}
public static HttpURLConnection createHttpConnection(URI url) throws IOException {
return (HttpURLConnection) createConnection(url);
}
private static void encodeCodePoint(StringBuilder builder, int codePoint) {
builder.append(encodeURL(Character.toString(codePoint)));
}
@@ -348,23 +356,25 @@ public final class NetworkUtils {
return conn;
}
public static String doGet(String uri) throws IOException {
return doGet(toURI(uri));
public static String doGet(String url) throws IOException {
return doGet(WebURL.parse(url));
}
public static String doGet(URI uri) throws IOException {
URLConnection connection = createConnection(uri);
/// Reads a URL as text, following HTTP redirects and decoding the response content.
public static String doGet(WebURL url) throws IOException {
URLConnection connection = createConnection(url);
if (connection instanceof HttpURLConnection httpURLConnection) {
connection = resolveConnection(httpURLConnection);
}
return readFullyAsString(connection);
}
public static String doGet(List<URI> uris) throws IOException {
List<IOException> exceptions = null;
for (URI uri : uris) {
/// Tries candidate URLs in order until one succeeds, or throws their I/O failures.
public static String doGet(List<WebURL> urls) throws IOException {
@Nullable List<IOException> exceptions = null;
for (WebURL url : urls) {
try {
return doGet(uri);
return doGet(url);
} catch (IOException e) {
if (exceptions == null) {
exceptions = new ArrayList<>(1);
@@ -386,24 +396,27 @@ public final class NetworkUtils {
}
}
public static String doPost(URI uri, String post) throws IOException {
return doPost(uri, post, "application/x-www-form-urlencoded");
/// Posts UTF-8 form content and reads the response as text.
public static String doPost(WebURL url, String post) throws IOException {
return doPost(url, post, "application/x-www-form-urlencoded");
}
public static String doPost(URI u, Map<String, String> params) throws IOException {
/// Form-encodes the parameters and posts them to the URL.
public static String doPost(WebURL url, @Nullable Map<String, String> params) throws IOException {
StringBuilder sb = new StringBuilder();
if (params != null) {
for (Map.Entry<String, String> e : params.entrySet())
sb.append(encodeURL(e.getKey())).append("=").append(encodeURL(e.getValue())).append("&");
sb.deleteCharAt(sb.length() - 1);
}
return doPost(u, sb.toString());
return doPost(url, sb.toString());
}
public static String doPost(URI uri, String post, String contentType) throws IOException {
/// Posts UTF-8 content with the given media type and reads the response as text.
public static String doPost(WebURL url, String post, String contentType) throws IOException {
byte[] bytes = post.getBytes(UTF_8);
HttpURLConnection con = createHttpConnection(uri);
HttpURLConnection con = createHttpConnection(url);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", contentType + "; charset=utf-8");
@@ -458,13 +471,14 @@ public final class NetworkUtils {
}
}
public static String detectFileName(URI uri) throws IOException {
HttpURLConnection conn = resolveConnection(createHttpConnection(uri));
/// Reads a successful HTTP response's filename from its disposition header or final URL.
public static String detectFileName(WebURL url) throws IOException {
HttpURLConnection conn = resolveConnection(createHttpConnection(url));
int code = conn.getResponseCode();
if (code / 100 == 4)
throw new FileNotFoundException();
if (code / 100 != 2)
throw new ResponseCodeException(uri, conn.getResponseCode());
throw new ResponseCodeException(url, conn.getResponseCode());
String disposition = conn.getHeaderField("Content-Disposition");
if (disposition == null || !disposition.contains("filename=")) {
@@ -484,19 +498,8 @@ public final class NetworkUtils {
return URLDecoder.decode(toDecode, UTF_8);
}
/// @throws IllegalArgumentException if the string is not a valid URI
public static @NotNull URI toURI(@NotNull String uri) {
return WebURL.toURI(uri);
}
public static @Nullable URI toURIOrNull(String uri) {
if (StringUtils.isNotBlank(uri)) {
try {
return toURI(uri);
} catch (Exception ignored) {
}
}
return null;
/// Parses an absolute URL, or returns `null` for null, blank, or invalid input.
public static @Nullable WebURL toWebURLOrNull(@Nullable String url) {
return StringUtils.isBlank(url) ? null : WebURL.tryParse(url);
}
}
@@ -17,57 +17,71 @@
*/
package org.jackhuang.hmcl.util.io;
import org.glavo.url.WebURL;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URI;
/// Reports an unsuccessful HTTP response and its request address.
public final class ResponseCodeException extends IOException {
private final String uri;
/// Request address reported by the caller.
private final String url;
/// HTTP status code reported by the server.
private final int responseCode;
private final String data;
/// Optional response text supplied by the caller.
private final @Nullable String data;
public ResponseCodeException(URI uri, int responseCode) {
this(uri.toString(), responseCode);
/// Creates a failure for a URL and HTTP status code.
public ResponseCodeException(WebURL url, int responseCode) {
this(url.toString(), responseCode);
}
public ResponseCodeException(URI uri, int responseCode, Throwable cause) {
this(uri.toString(), responseCode, cause);
/// Creates a failure for a URL and HTTP status code with an optional cause.
public ResponseCodeException(WebURL url, int responseCode, @Nullable Throwable cause) {
this(url.toString(), responseCode, cause);
}
public ResponseCodeException(URI uri, int responseCode, String data) {
this(uri.toString(), responseCode, data);
/// Creates a failure for a URL and HTTP status code with optional response text.
public ResponseCodeException(WebURL url, int responseCode, @Nullable String data) {
this(url.toString(), responseCode, data);
}
public ResponseCodeException(String uri, int responseCode) {
super("Unable to request url " + uri + ", response code: " + responseCode);
this.uri = uri;
/// Creates a failure for a request address and HTTP status code.
public ResponseCodeException(String url, int responseCode) {
super("Unable to request url " + url + ", response code: " + responseCode);
this.url = url;
this.responseCode = responseCode;
this.data = null;
}
public ResponseCodeException(String uri, int responseCode, Throwable cause) {
super("Unable to request url " + uri + ", response code: " + responseCode, cause);
this.uri = uri;
/// Creates a failure for a request address and HTTP status code with an optional cause.
public ResponseCodeException(String url, int responseCode, @Nullable Throwable cause) {
super("Unable to request url " + url + ", response code: " + responseCode, cause);
this.url = url;
this.responseCode = responseCode;
this.data = null;
}
public ResponseCodeException(String uri, int responseCode, String data) {
super("Unable to request url " + uri + ", response code: " + responseCode + ", data: " + data);
this.uri = uri;
/// Creates a failure for a request address and HTTP status code with optional response text.
public ResponseCodeException(String url, int responseCode, @Nullable String data) {
super("Unable to request url " + url + ", response code: " + responseCode + ", data: " + data);
this.url = url;
this.responseCode = responseCode;
this.data = data;
}
public String getUri() {
return uri;
/// Returns the request address supplied by the caller.
public String getUrl() {
return url;
}
/// Returns the HTTP status code.
public int getResponseCode() {
return responseCode;
}
public String getData() {
/// Returns the supplied response text, or `null` if none was supplied.
public @Nullable String getData() {
return data;
}
}
@@ -17,6 +17,9 @@
*/
package org.jackhuang.hmcl.util.io;
import org.glavo.url.WebURL;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.*;
import java.net.http.HttpHeaders;
@@ -24,35 +27,38 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/// Response URI and headers captured from a URL response.
/// Captures the status, URL, and headers of an HTTP response.
///
/// @param responseCode the HTTP response status code
/// @param url the response URL
/// @param headers the response headers
/// @author Glavo
public record UrlResponseInfo(int responseCode, URI uri, HttpHeaders headers) {
public record UrlResponseInfo(int responseCode, WebURL url, HttpHeaders headers) {
/// Creates response metadata from a URL connection.
public static UrlResponseInfo of(HttpURLConnection connection) throws IOException {
return new UrlResponseInfo(connection.getResponseCode(), toURI(connection.getURL()), headers(connection));
return new UrlResponseInfo(connection.getResponseCode(), toWebURL(connection.getURL()), headers(connection));
}
/// Converts a response URL into a URI.
private static URI toURI(URL url) throws IOException {
/// Parses a response URL, reporting invalid response addresses as I/O failures.
private static WebURL toWebURL(URL url) throws IOException {
try {
return url.toURI();
} catch (URISyntaxException e) {
return WebURL.of(url);
} catch (IllegalArgumentException e) {
throw new IOException("Invalid response URL: " + url, e);
}
}
/// Copies named header fields from a URL connection.
private static HttpHeaders headers(URLConnection connection) {
Map<String, List<String>> headerFields = connection.getHeaderFields();
@Nullable Map<@Nullable String, @Nullable List<String>> headerFields = connection.getHeaderFields();
if (headerFields == null || headerFields.isEmpty()) {
return HttpHeaders.of(Map.of(), (k, v) -> true);
}
LinkedHashMap<String, List<String>> headers = new LinkedHashMap<>();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
String name = entry.getKey();
List<String> values = entry.getValue();
for (Map.Entry<@Nullable String, @Nullable List<String>> entry : headerFields.entrySet()) {
@Nullable String name = entry.getKey();
@Nullable List<String> values = entry.getValue();
if (name != null && values != null) {
headers.put(name, List.copyOf(values));
}
@@ -17,6 +17,7 @@
*/
package org.jackhuang.hmcl.game;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.download.DefaultCacheRepository;
import org.jackhuang.hmcl.download.DefaultDependencyManager;
import org.jackhuang.hmcl.download.MojangDownloadProvider;
@@ -33,7 +34,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpHeaders;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
@@ -370,7 +370,7 @@ public final class DefaultGameRepositoryDraftTest {
Files.writeString(target, "old client");
String newUrl = "https://example.invalid/new.jar";
DefaultDependencyManager manager = createDependencyManager(repository);
manager.getCacheRepository().cacheText(new UrlResponseInfo(200, URI.create(newUrl),
manager.getCacheRepository().cacheText(new UrlResponseInfo(200, WebURL.parse(newUrl),
HttpHeaders.of(Map.of("etag", List.of("new-client"),
"cache-control", List.of("max-age=3600")), (name, value) -> true)), "new client");
GameInstanceManifest replacement = original.withDownloads(Map.of(DownloadType.CLIENT, new DownloadInfo(newUrl)));
@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.task;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import org.glavo.url.WebURL;
import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
@@ -32,13 +33,14 @@ import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpHeaders;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
@@ -54,6 +56,60 @@ public final class FetchTaskTest {
FetchTask.notifyInitialized();
}
/// Ensures URL normalization survives cache-index persistence without merging escaped path separators.
@Test
public void cacheKeysPreserveEncodedPaths(@TempDir Path tempDir) throws IOException {
CacheRepository repository = newRepository(tempDir);
WebURL encoded = WebURL.parse("HTTPS://EXAMPLE.COM:443/a%2Fb?token=first#part");
WebURL separated = WebURL.parse("https://example.com/a/b?token=second");
HttpHeaders headers = HttpHeaders.of(Map.of("etag", List.of("\"version\"")), (name, value) -> true);
repository.cacheText(new UrlResponseInfo(200, encoded, headers), "encoded");
repository.cacheText(new UrlResponseInfo(200, separated, headers), "separated");
CacheRepository reloaded = new CacheRepository();
reloaded.changeDirectory(repository.getCommonDirectory());
assertEquals("encoded", Files.readString(reloaded.getCachedRemoteFile(
WebURL.parse("https://example.com/a%2Fb?token=changed"), false)));
assertEquals("separated", Files.readString(reloaded.getCachedRemoteFile(separated, false)));
assertEquals(Map.of("if-none-match", "\"version\""), reloaded.injectConnection(encoded));
reloaded.removeRemoteEntry(encoded);
assertThrows(IOException.class, () -> reloaded.getCachedRemoteFile(encoded, false));
assertEquals("separated", Files.readString(reloaded.getCachedRemoteFile(separated, false)));
}
/// Ensures non-HTTP downloads still accept file URLs with escaped path characters.
@Test
public void readsFileURL(@TempDir Path tempDir) throws IOException {
Path source = Files.writeString(tempDir.resolve("file with spaces.txt"), "file content");
TextFetchTask task = new TextFetchTask(WebURL.of(source.toUri()));
task.setCacheRepository(newRepository(tempDir));
task.setRetry(1);
assertTrue(task.test());
assertEquals("file content", task.getResult());
}
/// Ensures query-only redirects retain the current path and preserve literal query spaces.
@Test
public void resolvesQueryOnlyRedirect(@TempDir Path tempDir) throws IOException {
AtomicInteger requests = new AtomicInteger();
try (TestHttpServer server = TestHttpServer.start(exchange -> {
if (requests.getAndIncrement() == 0) {
exchange.getResponseHeaders().set("Location", "?token=a b&path=%2F");
exchange.sendResponseHeaders(302, -1);
exchange.close();
} else {
sendBytes(exchange, 200, exchange.getRequestURI().toASCIIString().getBytes(UTF_8));
}
})) {
TextFetchTask task = new TextFetchTask(server.url());
task.setCacheRepository(newRepository(tempDir));
task.setRetry(1);
assertTrue(task.test());
assertEquals("/file?token=a%20b&path=%2F", task.getResult());
assertEquals(2, requests.get());
}
}
/// Ensures checksum failures are reported and do not replace an existing target file.
@Test
public void checksumMismatchFailsWithoutReplacingTarget(@TempDir Path tempDir) throws IOException {
@@ -63,7 +119,7 @@ public final class FetchTaskTest {
try (TestHttpServer server = TestHttpServer.start(exchange -> sendBytes(exchange, 200, data))) {
FileDownloadTask task = new FileDownloadTask(
server.uri(),
server.url(),
target,
new FileDownloadTask.IntegrityCheck("SHA-1", "0000000000000000000000000000000000000000")
);
@@ -80,9 +136,9 @@ public final class FetchTaskTest {
public void cacheFileTaskUsesExpectedSha1(@TempDir Path tempDir) throws IOException {
byte[] data = "cached content".getBytes(UTF_8);
String sha1 = DigestUtils.digestToString(CacheRepository.SHA1, data);
URI uri = URI.create("https://example.invalid/file");
WebURL url = WebURL.parse("https://example.invalid/file");
CacheRepository repository = newRepository(tempDir);
CacheFileTask first = new CacheFileTask(List.of(uri), sha1);
CacheFileTask first = new CacheFileTask(List.of(url), sha1);
first.setCacheRepository(repository);
try (FetchTask.Context context = first.getContext(null, false, null)) {
@@ -93,7 +149,7 @@ public final class FetchTaskTest {
Path cached = Objects.requireNonNull(first.getResult());
assertArrayEquals(data, Files.readAllBytes(cached));
CacheFileTask second = new CacheFileTask(List.of(uri), sha1);
CacheFileTask second = new CacheFileTask(List.of(url), sha1);
second.setCacheRepository(repository);
assertEquals(FetchTask.EnumCheckETag.CACHED, second.shouldCheckETag());
assertEquals(cached, second.getResult());
@@ -106,7 +162,7 @@ public final class FetchTaskTest {
String expectedSha1 = "0000000000000000000000000000000000000000";
CacheRepository repository = newRepository(tempDir);
CacheFileTask task = new CacheFileTask(
List.of(URI.create("https://example.invalid/file")), expectedSha1);
List.of(WebURL.parse("https://example.invalid/file")), expectedSha1);
task.setCacheRepository(repository);
FetchTask.Context context = task.getContext(null, false, null);
context.write(data, 0, data.length);
@@ -145,7 +201,7 @@ public final class FetchTaskTest {
}
})) {
Path target = tempDir.resolve("target.bin");
FileDownloadTask task = new FileDownloadTask(server.uri(), target);
FileDownloadTask task = new FileDownloadTask(server.url(), target);
task.setCacheRepository(newRepository(tempDir));
task.setRetry(3);
@@ -185,7 +241,7 @@ public final class FetchTaskTest {
}
})) {
Path target = tempDir.resolve("target.bin");
FileDownloadTask task = new FileDownloadTask(server.uri(), target);
FileDownloadTask task = new FileDownloadTask(server.url(), target);
task.setCacheRepository(newRepository(tempDir));
task.setRetry(3);
@@ -232,7 +288,7 @@ public final class FetchTaskTest {
}
})) {
Path target = tempDir.resolve("target.bin");
FileDownloadTask task = new FileDownloadTask(server.uri(), target);
FileDownloadTask task = new FileDownloadTask(server.url(), target);
task.setCacheRepository(newRepository(tempDir));
task.setRetry(3);
@@ -274,7 +330,7 @@ public final class FetchTaskTest {
}
}
})) {
TextFetchTask task = new TextFetchTask(server.uri());
TextFetchTask task = new TextFetchTask(server.url());
task.setCacheRepository(newRepository(tempDir));
task.setRetry(2);
@@ -286,9 +342,9 @@ public final class FetchTaskTest {
/// Text fetch task that avoids JavaFX progress updates in isolated unit tests.
private static final class TextFetchTask extends FetchTask<String> {
/// Creates a text fetch task for one URI.
TextFetchTask(URI uri) {
super(List.of(uri));
/// Creates a text fetch task for one URL.
TextFetchTask(WebURL url) {
super(List.of(url));
}
@Override
@@ -361,9 +417,9 @@ public final class FetchTaskTest {
return new TestHttpServer(server);
}
/// Returns the file endpoint URI.
URI uri() {
return URI.create("http://127.0.0.1:" + server.getAddress().getPort() + "/file");
/// Returns the file endpoint URL.
WebURL url() {
return WebURL.parse("http://127.0.0.1:" + server.getAddress().getPort() + "/file");
}
@Override
@@ -17,10 +17,11 @@
*/
package org.jackhuang.hmcl.util.io;
import org.glavo.url.WebURL;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.List;
import java.util.Map;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.jackhuang.hmcl.util.io.NetworkUtils.*;
@@ -31,17 +32,48 @@ import static org.junit.jupiter.api.Assertions.*;
*/
public class NetworkUtilsTest {
/// Ensures cache keys retain escaped delimiters, credentials, and non-default ports.
@Test
public void dropQueryPreservesEncodedComponents() {
WebURL url = WebURL.parse("https://user:p%[email protected]:8443/a%2Fb%3Fc%23d?q=1#fragment");
WebURL key = dropQuery(url);
assertEquals("https://user:p%[email protected]:8443/a%2Fb%3Fc%23d", key.toString());
assertSame(key, dropQuery(key));
assertNotEquals(dropQuery(WebURL.parse("https://example.com/a%2Fb?q=1")),
dropQuery(WebURL.parse("https://example.com/a/b?q=1")));
}
/// Ensures query replacement does not append parameters inside a fragment or decode the path.
@Test
public void withQueryPreservesPathAndFragment() {
WebURL url = WebURL.parse("https://example.com/a%2Fb?old=1#section");
assertEquals(List.of(WebURL.parse("https://example.com/a%2Fb?q=a+b%26c#section")),
withQuery(List.of(url), Map.of("q", "a b&c")));
assertEquals(List.of(url), withQuery(List.of(url), Map.of()));
}
/// Ensures optional URL parsing rejects absent and relative input and normalizes absolute URLs.
@Test
public void optionalWebURLParsing() {
assertNull(toWebURLOrNull(null));
assertNull(toWebURLOrNull(" "));
assertNull(toWebURLOrNull("/relative/path"));
assertNull(toWebURLOrNull("https://[invalid"));
assertEquals(WebURL.parse("https://example.com/a%20b"),
toWebURLOrNull("HTTPS://EXAMPLE.COM:443/a b"));
}
@Test
public void testIsLoopbackAddress() {
assertTrue(isLoopbackAddress(URI.create("https://127.0.0.1/test")));
assertTrue(isLoopbackAddress(URI.create("https://127.0.0.1:8080/test")));
assertTrue(isLoopbackAddress(URI.create("https://localhost/test")));
assertTrue(isLoopbackAddress(URI.create("https://localhost:8080/test")));
assertTrue(isLoopbackAddress(URI.create("https://[::1]/test")));
assertTrue(isLoopbackAddress(URI.create("https://[::1]:8080/test")));
assertTrue(isLoopbackAddress(WebURL.parse("https://127.0.0.1/test")));
assertTrue(isLoopbackAddress(WebURL.parse("https://127.0.0.1:8080/test")));
assertTrue(isLoopbackAddress(WebURL.parse("https://localhost/test")));
assertTrue(isLoopbackAddress(WebURL.parse("https://localhost:8080/test")));
assertTrue(isLoopbackAddress(WebURL.parse("https://[::1]/test")));
assertTrue(isLoopbackAddress(WebURL.parse("https://[::1]:8080/test")));
assertFalse(isLoopbackAddress(URI.create("https://www.example.com/test")));
assertFalse(isLoopbackAddress(URI.create("https://www.example.com:8080/test")));
assertFalse(isLoopbackAddress(WebURL.parse("https://www.example.com/test")));
assertFalse(isLoopbackAddress(WebURL.parse("https://www.example.com:8080/test")));
}
@Test
@@ -79,8 +79,8 @@ public abstract class CheckUpdate extends DefaultTask {
@TaskAction
public void run() throws Exception {
String uri = getUri().get();
URI apiUri = URI.create(concatUri(uri, "api", "json"));
String url = getUri().get();
URI apiUri = URI.create(concatUri(url, "api", "json"));
LOGGER.quiet("Fetching metadata from {}", apiUri);
BuildMetadata buildMetadata;
@@ -113,7 +113,7 @@ public abstract class CheckUpdate extends DefaultTask {
buildMetadata = metadatas.get(metadatas.size() - 1);
} else if (WORKFLOW_JOB.equals(jobType) || FREE_STYLE_PROJECT.equals(jobType)) {
buildMetadata = fetchBuildInfo(helper, uri);
buildMetadata = fetchBuildInfo(helper, url);
} else {
throw new GradleException("Unsupported job type: " + jobType);
}