mirror of
https://github.com/JonasunderscoreJones/McWebserver.git
synced 2025-10-23 03:19:19 +02:00
Changed 501 response to json
Removed config option of a not_supported.html file
This commit is contained in:
parent
236f62c141
commit
97e695536c
4 changed files with 17 additions and 24 deletions
|
@ -19,7 +19,6 @@ public class ModConfigs {
|
|||
public static Boolean SERVER_API_ENABLED;
|
||||
public static Boolean ADV_API_ENABLED;
|
||||
public static Boolean API_INGAME_COMMAND_ENABLED;
|
||||
public static String WEB_FILE_NOSUPPORT;
|
||||
|
||||
public static Boolean PHP_ENABLED;
|
||||
public static Boolean VERBOSE = false; //needs to be set to false since the verbose logger is called before config file is fully loaded
|
||||
|
@ -50,7 +49,6 @@ public class ModConfigs {
|
|||
config.addKeyValuePair(new Pair<>("web.api", true), "whether or not the webserver api should be enabled or not");
|
||||
config.addKeyValuePair(new Pair<>("web.api.adv", true), "whether or not the api should expose information such as player coordinates and inventory");
|
||||
config.addKeyValuePair(new Pair<>("web.api.cmd", true), "whether or not the ingame command to manage tokens should be enabled or not");
|
||||
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", false), "whether or not to log verbose output");
|
||||
config.addKeyValuePair(new Pair<>("web.php", false), "enable php");
|
||||
}
|
||||
|
@ -65,7 +63,6 @@ public class ModConfigs {
|
|||
SERVER_API_ENABLED = CONFIG.getOrDefault("web.api", true);
|
||||
ADV_API_ENABLED = CONFIG.getOrDefault("web.api.adv", true);
|
||||
API_INGAME_COMMAND_ENABLED = CONFIG.getOrDefault("web.api.cmd", true);
|
||||
WEB_FILE_NOSUPPORT = CONFIG.getOrDefault("web.file.notSupported", "not_supported.html");
|
||||
VERBOSE = CONFIG.getOrDefault("logger.verbose", true);
|
||||
PHP_ENABLED = CONFIG.getOrDefault("web.php", false);
|
||||
}
|
||||
|
|
|
@ -36,11 +36,8 @@ public class HttpServer implements Runnable {
|
|||
static Path 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;
|
||||
|
||||
private static final byte[] NOT_IMPLEMENTED = "HTTP/1.1 405 Method Not Allowed\r\n".getBytes(StandardCharsets.UTF_8);
|
||||
private static final byte[] OK = "HTTP/1.1 200 OK\r\n".getBytes(StandardCharsets.UTF_8);
|
||||
private static final byte[] NOT_FOUND = "HTTP/1.1 404 Not Found\r\n".getBytes(StandardCharsets.UTF_8);
|
||||
private static final byte[] HEADERS = String.join(
|
||||
|
@ -132,24 +129,17 @@ public class HttpServer implements Runnable {
|
|||
// we support only GET and HEAD methods, we check
|
||||
if (!method.equals("GET") && !method.equals("HEAD")) {
|
||||
isApiv1Request = false;
|
||||
VerboseLogger.info("501 Not Implemented : " + method + " method.");
|
||||
VerboseLogger.error("501 Not Implemented : " + method + " method.");
|
||||
|
||||
// we return the not supported file to the client
|
||||
Path file = WEB_ROOT.resolve(METHOD_NOT_SUPPORTED);
|
||||
long fileLength = Files.size(file);
|
||||
String contentMimeType = "text/html";
|
||||
//read content to return to client
|
||||
byte[] fileData = readFileData(file);
|
||||
|
||||
// we send HTTP Headers with data to client
|
||||
dataOut.write(NOT_IMPLEMENTED);
|
||||
dataOut.write(HEADERS); //hopefully enough credits
|
||||
dataOut.write("HTTP/1.1 501 Not Implemented\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Content-Type: %s\r\n".formatted(contentMimeType).getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Content-Length: %s\r\n".formatted(fileLength).getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write(CRLF); // blank line between headers and content, very important !
|
||||
// file
|
||||
dataOut.write(fileData, 0, fileData.length);
|
||||
dataOut.write("Content-Type: application/json\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
String jsonString = ErrorHandler.notImplementedErrorString();
|
||||
byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
|
||||
int contentLength = jsonBytes.length;
|
||||
dataOut.write(("Content-Length: " + contentLength + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content
|
||||
dataOut.write(jsonBytes, 0, contentLength);
|
||||
dataOut.flush();
|
||||
} else if (isApiV1Request(fileRequested)) {
|
||||
isApiv1Request = true;
|
||||
|
|
|
@ -101,10 +101,8 @@ public class ServerHandler implements Runnable {
|
|||
Path path = FabricLoader.getInstance().getGameDir();
|
||||
Path webroot = path.resolve(ModConfigs.WEB_ROOT);
|
||||
Path index = webroot.resolve(ModConfigs.WEB_FILE_ROOT);
|
||||
Path notsupported = webroot.resolve(ModConfigs.WEB_FILE_NOSUPPORT);
|
||||
Path notfound = webroot.resolve(ModConfigs.WEB_FILE_404);
|
||||
index.toFile().mkdirs();
|
||||
notsupported.toFile().mkdirs();
|
||||
notfound.toFile().mkdirs();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,14 @@ public class ErrorHandler {
|
|||
return gson.toJsonTree(notFoundError()).getAsJsonObject().toString();
|
||||
}
|
||||
|
||||
public static Error notImplementedError() {
|
||||
return new Error(501, "Not Implemented");
|
||||
}
|
||||
|
||||
public static String notImplementedErrorString() {
|
||||
return gson.toJsonTree(notImplementedError()).getAsJsonObject().toString();
|
||||
}
|
||||
|
||||
public static Error customError(int status, String message) {
|
||||
return new Error(status, message);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue