Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[라면] 1단계 - HTTP 웹서버 구현 미션 제출합니다! #110

Open
wants to merge 16 commits into
base: ramen6315
Choose a base branch
from

Conversation

Ramen6315
Copy link

작동하는 코드를 구현후에 간단히 정리 만 했습니다.
http에 익숙하지 않아 부족한 부분이 많이 있습니다.
감사합니다.!!!

Copy link

@kang-hyungu kang-hyungu left a comment

Choose a reason for hiding this comment

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

안녕하세요 라면~ 리뷰어 구구입니다 👋
구현하신 코드 확인했고 몇 가지 피드백 남겼어요
수정해보시고 궁금한 점 생기면 dm 주시거나 코멘트 남겨주세요
추가로 http 스펙도 읽어보시면 객체 설계하는데 도움이 될거에요
https://tools.ietf.org/html/rfc2616#section-4
4,5,6 section 보시면 http request, response 구조를 파악할 수 있을거에요

public class RequestHandler implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class);
private static final int PATH_INDEX = 1;

Choose a reason for hiding this comment

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

이 변수는 사용하는 곳이 없네요

}
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
HashMap<String, String> requestUrl = new HashMap<>();

Choose a reason for hiding this comment

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

Suggested change
HashMap<String, String> requestUrl = new HashMap<>();
Map<String, String> requestUrl = new HashMap<>();

HashMap이라는 타입으로 정하지 말고 Map 인터페이스를 사용하는게 좋습니다.
이 변수 외에도 컬렉션 타입의 다른 변수도 같이 변경해보아요

}
}
String[] request = RequestUtils.separateUrl(line);
IOUtils.parseHeaderToken(bufferedReader,line,requestUrl);

Choose a reason for hiding this comment

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

HttpRequest 객체를 추가해보면 어떨까요?
그 객체에서 http message를 읽고 header 객체도 처리하도록 객체 설계를 해보면 좋겠어요


import java.util.HashMap;

public class RequestUtils {

Choose a reason for hiding this comment

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

util로 만들지 말고 http request를 처리하는 객체를 추가해보는게 좋겠네요

logger.debug("header Line: {} " + line);
String[] headerToken = line.split(HEADER_TOKEN_SPLIT_VALUE);

if(headerToken.length == 2) {

Choose a reason for hiding this comment

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

객체지향 생활체조
규칙 1: 한 메서드에 오직 한 단계의 들여쓰기만 한다.

2뎁스를 없애려면 어떻께 하면 될까요?

String[] request = RequestUtils.separateUrl(line);
IOUtils.parseHeaderToken(bufferedReader,line,requestUrl);

UserController.postSignIn(out, bufferedReader, request, requestUrl);

Choose a reason for hiding this comment

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

컨트롤러에서 http request를 파라미터로 받아서 자신이 처리할 요청이 맞는지 판단하도록 설계하신 것 같네요
컨트롤러가 하는 역할은 무엇인가요?
내가 처리할 요청이 맞는지 판단하는 책임도 가져야 할까요?

String[] request = RequestUtils.separateUrl(line);
IOUtils.parseHeaderToken(bufferedReader,line,requestUrl);

UserController.postSignIn(out, bufferedReader, request, requestUrl);

Choose a reason for hiding this comment

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

was를 구현하는 과정이니 컨트롤러 대신 서블릿을 추가해보면 어떨까요?
서블릿으로 구현해보고 미션 진행해나가면서 Dispatcher Servlet을 추가해서 요청에 따라 어느 컨트롤러를 실행할지 결정하도록 구현하는 방향으로 가면 좋겠네요


String uri = request[PATH_INDEX];

if(request[PATH_INDEX].endsWith(HTML_EXTENSION_VALUE)) {

Choose a reason for hiding this comment

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

지금 구조라면 컨트롤러를 추가할 때마다 이 조건문과 http reponse를 처리하는 로직이 중복해서 생기겠네요
HttpRequest, HttpResponse 클래스를 만들고 각 클래스에서 input, output 처리를 하도록 만들면 중복을 줄이고 읽기 좋은 코드로 만들 수 있을거에요

public static String readData(BufferedReader br, int contentLength) throws IOException {
char[] body = new char[contentLength];
br.read(body, 0, contentLength);
return String.copyValueOf(body);
}

public static void parseHeaderToken(final BufferedReader bufferedReader, String line,

Choose a reason for hiding this comment

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

IOUtils는 IO 처리만 담당해야 합니다.
이 메서드는

  1. io로 데이터를 읽어오고
  2. 읽어온 데이터를 파싱
    하고 있습니다.
    1번만 하도록 수정하고 2번은 Http Header 클래스를 추가해서 그 클래스에게 책임을 부여하세요

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants