-
Notifications
You must be signed in to change notification settings - Fork 15
[ coco,K ] HTTP 웹 서버 2단계 - 리팩토링 #8
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
Open
choigiseong
wants to merge
22
commits into
codesquad-members-2021:coco-k
Choose a base branch
from
choigiseong:dev
base: coco-k
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
22 commits
Select commit
Hold shift + click to select a range
8845ec6
refactor : HttpRequest
choigiseong 60eccbe
Merge pull request #18 from ChoiGiSung/refactor-HttpRequest
kihyuk-sung 222894d
test: HttpResponse
kihyuk-sung ebc07f7
test : HttpResponse
choigiseong 45b9091
test : read css
choigiseong 3494f03
test : HttpResponse redirect
choigiseong 7501bb7
feat: sendRedirect 구현
choigiseong 0c496fb
test : HttpResponse Cookie
choigiseong 6bd7c82
feat : redirect with cookie
choigiseong 570fee7
refactor : HttpResponse
choigiseong 5c3547f
Merge pull request #19 from ChoiGiSung/http-response
kihyuk-sung 63aec98
test : ControllerKey
choigiseong 7729bf1
feat : ControllerKey override equals hashCode
choigiseong 66a9237
refactor : Use Controller in RequestHandler
choigiseong f8796f3
Merge pull request #20 from ChoiGiSung/controller
kihyuk-sung e84df0d
Update README.md
choigiseong 168ef41
refactor: Remove else
kihyuk-sung 81bdd6a
Merge pull request #24 from ChoiGiSung/remove-else
choigiseong aa77c93
test : RequestMapping test
choigiseong 1473b04
feat : RequestMapping using reflactions
choigiseong ef37ef5
refactor : HandlerAdapter
choigiseong a386c91
Merge pull request #25 from ChoiGiSung/mapping
kihyuk-sung 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 hidden or 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 hidden or 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 annotation; | ||
|
|
||
| import http.HttpMethod; | ||
| import http.HttpRequest; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.TYPE) | ||
| public @interface RequestMapping { | ||
| String path(); | ||
| HttpMethod method(); | ||
| } |
This file contains hidden or 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.HttpRequest; | ||
| import http.HttpResponse; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public interface Controller { | ||
| void service(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException; | ||
| } | ||
This file contains hidden or 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,29 @@ | ||
| package controller; | ||
|
|
||
| import http.HttpMethod; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class ControllerKey { | ||
|
|
||
| private HttpMethod method; | ||
| private String url; | ||
|
|
||
| public ControllerKey(HttpMethod method, String url) { | ||
| this.method = method; | ||
| this.url = url; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| ControllerKey that = (ControllerKey) o; | ||
| return method == that.method && Objects.equals(url, that.url); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(method, url); | ||
| } | ||
| } |
This file contains hidden or 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,25 @@ | ||
| package controller; | ||
|
|
||
| import annotation.RequestMapping; | ||
| import db.DataBase; | ||
| import http.HttpMethod; | ||
| import http.HttpRequest; | ||
| import http.HttpResponse; | ||
| import model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @RequestMapping(path = "/user/create", method = HttpMethod.POST) | ||
| public class CreateUserController implements Controller { | ||
| @Override | ||
| public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
| User user = new User( | ||
| httpRequest.data("userId"), | ||
| httpRequest.data("password"), | ||
| httpRequest.data("name"), | ||
| httpRequest.data("email") | ||
| ); | ||
| DataBase.addUser(user); | ||
| httpResponse.redirect("/index.html"); | ||
| } | ||
| } |
This file contains hidden or 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.HttpRequest; | ||
| import http.HttpResponse; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class DefaultController implements Controller { | ||
|
|
||
| @Override | ||
| public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
| httpResponse.forward(httpRequest.getUrl()); | ||
| } | ||
| } |
This file contains hidden or 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 annotation.RequestMapping; | ||
| import http.HttpMethod; | ||
| import http.HttpRequest; | ||
| import http.HttpResponse; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @RequestMapping(path = "/user/list", method = HttpMethod.GET) | ||
| public class ListUserController implements Controller { | ||
| @Override | ||
| public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
| if ("true".equals(httpRequest.cookie("logined"))) { | ||
| httpResponse.forward("/user/list.html"); | ||
| return; | ||
| } | ||
| httpResponse.redirect("/user/login.html"); | ||
| } | ||
| } |
This file contains hidden or 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,27 @@ | ||
| package controller; | ||
|
|
||
| import annotation.RequestMapping; | ||
| import db.DataBase; | ||
| import http.HttpMethod; | ||
| import http.HttpRequest; | ||
| import http.HttpResponse; | ||
| import model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @RequestMapping(path = "/user/login", method = HttpMethod.POST) | ||
| public class LoginController implements Controller { | ||
| @Override | ||
| public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
| User user = DataBase.findUserById(httpRequest.data("userId")); | ||
|
|
||
| if (user != null && user.checkPassword(httpRequest.data("password"))) { | ||
| httpResponse.addHeader("Set-Cookie", "logined=true; Path=/"); | ||
| httpResponse.redirect("/index.html"); | ||
| return; | ||
| } | ||
|
|
||
| httpResponse.addHeader("Set-Cookie", "logined=false; Path=/"); | ||
| httpResponse.redirect("/user/login_failed.html"); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
src/main/java/webserver/HttpMethod.java → src/main/java/http/HttpMethod.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package webserver; | ||
| package http; | ||
|
|
||
| public enum HttpMethod { | ||
| GET, | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package webserver; | ||
| package http; | ||
|
|
||
| import util.HttpRequestUtils; | ||
| import util.IOUtils; | ||
|
|
@@ -22,15 +22,32 @@ public class HttpRequest { | |
| private String body; | ||
|
|
||
| private HttpRequest() { | ||
|
|
||
| } | ||
|
|
||
|
|
||
| public String getUrl() { | ||
| return url; | ||
| } | ||
|
|
||
| public void addStartLine(String buffer) { | ||
| public static HttpRequest of(InputStream in) throws IOException { | ||
| HttpRequest httpRequest = new HttpRequest(); | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(in)); | ||
| String buffer; | ||
|
|
||
| buffer = br.readLine(); | ||
| httpRequest.addStartLine(buffer); | ||
| while (!(buffer = br.readLine()).equals("")) { | ||
| httpRequest.addHeaders(buffer); | ||
| } | ||
|
|
||
| String contentLength = httpRequest.header("Content-Length"); | ||
| if (contentLength != null) { | ||
| httpRequest.addBody(IOUtils.readData(br, Integer.parseInt(contentLength))); | ||
| } | ||
|
|
||
| return httpRequest; | ||
| } | ||
|
Comment on lines
+31
to
+48
Contributor
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. 스태틱 팩토리 메서드에 이렇게 로직이 많은 것은 크게 추천하고 싶진 않습니다. |
||
|
|
||
| private void addStartLine(String buffer) { | ||
| String[] startLine = buffer.split(" "); | ||
| method = HttpMethod.valueOf(startLine[0].toUpperCase()); | ||
| url = startLine[1]; | ||
|
|
@@ -44,7 +61,7 @@ public void addStartLine(String buffer) { | |
| } | ||
| } | ||
|
|
||
| public void addHeaders(String buffer) { | ||
| private void addHeaders(String buffer) { | ||
| HttpRequestUtils.Pair pair = HttpRequestUtils.parseHeader(buffer); | ||
| headers.put(pair.getKey(), pair.getValue()); | ||
| String cookies; | ||
|
|
@@ -53,31 +70,6 @@ public void addHeaders(String buffer) { | |
| } | ||
| } | ||
|
|
||
| public static HttpRequest of(InputStream in) { | ||
| HttpRequest httpRequest = new HttpRequest(); | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(in)); | ||
| String buffer; | ||
| try { | ||
| buffer = br.readLine(); | ||
| httpRequest.addStartLine(buffer); | ||
| while (!(buffer = br.readLine()).equals("")) { | ||
| httpRequest.addHeaders(buffer); | ||
| } | ||
|
|
||
| String contentLength = httpRequest.header("Content-Length"); | ||
| if (contentLength != null) { | ||
| httpRequest.addBody(IOUtils.readData(br, Integer.parseInt(contentLength))); | ||
| } | ||
|
|
||
|
|
||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
|
|
||
| return httpRequest; | ||
| } | ||
|
|
||
| private void addBody(String readData) { | ||
| body = readData; | ||
| if (method == HttpMethod.POST) { | ||
|
|
@@ -96,15 +88,12 @@ public String cookie(String key) { | |
| return cookies.get(key); | ||
| } | ||
|
|
||
| public HttpMethod getMethod() { | ||
| public HttpMethod method() { | ||
| return method; | ||
| } | ||
|
|
||
| public String data(String key) { | ||
| return data.get(key); | ||
| } | ||
|
|
||
| // public boolean loginCookie(){ | ||
| // | ||
| // } | ||
| } | ||
This file contains hidden or 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,61 @@ | ||
| package http; | ||
|
|
||
| import java.io.DataOutputStream; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.file.Files; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class HttpResponse { | ||
|
|
||
| private DataOutputStream dos; | ||
|
|
||
| private String startLine; | ||
| private Map<String, String> headers = new HashMap<>(); | ||
|
|
||
| public HttpResponse(OutputStream os) { | ||
| dos = new DataOutputStream(os); | ||
| } | ||
|
|
||
| public void forward(String url) throws IOException { | ||
| startLine = "HTTP/1.1 200 OK \r\n"; | ||
| byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath()); | ||
|
|
||
| String contentTypeValue = "text/html;charset=utf-8"; | ||
| if (url.endsWith(".css")) { | ||
| contentTypeValue = "text/css;charset=utf-8"; | ||
| } | ||
|
|
||
| addHeader("Content-Type", contentTypeValue); | ||
| addHeader("Content-Length", String.valueOf(body.length)); | ||
|
|
||
| processHeaders(); | ||
| responseBody(body); | ||
| } | ||
|
|
||
| private void responseBody(byte[] body) throws IOException { | ||
| dos.write(body, 0, body.length); | ||
| dos.flush(); | ||
| } | ||
|
|
||
| public void redirect(String url) throws IOException { | ||
| startLine = "HTTP/1.1 302 FOUND \r\n"; | ||
| addHeader("Location", url); | ||
| processHeaders(); | ||
| } | ||
|
|
||
| public void addHeader(String header, String value) { | ||
| headers.put(header, value); | ||
| } | ||
|
|
||
| private void processHeaders() throws IOException { | ||
| dos.writeBytes(startLine); | ||
| for (Map.Entry<String, String> header : headers.entrySet()) { | ||
| dos.writeBytes(header.getKey() + ": " + header.getValue() + "\r\n"); | ||
| } | ||
| dos.writeBytes("\r\n"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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,34 @@ | ||
| package webserver; | ||
|
|
||
| import annotation.RequestMapping; | ||
| import controller.Controller; | ||
| import controller.ControllerKey; | ||
| import controller.DefaultController; | ||
| import org.reflections.Reflections; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public class HandlerAdapter { | ||
| private static final Controller defaultController = new DefaultController(); | ||
| private static final Map<ControllerKey, Controller> controllerMap = new HashMap<>(); | ||
|
|
||
| public HandlerAdapter() { | ||
| Reflections reflections = new Reflections("controller"); | ||
| Set<Class<?>> requestMappingControllers = reflections.getTypesAnnotatedWith(RequestMapping.class); | ||
| for (Class<?> controller : requestMappingControllers) { | ||
| RequestMapping annotation = controller.getAnnotation(RequestMapping.class); | ||
| try { | ||
| controllerMap.put(new ControllerKey(annotation.method(), annotation.path()), (Controller) controller.newInstance()); | ||
| } catch (InstantiationException | IllegalAccessException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| public Controller controller(ControllerKey key){ | ||
| return controllerMap.getOrDefault(key,defaultController); | ||
| } | ||
| } |
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.
👍