diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 07b547416f..54c35d44ff 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -46,8 +46,9 @@ public void process(final Socket connection) { } private String getResponse(HttpRequest httpRequest, HttpResponse httpResponse) { - if (resourceProvider.haveResource(httpRequest.getRequestLine().getPath())) { - return resourceProvider.staticResourceResponse(httpRequest.getRequestLine().getPath()); + String path = httpRequest.getRequestLine().getPath(); + if (resourceProvider.haveResource(path)) { + return resourceProvider.staticResourceResponse(path); } if (handlerMapper.haveAvailableHandler(httpRequest)) { handlerMapper.process(httpRequest, httpResponse); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/AbstractController.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/AbstractController.java index acad392aaa..184604c973 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/controller/AbstractController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/controller/AbstractController.java @@ -10,24 +10,24 @@ public abstract class AbstractController implements Controller { @Override public void service(HttpRequest httpRequest, HttpResponse httpResponse) { - HttpMethod method = httpRequest.getRequestLine().getMethod(); - if (method == HttpMethod.GET) { + if (httpRequest.isRequestOf(HttpMethod.GET)) { doGet(httpRequest, httpResponse); return; } - if (method == HttpMethod.POST) { + if (httpRequest.isRequestOf(HttpMethod.POST)) { doPost(httpRequest, httpResponse); return; } - if (method == HttpMethod.PUT) { + if (httpRequest.isRequestOf(HttpMethod.PUT)) { doPut(httpRequest, httpResponse); return; } - if (method == HttpMethod.DELETE) { + if (httpRequest.isRequestOf(HttpMethod.DELETE)) { doDelete(httpRequest, httpResponse); return; } - throw new UnsupportedOperationException("해당 HttpMethod 는 아직 지원하지 않습니다." + method); + throw new UnsupportedOperationException( + "해당 HttpMethod 는 아직 지원하지 않습니다." + httpRequest.getRequestLine().getMethod()); } protected void doGet(HttpRequest httpRequest, HttpResponse httpResponse) { 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 f956baabf7..4651dbe32b 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 @@ -48,7 +48,7 @@ private static String readHeaders(BufferedReader inputReader) { private static String readBody(BufferedReader inputReader, HttpRequestHeaders httpRequestHeaders) { try { - Optional length = httpRequestHeaders.getContentLength(); + Optional length = httpRequestHeaders.contentLength(); if (length.isEmpty()) { return null; } @@ -61,6 +61,10 @@ private static String readBody(BufferedReader inputReader, HttpRequestHeaders ht } } + public boolean isRequestOf(HttpMethod method) { + return requestLine.isRequestOf(method); + } + public RequestLine getRequestLine() { return requestLine; } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequestHeaders.java b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequestHeaders.java index 386831a04d..6fe5bdf22b 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequestHeaders.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequestHeaders.java @@ -51,15 +51,15 @@ private String valueOf(String header) { return stringBuilder.toString().trim(); } - public Optional getCookie() { - return cookies; + public Optional contentLength() { + return Optional.ofNullable(headers.get(CONTENT_LENGTH)); } public Map getHeaders() { return headers; } - public Optional getContentLength() { - return Optional.ofNullable(headers.get(CONTENT_LENGTH)); + public Optional getCookie() { + return cookies; } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestLine.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestLine.java index 8d37131c35..59aaf89b0f 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestLine.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestLine.java @@ -72,6 +72,10 @@ public HttpMethod getMethod() { return httpMethod; } + public boolean isRequestOf(HttpMethod method) { + return this.getMethod() == method; + } + public String getPath() { return path; } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponseHeaders.java b/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponseHeaders.java index 0ef7f8e833..00b54c8c9a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponseHeaders.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponseHeaders.java @@ -8,6 +8,8 @@ public class HttpResponseHeaders { private static final ResourceProvider resourceProvider = new ResourceProvider(); + public static final String CONTENT_TYPE = "Content-Type"; + public static final String CONTENT_LENGTH = "Content-Length"; private final Map headers; @@ -19,8 +21,8 @@ public static HttpResponseHeaders from(ResponseEntity responseEntity, Op Map headers = new HashMap<>(); headers.putAll(responseEntity.getHeaders()); if (body.isPresent()) { - headers.put("Content-Type", typeOf(responseEntity)); - headers.put("Content-Length", body.get().getBytes().length + " "); + headers.put(CONTENT_TYPE, typeOf(responseEntity)); + headers.put(CONTENT_LENGTH, body.get().getBytes().length + " "); } return new HttpResponseHeaders(headers); }