Skip to content

Commit

Permalink
style : 메서드 순서 조정 및 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeel5839 committed Sep 11, 2023
1 parent 5ba959f commit 622a7fd
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 86 deletions.
3 changes: 1 addition & 2 deletions tomcat/src/main/java/org/apache/catalina/startup/Tomcat.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.apache.catalina.startup;

import java.io.IOException;
import org.apache.catalina.connector.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class Tomcat {

private static final Logger log = LoggerFactory.getLogger(Tomcat.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import nextstep.jwp.db.InMemoryUserRepository;
import nextstep.jwp.model.User;
import org.apache.catalina.HttpSession;
import org.apache.coyote.http11.Http11Processor;
import org.apache.coyote.http11.HttpRequest;
import org.apache.coyote.http11.HttpResponse;
import org.apache.coyote.http11.HttpStatus;
Expand Down Expand Up @@ -43,6 +42,7 @@ public void doGet(

try {
String body = findPage(httpRequest.getPath());

httpResponse.setStatus(OK);
httpResponse.setHeader(CONTENT_TYPE.getValue(), HTML.getValue());
httpResponse.setHeader(CONTENT_LENGTH.getValue(), String.valueOf(body.getBytes().length));
Expand Down
36 changes: 25 additions & 11 deletions tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,30 @@ public void run() {

@Override
public void process(final Socket connection) {
try (final InputStream inputStream = connection.getInputStream();
final OutputStream outputStream = connection.getOutputStream()) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
HttpRequestHeader httpRequestHeader = getHttpRequestHeader(bufferedReader);
HttpRequestBody httpRequestBody = getHttpRequestBody(httpRequestHeader, bufferedReader);
HttpRequest httpRequest = new HttpRequest(httpRequestHeader, httpRequestBody);
Handler handler = HandlerMapper.getHandle(httpRequest);
HttpResponse httpResponse = new HttpResponse();
handler.service(httpRequest, httpResponse);
outputStream.write(httpResponse.toString().getBytes());
outputStream.flush();
try (InputStream inputStream = connection.getInputStream();
OutputStream outputStream = connection.getOutputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
HttpRequest httpRequest = getHttpRequest(bufferedReader);
HttpResponse httpResponse = execute(httpRequest);
sendResponse(outputStream, httpResponse);
} catch (IOException | UncheckedServletException e) {
log.error(e.getMessage(), e);
}
}

private HttpResponse execute(final HttpRequest httpRequest) {
Handler handler = HandlerMapper.getHandle(httpRequest);
HttpResponse httpResponse = new HttpResponse();
handler.service(httpRequest, httpResponse);
return httpResponse;
}

private HttpRequest getHttpRequest(final BufferedReader bufferedReader) throws IOException {
HttpRequestHeader httpRequestHeader = getHttpRequestHeader(bufferedReader);
HttpRequestBody httpRequestBody = getHttpRequestBody(httpRequestHeader, bufferedReader);
return new HttpRequest(httpRequestHeader, httpRequestBody);
}

private HttpRequestHeader getHttpRequestHeader(BufferedReader bufferedReader) throws IOException {
StringBuilder header = new StringBuilder();
String line;
Expand Down Expand Up @@ -82,6 +90,7 @@ private HttpRequestHeader extractHttpRequest(StringBuilder content) {

private HttpRequestBody getHttpRequestBody(HttpRequestHeader httpRequestHeader, BufferedReader bufferedReader) throws IOException {
String header = httpRequestHeader.get(CONTENT_LENGTH.getValue());

if (header.isBlank()) {
return HttpRequestBody.from("");
}
Expand All @@ -94,4 +103,9 @@ private HttpRequestBody getHttpRequestBody(HttpRequestHeader httpRequestHeader,
return HttpRequestBody.from(requestBody);
}

private void sendResponse(final OutputStream outputStream, final HttpResponse httpResponse) throws IOException {
outputStream.write(httpResponse.toString().getBytes());
outputStream.flush();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ public String getPath() {
return httpRequestHeader.getPath();
}

public String getFilePath() {
return httpRequestHeader.getFilePath();
}

public String getContentType() {
return httpRequestHeader.getContentType();
}

public String getBodyAttribute(String key) {
return httpRequestBody.get(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ private static Map<String, String> parseRequestBody(String requestBody) {
return parseResultOfRequestBody;
}

private static void putParseResultOfComponent(Map<String, String> parseResultOfRequestBody, String component) {
private static void putParseResultOfComponent(
Map<String, String> parseResultOfRequestBody,
String component
) {
if (component.length() < 2) {
return;
}
Expand All @@ -38,8 +41,4 @@ public String get(String key) {
return requestBody.getOrDefault(key, "");
}

public Map<String, String> getRequestBody() {
return requestBody;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@

public class HttpRequestHeader {

private static final Map<String, String> requestUrlToFilePath = Map.of(
"/login", "/login.html",
"/register", "/register.html"
);

private HttpMethod method;
private String path;
private Map<String, String> extraContents;
private Map<String, String> queryStrings;
private Map<String, String> cookies;
private final HttpMethod method;
private final String path;
private final Map<String, String> extraContents;
private final Map<String, String> queryStrings;
private final Map<String, String> cookies;

private HttpRequestHeader(
HttpMethod method,
Expand All @@ -42,6 +37,29 @@ public static HttpRequestHeader of(String method, String path, String[] extraCon
);
}

private static Map<String, String> parseExtraContents(String[] extraContents) {
Map<String, String> parseResultOfExtraContents = new HashMap<>();

for (String component : extraContents) {
putParseResultOfComponent(parseResultOfExtraContents, component, ":");
}

return parseResultOfExtraContents;
}

private static void putParseResultOfComponent(
Map<String, String> parseResultOfRequestBody,
String component,
String delimiter
) {
if (component.length() < 2) {
return;
}

String[] keyAndValue = component.split(delimiter);
parseResultOfRequestBody.put(keyAndValue[0].trim(), keyAndValue[1].trim());
}

private static String getUrlRemovedQueryString(String path) {
int questionMarkIndex = path.lastIndexOf("?");

Expand Down Expand Up @@ -70,16 +88,6 @@ private static Map<String, String> parseQueryStrings(String path) {
return parseResultOfQueryStrings;
}

private static Map<String, String> parseExtraContents(String[] extraContents) {
Map<String, String> parseResultOfExtraContents = new HashMap<>();

for (String component : extraContents) {
putParseResultOfComponent(parseResultOfExtraContents, component, ":");
}

return parseResultOfExtraContents;
}

private static Map<String, String> parseCookies(String cookies) {
if (cookies == null) {
return Collections.emptyMap();
Expand All @@ -94,37 +102,6 @@ private static Map<String, String> parseCookies(String cookies) {
return parseResultOfCookies;
}

private static void putParseResultOfComponent(
Map<String, String> parseResultOfRequestBody,
String component,
String delimiter
) {
if (component.length() < 2) {
return;
}

String[] keyAndValue = component.split(delimiter);
parseResultOfRequestBody.put(keyAndValue[0].trim(), keyAndValue[1].trim());
}

public String getContentType() {
return "text/" + extractExtension();
}

public String getFilePath() {
return requestUrlToFilePath.getOrDefault(path, path);
}

public String extractExtension() {
int lastIndexOfComma = path.lastIndexOf(".");

if (lastIndexOfComma == -1) {
return "html";
}

return path.substring(lastIndexOfComma + 1);
}

public String get(String key) {
return extraContents.getOrDefault(key, "");
}
Expand All @@ -145,12 +122,4 @@ public String getPath() {
return path;
}

public Map<String, String> getExtraContents() {
return extraContents;
}

public Map<String, String> getQueryStrings() {
return queryStrings;
}

}

0 comments on commit 622a7fd

Please sign in to comment.