From 26d39be38a64496a8dcbf58f372398cf4b09df8c Mon Sep 17 00:00:00 2001 From: YunBok Date: Fri, 18 Feb 2022 22:47:18 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=ED=81=B4=EB=A1=A0=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/webserver/WebServer.java | 1 - 1 file changed, 1 deletion(-) 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); From a3101f0759921d4e58ba5bf04d40be627f3ec42b Mon Sep 17 00:00:00 2001 From: YunBok Date: Sat, 19 Feb 2022 22:39:38 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EC=9B=B9=EC=84=9C=EB=B2=84=20html=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=9D=BD=EC=96=B4=EC=98=A4=EB=8A=94=EB=B6=80?= =?UTF-8?q?=EB=B6=84=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/util/HeaderUtil.java | 9 ++++++ src/main/java/webserver/RequestHandler.java | 31 +++++++++++++++++---- src/test/java/util/HeaderUtilTest.java | 19 +++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/main/java/util/HeaderUtil.java create mode 100644 src/test/java/util/HeaderUtilTest.java diff --git a/src/main/java/util/HeaderUtil.java b/src/main/java/util/HeaderUtil.java new file mode 100644 index 000000000..32561f1df --- /dev/null +++ b/src/main/java/util/HeaderUtil.java @@ -0,0 +1,9 @@ +package util; + +public class HeaderUtil { + + public static String getUriInHeader(String header) { + String[] headerArr = header.split(" "); + return headerArr[1]; + } +} diff --git a/src/main/java/webserver/RequestHandler.java b/src/main/java/webserver/RequestHandler.java index 90195ec4e..5e896058b 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.HeaderUtil; public class RequestHandler extends Thread { private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); @@ -24,8 +23,30 @@ public void run() { try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { // TODO 사용자 요청에 대한 처리는 이 곳에 구현하면 된다. + + Reader reader = new InputStreamReader(in); + BufferedReader br = new BufferedReader(reader); + String line = null; + int index = 0; + String url = ""; + + while(!"".equals(line)) { + + line = br.readLine(); + + if( index == 0) { + url = HeaderUtil.getUriInHeader(line); + log.debug(url); + } + index++; + if (line == null) { + return; + } + } + DataOutputStream dos = new DataOutputStream(out); - byte[] body = "Hello World".getBytes(); +// byte[] body = "Hello World22".getBytes(); + byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath()); response200Header(dos, body.length); responseBody(dos, body); } catch (IOException e) { diff --git a/src/test/java/util/HeaderUtilTest.java b/src/test/java/util/HeaderUtilTest.java new file mode 100644 index 000000000..3c267aefa --- /dev/null +++ b/src/test/java/util/HeaderUtilTest.java @@ -0,0 +1,19 @@ +package util; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +public class HeaderUtilTest { + + @Test + public void url_분리() { + + String header = "GET /index.html HTTP/1.1"; + + String url = HeaderUtil.getUriInHeader(header); + + assertThat("/index.html", is(url)); + } +} From 19c1bc52c9da54fc9050917cb29aeae48d0098c8 Mon Sep 17 00:00:00 2001 From: Yunbok Date: Sun, 20 Feb 2022 18:36:34 +0900 Subject: [PATCH 3/5] =?UTF-8?q?Get=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85?= =?UTF-8?q?=20=EC=9A=94=EC=B2=AD=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/util/HeaderUtil.java | 52 +++++++++++++++++++++ src/main/java/util/HttpRequestUtils.java | 1 + src/main/java/webserver/RequestHandler.java | 45 +++++++++--------- src/test/java/util/HeaderUtilTest.java | 45 ++++++++++++++++++ webapp/user/form.html | 2 +- 5 files changed, 122 insertions(+), 23 deletions(-) diff --git a/src/main/java/util/HeaderUtil.java b/src/main/java/util/HeaderUtil.java index 32561f1df..1420685ef 100644 --- a/src/main/java/util/HeaderUtil.java +++ b/src/main/java/util/HeaderUtil.java @@ -1,9 +1,61 @@ package util; + +import java.util.HashMap; +import java.util.Map; + public class HeaderUtil { public static String getUriInHeader(String header) { String[] headerArr = header.split(" "); return headerArr[1]; } + + public static String getRealUrl(String url) { + int index = url.indexOf("?"); + + if (index == -1) { + return url; + } + return url.substring(0, index); + } + + public static String getParamInUrl(String url) { + int index = url.indexOf("?"); + + if (index == -1) { + return ""; + } + + return url.substring(index+1); + } + + public static Map paramToMap(String paramText) { + int length = paramText.length(); + String[] paramArr = paramText.split("&"); + Map paramMap = new HashMap<>(); + + if (length == 0) { + return paramMap; + } + + for (String param : paramArr) { + mapInputParam(param, paramMap); + } + + System.out.println(paramMap); + + return paramMap; + } + + private static void mapInputParam (String param, Map paramMap) { + int index = param.indexOf("="); + if(index > -1) { + String key = param.substring(0, index); + String value = param.substring(index + 1, param.length()); + + paramMap.put(key, value); + } + } + } diff --git a/src/main/java/util/HttpRequestUtils.java b/src/main/java/util/HttpRequestUtils.java index c4cd95c0d..03ed4f8b1 100644 --- a/src/main/java/util/HttpRequestUtils.java +++ b/src/main/java/util/HttpRequestUtils.java @@ -2,6 +2,7 @@ import java.util.Arrays; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import com.google.common.base.Strings; diff --git a/src/main/java/webserver/RequestHandler.java b/src/main/java/webserver/RequestHandler.java index 5e896058b..b5b08264d 100644 --- a/src/main/java/webserver/RequestHandler.java +++ b/src/main/java/webserver/RequestHandler.java @@ -3,10 +3,13 @@ import java.io.*; import java.net.Socket; import java.nio.file.Files; +import java.util.Map; +import model.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import util.HeaderUtil; +import util.HttpRequestUtils; public class RequestHandler extends Thread { private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); @@ -24,32 +27,30 @@ public void run() { try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { // TODO 사용자 요청에 대한 처리는 이 곳에 구현하면 된다. - Reader reader = new InputStreamReader(in); - BufferedReader br = new BufferedReader(reader); - String line = null; - int index = 0; - String url = ""; + final String line = br.readLine(); + final String url = HeaderUtil.getUriInHeader(line); + final String realUrl = HeaderUtil.getRealUrl(url); - while(!"".equals(line)) { + //리팩토링 힌트 - line = br.readLine(); +// param = HeaderUtil.getParamInUrl(url); +// Map map =HeaderUtil.paramToMap(param); - if( index == 0) { - url = HeaderUtil.getUriInHeader(line); - log.debug(url); - } - index++; - if (line == null) { - return; - } - } + if (url.startsWith("/user/create")) { + final int index = url.indexOf("?"); + final String queryString = url.substring(index + 1); + + final Map params = HttpRequestUtils.parseQueryString(queryString); - DataOutputStream dos = new DataOutputStream(out); + final User user = new User(params.get("userId"), params.get("password"), params.get("name"), params.get("email")); + } else { + final DataOutputStream dos = new DataOutputStream(out); // byte[] body = "Hello World22".getBytes(); - byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath()); - response200Header(dos, body.length); - responseBody(dos, body); - } catch (IOException e) { + final byte[] body = Files.readAllBytes(new File("./webapp" + realUrl).toPath()); + response200Header(dos, body.length); + responseBody(dos, body); + } + } catch (final IOException e) { log.error(e.getMessage()); } } @@ -73,4 +74,4 @@ private void responseBody(DataOutputStream dos, byte[] body) { log.error(e.getMessage()); } } -} +} \ No newline at end of file diff --git a/src/test/java/util/HeaderUtilTest.java b/src/test/java/util/HeaderUtilTest.java index 3c267aefa..d76fae2be 100644 --- a/src/test/java/util/HeaderUtilTest.java +++ b/src/test/java/util/HeaderUtilTest.java @@ -2,6 +2,8 @@ import org.junit.Test; +import java.util.Map; + import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; @@ -16,4 +18,47 @@ public class HeaderUtilTest { assertThat("/index.html", is(url)); } + + + + @Test + public void url_파라메터_제거버전() { + + String pullUrl = "/index.html?name=123&password=1234"; + + String url = HeaderUtil.getRealUrl(pullUrl); + + assertThat("/index.html", is(url)); + } + + @Test + public void url_제거버전() { + + String pullUrl = "/index.html?name=123&password=1234"; + + String url = HeaderUtil.getParamInUrl(pullUrl); + + assertThat("name=123&password=1234", is(url)); + } + + @Test + public void url_제거버전2() { + + String pullUrl = "/index.html"; + + String url = HeaderUtil.getParamInUrl(pullUrl); + + assertThat("", is(url)); + } + + @Test + public void paramToMap() { + + String pullUrl = "name=123&password=1234"; + + Map map = HeaderUtil.paramToMap(pullUrl); + + assertThat("123", is(map.get("name"))); + assertThat("1234", is(map.get("password"))); + } } 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 @@
-
+
From 234314932157e2ccbf38285d24204a4131f448db Mon Sep 17 00:00:00 2001 From: Yunbok <30767280+Yunbok@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:30:48 +0900 Subject: [PATCH 4/5] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 235fddaea..b0a9486e1 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ * ### 요구사항 4 - redirect 방식으로 이동 -* +* http 상태코드에대해 알게되었다. 302 코드를 이용하여 실무에서 사용했던 불필요한 요청로직을 지울수있을것같다. ### 요구사항 5 - cookie * @@ -33,4 +33,4 @@ * ### heroku 서버에 배포 후 -* \ No newline at end of file +* From 8e0ce377c50cd133536a1707f46b27fc7e8dab80 Mon Sep 17 00:00:00 2001 From: Yunbok <30767280+Yunbok@users.noreply.github.com> Date: Thu, 10 Mar 2022 17:55:57 +0900 Subject: [PATCH 5/5] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b0a9486e1..a6e48d6b0 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ * 각 요구사항을 구현하는 것이 중요한 것이 아니라 구현 과정을 통해 학습한 내용을 인식하는 것이 배움에 중요하다. ### 요구사항 1 - http://localhost:8080/index.html로 접속시 응답 -* +* ### 요구사항 2 - get 방식으로 회원가입 * @@ -27,10 +27,10 @@ * http 상태코드에대해 알게되었다. 302 코드를 이용하여 실무에서 사용했던 불필요한 요청로직을 지울수있을것같다. ### 요구사항 5 - cookie -* +* 개발하면서 헤더에 쿠키를 담아서 보내는데 생각보다 오래걸렷다. 문자열을 자르는것 부터 많은 리팩토링이 필요하다. ### 요구사항 6 - stylesheet 적용 -* +* css의 타입이 text/css 인지 여태 신경도 안쓰고 개발했던것같다. 프레임워크에서 자동으로 해주다보니 잘 적용되던것들을 가볍게 넘긴거같다. ### heroku 서버에 배포 후 *