diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java index a710c21991..e8b2928437 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java @@ -44,7 +44,7 @@ protected void doPost(HttpRequest request, HttpResponse response) { response.redirect(INDEX_URI.getFullPath()); return; } - final RequestBody requestBody = request.getRequestBody(); + final RequestBody requestBody = request.getRequestBody().orElseThrow(IllegalStateException::new); final String account = requestBody.getParamValue("account"); final String password = requestBody.getParamValue("password"); final Optional userOptional = InMemoryUserRepository.findByAccount(account); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/RegisterController.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/RegisterController.java index 622a7d2862..365cbd7836 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/controller/RegisterController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/controller/RegisterController.java @@ -28,7 +28,7 @@ protected void doGet(HttpRequest request, HttpResponse response) throws Exceptio @Override protected void doPost(HttpRequest request, HttpResponse response) { - final RequestBody requestBody = request.getRequestBody(); + final RequestBody requestBody = request.getRequestBody().orElseThrow(IllegalStateException::new); final String accountValue = requestBody.getParamValue("account"); final Optional userOptional = InMemoryUserRepository.findByAccount(accountValue); if (userOptional.isPresent()) { diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/RootController.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/RootController.java index d857ce24bf..000e2475d7 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/controller/RootController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/controller/RootController.java @@ -4,14 +4,22 @@ import org.apache.coyote.http11.response.HttpResponse; import org.apache.coyote.http11.response.HttpStatus; import org.apache.coyote.http11.response.ResponseBody; +import org.apache.coyote.http11.response.StaticResource; + +import java.io.IOException; public class RootController extends AbstractController { @Override protected void doGet(HttpRequest request, HttpResponse response) { - final ResponseBody responseBody = ResponseBody.rootContent(); - response.setHttpStatus(HttpStatus.OK); - response.setResponseBody(responseBody); - response.setResponseHeaders(responseBody); + try { + final StaticResource staticResource = StaticResource.from(request.getRequestLine().getPath()); + final ResponseBody responseBody = ResponseBody.from(staticResource); + response.setResponseBody(responseBody); + response.setHttpStatus(HttpStatus.OK); + response.setResponseHeaders(responseBody); + } catch (IOException e) { + throw new RuntimeException(e); + } } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java index 3cef10993e..5c036ee4b6 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java @@ -13,11 +13,11 @@ public class HttpRequest { private final RequestLine requestLine; private final RequestHeaders requestHeaders; - private final RequestBody requestBody; + private final Optional requestBody; private HttpRequest(final RequestLine requestLine, final RequestHeaders requestHeaders, - final RequestBody requestBody) { + final Optional requestBody) { this.requestLine = requestLine; this.requestHeaders = requestHeaders; this.requestBody = requestBody; @@ -26,7 +26,7 @@ private HttpRequest(final RequestLine requestLine, public static HttpRequest from(final BufferedReader bufferedReader) throws IOException { final RequestLine requestLine = RequestLine.from(bufferedReader.readLine()); final RequestHeaders requestHeaders = RequestHeaders.from(bufferedReader); - final RequestBody requestBody = RequestBody.of(requestHeaders, bufferedReader); + final Optional requestBody = RequestBody.of(requestHeaders, bufferedReader); return new HttpRequest(requestLine, requestHeaders, requestBody); } @@ -34,7 +34,7 @@ public RequestLine getRequestLine() { return requestLine; } - public RequestBody getRequestBody() { + public Optional getRequestBody() { return requestBody; } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestBody.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestBody.java index b381652e90..4fde0a844d 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestBody.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestBody.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.StringTokenizer; public class RequestBody { @@ -16,9 +17,9 @@ private RequestBody(final Map content) { this.content = content; } - public static RequestBody of(final RequestHeaders requestHeaders, final BufferedReader bufferedReader) throws IOException { + public static Optional of(final RequestHeaders requestHeaders, final BufferedReader bufferedReader) throws IOException { if (!requestHeaders.containsKey("Content-Length")) { - return EMPTY; + return Optional.empty(); } final Map result = new HashMap<>(); final int contentLength = Integer.parseInt(requestHeaders.getHeaderValue("Content-Length")); @@ -33,10 +34,10 @@ public static RequestBody of(final RequestHeaders requestHeaders, final Buffered final String[] split = param.split("="); result.put(split[0], split[1]); } - return new RequestBody(result); + return Optional.of(new RequestBody(result)); } - return EMPTY; + return Optional.empty(); } public String getParamValue(final String key) { diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseBody.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseBody.java index 57f23d111f..55069d8f6a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseBody.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseBody.java @@ -18,14 +18,6 @@ public static ResponseBody from(final StaticResource staticResource) { ); } - public static ResponseBody noContent() { - return new ResponseBody(new byte[]{}, null); - } - - public static ResponseBody rootContent() { - return new ResponseBody("Hello world!".getBytes(), ContentType.HTML); - } - public byte[] getContent() { return content; } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/StaticResource.java b/tomcat/src/main/java/org/apache/coyote/http11/response/StaticResource.java index ac9112a8fe..a3ddbc9a1e 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/StaticResource.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/StaticResource.java @@ -22,6 +22,9 @@ private StaticResource(final byte[] content, final String fileType) { } public static StaticResource from(final String fileName) throws IOException { + if (fileName.equals("/")) { + return new StaticResource("Hello World!".getBytes(), ""); + } return new StaticResource(extractContent(fileName), extractFileExtension(fileName)); }