优化 data URI 支持 (#4206)
This commit is contained in:
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher
|
||||
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.jackhuang.hmcl.util.io;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Base64;
|
||||
|
||||
public final class DataUri {
|
||||
public static final String SCHEME = "data";
|
||||
|
||||
private static IllegalArgumentException invalidUri(URI uri) {
|
||||
return new IllegalArgumentException("Invalid data URI: " + uri);
|
||||
}
|
||||
|
||||
public static boolean isDataUri(URI uri) {
|
||||
return uri != null && SCHEME.equals(uri.getScheme());
|
||||
}
|
||||
|
||||
private final @NotNull String mediaType;
|
||||
private final @NotNull Charset charset;
|
||||
private final boolean base64;
|
||||
private final @NotNull String rawData;
|
||||
|
||||
public DataUri(URI uri) {
|
||||
if (!uri.getScheme().equals(SCHEME)) {
|
||||
throw new IllegalArgumentException("URI scheme must be " + SCHEME);
|
||||
}
|
||||
|
||||
String schemeSpecificPart = uri.getSchemeSpecificPart();
|
||||
if (schemeSpecificPart == null)
|
||||
throw invalidUri(uri);
|
||||
|
||||
int comma = schemeSpecificPart.indexOf(',');
|
||||
if (comma < 0)
|
||||
throw invalidUri(uri);
|
||||
|
||||
String mediaType = schemeSpecificPart.substring(0, comma);
|
||||
boolean base64 = mediaType.endsWith(";base64");
|
||||
if (base64)
|
||||
mediaType = mediaType.substring(0, mediaType.length() - ";base64".length());
|
||||
|
||||
|
||||
this.mediaType = mediaType.trim();
|
||||
this.charset = NetworkUtils.getCharsetFromContentType(mediaType);
|
||||
this.base64 = base64;
|
||||
this.rawData = schemeSpecificPart.substring(comma + 1);
|
||||
}
|
||||
|
||||
public @NotNull String getMediaType() {
|
||||
return mediaType;
|
||||
}
|
||||
|
||||
public @NotNull Charset getCharset() {
|
||||
return charset;
|
||||
}
|
||||
|
||||
public boolean isBase64() {
|
||||
return base64;
|
||||
}
|
||||
|
||||
public @NotNull String getRawData() {
|
||||
return rawData;
|
||||
}
|
||||
|
||||
public byte[] readBytes() throws IOException {
|
||||
if (base64) {
|
||||
try {
|
||||
return Base64.getDecoder().decode(rawData);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
} else {
|
||||
return rawData.getBytes(charset);
|
||||
}
|
||||
}
|
||||
|
||||
public String readString() throws IOException {
|
||||
if (base64) {
|
||||
try {
|
||||
return new String(Base64.getDecoder().decode(rawData), charset);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
} else {
|
||||
return rawData;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("DataUri{mediaType='%s', charset=%s, base64=%s, body='%s'}", mediaType, charset, base64, rawData);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher
|
||||
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.jackhuang.hmcl.util.io;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public final class DataUriTest {
|
||||
|
||||
private static String readString(String uri) throws IOException {
|
||||
return new DataUri(URI.create(uri)).readString();
|
||||
}
|
||||
|
||||
private static byte[] readBytes(String uri) throws IOException {
|
||||
return new DataUri(URI.create(uri)).readBytes();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadString() throws IOException {
|
||||
assertEquals("Hello, World!", readString("data:,Hello%2C%20World%21"));
|
||||
assertEquals("Hello, World!", readString("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="));
|
||||
assertEquals("<h1>Hello, World!</h1>", readString("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E"));
|
||||
assertEquals("<script>alert('hi');</script>", readString("data:text/html,%3Cscript%3Ealert%28%27hi%27%29%3B%3C%2Fscript%3E"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user