mirror of
https://github.com/JonasunderscoreJones/McWebserver.git
synced 2025-10-23 11:29:19 +02:00
Added base mod
base mod with basic http server and simple config system
This commit is contained in:
commit
ec3c622896
231 changed files with 3298 additions and 0 deletions
33
src/main/java/me/jonasjones/mcwebserver/McWebserver.java
Normal file
33
src/main/java/me/jonasjones/mcwebserver/McWebserver.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package me.jonasjones.mcwebserver;
|
||||
|
||||
import me.jonasjones.mcwebserver.config.ModConfigs;
|
||||
import me.jonasjones.mcwebserver.web.HTTPServer;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
public class McWebserver implements ModInitializer {
|
||||
// This logger is used to write text to the console and the log file.
|
||||
// It is considered best practice to use your mod id as the logger's name.
|
||||
// That way, it's clear which mod wrote info, warnings, and errors.
|
||||
public static String MOD_ID = "mcwebserver";
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||
public static final Logger VERBOSELOGGER = LoggerFactory.getLogger(MOD_ID + " - VERBOSE LOGGER");
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
|
||||
// register configs
|
||||
ModConfigs.registerConfigs();
|
||||
|
||||
LOGGER.info("McWebserver initialized!");
|
||||
LOGGER.info("Starting Webserver...");
|
||||
|
||||
new Thread(() -> {
|
||||
new HTTPServer(new Socket());
|
||||
HTTPServer.main();
|
||||
}).start();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package me.jonasjones.mcwebserver.config;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
|
||||
import me.jonasjones.mcwebserver.config.SimpleConfig.DefaultConfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ModConfigProvider implements DefaultConfig {
|
||||
|
||||
private String configContents = "";
|
||||
|
||||
public List<Pair> getConfigsList() {
|
||||
return configsList;
|
||||
}
|
||||
|
||||
private final List<Pair> configsList = new ArrayList<>();
|
||||
|
||||
public void addKeyValuePair(Pair<String, ?> keyValuePair, String comment) {
|
||||
configsList.add(keyValuePair);
|
||||
configContents += keyValuePair.getFirst() + "=" + keyValuePair.getSecond() + " #"
|
||||
+ comment + " | default: " + keyValuePair.getSecond() + "\n";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String namespace) {
|
||||
return configContents;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package me.jonasjones.mcwebserver.config;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import me.jonasjones.mcwebserver.util.VerboseLogger;
|
||||
|
||||
public class ModConfigs {
|
||||
public static SimpleConfig CONFIG;
|
||||
private static ModConfigProvider config;
|
||||
|
||||
//config
|
||||
public static Boolean ISENABLED;
|
||||
public static int WEB_PORT;
|
||||
public static String WEB_ROOT;
|
||||
public static String WEB_FILE_ROOT;
|
||||
public static String WEB_FILE_404;
|
||||
public static String WEB_FILE_NOSUPPORT;
|
||||
public static Boolean VERBOSE = false; //needs to be set to false since the verbose logger is called before config file is fully loaded
|
||||
|
||||
|
||||
public static void registerConfigs() {
|
||||
|
||||
config = new ModConfigProvider();
|
||||
|
||||
createConfigs();
|
||||
|
||||
CONFIG = SimpleConfig.of(McWebserver.MOD_ID).provider(config).request();
|
||||
|
||||
assignConfigs();
|
||||
|
||||
//make verbose logger show that it is active and print configs to logger
|
||||
VerboseLogger.info("Verbose Logger is now logging.");
|
||||
VerboseLogger.info("Loaded config file successfully: found " + config.getConfigsList().size() + " overrides and configurations.");
|
||||
}
|
||||
|
||||
private static void createConfigs() {
|
||||
config.addKeyValuePair(new Pair<>("web.isEnabled", true), "whether or not the webserver should be enabled or not");
|
||||
config.addKeyValuePair(new Pair<>("web.port", 8080), "The port of the webserver");
|
||||
config.addKeyValuePair(new Pair<>("web.root", "webserver/"), "the root directory of the webserver, starting from the main server directory");
|
||||
config.addKeyValuePair(new Pair<>("web.file.root", "index.html"), "the name of the html file for the homepage");
|
||||
config.addKeyValuePair(new Pair<>("web.file.404", "404.html"), "the name of the html file for 404 page");
|
||||
config.addKeyValuePair(new Pair<>("web.file.notSupported", "not_supported.html"), "the name of the html file for 'not supported' page");
|
||||
config.addKeyValuePair(new Pair<>("logger.verbose", true), "whether or not to log verbose output");
|
||||
}
|
||||
|
||||
private static void assignConfigs() {
|
||||
ISENABLED = CONFIG.getOrDefault("basic.isEnabled", true);
|
||||
WEB_PORT = CONFIG.getOrDefault("web.port", 8080);
|
||||
WEB_ROOT = CONFIG.getOrDefault("web.root", "webserver/");
|
||||
WEB_FILE_ROOT = CONFIG.getOrDefault("web.file.root", "index.html");
|
||||
WEB_FILE_404 = CONFIG.getOrDefault("web.file.404", "404.html");
|
||||
WEB_FILE_NOSUPPORT = CONFIG.getOrDefault("web.file.notSupported", "not_supported.html");
|
||||
VERBOSE = CONFIG.getOrDefault("logger.verbose", true);
|
||||
}
|
||||
}
|
251
src/main/java/me/jonasjones/mcwebserver/config/SimpleConfig.java
Normal file
251
src/main/java/me/jonasjones/mcwebserver/config/SimpleConfig.java
Normal file
|
@ -0,0 +1,251 @@
|
|||
package me.jonasjones.mcwebserver.config;
|
||||
/*
|
||||
* Copyright (c) 2021 magistermaks
|
||||
* Slightly modified by Jonas_Jones 2022
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import me.jonasjones.mcwebserver.util.VerboseLogger;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SimpleConfig {
|
||||
private final HashMap<String, String> config = new HashMap<>();
|
||||
private final ConfigRequest request;
|
||||
private boolean broken = false;
|
||||
|
||||
public interface DefaultConfig {
|
||||
String get(String namespace);
|
||||
|
||||
static String empty(String namespace) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConfigRequest {
|
||||
|
||||
private final File file;
|
||||
private final String filename;
|
||||
private DefaultConfig provider;
|
||||
|
||||
private ConfigRequest(File file, String filename) {
|
||||
this.file = file;
|
||||
this.filename = filename;
|
||||
this.provider = DefaultConfig::empty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default config provider, used to generate the
|
||||
* config if it's missing.
|
||||
*
|
||||
* @param provider default config provider
|
||||
* @return current config request object
|
||||
* @see DefaultConfig
|
||||
*/
|
||||
public ConfigRequest provider(DefaultConfig provider) {
|
||||
this.provider = provider;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the config from the filesystem.
|
||||
*
|
||||
* @return config object
|
||||
* @see SimpleConfig
|
||||
*/
|
||||
public SimpleConfig request() {
|
||||
return new SimpleConfig(this);
|
||||
}
|
||||
|
||||
private String getConfig() {
|
||||
return provider.get(filename) + "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new config request object, ideally `namespace`
|
||||
* should be the name of the mod id of the requesting mod
|
||||
*
|
||||
* @param filename - name of the config file
|
||||
* @return new config request object
|
||||
*/
|
||||
public static ConfigRequest of(String filename) {
|
||||
Path path = FabricLoader.getInstance().getConfigDir();
|
||||
return new ConfigRequest(path.resolve(filename + ".properties").toFile(), filename);
|
||||
}
|
||||
|
||||
private void createConfig() throws IOException {
|
||||
|
||||
// try creating missing files
|
||||
request.file.getParentFile().mkdirs();
|
||||
Files.createFile(request.file.toPath());
|
||||
|
||||
// write default config data
|
||||
PrintWriter writer = new PrintWriter(request.file, "UTF-8");
|
||||
writer.write(request.getConfig());
|
||||
writer.close();
|
||||
|
||||
}
|
||||
|
||||
private void loadConfig() throws IOException {
|
||||
Scanner reader = new Scanner(request.file);
|
||||
for (int line = 1; reader.hasNextLine(); line++) {
|
||||
parseConfigEntry(reader.nextLine(), line);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseConfigEntry(String entry, int line) {
|
||||
if (!entry.isEmpty() && !entry.startsWith("#")) {
|
||||
String[] parts = entry.split("=", 2);
|
||||
if (parts.length == 2) {
|
||||
//Recognise comments after a value
|
||||
String temp = parts[1].split(" #")[0];
|
||||
config.put(parts[0], temp);
|
||||
} else {
|
||||
throw new RuntimeException("Syntax error in config file on line " + line + "!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private SimpleConfig(ConfigRequest request) {
|
||||
this.request = request;
|
||||
String identifier = "Config '" + request.filename + "'";
|
||||
|
||||
if (!request.file.exists()) {
|
||||
McWebserver.LOGGER.info(identifier + " is missing, generating default one...");
|
||||
|
||||
try {
|
||||
createConfig();
|
||||
} catch (IOException e) {
|
||||
McWebserver.LOGGER.error(identifier + " failed to generate!");
|
||||
McWebserver.LOGGER.trace(String.valueOf(e));
|
||||
broken = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!broken) {
|
||||
try {
|
||||
loadConfig();
|
||||
} catch (Exception e) {
|
||||
McWebserver.LOGGER.error(identifier + " failed to load!");
|
||||
McWebserver.LOGGER.trace(String.valueOf(e));
|
||||
broken = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries a value from config, returns `null` if the
|
||||
* key does not exist.
|
||||
*
|
||||
* @return value corresponding to the given key
|
||||
* @see SimpleConfig#getOrDefault
|
||||
*/
|
||||
@Deprecated
|
||||
public String get(String key) {
|
||||
return config.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string value from config corresponding to the given
|
||||
* key, or the default string if the key is missing.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public String getOrDefault(String key, String def) {
|
||||
String val = get(key);
|
||||
return val == null ? def : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns integer value from config corresponding to the given
|
||||
* key, or the default integer if the key is missing or invalid.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public int getOrDefault(String key, int def) {
|
||||
try {
|
||||
return Integer.parseInt(get(key));
|
||||
} catch (Exception e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns boolean value from config corresponding to the given
|
||||
* key, or the default boolean if the key is missing.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public boolean getOrDefault(String key, boolean def) {
|
||||
String val = get(key);
|
||||
if (val != null) {
|
||||
return val.equalsIgnoreCase("true");
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns double value from config corresponding to the given
|
||||
* key, or the default string if the key is missing or invalid.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public double getOrDefault(String key, double def) {
|
||||
try {
|
||||
return Double.parseDouble(get(key));
|
||||
} catch (Exception e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If any error occurred during loading or reading from the config
|
||||
* a 'broken' flag is set, indicating that the config's state
|
||||
* is undefined and should be discarded using `delete()`
|
||||
*
|
||||
* @return the 'broken' flag of the configuration
|
||||
*/
|
||||
public boolean isBroken() {
|
||||
return broken;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes the config file from the filesystem
|
||||
*
|
||||
* @return true if the operation was successful
|
||||
*/
|
||||
public boolean delete() {
|
||||
VerboseLogger.warn("Config '" + request.filename + "' was removed from existence! Restart the game to regenerate it.");
|
||||
return request.file.delete();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package me.jonasjones.mcwebserver.mixin;
|
||||
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import net.minecraft.client.gui.screen.TitleScreen;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(TitleScreen.class)
|
||||
public class InitializeMixin {
|
||||
@Inject(at = @At("HEAD"), method = "init()V")
|
||||
private void init(CallbackInfo info) {
|
||||
McWebserver.LOGGER.info("This line is printed by an example mod mixin!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package me.jonasjones.mcwebserver.util;
|
||||
|
||||
import jdk.jfr.StackTrace;
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import me.jonasjones.mcwebserver.config.ModConfigs;
|
||||
|
||||
public class VerboseLogger {
|
||||
public static void info(String message) {
|
||||
if (ModConfigs.VERBOSE) {
|
||||
McWebserver.VERBOSELOGGER.info(message);
|
||||
}
|
||||
}
|
||||
public void debug(String message) {
|
||||
if (ModConfigs.VERBOSE) {
|
||||
McWebserver.VERBOSELOGGER.debug(message);
|
||||
}
|
||||
}
|
||||
public static void error(String message) {
|
||||
if (ModConfigs.VERBOSE) {
|
||||
McWebserver.VERBOSELOGGER.error(message);
|
||||
}
|
||||
}
|
||||
public static void trace( String message) {
|
||||
if (ModConfigs.VERBOSE) {
|
||||
McWebserver.VERBOSELOGGER.trace(message);
|
||||
}
|
||||
}
|
||||
public static void warn( String message) {
|
||||
if (ModConfigs.VERBOSE) {
|
||||
McWebserver.VERBOSELOGGER.warn(message);
|
||||
}
|
||||
}
|
||||
}
|
200
src/main/java/me/jonasjones/mcwebserver/web/HTTPServer.java
Normal file
200
src/main/java/me/jonasjones/mcwebserver/web/HTTPServer.java
Normal file
|
@ -0,0 +1,200 @@
|
|||
package me.jonasjones.mcwebserver.web;
|
||||
|
||||
import me.jonasjones.mcwebserver.config.ModConfigs;
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import me.jonasjones.mcwebserver.util.VerboseLogger;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Date;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class HTTPServer implements Runnable {
|
||||
static final File WEB_ROOT = new File(ModConfigs.WEB_ROOT);
|
||||
static final String DEFAULT_FILE = ModConfigs.WEB_FILE_ROOT;
|
||||
static final String FILE_NOT_FOUND = ModConfigs.WEB_FILE_404;
|
||||
static final String METHOD_NOT_SUPPORTED = ModConfigs.WEB_FILE_NOSUPPORT;
|
||||
// port to listen connection
|
||||
static final int PORT = ModConfigs.WEB_PORT;
|
||||
|
||||
// Client Connection via Socket Class
|
||||
private Socket connect;
|
||||
|
||||
public HTTPServer(Socket c) {
|
||||
connect = c;
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
try {
|
||||
ServerSocket serverConnect = new ServerSocket(PORT);
|
||||
McWebserver.LOGGER.info("Server started.");
|
||||
VerboseLogger.info("Listening for connections on port : \" + PORT + \" ...");
|
||||
|
||||
// we listen until user halts server execution
|
||||
while (true) {
|
||||
HTTPServer myServer = new HTTPServer(serverConnect.accept());
|
||||
|
||||
VerboseLogger.info("Connecton opened. (" + new Date() + ")");
|
||||
|
||||
// create dedicated thread to manage the client connection
|
||||
Thread thread = new Thread(myServer);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
McWebserver.LOGGER.info("Server Connection error : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// we manage our particular client connection
|
||||
BufferedReader in = null; PrintWriter out = null; BufferedOutputStream dataOut = null;
|
||||
String fileRequested = null;
|
||||
|
||||
try {
|
||||
// we read characters from the client via input stream on the socket
|
||||
in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
|
||||
// we get character output stream to client (for headers)
|
||||
out = new PrintWriter(connect.getOutputStream());
|
||||
// get binary output stream to client (for requested data)
|
||||
dataOut = new BufferedOutputStream(connect.getOutputStream());
|
||||
|
||||
// get first line of the request from the client
|
||||
String input = in.readLine();
|
||||
// we parse the request with a string tokenizer
|
||||
StringTokenizer parse = new StringTokenizer(input);
|
||||
String method = parse.nextToken().toUpperCase(); // we get the HTTP method of the client
|
||||
// we get file requested
|
||||
fileRequested = parse.nextToken().toLowerCase();
|
||||
|
||||
// we support only GET and HEAD methods, we check
|
||||
if (!method.equals("GET") && !method.equals("HEAD")) {
|
||||
VerboseLogger.info("501 Not Implemented : " + method + " method.");
|
||||
|
||||
// we return the not supported file to the client
|
||||
File file = new File(WEB_ROOT, METHOD_NOT_SUPPORTED);
|
||||
int fileLength = (int) file.length();
|
||||
String contentMimeType = "text/html";
|
||||
//read content to return to client
|
||||
byte[] fileData = readFileData(file, fileLength);
|
||||
|
||||
// we send HTTP Headers with data to client
|
||||
out.println("HTTP/1.1 501 Not Implemented");
|
||||
out.println("Server: Java HTTP Server from SSaurel : 1.0");
|
||||
out.println("Date: " + new Date());
|
||||
out.println("Content-type: " + contentMimeType);
|
||||
out.println("Content-length: " + fileLength);
|
||||
out.println(); // blank line between headers and content, very important !
|
||||
out.flush(); // flush character output stream buffer
|
||||
// file
|
||||
dataOut.write(fileData, 0, fileLength);
|
||||
dataOut.flush();
|
||||
|
||||
} else {
|
||||
// GET or HEAD method
|
||||
if (fileRequested.endsWith("/")) {
|
||||
fileRequested += DEFAULT_FILE;
|
||||
}
|
||||
|
||||
File file = new File(WEB_ROOT, fileRequested);
|
||||
int fileLength = (int) file.length();
|
||||
String content = getContentType(fileRequested);
|
||||
|
||||
if (method.equals("GET")) { // GET method so we return content
|
||||
byte[] fileData = readFileData(file, fileLength);
|
||||
|
||||
// send HTTP Headers
|
||||
out.println("HTTP/1.1 200 OK");
|
||||
out.println("Server: Java HTTP Server from SSaurel : 1.0");
|
||||
out.println("Date: " + new Date());
|
||||
out.println("Content-type: " + content);
|
||||
out.println("Content-length: " + fileLength);
|
||||
out.println(); // blank line between headers and content, very important !
|
||||
out.flush(); // flush character output stream buffer
|
||||
|
||||
dataOut.write(fileData, 0, fileLength);
|
||||
dataOut.flush();
|
||||
}
|
||||
|
||||
VerboseLogger.info("File " + fileRequested + " of type " + content + " returned");
|
||||
|
||||
}
|
||||
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
try {
|
||||
fileNotFound(out, dataOut, fileRequested);
|
||||
} catch (IOException ioe) {
|
||||
McWebserver.LOGGER.info("Error with file not found exception : " + ioe.getMessage());
|
||||
}
|
||||
|
||||
} catch (IOException ioe) {
|
||||
McWebserver.LOGGER.info("Server error : " + ioe);
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
dataOut.close();
|
||||
connect.close(); // we close socket connection
|
||||
} catch (Exception e) {
|
||||
McWebserver.LOGGER.info("Error closing stream : " + e.getMessage());
|
||||
}
|
||||
|
||||
VerboseLogger.info("Connection closed.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private byte[] readFileData(File file, int fileLength) throws IOException {
|
||||
FileInputStream fileIn = null;
|
||||
byte[] fileData = new byte[fileLength];
|
||||
|
||||
try {
|
||||
fileIn = new FileInputStream(file);
|
||||
fileIn.read(fileData);
|
||||
} finally {
|
||||
if (fileIn != null)
|
||||
fileIn.close();
|
||||
}
|
||||
|
||||
return fileData;
|
||||
}
|
||||
|
||||
// return supported MIME Types
|
||||
private String getContentType(String fileRequested) {
|
||||
if (fileRequested.endsWith(".htm") || fileRequested.endsWith(".html"))
|
||||
return "text/html";
|
||||
else
|
||||
return "text/plain";
|
||||
}
|
||||
|
||||
private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException {
|
||||
File file = new File(WEB_ROOT, FILE_NOT_FOUND);
|
||||
int fileLength = (int) file.length();
|
||||
String content = "text/html";
|
||||
byte[] fileData = readFileData(file, fileLength);
|
||||
|
||||
out.println("HTTP/1.1 404 File Not Found");
|
||||
out.println("Server: Java HTTP Server from SSaurel : 1.0");
|
||||
out.println("Date: " + new Date());
|
||||
out.println("Content-type: " + content);
|
||||
out.println("Content-length: " + fileLength);
|
||||
out.println(); // blank line between headers and content, very important !
|
||||
out.flush(); // flush character output stream buffer
|
||||
|
||||
dataOut.write(fileData, 0, fileLength);
|
||||
dataOut.flush();
|
||||
|
||||
VerboseLogger.info("File " + fileRequested + " not found");
|
||||
}
|
||||
}
|
BIN
src/main/resources/assets/mcwebserver/icon.png
Normal file
BIN
src/main/resources/assets/mcwebserver/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 453 B |
39
src/main/resources/fabric.mod.json
Normal file
39
src/main/resources/fabric.mod.json
Normal file
|
@ -0,0 +1,39 @@
|
|||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "mcwebserver",
|
||||
"version": "0.1.0",
|
||||
|
||||
"name": "McWebserver",
|
||||
"description": "A simple webserver that runs alongside the Minecraft Server",
|
||||
"authors": [
|
||||
"Jonas_Jones"
|
||||
],
|
||||
"contact": {
|
||||
"homepage": "https://jonasjones.me/McWebserver",
|
||||
"sources": "https://github.com/J-onasJones/McWebserver",
|
||||
"issues": "https://github.com/J-onasJones/McWebserver/issues"
|
||||
},
|
||||
|
||||
"license": "CC0-1.0",
|
||||
"icon": "assets/mcwebserver/icon.png",
|
||||
|
||||
"environment": "server",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"me.jonasjones.mcwebserver.McWebserver"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
"mcwebserver.mixins.json"
|
||||
],
|
||||
|
||||
"depends": {
|
||||
"fabricloader": ">=0.14.6",
|
||||
"fabric": "*",
|
||||
"minecraft": "~1.19",
|
||||
"java": ">=17"
|
||||
},
|
||||
"suggests": {
|
||||
"another-mod": "*"
|
||||
}
|
||||
}
|
14
src/main/resources/mcwebserver.mixins.json
Normal file
14
src/main/resources/mcwebserver.mixins.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "net.fabricmc.example.mixin",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"mixins": [
|
||||
],
|
||||
"client": [
|
||||
"ExampleMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue