From a6346c92b32a64d89140f6a70c5fe5b38230bdf5 Mon Sep 17 00:00:00 2001 From: charlotte henriksson Date: Fri, 16 Feb 2024 16:29:15 +0100 Subject: [PATCH 1/2] updated clientHandler for 501 response and httpParser --- .../fungover/storm/client/ClientHandler.java | 20 +++++++++++++++++++ .../org/fungover/storm/client/HttpParser.java | 6 +++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/fungover/storm/client/ClientHandler.java b/src/main/java/org/fungover/storm/client/ClientHandler.java index 7168ee9..10578ba 100644 --- a/src/main/java/org/fungover/storm/client/ClientHandler.java +++ b/src/main/java/org/fungover/storm/client/ClientHandler.java @@ -14,6 +14,7 @@ import java.io.OutputStream; import java.net.Socket; import java.nio.file.Files; +import java.nio.file.Path; public class ClientHandler implements Runnable { private static final Logger LOGGER = LogManager.getLogger("CLIENT_HANDLER"); @@ -60,6 +61,11 @@ public void run() { } private byte[][] getResponse(String input) throws IOException { + String method = HttpParser.getRequestMethod(input); + if (!HttpParser.isMethodSupported(method)) { + return get501Response(); + } + FileInfo fileInfo; byte[][] response; try { @@ -73,6 +79,13 @@ private byte[][] getResponse(String input) throws IOException { return response; } + private byte[][] get501Response() { + ResponseCode responseCode = ResponseCode.NOT_IMPLEMENTED; + String description = responseCode.getCode(); + FileInfo fileInfo = new FileInfo(Path.of("/"), description.getBytes()); + return fileRequestHandler.writeResponse(fileInfo, description); + } + private byte[][] getFileNotFoundResponse(FileNotFoundException e) throws IOException { FileInfo fileInfo = fileRequestHandler.handleError(e.getParsedRequest(), e.getError404FileName(), e.getResponseCode()); byte[][] response; @@ -83,4 +96,11 @@ private byte[][] getFileNotFoundResponse(FileNotFoundException e) throws IOExcep } return response; } + + private void handleIOException(IOException e) { + if (e.getMessage().contains("500")) + LOGGER.error(ResponseCode.HTTP_RESPONSE_STATUS_CODES); + else + LOGGER.error(e.getMessage()); + } } diff --git a/src/main/java/org/fungover/storm/client/HttpParser.java b/src/main/java/org/fungover/storm/client/HttpParser.java index eaf8e3b..7c1e875 100644 --- a/src/main/java/org/fungover/storm/client/HttpParser.java +++ b/src/main/java/org/fungover/storm/client/HttpParser.java @@ -4,7 +4,7 @@ public class HttpParser { - private static final List methods = List.of("GET", "POST", "PUT"); + private static final List methods = List.of("GET", "POST", "PUT", "HEAD"); private static final int REQUEST_LINE_PROPERTIES = 3; private HttpParser() { @@ -31,6 +31,10 @@ public static String getPath(String requestLine) { } } + public static boolean isMethodSupported(String method) { + return methods.contains(method); + } + private static Map requestHeaders(List lines, String[] firstLine) { From 83d72290945d16f14048ddfbdc0c7163d6cf8c1e Mon Sep 17 00:00:00 2001 From: charlotte henriksson Date: Sat, 17 Feb 2024 14:19:03 +0100 Subject: [PATCH 2/2] added getRequestMethod to class httpParser --- src/main/java/org/fungover/storm/client/HttpParser.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/fungover/storm/client/HttpParser.java b/src/main/java/org/fungover/storm/client/HttpParser.java index 7c1e875..caeed13 100644 --- a/src/main/java/org/fungover/storm/client/HttpParser.java +++ b/src/main/java/org/fungover/storm/client/HttpParser.java @@ -35,7 +35,12 @@ public static boolean isMethodSupported(String method) { return methods.contains(method); } - + public static String getRequestMethod(String requestLine) { + String[] properties = requestLine.split(" "); + if (validRequest(properties)) { + return properties[0];} else { + return ""; } + } private static Map requestHeaders(List lines, String[] firstLine) { Map requestHeaders = new HashMap<>(parseFirstLine(firstLine));