-
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
base: coco-k
Are you sure you want to change the base?
Changes from 16 commits
8845ec6
60eccbe
222894d
ebc07f7
45b9091
3494f03
7501bb7
0c496fb
6bd7c82
570fee7
5c3547f
63aec98
7729bf1
66a9237
f8796f3
e84df0d
168ef41
81bdd6a
aa77c93
1473b04
ef37ef5
a386c91
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,10 @@ | ||
| package controller; | ||
|
|
||
| import http.HttpRequest; | ||
| import http.HttpResponse; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public interface Controller { | ||
| void service(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException; | ||
| } | ||
| 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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package controller; | ||
|
|
||
| import db.DataBase; | ||
| import http.HttpRequest; | ||
| import http.HttpResponse; | ||
| import model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| 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"); | ||
| } | ||
| } |
| 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()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package controller; | ||
|
|
||
| import http.HttpRequest; | ||
| import http.HttpResponse; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| 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"); | ||
| } else { | ||
|
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.
|
||
| httpResponse.redirect("/user/login.html"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package controller; | ||
|
|
||
| import db.DataBase; | ||
| import http.HttpRequest; | ||
| import http.HttpResponse; | ||
| import model.User; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| 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) { | ||
| httpResponse.addHeader("Set-Cookie", "logined=false; Path=/"); | ||
| httpResponse.redirect("/user/login_failed.html"); | ||
| } else if (user.checkPassword(httpRequest.data("password"))) { | ||
| httpResponse.addHeader("Set-Cookie", "logined=true; Path=/"); | ||
| httpResponse.redirect("/index.html"); | ||
| } else { | ||
|
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. 이번 미션에서 |
||
| httpResponse.addHeader("Set-Cookie", "logined=false; Path=/"); | ||
| httpResponse.redirect("/user/login_failed.html"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package webserver; | ||
| package http; | ||
|
|
||
| public enum HttpMethod { | ||
| GET, | ||
|
|
||
| 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(){ | ||
| // | ||
| // } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| 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()); | ||
|
|
||
| if (url.endsWith(".css")) { | ||
| addHeader("Content-Type", "text/css;charset=utf-8"); | ||
| } else { | ||
| addHeader("Content-Type", "text/html;charset=utf-8"); | ||
| } | ||
|
Comment on lines
+26
to
+29
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. 리팩토링 제안 |
||
| 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"); | ||
| } | ||
|
|
||
| } | ||
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.
👍