-
Notifications
You must be signed in to change notification settings - Fork 16
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조 - 함형주 #1
base: main
Are you sure you want to change the base?
Changes from 35 commits
879debe
7a02b41
a422d16
4250440
32861a6
202a111
f7da28c
3ed8c66
7653244
e32cd86
1b553cb
6721615
7766209
0b04e80
54b8f28
56b06b1
93fc568
e0868a2
ddde0a2
cfeb215
00ca9c7
5769f27
b81de08
d469f60
80f80ca
8a346dc
96a95fc
627c167
17b61b6
ce275e7
efbc6a0
2558948
9a710cc
b0b5aa6
b586e20
9a5c1df
ce1c5fc
4fc6fad
c8a159d
4c9e430
b510c73
ae3cc99
ddec33f
ebefa54
237c3bd
d1724b9
aa8ed95
9bfb4b0
082b9f1
e462b34
82c3c28
112cf8b
adde06d
8dcdb56
13d32e5
2163fd7
f9ce2d8
90f18ae
ead89af
e9e3423
8891954
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package webserver.CustomHandler; | ||
|
||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
import webserver.constant.Http; | ||
|
||
import java.io.IOException; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class CssHandler implements CustomHandler{ | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
response.setHeader(CONTENT_TYPE.getValue(), TEXT_CSS.getValue()); | ||
return "Content-Type".getBytes(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package webserver.CustomHandler; | ||
|
||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public interface CustomHandler { | ||
|
||
byte[] process(HttpRequest request, HttpResponse response) throws IOException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package webserver.CustomHandler; | ||
|
||
import http.util.IOUtils; | ||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
import webserver.constant.Http; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class IndexHandler implements CustomHandler{ | ||
|
||
private final File file = new File(WEBAPP.getValue() + INDEX.getValue()); | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
BufferedReader fr = new BufferedReader(new FileReader(file)); | ||
String fileData = IOUtils.readData(fr, (int) file.length()); | ||
return fileData.getBytes(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package webserver.CustomHandler; | ||
|
||
import http.util.IOUtils; | ||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
import webserver.constant.Http; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class LoginFailFormHandler implements CustomHandler{ | ||
|
||
private final File file = new File(WEBAPP.getValue() + USER_LOGIN_FAILED.getValue()); | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
BufferedReader fr = new BufferedReader(new FileReader(file)); | ||
String fileData = IOUtils.readData(fr, (int) file.length()); | ||
return fileData.getBytes(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package webserver.CustomHandler; | ||
|
||
import http.util.IOUtils; | ||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class LoginFormHandler implements CustomHandler{ | ||
|
||
private final File file = new File(WEBAPP.getValue() + LOGIN_FORM.getValue()); | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
BufferedReader fr = new BufferedReader(new FileReader(file)); | ||
String fileData = IOUtils.readData(fr, (int) file.length()); | ||
return fileData.getBytes(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package webserver.CustomHandler; | ||
|
||
import db.MemoryUserRepository; | ||
import model.User; | ||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
import webserver.constant.Http; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class LoginHandler implements CustomHandler{ | ||
|
||
private final MemoryUserRepository repository; | ||
|
||
public LoginHandler() { | ||
repository = MemoryUserRepository.getInstance(); | ||
} | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
try { | ||
Map<String, String> paramMap = request.getParamMap(); | ||
User findById = repository.findUserById(paramMap.get("userId")); | ||
return passwordCheck(paramMap, findById, response); | ||
} catch (NullPointerException e) { | ||
setStatusCodeAndLocation(USER_LOGIN_FAILED.getValue(), response); | ||
return USER_LOGIN_FAILED.getValue().getBytes(); | ||
} | ||
} | ||
|
||
private static byte[] passwordCheck(Map<String, String> paramMap, User findById, HttpResponse response) { | ||
if (!findById.getPassword().equals(paramMap.get("password"))) { | ||
return USER_LOGIN_FAILED.getValue().getBytes(); | ||
} | ||
setStatusCodeAndLocation(INDEX.getValue(), response); | ||
response.setHeader(SET_COOKIE.getValue(), LOGINED_TRUE.getValue()); | ||
return INDEX.getValue().getBytes(); | ||
} | ||
|
||
private static void setStatusCodeAndLocation(String location, HttpResponse response) { | ||
response.setStatusCode(FOUND.getValue()); | ||
response.setHeader(LOCATION.getValue(), location); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package webserver.CustomHandler; | ||
|
||
import http.util.IOUtils; | ||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class SignUpFormHandler implements CustomHandler{ | ||
|
||
private final File file = new File(WEBAPP.getValue() + SIGNUP_FORM.getValue()); | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
BufferedReader fr = new BufferedReader(new FileReader(file)); | ||
String fileData = IOUtils.readData(fr, (int) file.length()); | ||
return fileData.getBytes(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package webserver.CustomHandler; | ||
|
||
import db.MemoryUserRepository; | ||
import model.User; | ||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
import webserver.constant.Http; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class SignUpHandler implements CustomHandler { | ||
|
||
private final MemoryUserRepository repository; | ||
|
||
public SignUpHandler() { | ||
repository = MemoryUserRepository.getInstance(); | ||
} | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
Map<String, String> paramMap = request.getParamMap(); | ||
|
||
User user = new User( | ||
paramMap.get("userId"), | ||
paramMap.get("password"), | ||
paramMap.get("name"), | ||
paramMap.get("email") | ||
); | ||
|
||
repository.addUser(user); | ||
|
||
setStatusCodeAndLocation(INDEX.getValue(), response); | ||
|
||
return INDEX.getValue().getBytes(); | ||
} | ||
|
||
private static void setStatusCodeAndLocation(String location, HttpResponse response) { | ||
response.setStatusCode(FOUND.getValue()); | ||
response.setHeader(LOCATION.getValue(), location); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package webserver.CustomHandler; | ||
|
||
import http.util.IOUtils; | ||
import webserver.HttpRequest; | ||
import webserver.HttpResponse; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class UserListHandler implements CustomHandler{ | ||
|
||
private final File file = new File(WEBAPP.getValue() + LIST.getValue()); | ||
|
||
@Override | ||
public byte[] process(HttpRequest request, HttpResponse response) throws IOException { | ||
System.out.println("진입"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sysout보단 Logger를 활용해보세요! |
||
Map<String, String> headerMap = request.getHeaderMap(); | ||
String cookieValue = headerMap.get(COOKIE.getValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. headerMap을 getter로 꺼내와서 값을 읽어오는 것보단 request.getHeader(COOKIE.getValue()) 이런 식으로 하는 처리하는 것이 좋아요. |
||
String[] split = cookieValue.split(";"); | ||
System.out.println("split[0] = " + split[0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cookieValue를 parsing하는 일은 util 클래스를 하나 만들어도 더욱 좋을 것 같습니다!🧐 |
||
if (split[0].equals(LOGINED_TRUE.getValue())) { | ||
BufferedReader fr = new BufferedReader(new FileReader(file)); | ||
String fileData = IOUtils.readData(fr, (int) file.length()); | ||
return fileData.getBytes(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 파일로부터 데이터를 읽어와서 ~ 해주는 작업을 HttpResponse에 캡슐화해보세요! |
||
} | ||
|
||
setStatusCodeAndLocation(response); | ||
return INDEX.getValue().getBytes(); | ||
} | ||
|
||
private static void setStatusCodeAndLocation(HttpResponse response) { | ||
response.setStatusCode(FOUND.getValue()); | ||
response.setHeader(LOCATION.getValue(), INDEX.getValue()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package webserver; | ||
|
||
import webserver.CustomHandler.*; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static webserver.constant.Http.*; | ||
|
||
public class FrontHandler { | ||
|
||
private static final Map<String, CustomHandler> handlerMappingMap = new HashMap<>(); | ||
private static FrontHandler frontHandler; | ||
|
||
private FrontHandler() { | ||
} | ||
|
||
public static FrontHandler getInstance() { | ||
if (handlerMappingMap.isEmpty()) { | ||
frontHandler = new FrontHandler(); | ||
handlerMapping(); | ||
return frontHandler; | ||
} | ||
return frontHandler; | ||
} | ||
|
||
private static void handlerMapping() { | ||
handlerMappingMap.put(INDEX.getValue(), new IndexHandler()); | ||
handlerMappingMap.put("/", new IndexHandler()); | ||
handlerMappingMap.put(SIGNUP_FORM.getValue(), new SignUpFormHandler()); | ||
handlerMappingMap.put(SIGNUP.getValue(), new SignUpHandler()); | ||
handlerMappingMap.put(LOGIN_FORM.getValue(), new LoginFormHandler()); | ||
handlerMappingMap.put(USER_LOGIN_FAILED.getValue(), new LoginFailFormHandler()); | ||
handlerMappingMap.put(LOGIN.getValue(), new LoginHandler()); | ||
handlerMappingMap.put(USER_LIST.getValue(), new UserListHandler()); | ||
handlerMappingMap.put(CSS.getValue(), new CssHandler()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단순히 파일 내용을 읽어오는 작업만 있는 핸들러들은 FowardHandler를 만들어서 FowardHandler(String url)로 하나로 묶을 수 있을 것 같아요! |
||
|
||
public void service(HttpRequest request, HttpResponse response) throws IOException { | ||
|
||
String requestUri = request.getRequestUri(); | ||
CustomHandler handler; | ||
if (requestUri.contains(CSS.getValue())) handler = handlerMappingMap.get(CSS.getValue()); | ||
else handler = handlerMappingMap.get(request.getRequestUri()); | ||
|
||
byte[] bytes = handler.process(request, response); | ||
|
||
response.createStartLine(); | ||
response.createHeader(); | ||
response.responseBody(bytes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런식으로 3가지 메서드를 호출하게 하면, 다른 개발자가 HttpResponse 객체를 사용한다고 했을 때 하나만 빼먹을 수 있을 것 같지 않나요?🧐 또 redirect 처리도 보다 자유롭게 할 수 있습니다. |
||
|
||
} | ||
} |
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.
다른 곳에서도 redirect가 필요하다면, 이 코드가 똑같이 또 필요하지 않을까요?
HttpResponse에 sendRedirect() 메서드를 만들어보세요!