From 50cd97e3df988c9c6dd47fa425ad6e6a4efee9c9 Mon Sep 17 00:00:00 2001 From: Seyoung CHOE Date: Sun, 13 Oct 2024 01:33:47 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20index.html=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/util/HttpRequestUtils.java | 21 ++++++++++++++++++-- src/main/java/webserver/RequestHandler.java | 14 +++++++------ src/test/java/util/HttpRequestUtilsTest.java | 18 +++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main/java/util/HttpRequestUtils.java b/src/main/java/util/HttpRequestUtils.java index c4cd95c0d..daff24af8 100644 --- a/src/main/java/util/HttpRequestUtils.java +++ b/src/main/java/util/HttpRequestUtils.java @@ -1,5 +1,9 @@ package util; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.Arrays; import java.util.Map; import java.util.stream.Collectors; @@ -8,6 +12,20 @@ import com.google.common.collect.Maps; public class HttpRequestUtils { + + public static String headerRequest(InputStream in) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + String line = reader.readLine(); + String url = line.split(" ")[1]; + + while (!"".equals(line)) { + if (line == null) { return null; } + System.out.println(line); + line = reader.readLine(); + } + return url; + } + /** * @param queryString은 * URL에서 ? 이후에 전달되는 field1=value1&field2=value2 형식임 @@ -18,8 +36,7 @@ public static Map parseQueryString(String queryString) { } /** - * @param 쿠키 - * 값은 name1=value1; name2=value2 형식임 + * @param 쿠키 값은 name1=value1; name2=value2 형식임 * @return */ public static Map parseCookies(String cookies) { diff --git a/src/main/java/webserver/RequestHandler.java b/src/main/java/webserver/RequestHandler.java index 90195ec4e..80d1501b8 100644 --- a/src/main/java/webserver/RequestHandler.java +++ b/src/main/java/webserver/RequestHandler.java @@ -1,13 +1,12 @@ 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.file.Files; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import util.HttpRequestUtils; public class RequestHandler extends Thread { private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); @@ -23,9 +22,10 @@ public void run() { connection.getPort()); try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { - // TODO 사용자 요청에 대한 처리는 이 곳에 구현하면 된다. + DataOutputStream dos = new DataOutputStream(out); - byte[] body = "Hello World".getBytes(); + + byte[] body = Files.readAllBytes(new File("./webapp" + HttpRequestUtils.headerRequest(in)).toPath()); response200Header(dos, body.length); responseBody(dos, body); } catch (IOException e) { @@ -33,6 +33,8 @@ public void run() { } } + // private void stringSplit + private void response200Header(DataOutputStream dos, int lengthOfBodyContent) { try { dos.writeBytes("HTTP/1.1 200 OK \r\n"); diff --git a/src/test/java/util/HttpRequestUtilsTest.java b/src/test/java/util/HttpRequestUtilsTest.java index a4265f5e7..b6fa42f0b 100644 --- a/src/test/java/util/HttpRequestUtilsTest.java +++ b/src/test/java/util/HttpRequestUtilsTest.java @@ -3,6 +3,9 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.Map; import org.junit.Test; @@ -10,6 +13,21 @@ import util.HttpRequestUtils.Pair; public class HttpRequestUtilsTest { + + @Test + public void headerRequest() throws IOException { + + String httpRequest = "GET /path/resource HTTP/1.1\n" + + "Host: localhost\n" + + "Accept: */*\n" + + "\n"; + + InputStream inputStream = new ByteArrayInputStream(httpRequest.getBytes()); + String url = HttpRequestUtils.headerRequest(inputStream); + + assertEquals("/path/resource", url); + } + @Test public void parseQueryString() { String queryString = "userId=javajigi"; From 090046178224fe5f4127b9ee16a015ae824d1558 Mon Sep 17 00:00:00 2001 From: Seyoung CHOE Date: Sat, 26 Oct 2024 19:46:28 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20get=20=EB=B0=A9=EC=8B=9D=20?= =?UTF-8?q?=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/db/DataBase.java | 1 + src/main/java/util/HttpRequestUtils.java | 26 ++++++++++++++++++-- src/main/java/webserver/RequestHandler.java | 10 +++++++- src/test/java/util/HttpRequestUtilsTest.java | 20 ++++++++++++++- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/main/java/db/DataBase.java b/src/main/java/db/DataBase.java index b9419a4a3..cbb6768fb 100644 --- a/src/main/java/db/DataBase.java +++ b/src/main/java/db/DataBase.java @@ -21,4 +21,5 @@ public static User findUserById(String userId) { public static Collection findAll() { return users.values(); } + } diff --git a/src/main/java/util/HttpRequestUtils.java b/src/main/java/util/HttpRequestUtils.java index daff24af8..3c6d18334 100644 --- a/src/main/java/util/HttpRequestUtils.java +++ b/src/main/java/util/HttpRequestUtils.java @@ -1,5 +1,7 @@ package util; +import model.User; +import db.DataBase; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -16,16 +18,36 @@ public class HttpRequestUtils { public static String headerRequest(InputStream in) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line = reader.readLine(); + + if (line == null) { return null; } + String url = line.split(" ")[1]; + System.out.println(line); + + //if (!url.endsWith(".html")) {return null;} - while (!"".equals(line)) { + /*while (!"".equals(line)) { if (line == null) { return null; } System.out.println(line); line = reader.readLine(); - } + }*/ return url; } + public static String parseUrl(String url) { + int index = url.indexOf("?"); + String requestPath = url.substring(0, index); + return url.substring(index+1); + + } + + public static void signUpRequest(String url) { + String params = parseUrl(url); + Map map = parseQueryString(params); + DataBase.addUser(new User(map.get("userId"), map.get("password"), + map.get("name"), map.get("email"))); + } + /** * @param queryString은 * URL에서 ? 이후에 전달되는 field1=value1&field2=value2 형식임 diff --git a/src/main/java/webserver/RequestHandler.java b/src/main/java/webserver/RequestHandler.java index 80d1501b8..9137ae0f8 100644 --- a/src/main/java/webserver/RequestHandler.java +++ b/src/main/java/webserver/RequestHandler.java @@ -7,6 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import util.HttpRequestUtils; +import db.DataBase; public class RequestHandler extends Thread { private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); @@ -24,10 +25,17 @@ public void run() { try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { DataOutputStream dos = new DataOutputStream(out); + String url = HttpRequestUtils.headerRequest(in); - byte[] body = Files.readAllBytes(new File("./webapp" + HttpRequestUtils.headerRequest(in)).toPath()); + if (url.startsWith("/user/create")) { + HttpRequestUtils.signUpRequest(url); + } + + byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath()); response200Header(dos, body.length); responseBody(dos, body); + + } catch (IOException e) { log.error(e.getMessage()); } diff --git a/src/test/java/util/HttpRequestUtilsTest.java b/src/test/java/util/HttpRequestUtilsTest.java index b6fa42f0b..cba40155a 100644 --- a/src/test/java/util/HttpRequestUtilsTest.java +++ b/src/test/java/util/HttpRequestUtilsTest.java @@ -8,15 +8,16 @@ import java.io.InputStream; import java.util.Map; +import model.User; import org.junit.Test; import util.HttpRequestUtils.Pair; +import db.DataBase; public class HttpRequestUtilsTest { @Test public void headerRequest() throws IOException { - String httpRequest = "GET /path/resource HTTP/1.1\n" + "Host: localhost\n" + "Accept: */*\n" + @@ -28,6 +29,23 @@ public void headerRequest() throws IOException { assertEquals("/path/resource", url); } + @Test + public void parseUrl() { + String url = "/?data=234"; + String params = HttpRequestUtils.parseUrl(url); + assertEquals("data=234", params); + } + + @Test + public void signUpRequest() { + String url = "/user/create?userId=user&password=pass&name=user&email=user%40naver.com"; + Map map = HttpRequestUtils.parseQueryString(url); + DataBase.addUser(new User(map.get("userId"), map.get("password"), + map.get("name"), map.get("email"))); + + assertEquals(1, DataBase.findAll().size()); + } + @Test public void parseQueryString() { String queryString = "userId=javajigi"; From 991f8f061af27b0e2b2cc9d3f65a761ce0bfbd3f Mon Sep 17 00:00:00 2001 From: Seyoung CHOE Date: Sat, 26 Oct 2024 20:26:43 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EA=B2=8C=20User=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/util/HttpRequestUtils.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/util/HttpRequestUtils.java b/src/main/java/util/HttpRequestUtils.java index 3c6d18334..4b6568e85 100644 --- a/src/main/java/util/HttpRequestUtils.java +++ b/src/main/java/util/HttpRequestUtils.java @@ -44,8 +44,12 @@ public static String parseUrl(String url) { public static void signUpRequest(String url) { String params = parseUrl(url); Map map = parseQueryString(params); - DataBase.addUser(new User(map.get("userId"), map.get("password"), - map.get("name"), map.get("email"))); + User user = new User( + map.get("userId"), + map.get("password"), + map.get("name"), + map.get("email") + ); } /**