diff --git a/src/main/java/webserver/RequestHandler.java b/src/main/java/webserver/RequestHandler.java index 90195ec4e..2d9beae51 100644 --- a/src/main/java/webserver/RequestHandler.java +++ b/src/main/java/webserver/RequestHandler.java @@ -1,13 +1,19 @@ package webserver; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Collection; +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); @@ -19,25 +25,165 @@ public RequestHandler(Socket connectionSocket) { } public void run() { - log.debug("New Client Connect! Connected IP : {}, Port : {}", connection.getInetAddress(), - connection.getPort()); try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { - // TODO 사용자 요청에 대한 처리는 이 곳에 구현하면 된다. - DataOutputStream dos = new DataOutputStream(out); - byte[] body = "Hello World".getBytes(); - response200Header(dos, body.length); - responseBody(dos, body); + BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); + String line = br.readLine(); + log.debug("===== request line : {} =====", line); // 요청 라인 + + if (line == null) { + return; + } + + String[] tokens = line.split(" "); + int contentLength = 0; + boolean logined = false; + while (!line.equals("")) { + line = br.readLine(); + log.debug("header : {}", line); + + if (line.contains("Content-Length")) { + contentLength = getContentLength(line); + } + + if (line.contains("Cookie")) { + logined = isLogin(line); + } + } + + String url = tokens[1]; + + if (url.equals("/user/create")) { + String body = IOUtils.readData(br, contentLength); + Map params = HttpRequestUtils.parseQueryString(body); + + User user = new User( + params.get("userId"), + params.get("password"), + params.get("name"), + params.get("email") + ); + DataBase.addUser(user); + log.debug("user : {}", user); + + DataOutputStream dos = new DataOutputStream(out); + response302Header(dos, "/index.html"); + } else if (url.equals("/user/login")) { + String body = IOUtils.readData(br, contentLength); + Map params = HttpRequestUtils.parseQueryString(body); + + User user = DataBase.findUserById(params.get("userId")); + + if (user == null) { + responseResource(out, "/user/login_failed.html"); + return; + } + + if (user.getPassword().equals(params.get("password"))) { + DataOutputStream dos = new DataOutputStream(out); + response302LoginSuccessHeader(dos); + } else { + responseResource(out, "/user/login_failed.html"); + } + + } else if (url.equals("/user/list")) { + if (!logined) { + responseResource(out, "/user/login.html"); + return; + } + + Collection users = DataBase.findAll(); + StringBuilder sb = new StringBuilder(); + sb.append(""); + for (User user : users) { + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + } + sb.append("
" + user.getUserId() + "" + user.getName() + "" + user.getEmail() + "
"); + + byte[] body = sb.toString().getBytes(StandardCharsets.UTF_8); + DataOutputStream dos = new DataOutputStream(out); + response200Header(dos, body.length); + responseBody(dos, body); + } else if (url.endsWith(".css")) { + DataOutputStream dos = new DataOutputStream(out); + byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath()); + response200CssHeader(dos, body.length); + responseBody(dos, body); + } else { + responseResource(out, url); + } + + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + private void response200CssHeader(DataOutputStream dos, int lengthOfBodyContent) { + try { + dos.writeBytes("HTTP/1.1 200 OK \r\n"); + dos.writeBytes("Content-Type: text/css \r\n"); + dos.writeBytes("Content-Length: " + lengthOfBodyContent + " \r\n"); + dos.writeBytes("\r\n"); } catch (IOException e) { log.error(e.getMessage()); } } + private boolean isLogin(String line) { + String[] tokens = line.split(":"); + Map cookies = HttpRequestUtils.parseCookies(tokens[1].trim()); + String value = cookies.get("logined"); + + if (value == null) { + return false; + } + + return Boolean.parseBoolean(value); + } + + private void responseResource(OutputStream out, String url) throws IOException { + DataOutputStream dos = new DataOutputStream(out); + byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath()); + response200Header(dos, body.length); + responseBody(dos, body); + } + + private void response302LoginSuccessHeader(DataOutputStream dos) { + try { + dos.writeBytes("HTTP/1.1 302 Redirect \r\n"); + dos.writeBytes("Set-Cookie: logined=true \r\n"); + dos.writeBytes("Location: /index.html \r\n"); + dos.writeBytes("\r\n"); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + private void response302Header(DataOutputStream dos, String url) { + try { + dos.writeBytes("HTTP/1.1 302 Redirect \r\n"); + dos.writeBytes("Location: " + url + " \r\n"); + dos.writeBytes("\r\n"); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + private int getContentLength(String line) { + String[] tokens = line.split(":"); + + return Integer.parseInt(tokens[1].trim()); + } + private void response200Header(DataOutputStream dos, int lengthOfBodyContent) { try { dos.writeBytes("HTTP/1.1 200 OK \r\n"); - dos.writeBytes("Content-Type: text/html;charset=utf-8\r\n"); - dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n"); + dos.writeBytes("Content-Type: text/html;charset=utf-8 \r\n"); + dos.writeBytes("Content-Length: " + lengthOfBodyContent + " \r\n"); dos.writeBytes("\r\n"); } catch (IOException e) { log.error(e.getMessage()); diff --git a/src/main/java/webserver/WebServer.java b/src/main/java/webserver/WebServer.java index 91f4a0fbc..a06d2bebb 100644 --- a/src/main/java/webserver/WebServer.java +++ b/src/main/java/webserver/WebServer.java @@ -19,7 +19,6 @@ public static void main(String args[]) throws Exception { } // 서버소켓을 생성한다. 웹서버는 기본적으로 8080번 포트를 사용한다. - try (ServerSocket listenSocket = new ServerSocket(port)) { log.info("Web Application Server started {} port.", port); diff --git a/webapp/user/form.html b/webapp/user/form.html index 96fe1bd3a..f7a3b5612 100644 --- a/webapp/user/form.html +++ b/webapp/user/form.html @@ -75,7 +75,7 @@
-
+