mirror of
https://github.com/JonasunderscoreJones/McWebserver.git
synced 2025-10-22 19:09:19 +02:00
Fix error when 404 file not present
This commit is contained in:
parent
0a71b04b66
commit
cc6152f471
1 changed files with 30 additions and 14 deletions
|
@ -359,6 +359,7 @@ public class HttpServer implements Runnable {
|
|||
}
|
||||
|
||||
private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException {
|
||||
try {
|
||||
Path file = WEB_ROOT.resolve(FILE_NOT_FOUND);
|
||||
int fileLength = (int) Files.size(file);
|
||||
String contentType = "text/html";
|
||||
|
@ -375,6 +376,21 @@ public class HttpServer implements Runnable {
|
|||
dataOut.write(fileData, 0, fileLength);
|
||||
dataOut.flush();
|
||||
|
||||
VerboseLogger.error("File " + fileRequested + " not found");
|
||||
} catch (Exception e) {
|
||||
VerboseLogger.error("Error with file not found exception : " + e.getMessage());
|
||||
dataOut.write("HTTP/1.1 404 Not Found\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8));
|
||||
dataOut.write("Content-Type: application/json\r\n".getBytes(StandardCharsets.UTF_8));
|
||||
String jsonString = ErrorHandler.notFoundErrorString();
|
||||
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();
|
||||
|
||||
VerboseLogger.error("File " + fileRequested + " not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue