mirror of
https://github.com/JonasunderscoreJones/McWebserver.git
synced 2025-10-23 03:19:19 +02:00
fixed config toggle for api to be respected
This commit is contained in:
parent
46f80476ef
commit
2348921fb1
1 changed files with 46 additions and 35 deletions
|
@ -49,7 +49,7 @@ public class HttpServer implements Runnable {
|
|||
// Client Connection via Socket Class
|
||||
private final Socket connect;
|
||||
private final MimeTypeIdentifier mimetypeidentifier = new MimeTypeIdentifier();
|
||||
private Boolean isApiRequest = false;
|
||||
private Boolean isApiv1Request = false;
|
||||
|
||||
static {
|
||||
try {
|
||||
|
@ -115,7 +115,7 @@ public class HttpServer implements Runnable {
|
|||
|
||||
// we support only GET and HEAD methods, we check
|
||||
if (!method.equals("GET") && !method.equals("HEAD")) {
|
||||
isApiRequest = false;
|
||||
isApiv1Request = false;
|
||||
VerboseLogger.info("501 Not Implemented : " + method + " method.");
|
||||
|
||||
// we return the not supported file to the client
|
||||
|
@ -136,49 +136,60 @@ public class HttpServer implements Runnable {
|
|||
dataOut.write(fileData, 0, fileData.length);
|
||||
dataOut.flush();
|
||||
} else if (isApiRequest(fileRequested)) {
|
||||
isApiRequest = true;
|
||||
isApiv1Request = true;
|
||||
if (ModConfigs.SERVER_API_ENABLED) {
|
||||
// Set appropriate response headers
|
||||
dataOut.write("HTTP/1.1 200 OK\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8));
|
||||
if (fileRequested.equals("/api/v1/servericon")) {
|
||||
dataOut.write("Content-Type: image/png\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
// Get server icon from ApiHandler
|
||||
byte[] serverIcon = ApiRequestsUtil.getServerIcon();
|
||||
int contentLength = serverIcon.length;
|
||||
|
||||
// Set appropriate response headers
|
||||
dataOut.write("HTTP/1.1 200 OK\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8));
|
||||
if (fileRequested.equals("/api/v1/servericon")) {
|
||||
dataOut.write("Content-Type: image/png\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
// Get server icon from ApiHandler
|
||||
byte[] serverIcon = ApiRequestsUtil.getServerIcon();
|
||||
int contentLength = serverIcon.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(("Content-Length: " + contentLength + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content
|
||||
// Send server icon
|
||||
dataOut.write(serverIcon, 0, contentLength);
|
||||
dataOut.flush();
|
||||
} else {
|
||||
dataOut.write("Content-Type: application/json\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
String jsonString = "";
|
||||
try {
|
||||
// Get JSON data from ApiHandler
|
||||
jsonString = ApiHandler.handle(fileRequested);
|
||||
} catch (Exception e) {
|
||||
VerboseLogger.error("Error getting JSON data from ApiHandler: " + e.getMessage());
|
||||
jsonString = ApiRequests.internalServerError();
|
||||
}
|
||||
|
||||
// Send server icon
|
||||
dataOut.write(serverIcon, 0, contentLength);
|
||||
dataOut.flush();
|
||||
} else {
|
||||
dataOut.write("Content-Type: application/json\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
String jsonString = "";
|
||||
try {
|
||||
// Get JSON data from ApiHandler
|
||||
jsonString = ApiHandler.handle(fileRequested);
|
||||
} catch (Exception e) {
|
||||
VerboseLogger.error("Error getting JSON data from ApiHandler: " + e.getMessage());
|
||||
jsonString = ApiRequests.internalServerError();
|
||||
|
||||
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
|
||||
|
||||
// Send JSON data
|
||||
dataOut.write(jsonBytes, 0, contentLength);
|
||||
dataOut.flush();
|
||||
}
|
||||
|
||||
|
||||
byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
|
||||
int contentLength = jsonBytes.length;
|
||||
|
||||
dataOut.write(("Content-Length: " + contentLength + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||
} else {
|
||||
// Server API is disabled
|
||||
dataOut.write("HTTP/1.1 403 Forbidden\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Content-Type: text/html\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Content-Length: 0\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content
|
||||
|
||||
// Send JSON data
|
||||
dataOut.write(jsonBytes, 0, contentLength);
|
||||
dataOut.flush();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
isApiRequest = false;
|
||||
isApiv1Request = false;
|
||||
// GET or HEAD method
|
||||
if (fileRequested.endsWith("/")) {
|
||||
fileRequested += DEFAULT_FILE;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue