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,8 +136,8 @@ 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 // Set appropriate response headers
dataOut.write("HTTP/1.1 200 OK\r\n".getBytes(StandardCharsets.UTF_8)); 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)); dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8));
@ -175,10 +175,21 @@ public class HttpServer implements Runnable {
dataOut.write(jsonBytes, 0, contentLength); dataOut.write(jsonBytes, 0, contentLength);
dataOut.flush(); dataOut.flush();
} }
} 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
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;