Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/org/fungover/storm/client/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}

Comment on lines +64 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code is added in the getResponse method it will not be used for requests handled before this point, i.e. isCoffeeRequest. But maybe it is the check for coffeerequest and return of 418 response that should be moved into the getresponse method instead to be a part of this check.

FileInfo fileInfo;
byte[][] response;
try {
Expand All @@ -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);
Comment on lines +85 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really try to return root path that will be translated into index.html? Check Teapot.write418Response() for how to make a response in code without returning a file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with returning a file as a response is that it can include sensitive information.

}

private byte[][] getFileNotFoundResponse(FileNotFoundException e) throws IOException {
FileInfo fileInfo = fileRequestHandler.handleError(e.getParsedRequest(), e.getError404FileName(), e.getResponseCode());
byte[][] response;
Expand All @@ -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());
}
Comment on lines +99 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is never used and can be removed?

}
11 changes: 10 additions & 1 deletion src/main/java/org/fungover/storm/client/HttpParser.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be missing a getRequestMethod that is being called at ClientHandler.java Line 64

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a getRequestMethod and it seems to be working now, thanks for the feedback!

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class HttpParser {

private static final List<String> methods = List.of("GET", "POST", "PUT");
private static final List<String> methods = List.of("GET", "POST", "PUT", "HEAD");
private static final int REQUEST_LINE_PROPERTIES = 3;

private HttpParser() {
Expand All @@ -31,7 +31,16 @@ public static String getPath(String requestLine) {
}
}

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<String, String> requestHeaders(List<String> lines, String[] firstLine) {
Map<String, String> requestHeaders = new HashMap<>(parseFirstLine(firstLine));
Expand Down