Rewrite LibWeb as a LibJF module

This commit is contained in:
JFronny 2021-11-10 16:47:15 +01:00
parent 393d7f1676
commit 8096596683
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
25 changed files with 1326 additions and 3 deletions

View File

@ -105,8 +105,7 @@ allprojects {
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modRuntimeOnly modCompileOnly("com.terraformersmc:modmenu:2.0.14")
include modImplementation(fabricApi.module("fabric-tag-extensions-v0", "${project.fabric_version}"))
include modImplementation(fabricApi.module("fabric-resource-loader-v0", "${project.fabric_version}"))
modRuntime("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
}
configurations {

View File

@ -2,4 +2,5 @@ archivesBaseName = "libjf-config-v0"
dependencies {
moduleDependencies(project, ["libjf-base", "libjf-unsafe-v0"])
include(fabricApi.module("fabric-resource-loader-v0", "${project.fabric_version}"))
}

View File

@ -2,4 +2,6 @@ archivesBaseName = "libjf-data-v0"
dependencies {
moduleDependencies(project, ["libjf-base"])
include modImplementation(fabricApi.module("fabric-tag-extensions-v0", "${project.fabric_version}"))
include(fabricApi.module("fabric-resource-loader-v0", "${project.fabric_version}"))
}

View File

@ -0,0 +1,7 @@
archivesBaseName = "libjf-web-v0"
dependencies {
moduleDependencies(project, ["libjf-base", "libjf-config-v0"])
include modImplementation(fabricApi.module("fabric-lifecycle-events-v1", "${rootProject.fabric_version}"))
include modImplementation(fabricApi.module("fabric-command-api-v1", "${rootProject.fabric_version}"))
}

View File

@ -0,0 +1,6 @@
package io.gitlab.jfronny.libjf.web.api;
public interface AdvancedSubServer extends SubServer {
void onStop();
void onStart();
}

View File

@ -0,0 +1,10 @@
package io.gitlab.jfronny.libjf.web.api;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpRequest;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpResponse;
import java.io.IOException;
public interface ContentProvider {
HttpResponse handle(HttpRequest request) throws IOException;
}

View File

@ -0,0 +1,10 @@
package io.gitlab.jfronny.libjf.web.api;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpRequest;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpResponse;
import java.io.IOException;
public interface SubServer {
HttpResponse handle(HttpRequest request, String[] segments) throws IOException;
}

View File

@ -0,0 +1,5 @@
package io.gitlab.jfronny.libjf.web.api;
public interface WebInit {
void register(WebServer api);
}

View File

@ -0,0 +1,17 @@
package io.gitlab.jfronny.libjf.web.api;
import java.io.IOException;
import java.nio.file.Path;
public interface WebServer {
String register(String webPath, ContentProvider provider);
String registerFile(String webPath, Path file, Boolean readOnSend) throws IOException;
String registerFile(String webPath, byte[] data, String contentType);
String registerDir(String webPath, Path dir, Boolean readOnSend) throws IOException;
String registerSubServer(String webPath, SubServer subServer);
String registerSubServer(String webPath, AdvancedSubServer subServer);
String getServerRoot();
void stop();
void restart();
boolean isActive();
}

View File

@ -0,0 +1,29 @@
package io.gitlab.jfronny.libjf.web.impl;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.web.api.WebInit;
import io.gitlab.jfronny.libjf.web.api.WebServer;
import net.fabricmc.loader.api.FabricLoader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class DefaultFileHost implements WebInit {
@Override
public void register(WebServer api) {
Path p = FabricLoader.getInstance().getConfigDir().resolve("wwwroot");
if (!Files.isDirectory(p)) {
try {
Files.createDirectory(p);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
LibJf.LOGGER.info(api.registerDir("/", p, false));
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,79 @@
package io.gitlab.jfronny.libjf.web.impl;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.gitlab.jfronny.libjf.Flags;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.web.api.WebServer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
public class JfWeb implements ClientModInitializer, DedicatedServerModInitializer, ModInitializer {
public final WebServer server;
public JfWeb() {
JfWebConfig.ensureValidPort();
server = new JfWebServer(JfWebConfig.port, JfWebConfig.maxConnections);
}
@Override
public void onInitializeClient() {
if (isEnabled()) {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> server.restart());
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> server.stop());
}
}
@Override
public void onInitializeServer() {
if (isEnabled()) {
ServerLifecycleEvents.SERVER_STARTED.register(client -> server.restart());
ServerLifecycleEvents.SERVER_STOPPED.register(client -> server.stop());
}
}
@Override
public void onInitialize() {
if (isEnabled()) {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
LiteralArgumentBuilder<ServerCommandSource> base = CommandManager.literal(LibJf.MOD_ID).requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4));
LiteralArgumentBuilder<ServerCommandSource> web = CommandManager.literal("web");
base.then(web);
web.executes(context -> {
if (server.isActive()) {
context.getSource().sendFeedback(new LiteralText("LibWeb is active. Use libweb restart to reload"), false);
}
else {
context.getSource().sendFeedback(new LiteralText("LibWeb is not active. Use libweb restart to reload"), false);
}
return Command.SINGLE_SUCCESS;
});
web.then(CommandManager.literal("restart").executes(context -> {
try {
context.getSource().sendFeedback(new LiteralText("Restarting LibWeb"), true);
server.restart();
}
catch (Exception e) {
LibJf.LOGGER.error("Failed to run restart command", e);
context.getSource().sendError(new LiteralText(e.getMessage()));
}
return Command.SINGLE_SUCCESS;
}));
dispatcher.register(base);
});
}
Runtime.getRuntime().addShutdownHook(new Thread(server::stop));
}
private boolean isEnabled() {
boolean enable = JfWebConfig.enableFileHost;
for (Flags.BooleanFlag web : Flags.getBoolFlags("web")) enable |= web.value();
return enable;
}
}

View File

@ -0,0 +1,32 @@
package io.gitlab.jfronny.libjf.web.impl;
import io.gitlab.jfronny.libjf.config.api.ConfigHolder;
import io.gitlab.jfronny.libjf.config.api.Entry;
import io.gitlab.jfronny.libjf.config.api.JfConfig;
import java.io.IOException;
import java.net.ServerSocket;
public class JfWebConfig implements JfConfig {
@Entry
public static String serverIp = "http://127.0.0.1";
@Entry
public static int port = 0;
@Entry
public static int portOverride = -1;
@Entry
public static int maxConnections = 20;
@Entry
public static boolean enableFileHost = false;
public static void ensureValidPort() {
if (port == 0) {
try (ServerSocket socket = new ServerSocket(0)) {
port = socket.getLocalPort();
} catch (IOException e) {
e.printStackTrace();
}
ConfigHolder.getInstance().getRegistered().get("libjf-web-v0").write();
}
}
}

View File

@ -0,0 +1,203 @@
package io.gitlab.jfronny.libjf.web.impl;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.web.api.*;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpRequest;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpResponse;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpServer;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpStatusCode;
import io.gitlab.jfronny.libjf.web.impl.util.WebPaths;
import net.fabricmc.loader.api.FabricLoader;
import javax.management.openmbean.KeyAlreadyExistsException;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;
public class JfWebServer implements WebServer {
private HttpServer server;
private final RequestHandler handler = new RequestHandler();
private final int port;
private final int maxConnections;
private final DefaultFileHost dfh = new DefaultFileHost();
public JfWebServer(int port, int maxConnections) {
this.port = port;
this.maxConnections = maxConnections;
}
@Override
public String register(String webPath, ContentProvider provider) {
webPath = WebPaths.simplify(webPath);
if (handler.contentProviders.containsKey(webPath))
throw new KeyAlreadyExistsException("A ContentProvider already exists at that address (" + handler.contentProviders.get(webPath).getClass() + ")");
handler.contentProviders.put(webPath, provider);
return WebPaths.concat(getServerRoot(), webPath);
}
@Override
public String registerFile(String webPath, Path file, Boolean readOnSend) throws IOException {
if (readOnSend) {
if (!Files.exists(file))
throw new FileNotFoundException();
register(webPath, s -> {
HttpResponse resp = new HttpResponse(HttpStatusCode.OK);
resp.addHeader("Content-Type", Files.probeContentType(file));
FileInputStream fs = new FileInputStream(file.toFile());
resp.setData(fs);
return resp;
});
return WebPaths.concat(getServerRoot(), webPath);
}
else {
return registerFile(webPath, Files.readAllBytes(file), Files.probeContentType(file));
}
}
@Override
public String registerFile(String webPath, byte[] data, String contentType) {
return register(webPath, s -> {
HttpResponse resp = new HttpResponse(HttpStatusCode.OK);
resp.addHeader("Content-Type", contentType);
ByteArrayInputStream fs = new ByteArrayInputStream(data);
resp.setData(fs);
return resp;
});
}
@Override
public String registerDir(String webPath, Path dir, Boolean readOnSend) throws IOException {
try (Stream<Path> contentPath = Files.walk(dir)) {
if (readOnSend) {
return registerSubServer(webPath, (s, t) -> {
final boolean[] c = {false};
final Path[] p_f = new Path[1];
contentPath.filter(Files::isRegularFile)
.filter(Files::isReadable)
.forEach(q -> {
if (c[0])
return;
Path p = dir.toAbsolutePath().relativize(q.toAbsolutePath());
String wp = webPath;
for (Path path : p) {
wp = WebPaths.concat(wp, path.toString());
}
if (Objects.equals(WebPaths.simplify(wp), WebPaths.simplify(WebPaths.concat(t)))) {
p_f[0] = q;
c[0] = true;
}
});
HttpResponse resp;
if (c[0]) {
resp = new HttpResponse(HttpStatusCode.OK);
resp.addHeader("Content-Type", Files.probeContentType(p_f[0]));
FileInputStream fs = new FileInputStream(p_f[0].toFile());
resp.setData(fs);
} else {
resp = new HttpResponse(HttpStatusCode.NOT_FOUND);
}
return resp;
});
} else {
contentPath.filter(Files::isRegularFile)
.filter(Files::isReadable)
.forEach(s -> {
Path p = dir.toAbsolutePath().relativize(s.toAbsolutePath());
String wp = webPath;
for (Path path : p) wp = WebPaths.concat(wp, path.toString());
try {
registerFile(wp, s, false);
} catch (IOException e) {
e.printStackTrace();
}
});
}
return WebPaths.concat(getServerRoot(), webPath);
}
}
@Override
public String registerSubServer(String webPath, SubServer subServer) {
return registerSubServer(webPath, new AdvancedSubServer() {
@Override
public void onStop() {
}
@Override
public void onStart() {
}
@Override
public HttpResponse handle(HttpRequest request, String[] segments) throws IOException {
return subServer.handle(request, segments);
}
});
}
@Override
public String registerSubServer(String webPath, AdvancedSubServer subServer) {
webPath = WebPaths.simplify(webPath);
if (handler.subServers.containsKey(webPath))
throw new KeyAlreadyExistsException("A Subserver already exists at that address (" + handler.subServers.get(webPath).getClass() + ")");
handler.subServers.put(webPath, subServer);
return WebPaths.concat(getServerRoot(), webPath);
}
@Override
public String getServerRoot() {
String ip = JfWebConfig.serverIp;
if (!ip.startsWith("http"))
ip = "http://" + ip;
if (JfWebConfig.portOverride != -1) {
return WebPaths.simplify(ip) + ":" + JfWebConfig.portOverride;
}
return WebPaths.simplify(ip) + ":" + JfWebConfig.port;
}
@Override
public void stop() {
for (AdvancedSubServer subServer : handler.subServers.values()) subServer.onStop();
if (isActive()) server.close();
if (server != null) {
try {
server.join();
}
catch (InterruptedException e) {
//It is most likely already dead
}
}
}
@Override
public void restart() {
JfWebConfig.ensureValidPort();
int tmpPort = port;
if (server != null) {
tmpPort = server.getPort();
stop();
}
handler.clear();
server = new HttpServer(null, tmpPort, maxConnections, handler, () -> {
if (JfWebConfig.enableFileHost) dfh.register(this);
FabricLoader.getInstance().getEntrypointContainers(LibJf.MOD_ID + ":web", WebInit.class).forEach(entrypoint -> {
WebInit init = entrypoint.getEntrypoint();
init.register(this);
});
});
server.start();
for (AdvancedSubServer subServer : handler.subServers.values()) {
subServer.onStart();
}
}
@Override
public boolean isActive() {
return server != null && server.isAlive();
}
}

View File

@ -0,0 +1,58 @@
package io.gitlab.jfronny.libjf.web.impl;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.web.api.AdvancedSubServer;
import io.gitlab.jfronny.libjf.web.api.ContentProvider;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpRequest;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpRequestHandler;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpResponse;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpStatusCode;
import io.gitlab.jfronny.libjf.web.impl.util.WebPaths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class RequestHandler implements HttpRequestHandler {
public Map<String, AdvancedSubServer> subServers = new HashMap<>();
public Map<String, ContentProvider> contentProviders = new HashMap<>();
@Override
public HttpResponse handle(HttpRequest request) {
HttpResponse resp = null;
try {
String webPath = WebPaths.simplify(request.getPath());
if (webPath.length() == 0)
webPath = "index.html";
if (contentProviders.containsKey(webPath)) {
resp = contentProviders.get(webPath).handle(request);
}
else {
String[] segments = webPath.split("/");
for (int i = segments.length - 1; i >= 0; i--) {
String wp = WebPaths.concat(Arrays.copyOfRange(segments, 0, i));
if (subServers.containsKey(wp)) {
resp = subServers.get(wp).handle(request, Arrays.copyOfRange(segments, i, segments.length));
break;
}
}
if (resp == null) {
resp = new HttpResponse(HttpStatusCode.NOT_FOUND);
}
}
} catch (Throwable e) {
LibJf.LOGGER.error("Caught error while sending", e);
resp = new HttpResponse(HttpStatusCode.INTERNAL_SERVER_ERROR);
}
if (resp.getHeader("Cache-Control").size() == 0)
resp.addHeader("Cache-Control", "no-cache");
if (resp.getHeader("Server").size() == 0)
resp.addHeader("Server", "LibWeb using BlueMapCore");
return resp;
}
public void clear() {
subServers.clear();
contentProviders.clear();
}
}

View File

@ -0,0 +1,50 @@
package io.gitlab.jfronny.libjf.web.impl.util;
public class WebPaths {
public static String concat(String s1, String s2) {
return simplify(s1) + "/" + simplify(s2);
}
public static String concat(String[] elements) {
StringBuilder s = new StringBuilder();
for (String element : elements) {
s.append("/").append(element);
}
return simplify(s.toString());
}
public static String simplify(String s) {
boolean http = false;
boolean https = false;
if (s.startsWith("http://")) {
http = true;
s = s.substring(7);
}
if (s.startsWith("https://")) {
https = true;
s = s.substring(8);
}
StringBuilder q = new StringBuilder();
for (String s1 : simplifyPart(s, false).split("/")) {
String w = simplifyPart(s1, true);
if (w != null && w.length() != 0)
q.append("/").append(w);
}
String result = simplifyPart(q.toString(), false);
if (http) result = "http://" + result;
if (https) result = "https://" + result;
return result;
}
private static String simplifyPart(String s, boolean alpha) {
String path = s.toLowerCase();
while (path.startsWith("/")) path = path.substring(1);
while (path.endsWith("/")) path = path.substring(0, path.length() - 1);
while (path.startsWith(".")) path = path.substring(1);
while (path.endsWith(".")) path = path.substring(0, path.length() - 1);
if (alpha)
path = path.replaceAll("[^A-Za-z0-9.:]", "");
return path;
}
}

View File

@ -0,0 +1,138 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.gitlab.jfronny.libjf.web.impl.util.bluemapcore;
import io.gitlab.jfronny.libjf.LibJf;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HttpConnection implements Runnable {
private final HttpRequestHandler handler;
private final ServerSocket server;
private final Socket connection;
private final InputStream in;
private final OutputStream out;
public HttpConnection(ServerSocket server, Socket connection, HttpRequestHandler handler, int timeout, TimeUnit timeoutUnit) throws IOException {
this.server = server;
this.connection = connection;
this.handler = handler;
if (isClosed()){
throw new IOException("Socket already closed!");
}
connection.setSoTimeout((int) timeoutUnit.toMillis(timeout));
in = this.connection.getInputStream();
out = this.connection.getOutputStream();
}
@Override
public void run() {
while (!isClosed() && !server.isClosed()){
try {
HttpRequest request = acceptRequest();
HttpResponse response = handler.handle(request);
sendResponse(response);
} catch (InvalidRequestException e){
try {
sendResponse(new HttpResponse(HttpStatusCode.BAD_REQUEST));
} catch (IOException e1) {}
break;
} catch (SocketTimeoutException | SocketException | ConnectionClosedException e) {
break;
} catch (IOException e) {
LibJf.LOGGER.error("Unexpected error while processing a HttpRequest!", e);
break;
}
}
try {
close();
} catch (IOException e){
LibJf.LOGGER.error("Error while closing HttpConnection!", e);
}
}
private void log(HttpRequest request, HttpResponse response) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
LibJf.LOGGER.info(
connection.getInetAddress().toString()
+ " [ "
+ dateFormat.format(date)
+ " ] \""
+ request.getMethod()
+ " " + request.getPath()
+ " " + request.getVersion()
+ "\" "
+ response.getStatusCode().toString());
}
private void sendResponse(HttpResponse response) throws IOException {
response.write(out);
out.flush();
}
private HttpRequest acceptRequest() throws IOException {
return HttpRequest.read(in);
}
public boolean isClosed(){
return !connection.isBound() || connection.isClosed() || !connection.isConnected() || connection.isOutputShutdown() || connection.isInputShutdown();
}
public void close() throws IOException {
try {
in.close();
} finally {
try {
out.close();
} finally {
connection.close();
}
}
}
public static class ConnectionClosedException extends IOException {
private static final long serialVersionUID = 1L;
}
public static class InvalidRequestException extends IOException {
private static final long serialVersionUID = 1L;
}
}

View File

@ -0,0 +1,242 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.gitlab.jfronny.libjf.web.impl.util.bluemapcore;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HttpRequest {
private static final Pattern REQUEST_PATTERN = Pattern.compile("^(\\w+) (\\S+) (.+)$");
private final String method;
private final String adress;
private final String version;
private final Map<String, Set<String>> header;
private final Map<String, Set<String>> headerLC;
private byte[] data;
private String path = null;
private Map<String, String> getParams = null;
private String getParamString = null;
public HttpRequest(String method, String adress, String version, Map<String, Set<String>> header) {
this.method = method;
this.adress = adress;
this.version = version;
this.header = header;
this.headerLC = new HashMap<>();
for (Entry<String, Set<String>> e : header.entrySet()){
Set<String> values = new HashSet<>();
for (String v : e.getValue()){
values.add(v.toLowerCase());
}
headerLC.put(e.getKey().toLowerCase(), values);
}
this.data = new byte[0];
}
public String getMethod() {
return method;
}
public String getAdress(){
return adress;
}
public String getVersion() {
return version;
}
public Map<String, Set<String>> getHeader() {
return header;
}
public Map<String, Set<String>> getLowercaseHeader() {
return headerLC;
}
public Set<String> getHeader(String key){
Set<String> headerValues = header.get(key);
if (headerValues == null) return Collections.emptySet();
return headerValues;
}
public Set<String> getLowercaseHeader(String key){
Set<String> headerValues = headerLC.get(key.toLowerCase());
if (headerValues == null) return Collections.emptySet();
return headerValues;
}
public String getPath() {
if (path == null) parseAdress();
return path;
}
public Map<String, String> getGETParams() {
if (getParams == null) parseAdress();
return Collections.unmodifiableMap(getParams);
}
public String getGETParamString() {
if (getParamString == null) parseAdress();
return getParamString;
}
private void parseAdress() {
String adress = this.adress;
if (adress.isEmpty()) adress = "/";
String[] adressParts = adress.split("\\?", 2);
String path = adressParts[0];
this.getParamString = adressParts.length > 1 ? adressParts[1] : "";
Map<String, String> getParams = new HashMap<>();
for (String getParam : this.getParamString.split("&")){
if (getParam.isEmpty()) continue;
String[] kv = getParam.split("=", 2);
String key = kv[0];
String value = kv.length > 1 ? kv[1] : "";
getParams.put(key, value);
}
this.path = path;
this.getParams = getParams;
}
public InputStream getData(){
return new ByteArrayInputStream(data);
}
public static HttpRequest read(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
List<String> header = new ArrayList<>(20);
while(header.size() < 1000){
String headerLine = readLine(reader);
if (headerLine.isEmpty()) break;
header.add(headerLine);
}
if (header.isEmpty()) throw new HttpConnection.InvalidRequestException();
Matcher m = REQUEST_PATTERN.matcher(header.remove(0));
if (!m.find()) throw new HttpConnection.InvalidRequestException();
String method = m.group(1);
if (method == null) throw new HttpConnection.InvalidRequestException();
String adress = m.group(2);
if (adress == null) throw new HttpConnection.InvalidRequestException();
String version = m.group(3);
if (version == null) throw new HttpConnection.InvalidRequestException();
Map<String, Set<String>> headerMap = new HashMap<String, Set<String>>();
for (String line : header){
if (line.trim().isEmpty()) continue;
String[] kv = line.split(":", 2);
if (kv.length < 2) continue;
Set<String> values = new HashSet<>();
if (kv[0].trim().equalsIgnoreCase("If-Modified-Since")){
values.add(kv[1].trim());
} else {
for(String v : kv[1].split(",")){
values.add(v.trim());
}
}
headerMap.put(kv[0].trim(), values);
}
HttpRequest request = new HttpRequest(method, adress, version, headerMap);
if (request.getLowercaseHeader("Transfer-Encoding").contains("chunked")){
try {
ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
while (dataStream.size() < 1000000){
String hexSize = reader.readLine();
int chunkSize = Integer.parseInt(hexSize, 16);
if (chunkSize <= 0) break;
byte[] data = new byte[chunkSize];
in.read(data);
dataStream.write(data);
}
if (dataStream.size() >= 1000000) {
throw new HttpConnection.InvalidRequestException();
}
request.data = dataStream.toByteArray();
return request;
} catch (NumberFormatException ex){
return request;
}
} else {
Set<String> clSet = request.getLowercaseHeader("Content-Length");
if (clSet.isEmpty()){
return request;
} else {
try {
int cl = Integer.parseInt(clSet.iterator().next());
byte[] data = new byte[cl];
in.read(data);
request.data = data;
return request;
} catch (NumberFormatException ex){
return request;
}
}
}
}
private static String readLine(BufferedReader in) throws IOException {
String line = in.readLine();
if (line == null){
throw new HttpConnection.ConnectionClosedException();
}
return line;
}
}

View File

@ -0,0 +1,30 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.gitlab.jfronny.libjf.web.impl.util.bluemapcore;
@FunctionalInterface
public interface HttpRequestHandler {
HttpResponse handle(HttpRequest request);
}

View File

@ -0,0 +1,148 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.gitlab.jfronny.libjf.web.impl.util.bluemapcore;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
public class HttpResponse implements Closeable {
private final String version;
private final HttpStatusCode statusCode;
private final Map<String, Set<String>> header;
private InputStream data;
public HttpResponse(HttpStatusCode statusCode) {
this.version = "HTTP/1.1";
this.statusCode = statusCode;
this.header = new HashMap<>();
addHeader("Connection", "keep-alive");
}
public HttpResponse addHeader(String key, String value){
Set<String> valueSet = header.computeIfAbsent(key, k -> new HashSet<>());
valueSet.add(value);
return this;
}
public HttpResponse removeHeader(String key, String value){
Set<String> valueSet = header.computeIfAbsent(key, k -> new HashSet<>());
valueSet.remove(value);
return this;
}
public HttpResponse setData(InputStream dataStream){
this.data = dataStream;
return this;
}
public HttpResponse setData(String data){
setData(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)));
return this;
}
/**
* Writes this Response to an Output-Stream.<br>
* <br>
* This method closes the data-Stream of this response so it can't be used again!
*/
public HttpResponse write(OutputStream out) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
if (data != null){
addHeader("Transfer-Encoding", "chunked");
} else {
addHeader("Content-Length", "0");
}
writeLine(writer, version + " " + statusCode.getCode() + " " + statusCode.getMessage());
for (Entry<String, Set<String>> e : header.entrySet()){
if (e.getValue().isEmpty()) continue;
writeLine(writer, e.getKey() + ": " + StringUtils.join(e.getValue(), ", "));
}
writeLine(writer, "");
writer.flush();
if(data != null){
chunkedPipe(data, out);
out.flush();
data.close();
}
return this;
}
@Override
public void close() throws IOException {
data.close();
}
private void writeLine(OutputStreamWriter writer, String line) throws IOException {
writer.write(line + "\r\n");
}
private void chunkedPipe(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[1024];
int byteCount;
while ((byteCount = input.read(buffer)) != -1) {
output.write((Integer.toHexString(byteCount) + "\r\n").getBytes());
output.write(buffer, 0, byteCount);
output.write("\r\n".getBytes());
}
output.write("0\r\n\r\n".getBytes());
}
public HttpStatusCode getStatusCode(){
return statusCode;
}
public String getVersion(){
return version;
}
public Map<String, Set<String>> getHeader() {
return header;
}
public Set<String> getHeader(String key){
Set<String> headerValues = header.get(key);
if (headerValues == null) return Collections.emptySet();
return headerValues;
}
}

View File

@ -0,0 +1,127 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.gitlab.jfronny.libjf.web.impl.util.bluemapcore;
import io.gitlab.jfronny.libjf.LibJf;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class HttpServer extends Thread {
private int port;
private int maxConnections;
private final InetAddress bindAddress;
private final HttpRequestHandler handler;
private ThreadPoolExecutor connectionThreads;
private final Runnable callback;
private ServerSocket server;
public HttpServer(InetAddress bindAddress, int port, int maxConnections, HttpRequestHandler handler, Runnable startCallback) {
this.port = port;
this.maxConnections = maxConnections;
this.bindAddress = bindAddress;
this.handler = handler;
connectionThreads = null;
this.callback = startCallback;
}
@Override
public void run() {
close();
connectionThreads = new ThreadPoolExecutor(Math.min(maxConnections, 8), maxConnections, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
try {
server = new ServerSocket(port, maxConnections, bindAddress);
server.setSoTimeout(0);
} catch (IOException e){
LibJf.LOGGER.error("Error while starting the WebServer!", e);
return;
}
callback.run();
LibJf.LOGGER.info("WebServer started.");
while (!server.isClosed() && server.isBound()){
try {
Socket connection = server.accept();
try {
connectionThreads.execute(new HttpConnection(server, connection, handler, 10, TimeUnit.SECONDS));
} catch (RejectedExecutionException e){
connection.close();
LibJf.LOGGER.warn("Dropped an incoming HttpConnection! (Too many connections?)");
}
} catch (SocketException e){
// this mainly occurs if the socket got closed, so we ignore this error
} catch (IOException e){
LibJf.LOGGER.error("Error while creating a new HttpConnection!", e);
}
}
LibJf.LOGGER.info("WebServer closed.");
}
public int getPort() {
return server == null ? port : server.getLocalPort();
}
public void setPort(int port) {
this.port = port;
}
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
public synchronized void close(){
if (connectionThreads != null) connectionThreads.shutdown();
try {
if (server != null && !server.isClosed()){
server.close();
}
} catch (IOException e) {
LibJf.LOGGER.error("Error while closing WebServer!", e);
}
}
}

View File

@ -0,0 +1,68 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.gitlab.jfronny.libjf.web.impl.util.bluemapcore;
public enum HttpStatusCode {
CONTINUE (100, "Continue"),
PROCESSING (102, "Processing"),
OK (200, "OK"),
MOVED_PERMANENTLY (301, "Moved Permanently"),
FOUND (302, "Found"),
SEE_OTHER (303, "See Other"),
NOT_MODIFIED (304, "Not Modified"),
BAD_REQUEST (400, "Bad Request"),
UNAUTHORIZED (401, "Unauthorized"),
FORBIDDEN (403, "Forbidden"),
NOT_FOUND (404, "Not Found"),
INTERNAL_SERVER_ERROR (500, "Internal Server Error"),
NOT_IMPLEMENTED (501, "Not Implemented"),
SERVICE_UNAVAILABLE (503, "Service Unavailable"),
HTTP_VERSION_NOT_SUPPORTED (505, "HTTP Version not supported");
private final int code;
private final String message;
HttpStatusCode(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode(){
return code;
}
public String getMessage(){
return message;
}
@Override
public String toString() {
return getCode() + " " + getMessage();
}
}

View File

@ -0,0 +1,33 @@
{
"schemaVersion": 1,
"id": "libjf-web-v0",
"name": "LibJF Web",
"version": "${version}",
"authors": [
"JFronny"
],
"contact": {
"website": "https://jfronny.gitlab.io",
"repo": "https://gitlab.com/jfmods/libjf"
},
"license": "MIT",
"environment": "*",
"entrypoints": {
"main": ["io.gitlab.jfronny.libjf.web.impl.JfWeb"],
"client": ["io.gitlab.jfronny.libjf.web.impl.JfWeb"],
"server": ["io.gitlab.jfronny.libjf.web.impl.JfWeb"],
"libjf:config": ["io.gitlab.jfronny.libjf.web.impl.JfWebConfig"]
},
"depends": {
"fabricloader": ">=0.12.0",
"minecraft": "*",
"libjf-base": ">=${version}",
"libjf-config-v0": ">=${version}"
},
"custom": {
"modmenu": {
"parent": "libjf",
"badges": ["library"]
}
}
}

View File

@ -0,0 +1,14 @@
package io.gitlab.jfronny.libjf.web.test;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.web.api.WebInit;
import io.gitlab.jfronny.libjf.web.api.WebServer;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpResponse;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpStatusCode;
public class WebTest implements WebInit {
@Override
public void register(WebServer api) {
LibJf.LOGGER.info(api.register("/test.html", request -> new HttpResponse(HttpStatusCode.OK).setData("<html><head><title>Hello, World!</title></head><body>Hello,<br>World!</body></html>")));
}
}

View File

@ -0,0 +1,14 @@
{
"schemaVersion": 1,
"id": "libjf-web-v0-testmod",
"version": "1.0",
"environment": "*",
"entrypoints": {
"libjf:web": ["io.gitlab.jfronny.libjf.web.test.WebTest"]
},
"custom": {
"libjf": {
"web": true
}
}
}

View File

@ -16,4 +16,5 @@ include 'libjf-config-v0'
include 'libjf-data-v0'
include 'libjf-data-manipulation-v0'
//include 'libjf-devutil-v0' //TODO re-enable for 1.18
include 'libjf-unsafe-v0'
include 'libjf-unsafe-v0'
include 'libjf-web-v0'