-
Notifications
You must be signed in to change notification settings - Fork 24
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
3주차 미션 / 서버 3조 장현준 #10
Open
buzz0331
wants to merge
13
commits into
Konkuk-KUIT:main
Choose a base branch
from
buzz0331:buzz0331
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4ac2dd7
Feature: 1단계 완료
buzz0331 2f479fe
Feature: Enum 클래스 적용
buzz0331 b779544
Refactor: Enum 리팩토링 및 패키지 변경
buzz0331 03df4fa
Fix: html문서 a태그 링크 수정
buzz0331 b3a0b70
Refactor: HttpRequest 분리
buzz0331 7b348a3
Refactor: HttpRequest 적용
buzz0331 6d1ede4
Bug: 무한로딩 이슈 해결 (static 변수 삭제)
buzz0331 9d556cb
Test: HttpRequest 관련 테스트코드 작성
buzz0331 1141061
Refactor: HttpResponse 분리 및 적용
buzz0331 c92f43d
Refactor: 코드 단순화
buzz0331 450d1f5
Test: HttpResponse 테스트코드 작성
buzz0331 0d1d736
Refactor: Controller를 통한 분기점 처리
buzz0331 7f40e89
Refactor: 최종 리팩토링
buzz0331 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package constant; | ||
|
||
public enum Format { | ||
HTML("html"), | ||
CSS("css"); | ||
|
||
private String format; | ||
|
||
Format(String format) { | ||
this.format = format; | ||
} | ||
|
||
public String getFormat() { | ||
return format; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package constant; | ||
|
||
public enum HttpHeader { | ||
CONTENT_TYPE("Content-Type"), | ||
LOCATION("Location"), | ||
CONTENT_LENGTH("Content-Length"), | ||
COOKIE("Cookie"), | ||
SET_COOKIE("Set-Cookie") | ||
; | ||
|
||
private final String header; | ||
|
||
HttpHeader(String header) { | ||
this.header = header; | ||
} | ||
|
||
public String getHeader() { | ||
return header; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package constant; | ||
|
||
public enum HttpMethod { | ||
GET("GET"), POST("POST"); | ||
|
||
private final String method; | ||
|
||
HttpMethod(String method) { | ||
this.method = method; | ||
} | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public boolean isEqual(String method) { | ||
return this.method.equals(method); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package constant; | ||
|
||
public enum HttpStatus { | ||
REDIRECT("302 Found"), | ||
OK("200 OK"); | ||
|
||
private final String status; | ||
|
||
HttpStatus(String status) { | ||
this.status = status; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package constant; | ||
|
||
public enum QueryKey { | ||
USER_ID("userId"), | ||
PASSWORD("password"), | ||
NAME("name"), | ||
EMAIL("email"); | ||
|
||
private final String key; | ||
|
||
QueryKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package constant; | ||
|
||
public enum URL { | ||
ROOT("./webapp"), | ||
INDEX("/index.html"), | ||
LOGIN_HTML("/user/login.html"), | ||
LOGIN("/user/login"), | ||
LIST("/user/list.html"), | ||
USER_LIST("/user/userList"), | ||
LOGIN_FAILED("/user/login_failed.html"), | ||
SIGNUP("/user/signup"), | ||
CSS("/css/styles.css") | ||
; | ||
|
||
private final String url; | ||
|
||
URL(String url) { | ||
this.url = url; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package controller; | ||
|
||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Controller { | ||
void execute(HttpRequest request, HttpResponse response) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package controller; | ||
|
||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public class CssController implements Controller { | ||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws IOException { | ||
response.setCss(request.getUrl()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package controller; | ||
|
||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public class IndexController implements Controller { | ||
|
||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws IOException { | ||
response.forward(request.getUrl()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
import model.User; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static constant.QueryKey.PASSWORD; | ||
import static constant.QueryKey.USER_ID; | ||
import static constant.URL.INDEX; | ||
import static constant.URL.LOGIN_FAILED; | ||
|
||
public class LoginController implements Controller { | ||
|
||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws IOException { | ||
Map<String, String> loginInfo = request.getQueryParametersfromBody(); | ||
User findUser = findUser(loginInfo); | ||
if (findUser != null && findUser.getPassword().equals(loginInfo.get(PASSWORD.getKey()))) { | ||
response.redirect(INDEX.getUrl(), true); | ||
return; | ||
} | ||
response.redirect(LOGIN_FAILED.getUrl(), false); | ||
} | ||
|
||
private static User findUser(Map<String, String> loginInfo) { | ||
MemoryUserRepository memoryUserRepository = MemoryUserRepository.getInstance(); | ||
return memoryUserRepository.findUserById(loginInfo.get(USER_ID.getKey())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
import model.User; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static constant.QueryKey.*; | ||
import static constant.QueryKey.EMAIL; | ||
import static constant.URL.INDEX; | ||
|
||
public class SignUpController implements Controller { | ||
|
||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws IOException { | ||
createNewUser(request.getQueryParameters()); | ||
response.redirect(INDEX.getUrl(), request.isLogin()); | ||
} | ||
|
||
private void createNewUser(Map<String, String> queryParameter) { | ||
//새로운 User 객체 생성 후 Repository에 추가 | ||
User newUser = new User(queryParameter.get(USER_ID.getKey()), queryParameter.get(PASSWORD.getKey()), queryParameter.get(NAME.getKey()), queryParameter.get(EMAIL.getKey())); | ||
MemoryUserRepository memoryUserRepository = MemoryUserRepository.getInstance(); | ||
memoryUserRepository.addUser(newUser); | ||
// System.out.println("newUser_name = " + memoryUserRepository.findUserById(queryParameter.get("userId")).getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package controller; | ||
|
||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
import static constant.URL.LIST; | ||
import static constant.URL.LOGIN_HTML; | ||
|
||
public class UserListController implements Controller { | ||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws IOException { | ||
if (request.isLogin()) { | ||
response.redirect(LIST.getUrl(), request.isLogin()); | ||
return; | ||
} | ||
response.redirect(LOGIN_HTML.getUrl(), request.isLogin()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package http.request; | ||
|
||
import constant.URL; | ||
import http.util.HttpRequestUtils; | ||
import http.util.IOUtils; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static constant.HttpMethod.GET; | ||
import static constant.HttpMethod.POST; | ||
import static http.util.IOUtils.readData; | ||
|
||
public class HttpRequest { | ||
private final HttpRequestStartLine httpRequestStartLine; | ||
private final HttpRequestHeader httpRequestHeader; | ||
private final String body; | ||
|
||
private HttpRequest(HttpRequestStartLine httpRequestStartLine, HttpRequestHeader httpRequestHeader, String body) { | ||
this.httpRequestStartLine = httpRequestStartLine; | ||
this.httpRequestHeader = httpRequestHeader; | ||
this.body = body; | ||
} | ||
|
||
public static HttpRequest from(BufferedReader br) throws IOException { | ||
//startLine 추출 | ||
String startLine = br.readLine(); | ||
|
||
HttpRequestStartLine requestStartLine = HttpRequestStartLine.from(startLine); | ||
HttpRequestHeader requestHeader = HttpRequestHeader.from(br); | ||
String body = readBody(br, requestHeader); | ||
|
||
return new HttpRequest(requestStartLine, requestHeader, body); | ||
} | ||
|
||
private static String readBody(BufferedReader br, HttpRequestHeader requestHeader) throws IOException { | ||
if (!requestHeader.isEmptyContent()) { | ||
return readData(br, requestHeader.getContentLength()); | ||
} | ||
return ""; | ||
} | ||
|
||
public boolean isGetMethod() { | ||
return GET.isEqual(httpRequestStartLine.getMethod()); | ||
} | ||
|
||
public boolean isPostMethod() { | ||
return POST.isEqual(httpRequestStartLine.getMethod()); | ||
} | ||
|
||
public String getUrl() { | ||
if(httpRequestStartLine.getUrl().equals("/")) { | ||
return URL.INDEX.getUrl(); | ||
} | ||
return httpRequestStartLine.getUrl(); | ||
} | ||
|
||
public int getContentLength() { | ||
return httpRequestHeader.getContentLength(); | ||
} | ||
|
||
public boolean isLogin() { | ||
return httpRequestHeader.getLoginStatus(); | ||
} | ||
|
||
|
||
public Map<String, String> getQueryParameters() { | ||
if (isGetMethod()) { | ||
//url에서 queryString 분리 | ||
return getQueryParametersfromUrl(); | ||
} | ||
if (isPostMethod()) { | ||
//body에서 queryString 추출 | ||
return getQueryParametersfromBody(); | ||
} | ||
else { | ||
System.out.println("올바른 데이터 전송 방식이 아닙니다."); | ||
return null; | ||
} | ||
} | ||
|
||
public Map<String, String> getQueryParametersfromBody() { | ||
//POST 메서드인 경우 | ||
return HttpRequestUtils.parseQueryParameter(body); | ||
} | ||
public Map<String, String> getQueryParametersfromUrl() { | ||
//GET 메서드인 경우 | ||
String url = getUrl(); | ||
String queryString = url.substring(url.lastIndexOf("?") + 1); | ||
return HttpRequestUtils.parseQueryParameter(queryString); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
repository 객체를 생성자 주입으로 받아서 사용해보는 건 어떨까요?