diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java index aaef6607bc..7e05ac7389 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java @@ -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) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/DownloadProviders.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/DownloadProviders.java index 3cca9f537e..44f1438f27 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/DownloadProviders.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/DownloadProviders.java @@ -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"); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaBundle.java b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaBundle.java index ecc99f3acd..1b1ab59360 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaBundle.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaBundle.java @@ -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 links; + /// Candidate bundle URLs in attempt order. + private final List links; private final FileDownloadTask.IntegrityCheck hash; private final Map files; - public TerracottaBundle(Path root, List links, FileDownloadTask.IntegrityCheck hash, Map 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 links, FileDownloadTask.IntegrityCheck hash, Map files) { this.root = root; this.links = links; this.hash = hash; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaManager.java b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaManager.java index 75c2d362f3..cf67647e50 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaManager.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaManager.java @@ -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 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> 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> 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)) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaMetadata.java b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaMetadata.java index b7ad8d8c4e..dd65c0a8f3 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaMetadata.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaMetadata.java @@ -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 stream = downloads.stream(), streamCN = downloadsCN.stream(); - List links = (LocaleUtils.IS_CHINA_MAINLAND ? Stream.concat(streamCN, stream) : Stream.concat(stream, streamCN)) - .map(link -> URI.create(options.replace(link))) + @Unmodifiable List links = (LocaleUtils.IS_CHINA_MAINLAND ? Stream.concat(streamCN, stream) : Stream.concat(stream, streamCN)) + .map(link -> WebURL.parse(options.replace(link))) .toList(); Map files = pkg.files.entrySet().stream().collect(Collectors.toUnmodifiableMap( diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaNodeList.java b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaNodeList.java index bb5171ed13..d0b2c8d61b 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaNodeList.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaNodeList.java @@ -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 list; + /// Cached node URLs, or `null` before the first fetch completes. + private static volatile @Nullable @Unmodifiable List list; - public static List fetch() { - List list = TerracottaNodeList.list; + /// Returns the cached unmodifiable node URLs, fetching once and caching an empty list on failure. + public static @Unmodifiable List fetch() { + @Nullable @Unmodifiable List 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); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/theme/ThemePackManager.java b/HMCL/src/main/java/org/jackhuang/hmcl/theme/ThemePackManager.java index ef34371ee5..c800b1041d 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/theme/ThemePackManager.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/theme/ThemePackManager.java @@ -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 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); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java index 52411fb21b..6c0abf45ae 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java @@ -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); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java index 5b77263a38..e7fa733589 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java @@ -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 getRemoteImageTask(List 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 getRemoteImageTask(List 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); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/UpdateInstallerWizardProvider.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/UpdateInstallerWizardProvider.java index 23a6612c03..cf2c2b254a 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/UpdateInstallerWizardProvider.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/UpdateInstallerWizardProvider.java @@ -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()) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/DownloadListPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/DownloadListPage.java index a8f41eec27..d3831b6deb 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/DownloadListPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/DownloadListPage.java @@ -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 createLoadTask(@NotNull List uris) { - return FXUtils.getRemoteImageTask(uris, 80, 80, true, true); + protected @NotNull Task createLoadTask(@NotNull List urls) { + return FXUtils.getRemoteImageTask(urls, 80, 80, true, true); } }; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/Instances.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/Instances.java index 665983ba4a..8d5b2ed539 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/Instances.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/instances/Instances.java @@ -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 downloadURLs; + @Unmodifiable List downloadURLs; try { downloadURLs = downloadProvider.injectURLWithCandidates(file.file().url()); modpack = Files.createTempFile("modpack", ".zip"); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/RemoteImageLoader.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/RemoteImageLoader.java index 6cc4ee7aa9..68b6b3b7c9 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/RemoteImageLoader.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/RemoteImageLoader.java @@ -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> cache = new HashMap<>(); - private final Map>>> pendingRequests = new HashMap<>(); - private final WeakHashMap, URI> reverseLookup = new WeakHashMap<>(); + /// Loaded images indexed by normalized URL; image references do not prevent garbage collection. + private final Map> cache = new HashMap<>(); + /// Image targets awaiting each URL's active load task. + private final Map>>> pendingRequests = new HashMap<>(); + /// Current requested URL for each target, used to discard obsolete load completions. + private final WeakHashMap, 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 createLoadTask(@NotNull List uris); + /// Creates a task that loads an image from candidate URLs in attempt order. + protected abstract @NotNull Task createLoadTask(@NotNull List urls); + /// Loads an image or assigns the placeholder when the URL is absent or invalid. @FXThread - public void load(@NotNull WritableValue writableValue, String url) { - URI uri = NetworkUtils.toURIOrNull(url); - if (uri == null) { + public void load(@NotNull WritableValue writableValue, @Nullable String url) { + @Nullable WebURL webUrl = NetworkUtils.toWebURLOrNull(url); + if (webUrl == null) { reverseLookup.remove(writableValue); writableValue.setValue(getPlaceholder()); return; } - WeakReference 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>> list = pendingRequests.get(uri); + @Nullable List>> 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>> list = pendingRequests.remove(uri); + cache.put(webUrl, new WeakReference<>(image)); + @Nullable List>> list = pendingRequests.remove(webUrl); if (list != null) { for (WeakReference> ref : list) { - WritableValue target = ref.get(); - if (target != null && uri.equals(reverseLookup.get(target))) { + @Nullable WritableValue target = ref.get(); + if (target != null && webUrl.equals(reverseLookup.get(target))) { reverseLookup.remove(target); target.setValue(image); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURL.java similarity index 52% rename from HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java rename to HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURL.java index 56c46871ea..4a65f871b9 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURL.java @@ -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 { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLConnection.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLConnection.java index 642d5da651..d6eaefec41 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLConnection.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLConnection.java @@ -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 diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLHandle.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLHandle.java index 66559ba158..7c25c66f7e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLHandle.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLHandle.java @@ -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; } } } diff --git a/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataURLTest.java b/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataURLTest.java new file mode 100644 index 0000000000..de42aca5b0 --- /dev/null +++ b/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataURLTest.java @@ -0,0 +1,110 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui 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 . + */ +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("

Hello, World!

", readString("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E")); + assertEquals("", 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()); + } +} diff --git a/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataUriTest.java b/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataUriTest.java deleted file mode 100644 index e1447e0319..0000000000 --- a/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataUriTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Hello Minecraft! Launcher - * Copyright (C) 2025 huangyuhui 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 . - */ -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("

Hello, World!

", readString("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E")); - assertEquals("", readString("data:text/html,%3Cscript%3Ealert%28%27hi%27%29%3B%3C%2Fscript%3E")); - } -} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/CurseForgeRemoteAddonRepository.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/CurseForgeRemoteAddonRepository.java index e044ff332b..1cde59d1bb 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/CurseForgeRemoteAddonRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/CurseForgeRemoteAddonRepository.java @@ -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> response = null; @Nullable IOException exception = null; - List candidates = downloadProvider.injectURLWithCandidates(NetworkUtils.withQuery(PREFIX + "/v1/mods/search", query)); - for (URI candidate : candidates) { + @Unmodifiable List 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())) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/ModrinthRemoteAddonRepository.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/ModrinthRemoteAddonRepository.java index efe88b5f00..ee52bb5d5e 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/ModrinthRemoteAddonRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/repository/ModrinthRemoteAddonRepository.java @@ -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 candidates = downloadProvider.injectURLWithCandidates(NetworkUtils.withQuery(PREFIX + "/v2/search", query)); + @Unmodifiable List 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 response = HttpRequest.GET(candidate.toString()) @@ -246,10 +246,10 @@ public final class ModrinthRemoteAddonRepository implements RemoteAddonRepositor SEMAPHORE.acquireUninterruptibly(); try { id = StringUtils.removePrefix(id, "local-"); - List candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/project/" + id); + @Unmodifiable List 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 candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/project/" + id + "/version?include_changelog=false"); + @Unmodifiable List candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/project/" + id + "/version?include_changelog=false"); IOException exception = null; - for (URI candidate : candidates) { + for (WebURL candidate : candidates) { try { List 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 candidates = downloadProvider.injectURLWithCandidates(PREFIX + "/v2/version/" + versionId); + @Unmodifiable List 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 { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java index 8087ad6125..9003ad1d5f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java @@ -39,12 +39,12 @@ public final class AuthlibInjectorDnD { private AuthlibInjectorDnD() {} public static Optional 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(); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorProvider.java index ee9c05acd8..5769c006c3 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorProvider.java @@ -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 diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/microsoft/MicrosoftService.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/microsoft/MicrosoftService.java index b65e258795..bed14b0eb7 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/microsoft/MicrosoftService.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/microsoft/MicrosoftService.java @@ -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); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/offline/Skin.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/offline/Skin.java index fea39c490a..317fff38d1 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/offline/Skin.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/offline/Skin.java @@ -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 { - public FetchBytesTask(String uri) { - super(List.of(NetworkUtils.toURI(uri))); + public FetchBytesTask(String url) { + super(List.of(WebURL.parse(url))); } @Override diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilProvider.java index db90e38f1a..431a2f23f5 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilProvider.java @@ -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 http://wiki.vg - */ +/// Supplies the endpoints used by [YggdrasilService] for authentication and profile operations. +/// +/// @see http://wiki.vg 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 <access token> - * - * 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 https://wiki.vg/Mojang_API#Upload_Skin - * @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 <access token> + /// + /// 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 https://wiki.vg/Mojang_API#Upload_Skin + /// @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; } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java index 540ba623d7..f3c3136f65 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java @@ -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); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/AutoDownloadProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/AutoDownloadProvider.java index 110dced217..793a457b63 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/AutoDownloadProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/AutoDownloadProvider.java @@ -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 getAll( + /// Collects candidates in provider order, removing duplicates by normalized URL equality. + private static @Unmodifiable List getAll( List providers, - Function> function) { - LinkedHashSet result = new LinkedHashSet<>(); + Function> function) { + LinkedHashSet 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 getVersionListURLs() { + public @Unmodifiable List getVersionListURLs() { return getAll(versionListProviders, DownloadProvider::getVersionListURLs); } @@ -86,17 +88,17 @@ public final class AutoDownloadProvider implements DownloadProvider { } @Override - public List getAssetObjectCandidates(String assetObjectLocation) { + public @Unmodifiable List getAssetObjectCandidates(String assetObjectLocation) { return getAll(fileProviders, provider -> provider.getAssetObjectCandidates(assetObjectLocation)); } @Override - public List injectURLWithCandidates(String baseURL) { + public @Unmodifiable List injectURLWithCandidates(String baseURL) { return getAll(fileProviders, provider -> provider.injectURLWithCandidates(baseURL)); } @Override - public List injectURLsWithCandidates(List urls) { + public @Unmodifiable List injectURLsWithCandidates(List urls) { return getAll(fileProviders, provider -> provider.injectURLsWithCandidates(urls)); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/BMCLAPIDownloadProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/BMCLAPIDownloadProvider.java index b301f6f196..6f41ad595a 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/BMCLAPIDownloadProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/BMCLAPIDownloadProvider.java @@ -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 getVersionListURLs() { - return List.of(URI.create(apiRoot + "/mc/game/version_manifest.json")); + public @Unmodifiable List getVersionListURLs() { + return List.of(WebURL.parse(apiRoot + "/mc/game/version_manifest.json")); } @Override - public List getAssetObjectCandidates(String assetObjectLocation) { - return List.of(NetworkUtils.toURI(apiRoot + "/assets/" + assetObjectLocation)); + public @Unmodifiable List 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 injectURLWithCandidates(String baseURL) { + public @Unmodifiable List 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)); } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProvider.java index 10a1347d89..06ccba1e44 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProvider.java @@ -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 getVersionListURLs(); + /// Returns unmodifiable candidate URLs for the Minecraft version manifest, in attempt order. + @Unmodifiable List getVersionListURLs(); - List 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 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 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 injectURLWithCandidates(String baseURL) { + return List.of(WebURL.parse(injectURL(baseURL))); } - default List injectURLsWithCandidates(List urls) { - LinkedHashSet result = new LinkedHashSet<>(); + /// Returns unmodifiable candidates for all URLs, preserving first occurrence order and removing duplicates. + default @Unmodifiable List injectURLsWithCandidates(List urls) { + LinkedHashSet result = new LinkedHashSet<>(); for (String url : urls) { result.addAll(injectURLWithCandidates(url)); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProviderWrapper.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProviderWrapper.java index 5570dbb1ad..b2369e5a12 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProviderWrapper.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DownloadProviderWrapper.java @@ -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 getAssetObjectCandidates(String assetObjectLocation) { + public @Unmodifiable List getAssetObjectCandidates(String assetObjectLocation) { return getProvider().getAssetObjectCandidates(assetObjectLocation); } @Override - public List getVersionListURLs() { + public @Unmodifiable List getVersionListURLs() { return getProvider().getVersionListURLs(); } @@ -59,12 +60,12 @@ public final class DownloadProviderWrapper implements DownloadProvider { } @Override - public List injectURLWithCandidates(String baseURL) { + public @Unmodifiable List injectURLWithCandidates(String baseURL) { return getProvider().injectURLWithCandidates(baseURL); } @Override - public List injectURLsWithCandidates(List urls) { + public @Unmodifiable List injectURLsWithCandidates(List urls) { return getProvider().injectURLsWithCandidates(urls); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MojangDownloadProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MojangDownloadProvider.java index 5e2ec3cac9..c25755a687 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MojangDownloadProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MojangDownloadProvider.java @@ -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 getVersionListURLs() { - return List.of(URI.create("https://piston-meta.mojang.com/mc/game/version_manifest.json")); + public @Unmodifiable List getVersionListURLs() { + return List.of(WebURL.parse("https://piston-meta.mojang.com/mc/game/version_manifest.json")); } @Override - public List getAssetObjectCandidates(String assetObjectLocation) { - return List.of(NetworkUtils.toURI("https://resources.download.minecraft.net/" + assetObjectLocation)); + public @Unmodifiable List getAssetObjectCandidates(String assetObjectLocation) { + return List.of(WebURL.parse("https://resources.download.minecraft.net/" + assetObjectLocation)); } @Override diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallTask.java index 2d5d60b41f..cb9bc5abc9 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallTask.java @@ -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 { throw new Exception("client_mappings download info not found"); } - List mappingsUrl = dependencyManager.getDownloadProvider() + @Unmodifiable List mappingsUrl = dependencyManager.getDownloadProvider() .injectURLWithCandidates(mappings.getUrl()); var mappingsTask = new FileDownloadTask( mappingsUrl, diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java index be63c85398..c71c0fb2ce 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeVersionList.java @@ -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 { LOG.warning("Unable to calc hash value of file " + file, e); } if (download) { - List uris = dependencyManager.getDownloadProvider().getAssetObjectCandidates(assetObject.getLocation()); + @Unmodifiable List 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())); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameLibrariesTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameLibrariesTask.java index fcd6e3ea71..93888b4c9f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameLibrariesTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameLibrariesTask.java @@ -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 { for (FMLLib fmlLib : fmlLibs) { Path file = libDir.resolve(fmlLib.name); if (shouldDownloadFMLLib(fmlLib, file)) { - List uris = dependencyManager.getDownloadProvider() + @Unmodifiable List urls = dependencyManager.getDownloadProvider() .injectURLWithCandidates(fmlLib.downloadUrl()); - dependencies.add(new FileDownloadTask(uris, file) + dependencies.add(new FileDownloadTask(urls, file) .withCounter("hmcl.install.libraries")); } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java index 8c735e2b01..d900f674e4 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java @@ -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 { } - List uris = dependencyManager.getDownloadProvider().injectURLWithCandidates(url); - task = new FileDownloadTask(uris, jar, + @Unmodifiable List 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); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeOldInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeOldInstallTask.java index 522729d4df..a71ca00228 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeOldInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeOldInstallTask.java @@ -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 { throw new Exception("client_mappings download info not found"); } - List mappingsUri = dependencyManager.getDownloadProvider() + @Unmodifiable List mappingsUri = dependencyManager.getDownloadProvider() .injectURLWithCandidates(mappings.getUrl()); var mappingsTask = new FileDownloadTask( mappingsUri, diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/curse/CurseInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/curse/CurseInstallTask.java index 600924796a..4a379511bf 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/curse/CurseInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/curse/CurseInstallTask.java @@ -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 { 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)) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/mcbbs/McbbsModpackCompletionTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/mcbbs/McbbsModpackCompletionTask.java index cc3f7ebada..8c2b090fca 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/mcbbs/McbbsModpackCompletionTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/mcbbs/McbbsModpackCompletionTask.java @@ -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 { 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())); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthInstallTask.java index 7f8aa65c03..0d8d894e05 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthInstallTask.java @@ -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 { 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)) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/multimc/MultiMCComponents.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/multimc/MultiMCComponents.java index b4b8aa2a5b..ab0194843a 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/multimc/MultiMCComponents.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/multimc/MultiMCComponents.java @@ -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)); } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/CacheFileTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/CacheFileTask.java index 37c9433e7e..e97b3d80ca 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/CacheFileTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/CacheFileTask.java @@ -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 { /// 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 { } // 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) { } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadException.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadException.java index dab0e48cd0..e30eafac29 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadException.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadException.java @@ -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; } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java index 8c3a2b8796..d96f732ba8 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java @@ -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 extends Task { protected static final int DEFAULT_RETRY = 5; - protected final List uris; + /// Immutable snapshot of candidate URLs in attempt order. + protected final @Unmodifiable List 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 extends Task { 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 extends Task { } } - ArrayList exceptions = null; + @Nullable ArrayList 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 extends Task { 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 extends Task { 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 extends Task { } } - 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 exceptions = null; + @Nullable ArrayList 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 extends Task { throw new InterruptedException(); } - List redirects = null; + @Nullable List 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 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 extends Task { Optional 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 extends Task { 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 extends Task { } } 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 extends Task { 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 extends Task { 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 extends Task { } 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 extends Task { } 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 extends Task { 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 extends Task { } } - private void downloadNotHttp(URI uri) throws DownloadException, InterruptedException { - ArrayList 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 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 extends Task { } 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 exceptions) { + /// Wraps the final failure and attaches preceding failures as suppressed exceptions. + private static DownloadException toDownloadException(WebURL url, @Nullable Exception last, @Nullable ArrayList 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 extends Task { for (Exception e : exceptions) { last.addSuppressed(e); } - return new DownloadException(uri, last); + return new DownloadException(url, last); } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java index ef336bccd6..b50d64da3c 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java @@ -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 { public record IntegrityCheck(String algorithm, String checksum) { @@ -66,64 +63,61 @@ public class FileDownloadTask extends FetchTask { } 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 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 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 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 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 urls, Path path, @Nullable IntegrityCheck integrityCheck) { + super(urls); this.file = path; this.integrityCheck = integrityCheck; @@ -155,7 +149,7 @@ public class FileDownloadTask extends FetchTask { 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 { } @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 { } 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; } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/GetTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/GetTask.java index 0c28d05767..d02e1d6367 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/GetTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/GetTask.java @@ -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 { - 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 url) { + /// Creates a text download task with a snapshot of nonempty candidate URLs in attempt order. + public GetTask(List url) { super(url); setName(url.get(0).toString()); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java index 709376a449..5f5f329b0c 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java @@ -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 index; + /// Remote entries keyed by URLs without queries or fragments; initialized by [#changeDirectory(Path)]. + private @Nullable LinkedHashMap 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 injectConnection(URI uri) { + /// Returns immutable conditional request headers for the URL's cached ETag, or an empty map. + public @NotNull @Unmodifiable Map 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 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 updateEntity(ETagItem newItem, boolean force) { + /// Creates a merge operation that selects an entry and removes replaced content when its hash differs. + private BiFunction 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 joinETagIndexes(Collection... indexes) { - var eTags = new LinkedHashMap(); - for (Collection eTagItems : indexes) { + private LinkedHashMap joinETagIndexes(@Nullable Collection... indexes) { + var eTags = new LinkedHashMap(); + for (@Nullable Collection 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); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java index 284e2a1f20..a218fc9246 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java @@ -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 withQuery(List list, Map 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 withQuery(List list, Map 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> parseQuery(URI uri) { - return parseQuery(uri.getRawQuery()); + /// Parses the URL query as form-encoded name/value pairs. + public static List> parseQuery(WebURL url) { + return parseQuery(url.getRawQuery()); } public static List> 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> 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 uris) throws IOException { - List 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 urls) throws IOException { + @Nullable List 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 params) throws IOException { + /// Form-encodes the parameters and posts them to the URL. + public static String doPost(WebURL url, @Nullable Map params) throws IOException { StringBuilder sb = new StringBuilder(); if (params != null) { for (Map.Entry 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); } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/ResponseCodeException.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/ResponseCodeException.java index 67ed53dfea..2a7b1ad1f1 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/ResponseCodeException.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/ResponseCodeException.java @@ -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; } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/UrlResponseInfo.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/UrlResponseInfo.java index f6fb9de43c..cb63f767fa 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/UrlResponseInfo.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/UrlResponseInfo.java @@ -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> headerFields = connection.getHeaderFields(); + @Nullable Map<@Nullable String, @Nullable List> headerFields = connection.getHeaderFields(); if (headerFields == null || headerFields.isEmpty()) { return HttpHeaders.of(Map.of(), (k, v) -> true); } LinkedHashMap> headers = new LinkedHashMap<>(); - for (Map.Entry> entry : headerFields.entrySet()) { - String name = entry.getKey(); - List values = entry.getValue(); + for (Map.Entry<@Nullable String, @Nullable List> entry : headerFields.entrySet()) { + @Nullable String name = entry.getKey(); + @Nullable List values = entry.getValue(); if (name != null && values != null) { headers.put(name, List.copyOf(values)); } diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/game/DefaultGameRepositoryDraftTest.java b/HMCLCore/src/test/java/org/jackhuang/hmcl/game/DefaultGameRepositoryDraftTest.java index cdda05ba24..94a41da47b 100644 --- a/HMCLCore/src/test/java/org/jackhuang/hmcl/game/DefaultGameRepositoryDraftTest.java +++ b/HMCLCore/src/test/java/org/jackhuang/hmcl/game/DefaultGameRepositoryDraftTest.java @@ -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))); diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/task/FetchTaskTest.java b/HMCLCore/src/test/java/org/jackhuang/hmcl/task/FetchTaskTest.java index da6000664b..0679b6b88e 100644 --- a/HMCLCore/src/test/java/org/jackhuang/hmcl/task/FetchTaskTest.java +++ b/HMCLCore/src/test/java/org/jackhuang/hmcl/task/FetchTaskTest.java @@ -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 { - /// 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 diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java index 5d87584bb3..5f05716746 100644 --- a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java +++ b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java @@ -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%40ss@example.com:8443/a%2Fb%3Fc%23d?q=1#fragment"); + WebURL key = dropQuery(url); + assertEquals("https://user:p%40ss@example.com: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 diff --git a/buildSrc/src/main/java/org/jackhuang/hmcl/gradle/ci/CheckUpdate.java b/buildSrc/src/main/java/org/jackhuang/hmcl/gradle/ci/CheckUpdate.java index 3c2e4ae968..afc20e17c6 100644 --- a/buildSrc/src/main/java/org/jackhuang/hmcl/gradle/ci/CheckUpdate.java +++ b/buildSrc/src/main/java/org/jackhuang/hmcl/gradle/ci/CheckUpdate.java @@ -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); }