diff --git a/src/main/java/model/User.java b/src/main/java/model/User.java index b7abb7304..2ff841c2d 100644 --- a/src/main/java/model/User.java +++ b/src/main/java/model/User.java @@ -28,6 +28,10 @@ public String getName() { public String getEmail() { return email; } + + public static User build(String userId, String password, String name, String email) { + return new User(userId, password, name, email); + } @Override public String toString() { diff --git a/src/main/java/webserver/HttpRequest.java b/src/main/java/webserver/HttpRequest.java new file mode 100644 index 000000000..b19d9a220 --- /dev/null +++ b/src/main/java/webserver/HttpRequest.java @@ -0,0 +1,77 @@ +package webserver; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import util.HttpRequestUtils; +import util.IOUtils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class HttpRequest { + + private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); + + String httpMethod; + String urlPath; + + HttpRequestHeader httpRequestHeader = new HttpRequestHeader(); + Map requestBody = new HashMap<>(); + + public HttpRequest(InputStream in) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); + getRequestLine(br); + getHeadersFromReader(br); + getBodyFromReader(br); + } + + private void getRequestLine(BufferedReader br) throws Exception { + String line = br.readLine(); + + if(line == null) { + throw new Exception("헤더정보를 만들수 없습니다."); + } + + this.httpMethod = HttpRequestLine.getPath(line.split(" ")[0]); + this.urlPath = HttpRequestLine.getPath(line.split(" ")[1]); + } + + private void getHeadersFromReader(BufferedReader br) throws IOException { + String line; + while (!"".equals(line = br.readLine())) { + httpRequestHeader.add(line); + } + } + + private void getBodyFromReader(BufferedReader br) throws IOException { + if (Optional.ofNullable(this.getHeader("Content-Length")).isPresent()) { + setRequestBody(br, Integer.parseInt(this.getHeader("Content-Length"))); + } + } + + private void setRequestBody(BufferedReader br, int contentLength) throws IOException { + this.requestBody = HttpRequestUtils.parseQueryString(IOUtils.readData(br, contentLength)); + } + + public String getHeader(String headerType) { + return httpRequestHeader.getHeader(headerType); + } + + public String getHttpMethod() { + return this.httpMethod; + } + + public String getPath() { + return this.urlPath; + } + + public Map getRequestBodies() { + return requestBody; + } + +} diff --git a/src/main/java/webserver/HttpRequestHeader.java b/src/main/java/webserver/HttpRequestHeader.java new file mode 100644 index 000000000..7e32d60c7 --- /dev/null +++ b/src/main/java/webserver/HttpRequestHeader.java @@ -0,0 +1,18 @@ +package webserver; + +import util.HttpRequestUtils; + +import java.util.HashMap; +import java.util.Map; + +public class HttpRequestHeader { + Map requestHeaders = new HashMap<>(); + + public void add(String headerLine) { + requestHeaders.put(HttpRequestUtils.parseHeader(headerLine).getKey(),HttpRequestUtils.parseHeader(headerLine).getValue()); + } + + public String getHeader(String key) { + return this.requestHeaders.get(key); + } +} diff --git a/src/main/java/webserver/HttpRequestLine.java b/src/main/java/webserver/HttpRequestLine.java new file mode 100644 index 000000000..3f253d408 --- /dev/null +++ b/src/main/java/webserver/HttpRequestLine.java @@ -0,0 +1,8 @@ +package webserver; + +public class HttpRequestLine { + + public static String getPath(String requestLine) { + return requestLine.split(" ")[1]; + } +} diff --git a/src/main/java/webserver/RequestHandler.java b/src/main/java/webserver/RequestHandler.java index 90195ec4e..4ae248cb6 100644 --- a/src/main/java/webserver/RequestHandler.java +++ b/src/main/java/webserver/RequestHandler.java @@ -1,13 +1,25 @@ package webserver; +import java.io.BufferedReader; import java.io.DataOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import db.DataBase; +import model.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import util.HttpRequestUtils; +import util.IOUtils; public class RequestHandler extends Thread { private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); @@ -23,16 +35,74 @@ public void run() { connection.getPort()); try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { + + HttpRequest httpRequest = new HttpRequest(in); + + String http_method = httpRequest.getHttpMethod(); + String target_url = httpRequest.getPath(); + // TODO 사용자 요청에 대한 처리는 이 곳에 구현하면 된다. DataOutputStream dos = new DataOutputStream(out); - byte[] body = "Hello World".getBytes(); - response200Header(dos, body.length); + byte[] body = new byte[0]; + + if(target_url.equals("/user/create")) { + addUserToDataBase(newUserFromParams(httpRequest.getRequestBodies())); + response302Header(dos, "../../index.html"); + } else if (http_method.equals("POST") && target_url.equals("/user/login")) { + Map userData = httpRequest.getRequestBodies(); + if(isCorrectPassword(userData, findUserFromDataBase(userData.get("userId")))) { + response302HeaderForCookie(dos, "../../index.html","logined=true"); + } else { + response302HeaderForCookie(dos, "./login_failed.html","logined=false"); + } + + } else if(target_url.equals("/user/list")) { + if(isLogined(httpRequest.getHeader("Cookie"))) { + response302Header(dos, "./list.html"); + } else { + response302Header(dos, "./login.html"); + } + } else { + body = Files.readAllBytes(new File("./webapp" + target_url).toPath()); + + if(target_url.contains(".css")) { + response200HeaderForCss(dos, body.length); + } else { + response200Header(dos, body.length); + } + } + responseBody(dos, body); - } catch (IOException e) { + } catch (Exception e) { log.error(e.getMessage()); } } + private boolean isCorrectPassword(Map params, User user) { + return user.getPassword().equals(params.get("password")); + } + + private boolean isLogined(String cookie) { + return cookie.contains("logined=true"); + } + + private void addUserToDataBase(User user) { + DataBase.addUser(user); + } + + private User findUserFromDataBase(String userId) { + return DataBase.findUserById(userId); + } + + private User newUserFromParams(Map params) { + return User.build( + params.get("userId"), + params.get("password"), + params.get("name"), + params.get("email") + ); + } + private void response200Header(DataOutputStream dos, int lengthOfBodyContent) { try { dos.writeBytes("HTTP/1.1 200 OK \r\n"); @@ -44,6 +114,38 @@ private void response200Header(DataOutputStream dos, int lengthOfBodyContent) { } } + private void response302Header(DataOutputStream dos, String redirectUrl) { + try { + dos.writeBytes("HTTP/1.1 302 Found\r\n"); + dos.writeBytes(String.format("Location: %s\r\n", redirectUrl)); + dos.writeBytes("\r\n"); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + private void response302HeaderForCookie(DataOutputStream dos, String redirectUrl, String cookie) { + try { + dos.writeBytes("HTTP/1.1 302 Found\r\n"); + dos.writeBytes(String.format("Location: %s\r\n", redirectUrl)); + dos.writeBytes("Set-Cookie: " + cookie + "\r\n"); + dos.writeBytes("\r\n"); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + private void response200HeaderForCss(DataOutputStream dos, int lengthOfBodyContent) { + try { + dos.writeBytes("HTTP/1.1 200 OK \r\n"); + dos.writeBytes("Content-Type: text/css;charset=utf-8\r\n"); + dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n"); + dos.writeBytes("\r\n"); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + private void responseBody(DataOutputStream dos, byte[] body) { try { dos.write(body, 0, body.length); diff --git a/src/test/java/webserver/HttpRequestHeaderTest.java b/src/test/java/webserver/HttpRequestHeaderTest.java new file mode 100644 index 000000000..10a59083a --- /dev/null +++ b/src/test/java/webserver/HttpRequestHeaderTest.java @@ -0,0 +1,15 @@ +package webserver; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class HttpRequestHeaderTest { + + @Test + public void getHeader() { + HttpRequestHeader httpRequestHeader = new HttpRequestHeader(); + httpRequestHeader.add("Connection : keep-alive"); + assertEquals("keep-alive", httpRequestHeader.getHeader("Connection")); + } +} diff --git a/src/test/java/webserver/HttpRequestLineTest.java b/src/test/java/webserver/HttpRequestLineTest.java new file mode 100644 index 000000000..35ab122b3 --- /dev/null +++ b/src/test/java/webserver/HttpRequestLineTest.java @@ -0,0 +1,13 @@ +package webserver; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class HttpRequestLineTest { + + @Test + public void getPath() { + assertEquals("./index.html", HttpRequestLine.getPath("GET ./index.html")); + } +} diff --git a/src/test/java/webserver/HttpRequestTest.java b/src/test/java/webserver/HttpRequestTest.java new file mode 100644 index 000000000..8bc9359a5 --- /dev/null +++ b/src/test/java/webserver/HttpRequestTest.java @@ -0,0 +1,16 @@ +package webserver; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class HttpRequestTest { + + + private String testDirectory = "./src/main/resources"; + + @Test + public void 헤더를_통해_URI를_가져온다() { + + } +} \ No newline at end of file diff --git a/webapp/user/form.html b/webapp/user/form.html index 96fe1bd3a..ac2249628 100644 --- a/webapp/user/form.html +++ b/webapp/user/form.html @@ -75,7 +75,7 @@
-
+