From cc6152f471e80a3be64c5d95554c36a707837111 Mon Sep 17 00:00:00 2001 From: J-onasJones Date: Mon, 11 Mar 2024 17:46:53 +0100 Subject: [PATCH] Fix error when 404 file not present --- .../mcwebserver/web/HttpServer.java | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/me/jonasjones/mcwebserver/web/HttpServer.java b/src/main/java/me/jonasjones/mcwebserver/web/HttpServer.java index a6a951e..16b9249 100644 --- a/src/main/java/me/jonasjones/mcwebserver/web/HttpServer.java +++ b/src/main/java/me/jonasjones/mcwebserver/web/HttpServer.java @@ -359,22 +359,38 @@ public class HttpServer implements Runnable { } private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException { - Path file = WEB_ROOT.resolve(FILE_NOT_FOUND); - int fileLength = (int) Files.size(file); - String contentType = "text/html"; - byte[] fileData = readFileData(file); + try { + Path file = WEB_ROOT.resolve(FILE_NOT_FOUND); + int fileLength = (int) Files.size(file); + String contentType = "text/html"; + byte[] fileData = readFileData(file); - dataOut.write(NOT_FOUND); - dataOut.write(HEADERS); - dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8)); - dataOut.write("Content-Type: %s\r\n".formatted(contentType).getBytes(StandardCharsets.UTF_8)); - dataOut.write("Content-Length: %s\r\n".formatted(fileLength).getBytes(StandardCharsets.UTF_8)); - dataOut.write(CRLF); // blank line between headers and content, very important ! - out.flush(); // flush character output stream buffer + dataOut.write(NOT_FOUND); + dataOut.write(HEADERS); + dataOut.write("Date: %s\r\n".formatted(Instant.now()).getBytes(StandardCharsets.UTF_8)); + dataOut.write("Content-Type: %s\r\n".formatted(contentType).getBytes(StandardCharsets.UTF_8)); + dataOut.write("Content-Length: %s\r\n".formatted(fileLength).getBytes(StandardCharsets.UTF_8)); + dataOut.write(CRLF); // blank line between headers and content, very important ! + out.flush(); // flush character output stream buffer - dataOut.write(fileData, 0, fileLength); - dataOut.flush(); + dataOut.write(fileData, 0, fileLength); + dataOut.flush(); - VerboseLogger.error("File " + fileRequested + " not found"); + 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"); + } } }