Skip to content

[HTTP 서버 구현하기 - 1단계] 포츈(정윤성) 미션 제출합니다. #32

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

Merged
merged 26 commits into from
Aug 31, 2021

Conversation

unluckyjung
Copy link

@unluckyjung unluckyjung commented Aug 29, 2021

반갑습니다 파피.

어디 내놓기에도 부끄러운 하드코딩이 범벅인 코드인데
리팩토링은 이 후 단계에 예정되어 있으니, 일단 기능구현에 초점을 맞춰서 미션을 진행했습니다. ㅋㅋ; (이번주 목요일 테코톡인건 안비밀)

학습테스트는 스프링을 까보면서 했는데,
막상 미션 구현은 스프링, RFC 문서도 안보고, 열심히 뇌피셜 돌리면서 그냥 쌩 헤딩 + 구글링으로 구현했네요.

아직 많이 부족한 코드인데, 보시다가 이해가 힘드신부분 있으시면 언제든 편하게 DM주세요!

참고할점

  • 유저 정보에 관한 아이디는 일부러 로깅을 하지않았어요. (민감한 내용이라고 생각)
  • User 도메인에서 getPassword 같은것을 만들지 말까 고민하다가, 실 서비스에서는 password 자체를 그대로 저장하지 않을뿐더러, 이미 httpRequest에서 body를 파싱할때 password는 노출되니, 이부분은 신경쓰지 않고 userId auto-increment 부분만 신경쓰고 구현했어요.
  • 테스트는 EtoE 테스트위주로 했습니다.

@unluckyjung unluckyjung requested a review from kixtaxwax August 29, 2021 19:34
@unluckyjung unluckyjung self-assigned this Aug 30, 2021
Copy link

@kixtaxwax kixtaxwax left a comment

Choose a reason for hiding this comment

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

안녕하세요, 포츈~ 리뷰가 늦어 죄송합니다!

급하게 무지성으로 짠다고 하셔놓고 깔끔하게 잘 구현해주셨네요 ㅎㅎ 특히 HttpRequest 객체가 깔끔해서 보기 좋았어요!

if-else문 사용은 취향 차이일 수도 있기 때문에 한 번만 언급했습니다.

그리고 몇 가지 의견 남겨보았는데 참고해주세요 ㅎㅎ 제가 틀린 부분이 있으면 알려주세요~

Comment on lines +21 to 27
private static final Logger LOG = LoggerFactory.getLogger(RequestHandler.class);

private final Socket connection;

public RequestHandler(Socket connection) {
public RequestHandler(final Socket connection) {
this.connection = Objects.requireNonNull(connection);
}

Choose a reason for hiding this comment

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

사소한 부분까지 챙기시는 군요!👍

Comment on lines +42 to +48
if (controller == null) {
httpResponse.transfer(httpRequest.getUrl());
} else if (httpRequest.getHttpMethod().equals(HttpMethod.GET)) {
controller.get(httpRequest, httpResponse);
} else if (httpRequest.getHttpMethod().equals(HttpMethod.POST)) {
controller.post(httpRequest, httpResponse);
}

Choose a reason for hiding this comment

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

if문들만 있어도 되지 않을까요? 취향 차이긴 하지만요 ㅎㅎ

Copy link
Author

Choose a reason for hiding this comment

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

지금의 경우 if로 문기를 4번 하는 경우에도 문제가 되지 않지만
해당 컨트롤러의 상태에 따라서 4개의 분기가 완전히 분리되는 경우를 의도하는 경우에는 else if를 쓰는게 저는 맞다고 생각해요.

아주 쉬운 문제인데도, 프로그래머들이 생각보다 많이 실수하는 문제중에 fizzbuzz 같은 문제들이 있죠.
이 경우도 if 문만으로 잘못 짜는경우 문제가 발생할수 있구요 ㅎ_ㅎ..

아무튼 저는 분기가 완전히 분리되는 경우를 의도 했기 때문에 else if를 사용했어요.
물론 if + return 조합도 가능했지만. 어차피 나중에 리팩토링 할부분으로 크게 힘을 안준부분이에요!

Choose a reason for hiding this comment

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

오오..덕분에 알고리즘 문제 하나 풀고 왔어요!!!!ㅋㅋㅋ

그런데 풀어보고 느낀 점은 fizzbuzz 문제는 공배수 개념 때문에 else if가 필수인 게 와 닿는데,

HttpMethod는 어떤 하나의 HttpMethod가 다른 HttpMethod 두 개를 포함하고 있다든지 하는 경우는 없어서 if만으로도 가능하지 않나요?

이건 제가 궁금해서 하는 질문입니다 ㅋㅋㅋㅋ 태클 아닙니다!!

Copy link
Author

@unluckyjung unluckyjung Aug 30, 2021

Choose a reason for hiding this comment

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

저는 어느 한상황에 대한, 분기가 여러개로 나뉘고 그것이 완전히 독립적이어야 하면, if문을 N번 반복하지 않으려고 해요.
그런식의 코딩은 혹시나 하는 실수의 여지를 남겨두는것이라고 생각하거든요.

지금의 경우 if로 문기를 4번 하는 경우에도 문제가 되지 않지만
저는 분기가 완전히 분리되는 경우를 의도 했기 때문에 else if를 사용했어요

Copy link

@kixtaxwax kixtaxwax Aug 30, 2021

Choose a reason for hiding this comment

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

아아 실수 여지와 의도를 중요시한 케이스군요!!!!!!! 이해했습니다👍👍

Comment on lines +39 to +40
if (url.equals("/")) {
responseBody = DEFAULT_MESSAGE;

Choose a reason for hiding this comment

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

localhost:8080일 때도 index.html을 보여주는 건 어떨까요?!

Copy link
Author

Choose a reason for hiding this comment

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

처음에 저도 index.html를 반환하게 짯었다가,
주어진 기본 테스트 코드에 helloworld를 검사하고 있고, 그것을 유지하는 형태로 가야할꺼 같아
지금의 형태로 재변경했어요!

Comment on lines +41 to +42
} else if (url.equals("/login") || url.equals("/register")) {
responseBody = getBodyByUrl(DEFAULT_RESOURCE_PATH + url + ".html");

Choose a reason for hiding this comment

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

httpResponse.transfer 메서드 발동 조건이 requestUri가 "/login", "/register" 둘 다 아니어서 컨트롤러가 null일 때인 것 같은데, 그러면 이 조건문을 타는 경우가 없지 않나요?

Copy link
Author

Choose a reason for hiding this comment

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

transfer 이 불리는 부분은 requestUri가 "/login", "/register" 인 경우에도 존재합니다.
만약 localhost:8080/register 으로 접속을 시도한다면

ControllerMapper 에서 register관련 컨트롤러를 찾겠죠.
이후. registerController 의 get이 호출될텐데,

이때 transfer(httpRequest.getUrl()) 에서 요청은 /register이었기때문에
transfer(/register) 가 수행되게 됩니다.

이후 httpResponse 에서, /register 에 대한 처리가 필요해요.

실제로 분기를 제외하면 테스트도 실패합니다.

image

Choose a reason for hiding this comment

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

transfer가 RequestHandler에서만 불리는 걸로 착각했었네요 ㅎㅎ 친절한 답변 고마워요!

Copy link
Author

@unluckyjung unluckyjung Aug 30, 2021

Choose a reason for hiding this comment

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

제가 코드를 알아보기 힘들게 작성한탓입니다 ㅋㅋ;

Comment on lines 21 to 24
@Override
public void post(final HttpRequest httpRequest, final HttpResponse httpResponse) {

}

Choose a reason for hiding this comment

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

일단은 get으로 처리하는 건가요?!

Copy link
Author

@unluckyjung unluckyjung Aug 30, 2021

Choose a reason for hiding this comment

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

image

마이 미스테잌

Choose a reason for hiding this comment

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

ㅋㅋㅋㅋㅋㅋㅋ

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 11 Code Smells

94.2% 94.2% Coverage
0.0% 0.0% Duplication

Comment on lines +18 to +19
public void post(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException {
loginService.login(httpRequest, httpResponse);
Copy link
Author

Choose a reason for hiding this comment

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

로그인을 post에서 처리하도록 수정했습니다 ㅎㅎ;

Comment on lines 13 to 25
private Map<String, String> parseBodyData(final Map<String, String> bodyType, final String body) {
String[] parseRequestBody = parseRequestBody(body);

for (String bodyData : parseRequestBody) {
String[] parseBodyData = bodyData.split("=");
bodyType.put(parseBodyData[0], parseBodyData[1]);
}
return bodyType;
}

private String[] parseRequestBody(final String body) {
return body.split("&");
}
Copy link
Author

Choose a reason for hiding this comment

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

수정하는김에 바디정보를 추출하는 기능도, BodyPrams 객체가 수행하도록했어요.

Comment on lines 15 to 17
private String body;
private BodyParams bodyParams;
Copy link
Author

Choose a reason for hiding this comment

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

기존에 String으로 가지고 있던 body 데이터를 BodyParams로 들고있게 했어요.
HttpRequest가 이전로부터 키값을 던지면 Body 데이터들을 반환하도록 했네요.

Copy link

@kixtaxwax kixtaxwax left a comment

Choose a reason for hiding this comment

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

빠르게 피드백을 반영해주셨네요 ㅎㅎ덕분에 fizzbuzz 문제도 풀어보고 아주 재밌었습니다 다음 단계도 화이팅입니다~~

@kixtaxwax kixtaxwax merged commit e08cc99 into woowacourse:unluckyjung Aug 31, 2021
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