added bad request response when api disabled

This commit is contained in:
Jonas_Jones 2023-09-06 20:46:50 +02:00
parent 3a88c33107
commit 00dc80c605
2 changed files with 11 additions and 6 deletions

View file

@ -137,6 +137,7 @@ public class HttpServer implements Runnable {
dataOut.flush(); dataOut.flush();
} else if (isApiRequest(fileRequested)) { } else if (isApiRequest(fileRequested)) {
isApiv1Request = true; isApiv1Request = true;
// Check if server API is enabled
if (ModConfigs.SERVER_API_ENABLED) { 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));
@ -177,17 +178,17 @@ public class HttpServer implements Runnable {
} }
} else { } else {
// Server API is disabled // Server API is disabled
String jsonString = ApiRequests.forbiddenRequest();
byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
int contentLength = jsonBytes.length;
dataOut.write("HTTP/1.1 403 Forbidden\r\n".getBytes(StandardCharsets.UTF_8)); 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("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-Type: application/json\r\n".getBytes(StandardCharsets.UTF_8));
dataOut.write("Content-Length: 0\r\n".getBytes(StandardCharsets.UTF_8)); 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("\r\n".getBytes(StandardCharsets.UTF_8)); // Blank line before content
dataOut.write(jsonBytes, 0, contentLength);
dataOut.flush(); dataOut.flush();
} }
} else { } else {
isApiv1Request = false; isApiv1Request = false;
// GET or HEAD method // GET or HEAD method

View file

@ -28,4 +28,8 @@ public class ApiRequests {
public static String internalServerError() { public static String internalServerError() {
return "{\"error\":{\"status\":500,\"message\":\"Internal Server Error\"}}"; return "{\"error\":{\"status\":500,\"message\":\"Internal Server Error\"}}";
} }
public static String forbiddenRequest() {
return "{\"error\":{\"status\":403,\"message\":\"Forbidden\"}}";
}
} }