fixed config toggle for api to be respected

This commit is contained in:
Jonas_Jones 2023-09-06 20:09:58 +02:00
parent 46f80476ef
commit 2348921fb1

View file

@ -49,7 +49,7 @@ public class HttpServer implements Runnable {
// Client Connection via Socket Class // Client Connection via Socket Class
private final Socket connect; private final Socket connect;
private final MimeTypeIdentifier mimetypeidentifier = new MimeTypeIdentifier(); private final MimeTypeIdentifier mimetypeidentifier = new MimeTypeIdentifier();
private Boolean isApiRequest = false; private Boolean isApiv1Request = false;
static { static {
try { try {
@ -115,7 +115,7 @@ public class HttpServer implements Runnable {
// we support only GET and HEAD methods, we check // we support only GET and HEAD methods, we check
if (!method.equals("GET") && !method.equals("HEAD")) { if (!method.equals("GET") && !method.equals("HEAD")) {
isApiRequest = false; isApiv1Request = false;
VerboseLogger.info("501 Not Implemented : " + method + " method."); VerboseLogger.info("501 Not Implemented : " + method + " method.");
// we return the not supported file to the client // 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.write(fileData, 0, fileData.length);
dataOut.flush(); dataOut.flush();
} else if (isApiRequest(fileRequested)) { } 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(("Content-Length: " + contentLength + "\r\n").getBytes(StandardCharsets.UTF_8));
dataOut.write("HTTP/1.1 200 OK\r\n".getBytes(StandardCharsets.UTF_8)); dataOut.write("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content
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)); // Send server icon
dataOut.write("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content 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); byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
dataOut.flush(); int contentLength = jsonBytes.length;
} else {
dataOut.write("Content-Type: application/json\r\n".getBytes(StandardCharsets.UTF_8)); dataOut.write(("Content-Length: " + contentLength + "\r\n").getBytes(StandardCharsets.UTF_8));
String jsonString = ""; dataOut.write("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content
try {
// Get JSON data from ApiHandler // Send JSON data
jsonString = ApiHandler.handle(fileRequested); dataOut.write(jsonBytes, 0, contentLength);
} catch (Exception e) { dataOut.flush();
VerboseLogger.error("Error getting JSON data from ApiHandler: " + e.getMessage());
jsonString = ApiRequests.internalServerError();
} }
} else {
// Server API is disabled
byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8); dataOut.write("HTTP/1.1 403 Forbidden\r\n".getBytes(StandardCharsets.UTF_8));
int contentLength = jsonBytes.length; 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: " + contentLength + "\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 dataOut.write("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content
// Send JSON data
dataOut.write(jsonBytes, 0, contentLength);
dataOut.flush(); dataOut.flush();
} }
} else { } else {
isApiRequest = false; isApiv1Request = false;
// GET or HEAD method // GET or HEAD method
if (fileRequested.endsWith("/")) { if (fileRequested.endsWith("/")) {
fileRequested += DEFAULT_FILE; fileRequested += DEFAULT_FILE;