mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-18 01:46:44 +01:00
Clean up http server content types
This commit is contained in:
@@ -6,6 +6,8 @@ import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.HttpUtils;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.core.util.JavalinLogger;
|
||||
import io.javalin.http.ContentType;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
@@ -145,8 +147,10 @@ public final class HttpServer {
|
||||
public static class DefaultRequestRouter implements Router {
|
||||
@Override public void applyRoutes(Javalin javalin) {
|
||||
javalin.get("/", ctx -> {
|
||||
// Send file
|
||||
File file = new File(HTTP_STATIC_FILES.indexFile);
|
||||
if (!file.exists())
|
||||
if (!file.exists()) {
|
||||
ctx.contentType(ContentType.TEXT_HTML);
|
||||
ctx.result("""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -156,10 +160,11 @@ public final class HttpServer {
|
||||
<body>%s</body>
|
||||
</html>
|
||||
""".formatted(translate("messages.status.welcome")));
|
||||
else {
|
||||
final var filePath = file.getPath();
|
||||
final HttpUtils.MediaType fromExtension = HttpUtils.MediaType.getByExtension(filePath.substring(filePath.lastIndexOf(".") + 1));
|
||||
ctx.contentType((fromExtension != null) ? fromExtension.getMIME() : "text/plain").result(FileUtils.read(filePath));
|
||||
} else {
|
||||
var filePath = file.getPath();
|
||||
ContentType fromExtension = ContentType.getContentTypeByExtension(filePath.substring(filePath.lastIndexOf(".") + 1));
|
||||
ctx.contentType(fromExtension != null ? fromExtension : ContentType.TEXT_HTML);
|
||||
ctx.result(FileUtils.read(filePath));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -170,30 +175,31 @@ public final class HttpServer {
|
||||
*/
|
||||
public static class UnhandledRequestRouter implements Router {
|
||||
@Override public void applyRoutes(Javalin javalin) {
|
||||
javalin.error(404, context -> {
|
||||
javalin.error(404, ctx -> {
|
||||
// Error log
|
||||
if (DISPATCH_INFO.logRequests == ServerDebugMode.MISSING)
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.unhandled_request_error", context.method(), context.url()));
|
||||
context.contentType("text/html");
|
||||
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.unhandled_request_error", ctx.method(), ctx.url()));
|
||||
// Send file
|
||||
File file = new File(HTTP_STATIC_FILES.errorFile);
|
||||
if (!file.exists())
|
||||
context.result("""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf8">
|
||||
</head>
|
||||
if (!file.exists()) {
|
||||
ctx.contentType(ContentType.TEXT_HTML);
|
||||
ctx.result("""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<img src="https://http.cat/404" />
|
||||
</body>
|
||||
</html>
|
||||
""");
|
||||
else {
|
||||
final var filePath = file.getPath();
|
||||
final HttpUtils.MediaType fromExtension = HttpUtils.MediaType.getByExtension(filePath.substring(filePath.lastIndexOf(".") + 1));
|
||||
context.contentType((fromExtension != null) ? fromExtension.getMIME() : "text/plain")
|
||||
.result(FileUtils.read(filePath));
|
||||
<body>
|
||||
<img src="https://http.cat/404" />
|
||||
</body>
|
||||
</html>
|
||||
""");
|
||||
} else {
|
||||
var filePath = file.getPath();
|
||||
ContentType fromExtension = ContentType.getContentTypeByExtension(filePath.substring(filePath.lastIndexOf(".") + 1));
|
||||
ctx.contentType(fromExtension != null ? fromExtension : ContentType.TEXT_HTML);
|
||||
ctx.result(FileUtils.read(filePath));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package emu.grasscutter.server.http.documentation;
|
||||
import emu.grasscutter.tools.Tools;
|
||||
import emu.grasscutter.utils.HttpUtils;
|
||||
import emu.grasscutter.utils.Language;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.DOCUMENT_LANGUAGE;
|
||||
@@ -19,6 +20,6 @@ final class GachaMappingRequestHandler implements DocumentationHandler {
|
||||
@Override
|
||||
public void handle(Context ctx) {
|
||||
final int langIdx = Language.TextStrings.MAP_LANGUAGES.getOrDefault(DOCUMENT_LANGUAGE, 0); // TODO: This should really be based off the client language somehow
|
||||
ctx.contentType(HttpUtils.MediaType._json.getMIME()).result(gachaJsons.get(langIdx));
|
||||
ctx.contentType(ContentType.APPLICATION_JSON).result(gachaJsons.get(langIdx));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.HttpUtils;
|
||||
import emu.grasscutter.utils.Language;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import java.io.File;
|
||||
@@ -41,7 +42,7 @@ final class HandbookRequestHandler implements DocumentationHandler {
|
||||
if (template == null) {
|
||||
ctx.status(500);
|
||||
} else {
|
||||
ctx.contentType(HttpUtils.MediaType._html.getMIME());
|
||||
ctx.contentType(ContentType.TEXT_HTML);
|
||||
ctx.result(handbookHtmls.get(langIdx));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.HttpUtils;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import java.io.File;
|
||||
@@ -36,7 +37,7 @@ final class RootRequestHandler implements DocumentationHandler {
|
||||
String content = template.replace("{{TITLE}}", translate("documentation.index.title"))
|
||||
.replace("{{ITEM_HANDBOOK}}", translate("documentation.index.handbook"))
|
||||
.replace("{{ITEM_GACHA_MAPPING}}", translate("documentation.index.gacha_mapping"));
|
||||
ctx.contentType(HttpUtils.MediaType._html.getMIME());
|
||||
ctx.contentType(ContentType.TEXT_HTML);
|
||||
ctx.result(content);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.HttpUtils;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.*;
|
||||
@@ -76,8 +77,8 @@ public final class AnnouncementsHandler implements Router {
|
||||
try (InputStream filestream = DataLoader.load(ctx.path())) {
|
||||
String possibleFilename = Utils.toFilePath(DATA(ctx.path()));
|
||||
|
||||
HttpUtils.MediaType fromExtension = HttpUtils.MediaType.getByExtension(possibleFilename.substring(possibleFilename.lastIndexOf(".") + 1));
|
||||
ctx.contentType((fromExtension != null) ? fromExtension.getMIME() : "application/octet-stream");
|
||||
ContentType fromExtension = ContentType.getContentTypeByExtension(possibleFilename.substring(possibleFilename.lastIndexOf(".") + 1));
|
||||
ctx.contentType(fromExtension != null ? fromExtension : ContentType.APPLICATION_OCTET_STREAM);
|
||||
ctx.result(filestream.readAllBytes());
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().warn("File does not exist: " + ctx.path());
|
||||
|
||||
@@ -11,6 +11,7 @@ import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.HttpUtils;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
import io.javalin.http.staticfiles.Location;
|
||||
|
||||
@@ -72,7 +73,7 @@ public final class GachaHandler implements Router {
|
||||
.replace("{{DATE}}", translate(player, "gacha.records.date"))
|
||||
.replace("{{ITEM}}", translate(player, "gacha.records.item"))
|
||||
.replace("{{LANGUAGE}}", Utils.getLanguageCode(account.getLocale()));
|
||||
ctx.contentType(HttpUtils.MediaType._html.getMIME());
|
||||
ctx.contentType(ContentType.TEXT_HTML);
|
||||
ctx.result(template);
|
||||
}
|
||||
|
||||
@@ -134,7 +135,7 @@ public final class GachaHandler implements Router {
|
||||
template = template.replace("{{THREE_STARS}}", "[" + String.join(",", threeStarItems) + "]");
|
||||
|
||||
// Done.
|
||||
ctx.contentType(HttpUtils.MediaType._html.getMIME());
|
||||
ctx.contentType(ContentType.TEXT_HTML);
|
||||
ctx.result(template);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package emu.grasscutter.server.http.objects;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.HttpUtils;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
import io.javalin.http.Handler;
|
||||
|
||||
@@ -23,9 +24,8 @@ public class WebStaticVersionResponse implements Handler {
|
||||
|
||||
private static void getPageResources(String path, Context ctx) {
|
||||
try (InputStream filestream = FileUtils.readResourceAsStream(path)) {
|
||||
|
||||
HttpUtils.MediaType fromExtension = HttpUtils.MediaType.getByExtension(path.substring(path.lastIndexOf(".") + 1));
|
||||
ctx.contentType((fromExtension != null) ? fromExtension.getMIME() : "application/octet-stream");
|
||||
ContentType fromExtension = ContentType.getContentTypeByExtension(path.substring(path.lastIndexOf(".") + 1));
|
||||
ctx.contentType(fromExtension != null ? fromExtension : ContentType.APPLICATION_OCTET_STREAM);
|
||||
ctx.result(filestream.readAllBytes());
|
||||
} catch (Exception e) {
|
||||
if (DISPATCH_INFO.logRequests == Grasscutter.ServerDebugMode.MISSING) {
|
||||
|
||||
Reference in New Issue
Block a user