From 0e806152b2c16c2b073e0ebc49746c44fc963a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Tue, 3 Sep 2024 16:10:52 +0900 Subject: [PATCH 01/29] =?UTF-8?q?test:=20=ED=95=99=EC=8A=B5=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/version/ResourceVersion.java | 3 ++- study/src/test/java/study/FileTest.java | 26 +++++++++++++++---- study/src/test/java/study/IOStreamTest.java | 25 +++++++++++++----- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/study/src/main/java/cache/com/example/version/ResourceVersion.java b/study/src/main/java/cache/com/example/version/ResourceVersion.java index 27a2f22813..8c531e3efa 100644 --- a/study/src/main/java/cache/com/example/version/ResourceVersion.java +++ b/study/src/main/java/cache/com/example/version/ResourceVersion.java @@ -2,10 +2,11 @@ import org.springframework.stereotype.Component; -import jakarta.annotation.PostConstruct; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import jakarta.annotation.PostConstruct; + @Component public class ResourceVersion { diff --git a/study/src/test/java/study/FileTest.java b/study/src/test/java/study/FileTest.java index e1b6cca042..f9ace62522 100644 --- a/study/src/test/java/study/FileTest.java +++ b/study/src/test/java/study/FileTest.java @@ -2,13 +2,23 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDirFactory; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; import java.util.Collections; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import javax.swing.filechooser.FileSystemView; + /** * 웹서버는 사용자가 요청한 html 파일을 제공 할 수 있어야 한다. * File 클래스를 사용해서 파일을 읽어오고, 사용자에게 전달한다. @@ -28,7 +38,10 @@ class FileTest { final String fileName = "nextstep.txt"; // todo - final String actual = ""; + URL url = getClass().getClassLoader().getResource(fileName); + File file = new File(url.getPath()); + + final String actual = file.getAbsolutePath(); assertThat(actual).endsWith(fileName); } @@ -40,15 +53,18 @@ class FileTest { * File, Files 클래스를 사용하여 파일의 내용을 읽어보자. */ @Test - void 파일의_내용을_읽는다() { + void 파일의_내용을_읽는다() throws IOException { final String fileName = "nextstep.txt"; // todo - final Path path = null; + URL url = getClass().getClassLoader().getResource(fileName); + File file = new File(url.getPath()); + final Path path = file.toPath(); + // TODO: 왜 아래와 같이 하면 테스트가 통과하지 않을까? File 클래스를 꼭 써야하는 이유는? + // Path path = Paths.get(url.getPath()); // todo - final List actual = Collections.emptyList(); - + final List actual = Files.readAllLines(path); assertThat(actual).containsOnly("nextstep"); } } diff --git a/study/src/test/java/study/IOStreamTest.java b/study/src/test/java/study/IOStreamTest.java index 47a79356b6..f0a9602dac 100644 --- a/study/src/test/java/study/IOStreamTest.java +++ b/study/src/test/java/study/IOStreamTest.java @@ -53,6 +53,7 @@ class OutputStream_학습_테스트 { * todo * OutputStream 객체의 write 메서드를 사용해서 테스트를 통과시킨다 */ + outputStream.write(bytes); final String actual = outputStream.toString(); @@ -78,6 +79,7 @@ class OutputStream_학습_테스트 { * flush를 사용해서 테스트를 통과시킨다. * ByteArrayOutputStream과 어떤 차이가 있을까? */ + outputStream.flush(); verify(outputStream, atLeastOnce()).flush(); outputStream.close(); @@ -97,6 +99,8 @@ class OutputStream_학습_테스트 { * java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다. */ + outputStream.close(); + verify(outputStream, atLeastOnce()).close(); } } @@ -128,7 +132,7 @@ class InputStream_학습_테스트 { * todo * inputStream에서 바이트로 반환한 값을 문자열로 어떻게 바꿀까? */ - final String actual = ""; + final String actual = new String(inputStream.readAllBytes()); assertThat(actual).isEqualTo("🤩"); assertThat(inputStream.read()).isEqualTo(-1); @@ -148,6 +152,7 @@ class InputStream_학습_테스트 { * try-with-resources를 사용한다. * java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다. */ + inputStream.close(); verify(inputStream, atLeastOnce()).close(); } @@ -169,12 +174,12 @@ class FilterStream_학습_테스트 { * 버퍼 크기를 지정하지 않으면 버퍼의 기본 사이즈는 얼마일까? */ @Test - void 필터인_BufferedInputStream를_사용해보자() { + void 필터인_BufferedInputStream를_사용해보자() throws IOException { final String text = "필터에 연결해보자."; final InputStream inputStream = new ByteArrayInputStream(text.getBytes()); - final InputStream bufferedInputStream = null; + final InputStream bufferedInputStream = new BufferedInputStream(inputStream); - final byte[] actual = new byte[0]; + final byte[] actual = bufferedInputStream.readAllBytes(); assertThat(bufferedInputStream).isInstanceOf(FilterInputStream.class); assertThat(actual).isEqualTo("필터에 연결해보자.".getBytes()); @@ -197,7 +202,7 @@ class InputStreamReader_학습_테스트 { * 필터인 BufferedReader를 사용하면 readLine 메서드를 사용해서 문자열(String)을 한 줄 씩 읽어올 수 있다. */ @Test - void BufferedReader를_사용하여_문자열을_읽어온다() { + void BufferedReader를_사용하여_문자열을_읽어온다() throws IOException { final String emoji = String.join("\r\n", "😀😃😄😁😆😅😂🤣🥲☺️😊", "😇🙂🙃😉😌😍🥰😘😗😙😚", @@ -206,7 +211,15 @@ class InputStreamReader_학습_테스트 { final InputStream inputStream = new ByteArrayInputStream(emoji.getBytes()); final StringBuilder actual = new StringBuilder(); - + try ( + InputStreamReader inputStreamReader = new InputStreamReader(inputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + ) { + String line; + while((line = bufferedReader.readLine()) != null) { + actual.append(line + "\r\n"); + } + } assertThat(actual).hasToString(emoji); } } From fe4981a6ec5c11664798e7b8665f8b8db5a928de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Wed, 4 Sep 2024 22:12:25 +0900 Subject: [PATCH 02/29] =?UTF-8?q?feat:=20`/index.html`=EC=97=90=20?= =?UTF-8?q?=EC=A0=91=EA=B7=BC=ED=96=88=EC=9D=84=20=EB=95=8C=20static/index?= =?UTF-8?q?.html=20=ED=8C=8C=EC=9D=BC=EC=9D=B4=20=EB=B3=B4=EC=97=AC?= =?UTF-8?q?=EC=A7=80=EB=8F=84=EB=A1=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index bb14184757..e07eb6f02d 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -5,8 +5,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.BufferedReader; +import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; import java.net.Socket; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; public class Http11Processor implements Runnable, Processor { @@ -29,6 +36,13 @@ public void process(final Socket connection) { try (final var inputStream = connection.getInputStream(); final var outputStream = connection.getOutputStream()) { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String urlPath = reader.readLine().split(" ")[1]; + if(urlPath.equals("/index.html")) { + printIndexHtmlFile(outputStream); + return; + } + final var responseBody = "Hello world!"; final var response = String.join("\r\n", @@ -44,4 +58,26 @@ public void process(final Socket connection) { log.error(e.getMessage(), e); } } + + private void printIndexHtmlFile(OutputStream outputStream) { + final String fileName = "static/index.html"; + final URL url = getClass().getClassLoader().getResource(fileName); + final File file = new File(url.getPath()); + final Path path = file.toPath(); + + try { + final var responseBody = String.join("\n", Files.readAllLines(path)); + final var response = String.join("\r\n", + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: " + responseBody.length() + " ", + "", + responseBody); + + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException | UncheckedServletException e) { + log.error(e.getMessage(), e); + } + } } From 97035964d66c79e946788ea7a244fdb72675b1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Wed, 4 Sep 2024 22:55:57 +0900 Subject: [PATCH 03/29] =?UTF-8?q?feat:=20=EC=9E=90=EC=9B=90=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=9D=BC=20=EB=8B=A4=EB=A5=B8=20Conent-Type=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=91=EB=8B=B5=EC=9D=84=20=EB=B3=B4=EB=82=B4?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apache/coyote/http11/Http11Processor.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index e07eb6f02d..8713b7ab1a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -39,7 +39,11 @@ public void process(final Socket connection) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String urlPath = reader.readLine().split(" ")[1]; if(urlPath.equals("/index.html")) { - printIndexHtmlFile(outputStream); + printFileResource("static/index.html", outputStream); + return; + } + if(urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { + printFileResource("static" + urlPath, outputStream); return; } @@ -59,17 +63,20 @@ public void process(final Socket connection) { } } - private void printIndexHtmlFile(OutputStream outputStream) { - final String fileName = "static/index.html"; + private void printFileResource(String fileName, OutputStream outputStream) { final URL url = getClass().getClassLoader().getResource(fileName); final File file = new File(url.getPath()); final Path path = file.toPath(); try { + String contentType = "text/html"; + if(fileName.endsWith("css")) { + contentType = "text/css"; + } final var responseBody = String.join("\n", Files.readAllLines(path)); final var response = String.join("\r\n", "HTTP/1.1 200 OK ", - "Content-Type: text/html;charset=utf-8 ", + String.format("Content-Type: %s;charset=utf-8 ", contentType), "Content-Length: " + responseBody.length() + " ", "", responseBody); From fed02f6f5f4308400e55c160d9495cad010f5bfb Mon Sep 17 00:00:00 2001 From: Gyeongho Yang Date: Thu, 5 Sep 2024 11:11:09 +0900 Subject: [PATCH 04/29] fix: remove implementation logback-classic on gradle (#501) --- study/build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/study/build.gradle b/study/build.gradle index 5c69542f84..87a1f0313c 100644 --- a/study/build.gradle +++ b/study/build.gradle @@ -19,7 +19,6 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-webflux' - implementation 'ch.qos.logback:logback-classic:1.5.7' implementation 'org.apache.commons:commons-lang3:3.14.0' implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.1' implementation 'pl.allegro.tech.boot:handlebars-spring-boot-starter:0.4.1' From bffee2ab92d8896fe85b42b11ad805942c604d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 11:41:30 +0900 Subject: [PATCH 05/29] =?UTF-8?q?fix:=20=EA=B3=B5=EB=B0=B1=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EB=95=8C=EB=AC=B8=EC=97=90=20index.html=EC=9D=B4?= =?UTF-8?q?=20=EB=B6=88=EB=9F=AC=EC=99=80=EC=A7=80=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=20=ED=98=84=EC=83=81=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 8713b7ab1a..1abbee651b 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -1,5 +1,6 @@ package org.apache.coyote.http11; + import com.techcourse.exception.UncheckedServletException; import org.apache.coyote.Processor; import org.slf4j.Logger; @@ -37,7 +38,8 @@ public void process(final Socket connection) { final var outputStream = connection.getOutputStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - String urlPath = reader.readLine().split(" ")[1]; + String line = reader.readLine(); + String urlPath = line.split(" ")[1]; if(urlPath.equals("/index.html")) { printFileResource("static/index.html", outputStream); return; @@ -72,14 +74,16 @@ private void printFileResource(String fileName, OutputStream outputStream) { String contentType = "text/html"; if(fileName.endsWith("css")) { contentType = "text/css"; + } else if(fileName.endsWith("js")) { + contentType = "application/javascript"; } - final var responseBody = String.join("\n", Files.readAllLines(path)); - final var response = String.join("\r\n", - "HTTP/1.1 200 OK ", - String.format("Content-Type: %s;charset=utf-8 ", contentType), - "Content-Length: " + responseBody.length() + " ", - "", - responseBody); + + final var responseBody = new String(Files.readAllBytes(path)); + var response = "HTTP/1.1 200 OK \r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: " + responseBody.getBytes().length + " \r\n" + + "\r\n"+ + responseBody; outputStream.write(response.getBytes()); outputStream.flush(); From 0964ae227e0023c95f1b1df51ecbf6b579b798ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 13:42:28 +0900 Subject: [PATCH 06/29] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=20=EC=8B=9C=20=EC=BF=BC=EB=A6=AC=20=ED=8C=8C?= =?UTF-8?q?=EB=9D=BC=EB=AF=B8=ED=84=B0=EB=A5=BC=20=ED=8C=8C=EC=8B=B1?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apache/coyote/http11/Http11Processor.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 1abbee651b..0f08604c21 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -1,7 +1,10 @@ package org.apache.coyote.http11; +import com.techcourse.db.InMemoryUserRepository; import com.techcourse.exception.UncheckedServletException; +import com.techcourse.model.User; + import org.apache.coyote.Processor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,6 +47,17 @@ public void process(final Socket connection) { printFileResource("static/index.html", outputStream); return; } + if(urlPath.startsWith("/login")) { + String parameters = urlPath.substring(7); + String account = parameters.split("&")[0].split("=")[1]; + String password = parameters.split("&")[1].split("=")[1]; + User user = InMemoryUserRepository.findByAccount(account) + .orElse(new User("guest", "guest", "guest")); + if(user.checkPassword(password)) { + System.out.println("user : " + user); + } + printFileResource("static/login.html", outputStream); + } if(urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { printFileResource("static" + urlPath, outputStream); return; From 7e9135698878932274ddc1f523ba817ed9c56c70 Mon Sep 17 00:00:00 2001 From: Gyeongho Yang Date: Thu, 5 Sep 2024 13:51:07 +0900 Subject: [PATCH 07/29] fix: add threads min-spare configuration on properties (#502) --- study/src/main/resources/application.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/study/src/main/resources/application.yml b/study/src/main/resources/application.yml index 4e8655a962..e3503a5fb9 100644 --- a/study/src/main/resources/application.yml +++ b/study/src/main/resources/application.yml @@ -6,4 +6,5 @@ server: accept-count: 1 max-connections: 1 threads: + min-spare: 2 max: 2 From 72958ed5b7d05c5ff275f48f9f78fd57d4ee12bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 14:17:32 +0900 Subject: [PATCH 08/29] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=EC=97=90=20=EC=84=B1=EA=B3=B5=ED=95=98=EA=B1=B0=EB=82=98=20?= =?UTF-8?q?=EC=8B=A4=ED=8C=A8=ED=95=9C=20=EA=B2=BD=EC=9A=B0=20=EB=8B=A4?= =?UTF-8?q?=EB=A5=B8=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=A1=9C=20=EB=A6=AC?= =?UTF-8?q?=EB=8B=A4=EC=9D=B4=EB=A0=89=ED=8A=B8=ED=95=98=EB=8A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 0f08604c21..ce42d16931 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -43,21 +43,29 @@ public void process(final Socket connection) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = reader.readLine(); String urlPath = line.split(" ")[1]; - if(urlPath.equals("/index.html")) { - printFileResource("static/index.html", outputStream); + if(urlPath.endsWith("html")) { + printFileResource("static" + urlPath, outputStream); return; } if(urlPath.startsWith("/login")) { + if(urlPath.equals("/login")) { + printFileResource("static" + urlPath +".html", outputStream); + return; + } String parameters = urlPath.substring(7); String account = parameters.split("&")[0].split("=")[1]; String password = parameters.split("&")[1].split("=")[1]; + User user = InMemoryUserRepository.findByAccount(account) .orElse(new User("guest", "guest", "guest")); if(user.checkPassword(password)) { - System.out.println("user : " + user); + redirect("http://localhost:8080/index.html", outputStream); + return; } - printFileResource("static/login.html", outputStream); + redirect("http://localhost:8080/401.html", outputStream); + return; } + if(urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { printFileResource("static" + urlPath, outputStream); return; @@ -105,4 +113,19 @@ private void printFileResource(String fileName, OutputStream outputStream) { log.error(e.getMessage(), e); } } + + private void redirect(String location, OutputStream outputStream) { + try { + String contentType = "text/html"; + var response = "HTTP/1.1 302 Found \r\n" + + "Location: " + location + "\r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: 0"; + + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException | UncheckedServletException e) { + log.error(e.getMessage(), e); + } + } } From a535e6d6a98799d71ce84b4d11e1f34c53e61bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 15:56:32 +0900 Subject: [PATCH 09/29] =?UTF-8?q?feat:=20POST=20=EC=9A=94=EC=B2=AD?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=ED=9A=8C=EC=9B=90=20=EA=B0=80=EC=9E=85=20?= =?UTF-8?q?=EC=8B=9C=20=EC=9A=94=EC=B2=AD=20=EB=B3=B8=EB=AC=B8=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20=EA=B0=80=EC=A0=B8?= =?UTF-8?q?=EC=99=80=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index ce42d16931..ba889b7458 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -18,6 +18,8 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; public class Http11Processor implements Runnable, Processor { @@ -41,7 +43,9 @@ public void process(final Socket connection) { final var outputStream = connection.getOutputStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String line = reader.readLine(); + String httpMethod = line.split(" ")[0]; String urlPath = line.split(" ")[1]; if(urlPath.endsWith("html")) { printFileResource("static" + urlPath, outputStream); @@ -65,6 +69,22 @@ public void process(final Socket connection) { redirect("http://localhost:8080/401.html", outputStream); return; } + if(urlPath.startsWith("/register")) { + if(httpMethod.equals("GET")) { + printFileResource("static" + urlPath + ".html", outputStream); + return; + } + String body = parseBody(reader); + if (body != null) { + String account = body.split("&")[0].split("=")[1]; + String mail = body.split("&")[1].split("=")[1]; + String password = body.split("&")[2].split("=")[1]; + User user = new User(account, mail, password); + InMemoryUserRepository.save(user); + redirect("http://localhost:8080/index.html", outputStream); + return; + } + } if(urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { printFileResource("static" + urlPath, outputStream); @@ -87,6 +107,23 @@ public void process(final Socket connection) { } } + private String parseBody(BufferedReader reader) throws IOException { + String line; + int contentLength = 0; + while((line = reader.readLine()) != null && !line.isEmpty()) { + if(line.startsWith("Content-Length")) { + contentLength = Integer.parseInt(line.split(":")[1].trim()); + } + } + + if(contentLength > 0) { + char[] body = new char[contentLength]; + reader.read(body, 0, contentLength); + return new String(body); + } + return null; + } + private void printFileResource(String fileName, OutputStream outputStream) { final URL url = getClass().getClassLoader().getResource(fileName); final File file = new File(url.getPath()); From 3d9dc3277b2bf665f2523e85f65fad7d4bbfd81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 16:03:44 +0900 Subject: [PATCH 10/29] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=8B=9C=20POST=20=EC=9A=94=EC=B2=AD=EC=9D=84=20=EB=B3=B4?= =?UTF-8?q?=EB=82=B4=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 25 ++++++++++--------- tomcat/src/main/resources/static/login.html | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index ba889b7458..bb3da43876 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -18,8 +18,6 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; public class Http11Processor implements Runnable, Processor { @@ -52,19 +50,22 @@ public void process(final Socket connection) { return; } if(urlPath.startsWith("/login")) { - if(urlPath.equals("/login")) { + if(urlPath.equals("/login") && httpMethod.equals("GET")) { printFileResource("static" + urlPath +".html", outputStream); return; } - String parameters = urlPath.substring(7); - String account = parameters.split("&")[0].split("=")[1]; - String password = parameters.split("&")[1].split("=")[1]; - - User user = InMemoryUserRepository.findByAccount(account) - .orElse(new User("guest", "guest", "guest")); - if(user.checkPassword(password)) { - redirect("http://localhost:8080/index.html", outputStream); - return; + String body = parseBody(reader); + if (body != null) { + System.out.println("body = " + body); + String account = body.split("&")[0].split("=")[1]; + String password = body.split("&")[1].split("=")[1]; + + User user = InMemoryUserRepository.findByAccount(account) + .orElse(new User("guest", "guest", "guest")); + if (user.checkPassword(password)) { + redirect("http://localhost:8080/index.html", outputStream); + return; + } } redirect("http://localhost:8080/401.html", outputStream); return; diff --git a/tomcat/src/main/resources/static/login.html b/tomcat/src/main/resources/static/login.html index f4ed9de875..bc933357f2 100644 --- a/tomcat/src/main/resources/static/login.html +++ b/tomcat/src/main/resources/static/login.html @@ -20,7 +20,7 @@

로그인

-
+
From 88c4a058a5da33cdea8e9d57dcb6dcad0fccd6b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 16:19:04 +0900 Subject: [PATCH 11/29] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=8B=9C=20=EC=84=B8=EC=85=98=20=EC=95=84=EC=9D=B4=EB=94=94?= =?UTF-8?q?=EB=A5=BC=20=EC=BF=A0=ED=82=A4=EC=97=90=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index bb3da43876..4dd4df40db 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -63,7 +63,7 @@ public void process(final Socket connection) { User user = InMemoryUserRepository.findByAccount(account) .orElse(new User("guest", "guest", "guest")); if (user.checkPassword(password)) { - redirect("http://localhost:8080/index.html", outputStream); + redirectWithSetCookie("http://localhost:8080/index.html", outputStream); return; } } @@ -166,4 +166,21 @@ private void redirect(String location, OutputStream outputStream) { log.error(e.getMessage(), e); } } + + private void redirectWithSetCookie(String location, OutputStream outputStream) { + try { + String sessionId = "JSESSIONID=sessionId"; + String contentType = "text/html"; + var response = "HTTP/1.1 302 Found \r\n" + + "Set-Cookie: " + sessionId + " \r\n" + + "Location: " + location + " \r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: 0"; + + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException | UncheckedServletException e) { + log.error(e.getMessage(), e); + } + } } From b0da4a617acb9e13db417ba65a784f066f52f730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 16:29:16 +0900 Subject: [PATCH 12/29] =?UTF-8?q?feat:=20=EC=BF=A0=ED=82=A4=EC=97=90=20JSE?= =?UTF-8?q?SSIONID=EA=B0=80=20=EC=97=86=EC=9D=84=20=EB=95=8C=20Set-Cookie?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=9C=EA=B8=89=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 4dd4df40db..5c6c5fe596 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -18,12 +18,16 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; public class Http11Processor implements Runnable, Processor { private static final Logger log = LoggerFactory.getLogger(Http11Processor.class); private final Socket connection; + private static final Map httpRequestHeader = new HashMap<>(); + private static final String sessionId = "JSESSIONID=sessionId"; public Http11Processor(final Socket connection) { this.connection = connection; @@ -42,9 +46,15 @@ public void process(final Socket connection) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String line = reader.readLine(); String httpMethod = line.split(" ")[0]; String urlPath = line.split(" ")[1]; + + while((line = reader.readLine()) != null && !line.isEmpty()) { + httpRequestHeader.put(line.split(":")[0].trim(), line.split(":")[1].trim()); + } + if(urlPath.endsWith("html")) { printFileResource("static" + urlPath, outputStream); return; @@ -109,14 +119,7 @@ public void process(final Socket connection) { } private String parseBody(BufferedReader reader) throws IOException { - String line; - int contentLength = 0; - while((line = reader.readLine()) != null && !line.isEmpty()) { - if(line.startsWith("Content-Length")) { - contentLength = Integer.parseInt(line.split(":")[1].trim()); - } - } - + int contentLength = Integer.parseInt(httpRequestHeader.get("Content-Length")); if(contentLength > 0) { char[] body = new char[contentLength]; reader.read(body, 0, contentLength); @@ -139,14 +142,26 @@ private void printFileResource(String fileName, OutputStream outputStream) { } final var responseBody = new String(Files.readAllBytes(path)); - var response = "HTTP/1.1 200 OK \r\n" + - String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - "Content-Length: " + responseBody.getBytes().length + " \r\n" + - "\r\n"+ - responseBody; + if(httpRequestHeader.get("Cookie").contains("JSESSIONID")) { + var response = "HTTP/1.1 200 OK \r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: " + responseBody.getBytes().length + " \r\n" + + "\r\n"+ + responseBody; + outputStream.write(response.getBytes()); + outputStream.flush(); + } else { + var response = "HTTP/1.1 200 OK \r\n" + + "Set-Cookie: " + sessionId + " \r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: " + responseBody.getBytes().length + " \r\n" + + "\r\n"+ + responseBody; + outputStream.write(response.getBytes()); + outputStream.flush(); + + } - outputStream.write(response.getBytes()); - outputStream.flush(); } catch (IOException | UncheckedServletException e) { log.error(e.getMessage(), e); } @@ -169,7 +184,6 @@ private void redirect(String location, OutputStream outputStream) { private void redirectWithSetCookie(String location, OutputStream outputStream) { try { - String sessionId = "JSESSIONID=sessionId"; String contentType = "text/html"; var response = "HTTP/1.1 302 Found \r\n" + "Set-Cookie: " + sessionId + " \r\n" + From 795a568c7e7c95a6ac7911411810be4ddeb985f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 17:22:55 +0900 Subject: [PATCH 13/29] =?UTF-8?q?feat:=20=EC=9D=B4=EB=AF=B8=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=ED=95=9C=20=ED=9A=8C=EC=9B=90=EC=9D=98=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=EC=97=90=20=EC=A0=91=EA=B7=BC=20=EB=B6=88?= =?UTF-8?q?=EA=B0=80=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 21 +++++++++++++++- .../org/apache/coyote/session/Session.java | 11 +++++++++ .../apache/coyote/session/SessionManager.java | 24 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tomcat/src/main/java/org/apache/coyote/session/Session.java create mode 100644 tomcat/src/main/java/org/apache/coyote/session/SessionManager.java diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 5c6c5fe596..e5f3067a16 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -6,6 +6,7 @@ import com.techcourse.model.User; import org.apache.coyote.Processor; +import org.apache.coyote.session.SessionManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,8 +19,10 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.Optional; public class Http11Processor implements Runnable, Processor { @@ -61,6 +64,20 @@ public void process(final Socket connection) { } if(urlPath.startsWith("/login")) { if(urlPath.equals("/login") && httpMethod.equals("GET")) { + if(httpRequestHeader.get("Cookie") != null) { + Optional sessionCookie = Arrays.asList(httpRequestHeader.get("Cookie").split("; ")) + .stream() + .filter(cookie -> cookie.startsWith("JSESSIONID")) + .findAny(); + if(!sessionCookie.isEmpty()) { + String sessionId = sessionCookie.get().split("=")[1]; + Optional userBySession = SessionManager.findUserBySession(sessionId); + if(userBySession.isPresent()) { + redirect("http://localhost:8080/index.html", outputStream); + return; + } + } + } printFileResource("static" + urlPath +".html", outputStream); return; } @@ -73,6 +90,7 @@ public void process(final Socket connection) { User user = InMemoryUserRepository.findByAccount(account) .orElse(new User("guest", "guest", "guest")); if (user.checkPassword(password)) { + SessionManager.createSession(user); redirectWithSetCookie("http://localhost:8080/index.html", outputStream); return; } @@ -142,7 +160,8 @@ private void printFileResource(String fileName, OutputStream outputStream) { } final var responseBody = new String(Files.readAllBytes(path)); - if(httpRequestHeader.get("Cookie").contains("JSESSIONID")) { + String cookie = httpRequestHeader.get("Cookie"); + if(cookie != null && cookie.contains("JSESSIONID")) { var response = "HTTP/1.1 200 OK \r\n" + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + "Content-Length: " + responseBody.getBytes().length + " \r\n" + diff --git a/tomcat/src/main/java/org/apache/coyote/session/Session.java b/tomcat/src/main/java/org/apache/coyote/session/Session.java new file mode 100644 index 0000000000..a750a229d4 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/session/Session.java @@ -0,0 +1,11 @@ +package org.apache.coyote.session; + +import com.techcourse.model.User; + +public class Session { + private final String sessionId; + + public Session(String sessionId) { + this.sessionId = sessionId; + } +} diff --git a/tomcat/src/main/java/org/apache/coyote/session/SessionManager.java b/tomcat/src/main/java/org/apache/coyote/session/SessionManager.java new file mode 100644 index 0000000000..4c07c882df --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/session/SessionManager.java @@ -0,0 +1,24 @@ +package org.apache.coyote.session; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import com.techcourse.model.User; + +public final class SessionManager { + private static final Map sessions = new HashMap<>(); + + public static void createSession(User user) { + String sessionId = "sessionId"; + sessions.put(sessionId, user); + } + + public static Optional findUserBySession(String sessionId) { + User user = sessions.getOrDefault(sessionId, null); + if(user == null) { + return Optional.empty(); + } + return Optional.of(user); + } +} From 9692263f83a0a5c9e6f17b3e58b925551ab7489a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Thu, 5 Sep 2024 17:35:28 +0900 Subject: [PATCH 14/29] =?UTF-8?q?refactor:=20=EC=BF=A0=ED=82=A4=EA=B0=80?= =?UTF-8?q?=20=EC=97=86=EC=9D=84=20=EB=95=8C=20=EC=9D=BC=EB=B0=98=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=EC=97=90=20=EC=A0=91=EA=B7=BC?= =?UTF-8?q?=ED=95=98=EB=A9=B4=20=EC=84=B8=EC=85=98=20=EC=95=84=EC=9D=B4?= =?UTF-8?q?=EB=94=94=EB=A5=BC=20=EC=A3=BC=EC=A7=80=20=EC=95=8A=EC=9D=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index e5f3067a16..ad2afc9a6d 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -160,27 +160,13 @@ private void printFileResource(String fileName, OutputStream outputStream) { } final var responseBody = new String(Files.readAllBytes(path)); - String cookie = httpRequestHeader.get("Cookie"); - if(cookie != null && cookie.contains("JSESSIONID")) { - var response = "HTTP/1.1 200 OK \r\n" + - String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - "Content-Length: " + responseBody.getBytes().length + " \r\n" + - "\r\n"+ - responseBody; - outputStream.write(response.getBytes()); - outputStream.flush(); - } else { - var response = "HTTP/1.1 200 OK \r\n" + - "Set-Cookie: " + sessionId + " \r\n" + - String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - "Content-Length: " + responseBody.getBytes().length + " \r\n" + - "\r\n"+ - responseBody; - outputStream.write(response.getBytes()); - outputStream.flush(); - - } - + var response = "HTTP/1.1 200 OK \r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: " + responseBody.getBytes().length + " \r\n" + + "\r\n"+ + responseBody; + outputStream.write(response.getBytes()); + outputStream.flush(); } catch (IOException | UncheckedServletException e) { log.error(e.getMessage(), e); } From 66597e3bdd84ff1d413caf844e98165b94aea61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Fri, 6 Sep 2024 17:12:53 +0900 Subject: [PATCH 15/29] =?UTF-8?q?feat:=20HTTP=20=ED=99=9C=EC=9A=A9=20?= =?UTF-8?q?=ED=95=99=EC=8A=B5=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=A7=84?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/com/example/GreetingController.java | 15 +++++++++++---- .../com/example/etag/EtagFilterConfiguration.java | 13 +++++++++---- .../example/version/CacheBustingWebConfig.java | 9 ++++++++- study/src/main/resources/application.yml | 2 ++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/study/src/main/java/cache/com/example/GreetingController.java b/study/src/main/java/cache/com/example/GreetingController.java index c0053cda42..99aa9aadcc 100644 --- a/study/src/main/java/cache/com/example/GreetingController.java +++ b/study/src/main/java/cache/com/example/GreetingController.java @@ -2,6 +2,7 @@ import org.springframework.http.CacheControl; import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -9,10 +10,14 @@ @Controller public class GreetingController { + private static final String eTag = "33a64df551425fcc55e4d42a148795d9f25f89d4"; @GetMapping("/") - public String index() { - return "index"; + public ResponseEntity index() { + return ResponseEntity.ok() + .cacheControl(CacheControl.noCache().cachePrivate()) + .header("Content-Encoding", "gzip") + .body("index"); } /** @@ -29,8 +34,10 @@ public String cacheControl(final HttpServletResponse response) { } @GetMapping("/etag") - public String etag() { - return "index"; + public ResponseEntity etag() { + return ResponseEntity.ok() + .eTag(eTag) + .body("index"); } @GetMapping("/resource-versioning") diff --git a/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java b/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java index 41ef7a3d9a..e955da0b2a 100644 --- a/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java +++ b/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java @@ -1,12 +1,17 @@ package cache.com.example.etag; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.ShallowEtagHeaderFilter; @Configuration public class EtagFilterConfiguration { -// @Bean -// public FilterRegistrationBean shallowEtagHeaderFilter() { -// return null; -// } + @Bean + public FilterRegistrationBean shallowEtagHeaderFilter() { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(new ShallowEtagHeaderFilter()); + filterRegistrationBean.addUrlPatterns("/*"); + return filterRegistrationBean; + } } diff --git a/study/src/main/java/cache/com/example/version/CacheBustingWebConfig.java b/study/src/main/java/cache/com/example/version/CacheBustingWebConfig.java index 6da6d2c795..a4297c335b 100644 --- a/study/src/main/java/cache/com/example/version/CacheBustingWebConfig.java +++ b/study/src/main/java/cache/com/example/version/CacheBustingWebConfig.java @@ -1,9 +1,13 @@ package cache.com.example.version; +import java.util.concurrent.TimeUnit; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.http.CacheControl; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.resource.VersionResourceResolver; @Configuration public class CacheBustingWebConfig implements WebMvcConfigurer { @@ -20,6 +24,9 @@ public CacheBustingWebConfig(ResourceVersion version) { @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler(PREFIX_STATIC_RESOURCES + "/" + version.getVersion() + "/**") - .addResourceLocations("classpath:/static/"); + .addResourceLocations("classpath:/static/") + .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic()) + .resourceChain(false) + .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")); } } diff --git a/study/src/main/resources/application.yml b/study/src/main/resources/application.yml index e3503a5fb9..4a38f2f811 100644 --- a/study/src/main/resources/application.yml +++ b/study/src/main/resources/application.yml @@ -8,3 +8,5 @@ server: threads: min-spare: 2 max: 2 + compression: + enabled: true From acde025d667c985c1eaee5d5ec535332f689d7c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Fri, 6 Sep 2024 17:45:46 +0900 Subject: [PATCH 16/29] =?UTF-8?q?refactor:=20=EB=AD=89=EC=B9=9C=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A5=BC=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=ED=95=98=EC=97=AC=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 316 +++++++++--------- .../coyote/http11/request/HttpRequest.java | 55 +++ .../coyote/http11/request/RequestHandler.java | 108 ++++++ .../http11/response/ResponseHandler.java | 91 +++++ .../coyote/{ => http11}/session/Session.java | 2 +- .../{ => http11}/session/SessionManager.java | 11 +- 6 files changed, 426 insertions(+), 157 deletions(-) create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java rename tomcat/src/main/java/org/apache/coyote/{ => http11}/session/Session.java (79%) rename tomcat/src/main/java/org/apache/coyote/{ => http11}/session/SessionManager.java (64%) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index ad2afc9a6d..3729cfb2e9 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -6,7 +6,9 @@ import com.techcourse.model.User; import org.apache.coyote.Processor; -import org.apache.coyote.session.SessionManager; +import org.apache.coyote.http11.request.HttpRequest; +import org.apache.coyote.http11.request.RequestHandler; +import org.apache.coyote.http11.session.SessionManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,158 +50,174 @@ public void process(final Socket connection) { final var outputStream = connection.getOutputStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + HttpRequest httpRequest = new HttpRequest(reader); + RequestHandler requestHandler = new RequestHandler(httpRequest, outputStream); + requestHandler.handleRequest(); - String line = reader.readLine(); - String httpMethod = line.split(" ")[0]; - String urlPath = line.split(" ")[1]; - - while((line = reader.readLine()) != null && !line.isEmpty()) { - httpRequestHeader.put(line.split(":")[0].trim(), line.split(":")[1].trim()); - } - - if(urlPath.endsWith("html")) { - printFileResource("static" + urlPath, outputStream); - return; - } - if(urlPath.startsWith("/login")) { - if(urlPath.equals("/login") && httpMethod.equals("GET")) { - if(httpRequestHeader.get("Cookie") != null) { - Optional sessionCookie = Arrays.asList(httpRequestHeader.get("Cookie").split("; ")) - .stream() - .filter(cookie -> cookie.startsWith("JSESSIONID")) - .findAny(); - if(!sessionCookie.isEmpty()) { - String sessionId = sessionCookie.get().split("=")[1]; - Optional userBySession = SessionManager.findUserBySession(sessionId); - if(userBySession.isPresent()) { - redirect("http://localhost:8080/index.html", outputStream); - return; - } - } - } - printFileResource("static" + urlPath +".html", outputStream); - return; - } - String body = parseBody(reader); - if (body != null) { - System.out.println("body = " + body); - String account = body.split("&")[0].split("=")[1]; - String password = body.split("&")[1].split("=")[1]; - - User user = InMemoryUserRepository.findByAccount(account) - .orElse(new User("guest", "guest", "guest")); - if (user.checkPassword(password)) { - SessionManager.createSession(user); - redirectWithSetCookie("http://localhost:8080/index.html", outputStream); - return; - } - } - redirect("http://localhost:8080/401.html", outputStream); - return; - } - if(urlPath.startsWith("/register")) { - if(httpMethod.equals("GET")) { - printFileResource("static" + urlPath + ".html", outputStream); - return; - } - String body = parseBody(reader); - if (body != null) { - String account = body.split("&")[0].split("=")[1]; - String mail = body.split("&")[1].split("=")[1]; - String password = body.split("&")[2].split("=")[1]; - User user = new User(account, mail, password); - InMemoryUserRepository.save(user); - redirect("http://localhost:8080/index.html", outputStream); - return; - } - } - - if(urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { - printFileResource("static" + urlPath, outputStream); - return; - } - - final var responseBody = "Hello world!"; - - final var response = String.join("\r\n", - "HTTP/1.1 200 OK ", - "Content-Type: text/html;charset=utf-8 ", - "Content-Length: " + responseBody.getBytes().length + " ", - "", - responseBody); - - outputStream.write(response.getBytes()); - outputStream.flush(); - } catch (IOException | UncheckedServletException e) { + } catch (IOException e) { log.error(e.getMessage(), e); } } - private String parseBody(BufferedReader reader) throws IOException { - int contentLength = Integer.parseInt(httpRequestHeader.get("Content-Length")); - if(contentLength > 0) { - char[] body = new char[contentLength]; - reader.read(body, 0, contentLength); - return new String(body); - } - return null; - } - - private void printFileResource(String fileName, OutputStream outputStream) { - final URL url = getClass().getClassLoader().getResource(fileName); - final File file = new File(url.getPath()); - final Path path = file.toPath(); - - try { - String contentType = "text/html"; - if(fileName.endsWith("css")) { - contentType = "text/css"; - } else if(fileName.endsWith("js")) { - contentType = "application/javascript"; - } - - final var responseBody = new String(Files.readAllBytes(path)); - var response = "HTTP/1.1 200 OK \r\n" + - String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - "Content-Length: " + responseBody.getBytes().length + " \r\n" + - "\r\n"+ - responseBody; - outputStream.write(response.getBytes()); - outputStream.flush(); - } catch (IOException | UncheckedServletException e) { - log.error(e.getMessage(), e); - } - } - - private void redirect(String location, OutputStream outputStream) { - try { - String contentType = "text/html"; - var response = "HTTP/1.1 302 Found \r\n" + - "Location: " + location + "\r\n" + - String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - "Content-Length: 0"; - - outputStream.write(response.getBytes()); - outputStream.flush(); - } catch (IOException | UncheckedServletException e) { - log.error(e.getMessage(), e); - } - } - - private void redirectWithSetCookie(String location, OutputStream outputStream) { - try { - String contentType = "text/html"; - var response = "HTTP/1.1 302 Found \r\n" + - "Set-Cookie: " + sessionId + " \r\n" + - "Location: " + location + " \r\n" + - String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - "Content-Length: 0"; - - outputStream.write(response.getBytes()); - outputStream.flush(); - } catch (IOException | UncheckedServletException e) { - log.error(e.getMessage(), e); - } - } + // @Override + // public void process(final Socket connection) { + // try (final var inputStream = connection.getInputStream(); + // final var outputStream = connection.getOutputStream()) { + // + // BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + // + // + // String line = reader.readLine(); + // String httpMethod = line.split(" ")[0]; + // String urlPath = line.split(" ")[1]; + // + // while((line = reader.readLine()) != null && !line.isEmpty()) { + // httpRequestHeader.put(line.split(":")[0].trim(), line.split(":")[1].trim()); + // } + // + // if(urlPath.endsWith("html")) { + // printFileResource("static" + urlPath, outputStream); + // return; + // } + // if(urlPath.startsWith("/login")) { + // if(urlPath.equals("/login") && httpMethod.equals("GET")) { + // if(httpRequestHeader.get("Cookie") != null) { + // Optional sessionCookie = Arrays.asList(httpRequestHeader.get("Cookie").split("; ")) + // .stream() + // .filter(cookie -> cookie.startsWith("JSESSIONID")) + // .findAny(); + // if(!sessionCookie.isEmpty()) { + // String sessionId = sessionCookie.get().split("=")[1]; + // Optional userBySession = SessionManager.findUserBySession(sessionId); + // if(userBySession.isPresent()) { + // redirect("http://localhost:8080/index.html", outputStream); + // return; + // } + // } + // } + // printFileResource("static" + urlPath +".html", outputStream); + // return; + // } + // String body = parseBody(reader); + // if (body != null) { + // System.out.println("body = " + body); + // String account = body.split("&")[0].split("=")[1]; + // String password = body.split("&")[1].split("=")[1]; + // + // User user = InMemoryUserRepository.findByAccount(account) + // .orElse(new User("guest", "guest", "guest")); + // if (user.checkPassword(password)) { + // SessionManager.createSession(user); + // redirectWithSetCookie("http://localhost:8080/index.html", outputStream); + // return; + // } + // } + // redirect("http://localhost:8080/401.html", outputStream); + // return; + // } + // if(urlPath.startsWith("/register")) { + // if(httpMethod.equals("GET")) { + // printFileResource("static" + urlPath + ".html", outputStream); + // return; + // } + // String body = parseBody(reader); + // if (body != null) { + // String account = body.split("&")[0].split("=")[1]; + // String mail = body.split("&")[1].split("=")[1]; + // String password = body.split("&")[2].split("=")[1]; + // User user = new User(account, mail, password); + // InMemoryUserRepository.save(user); + // redirect("http://localhost:8080/index.html", outputStream); + // return; + // } + // } + // + // if(urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { + // printFileResource("static" + urlPath, outputStream); + // return; + // } + // + // final var responseBody = "Hello world!"; + // + // final var response = String.join("\r\n", + // "HTTP/1.1 200 OK ", + // "Content-Type: text/html;charset=utf-8 ", + // "Content-Length: " + responseBody.getBytes().length + " ", + // "", + // responseBody); + // + // outputStream.write(response.getBytes()); + // outputStream.flush(); + // } catch (IOException | UncheckedServletException e) { + // log.error(e.getMessage(), e); + // } + // } + // + // private String parseBody(BufferedReader reader) throws IOException { + // int contentLength = Integer.parseInt(httpRequestHeader.get("Content-Length")); + // if(contentLength > 0) { + // char[] body = new char[contentLength]; + // reader.read(body, 0, contentLength); + // return new String(body); + // } + // return null; + // } + // + // private void printFileResource(String fileName, OutputStream outputStream) { + // final URL url = getClass().getClassLoader().getResource(fileName); + // final File file = new File(url.getPath()); + // final Path path = file.toPath(); + // + // try { + // String contentType = "text/html"; + // if(fileName.endsWith("css")) { + // contentType = "text/css"; + // } else if(fileName.endsWith("js")) { + // contentType = "application/javascript"; + // } + // + // final var responseBody = new String(Files.readAllBytes(path)); + // var response = "HTTP/1.1 200 OK \r\n" + + // String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + // "Content-Length: " + responseBody.getBytes().length + " \r\n" + + // "\r\n"+ + // responseBody; + // outputStream.write(response.getBytes()); + // outputStream.flush(); + // } catch (IOException | UncheckedServletException e) { + // log.error(e.getMessage(), e); + // } + // } + // + // private void redirect(String location, OutputStream outputStream) { + // try { + // String contentType = "text/html"; + // var response = "HTTP/1.1 302 Found \r\n" + + // "Location: " + location + "\r\n" + + // String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + // "Content-Length: 0"; + // + // outputStream.write(response.getBytes()); + // outputStream.flush(); + // } catch (IOException | UncheckedServletException e) { + // log.error(e.getMessage(), e); + // } + // } + // + // private void redirectWithSetCookie(String location, OutputStream outputStream) { + // try { + // String contentType = "text/html"; + // var response = "HTTP/1.1 302 Found \r\n" + + // "Set-Cookie: " + sessionId + " \r\n" + + // "Location: " + location + " \r\n" + + // String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + // "Content-Length: 0"; + // + // outputStream.write(response.getBytes()); + // outputStream.flush(); + // } catch (IOException | UncheckedServletException e) { + // log.error(e.getMessage(), e); + // } + // } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java new file mode 100644 index 0000000000..8c0dfae008 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java @@ -0,0 +1,55 @@ +package org.apache.coyote.http11.request; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class HttpRequest { + + private final String method; + private final String path; + private final Map headers = new HashMap<>(); + private final String body; + + public HttpRequest(BufferedReader reader) throws IOException { + String initialLine = reader.readLine(); + this.method = initialLine.split(" ")[0]; + this.path = initialLine.split(" ")[1]; + String line; + while ((line = reader.readLine()) != null && !line.isEmpty()) { + String[] header = line.split(":"); + headers.put(header[0].trim(), header[1].trim()); + } + this.body = parseBody(reader); + } + + private String parseBody(BufferedReader reader) throws IOException { + if(headers.get("Content-Length") == null) { + return null; + } + int contentLength = Integer.parseInt(headers.get("Content-Length")); + if(contentLength > 0) { + char[] body = new char[contentLength]; + reader.read(body, 0, contentLength); + return new String(body); + } + return null; + } + + public String getMethod() { + return method; + } + + public String getPath() { + return path; + } + + public String getHeader(String name) { + return headers.get(name); + } + + public String getBody() { + return body; + } +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java new file mode 100644 index 0000000000..d334893239 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java @@ -0,0 +1,108 @@ +package org.apache.coyote.http11.request; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.Optional; + +import org.apache.coyote.http11.response.ResponseHandler; +import org.apache.coyote.http11.session.SessionManager; + +import com.techcourse.db.InMemoryUserRepository; +import com.techcourse.model.User; + +public class RequestHandler { + private HttpRequest httpRequest; + private final OutputStream outputStream; + + public RequestHandler(HttpRequest httpRequest, OutputStream outputStream) { + this.httpRequest = httpRequest; + this.outputStream = outputStream; + } + + public void handleRequest() throws IOException { + String httpMethod = httpRequest.getMethod(); + String urlPath = httpRequest.getPath(); + + if (urlPath.endsWith("html")) { + ResponseHandler.printFileResource("static" + urlPath, outputStream); + } else if (urlPath.startsWith("/login")) { + handleLoginRequest(httpMethod, urlPath); + } else if (urlPath.startsWith("/register")) { + handleRegisterRequest(httpMethod, urlPath); + } else if (urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { + ResponseHandler.printFileResource("static" + urlPath, outputStream); + } else { + sendHelloWorldResponse(); + } + } + + private void handleLoginRequest(String httpMethod, String urlPath) throws IOException { + if(urlPath.equals("/login") && httpMethod.equals("GET")) { + if(httpRequest.getHeader("Cookie") != null) { + Optional sessionCookie = Arrays.asList(httpRequest.getHeader("Cookie").split("; ")) + .stream() + .filter(cookie -> cookie.startsWith("JSESSIONID")) + .findAny(); + if(!sessionCookie.isEmpty()) { + String sessionId = sessionCookie.get().split("=")[1]; + Optional userBySession = SessionManager.findUserBySession(sessionId); + if(userBySession.isPresent()) { + ResponseHandler.redirect("http://localhost:8080/index.html", outputStream); + return; + } + } + } + ResponseHandler.printFileResource("static" + urlPath +".html", outputStream); + } else if (httpMethod.equals("POST")) { + login(); + } + } + + private void login() { + String body = httpRequest.getBody(); + if (body != null) { + String account = body.split("&")[0].split("=")[1]; + String password = body.split("&")[1].split("=")[1]; + User user = InMemoryUserRepository.findByAccount(account) + .orElse(new User("guest", "guest", "guest")); + if (user.checkPassword(password)) { + SessionManager.createSession(user); + ResponseHandler.redirectWithSetCookie("http://localhost:8080/index.html", outputStream); + return; + } + } + ResponseHandler.redirect("http://localhost:8080/401.html", outputStream); + return; + } + + + private void handleRegisterRequest(String httpMethod, String urlPath) throws IOException { + if (httpMethod.equals("GET")) { + ResponseHandler.printFileResource("static" + urlPath + ".html", outputStream); + return; + } + String body = httpRequest.getBody(); + if (body != null) { + String account = body.split("&")[0].split("=")[1]; + String mail = body.split("&")[1].split("=")[1]; + String password = body.split("&")[2].split("=")[1]; + User user = new User(account, mail, password); + InMemoryUserRepository.save(user); + ResponseHandler.redirect("http://localhost:8080/index.html", outputStream); + return; + } + } + + private void sendHelloWorldResponse() throws IOException { + String responseBody = "Hello world!"; + String response = String.join("\r\n", + "HTTP/1.1 200 OK", + "Content-Type: text/html;charset=utf-8", + "Content-Length: " + responseBody.getBytes().length, + "", + responseBody); + outputStream.write(response.getBytes()); + outputStream.flush(); + } +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java new file mode 100644 index 0000000000..13b3622e7e --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java @@ -0,0 +1,91 @@ +package org.apache.coyote.http11.response; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URL; +import java.nio.file.Files; +import java.util.UUID; + +import org.apache.coyote.http11.Http11Processor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.techcourse.exception.UncheckedServletException; + +public class ResponseHandler { + + private static final Logger log = LoggerFactory.getLogger(ResponseHandler.class); + + public static void printFileResource(String fileName, OutputStream outputStream) { + final URL url = ResponseHandler.class.getClassLoader().getResource(fileName); + if (url == null) { + sendNotFoundResponse(outputStream); + return; + } + try { + File file = new File(url.getPath()); + String contentType = determineContentType(fileName); + String responseBody = new String(Files.readAllBytes(file.toPath())); + String response = String.join("\r\n", + "HTTP/1.1 200 OK", + "Content-Type: " + contentType + ";charset=utf-8", + "Content-Length: " + responseBody.getBytes().length, + "", + responseBody); + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static String determineContentType(String fileName) { + if (fileName.endsWith("css")) return "text/css"; + if (fileName.endsWith("js")) return "application/javascript"; + return "text/html"; + } + + private static void sendNotFoundResponse(OutputStream outputStream) { + try { + String response = "HTTP/1.1 404 Not Found \r\n" + + "Content-Length: 0 \r\n" + + "\r\n"; + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void redirect(String location, OutputStream outputStream) { + try { + String contentType = "text/html"; + var response = "HTTP/1.1 302 Found \r\n" + + "Location: " + location + "\r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: 0"; + + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException | UncheckedServletException e) { + log.error(e.getMessage(), e); + } + } + + public static void redirectWithSetCookie(String location, OutputStream outputStream) { + try { + String contentType = "text/html"; + var response = "HTTP/1.1 302 Found \r\n" + + "Set-Cookie: " + UUID.randomUUID().toString() + " \r\n" + + "Location: " + location + " \r\n" + + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + + "Content-Length: 0"; + + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException | UncheckedServletException e) { + log.error(e.getMessage(), e); + } + } +} diff --git a/tomcat/src/main/java/org/apache/coyote/session/Session.java b/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java similarity index 79% rename from tomcat/src/main/java/org/apache/coyote/session/Session.java rename to tomcat/src/main/java/org/apache/coyote/http11/session/Session.java index a750a229d4..48b4a75b45 100644 --- a/tomcat/src/main/java/org/apache/coyote/session/Session.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java @@ -1,4 +1,4 @@ -package org.apache.coyote.session; +package org.apache.coyote.http11.session; import com.techcourse.model.User; diff --git a/tomcat/src/main/java/org/apache/coyote/session/SessionManager.java b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java similarity index 64% rename from tomcat/src/main/java/org/apache/coyote/session/SessionManager.java rename to tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java index 4c07c882df..64961ac4b9 100644 --- a/tomcat/src/main/java/org/apache/coyote/session/SessionManager.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java @@ -1,8 +1,9 @@ -package org.apache.coyote.session; +package org.apache.coyote.http11.session; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.UUID; import com.techcourse.model.User; @@ -10,15 +11,11 @@ public final class SessionManager { private static final Map sessions = new HashMap<>(); public static void createSession(User user) { - String sessionId = "sessionId"; + String sessionId = UUID.randomUUID().toString(); sessions.put(sessionId, user); } public static Optional findUserBySession(String sessionId) { - User user = sessions.getOrDefault(sessionId, null); - if(user == null) { - return Optional.empty(); - } - return Optional.of(user); + return Optional.ofNullable(sessions.get(sessionId)); } } From df161b15338b9f41a8871450b4de8c12960244d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Fri, 6 Sep 2024 17:55:46 +0900 Subject: [PATCH 17/29] =?UTF-8?q?fix:=20JSESSIONID=EB=A5=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=ED=95=98=EC=A7=80=20=EC=95=8A=EC=95=84=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/apache/coyote/http11/request/RequestHandler.java | 2 +- .../java/org/apache/coyote/http11/response/ResponseHandler.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java index d334893239..c5210e8156 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java @@ -37,7 +37,7 @@ public void handleRequest() throws IOException { } } - private void handleLoginRequest(String httpMethod, String urlPath) throws IOException { + private void handleLoginRequest(String httpMethod, String urlPath) { if(urlPath.equals("/login") && httpMethod.equals("GET")) { if(httpRequest.getHeader("Cookie") != null) { Optional sessionCookie = Arrays.asList(httpRequest.getHeader("Cookie").split("; ")) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java index 13b3622e7e..15712b0d12 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java @@ -77,7 +77,7 @@ public static void redirectWithSetCookie(String location, OutputStream outputStr try { String contentType = "text/html"; var response = "HTTP/1.1 302 Found \r\n" + - "Set-Cookie: " + UUID.randomUUID().toString() + " \r\n" + + "Set-Cookie: JSESSIONID" + UUID.randomUUID().toString() + " \r\n" + "Location: " + location + " \r\n" + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + "Content-Length: 0"; From 5dc81b2cfaf12980b340f1926aed4c491be03390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sat, 7 Sep 2024 14:43:25 +0900 Subject: [PATCH 18/29] =?UTF-8?q?fix:=20=EC=84=B8=EC=85=98=20=EC=95=84?= =?UTF-8?q?=EC=9D=B4=EB=94=94=EA=B0=80=20=EB=8B=AC=EB=9D=BC=EC=84=9C=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=8B=9C=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=ED=8E=98=EC=9D=B4=EC=A7=80=EA=B0=80=20=EC=A0=91?= =?UTF-8?q?=EC=86=8D=EB=90=98=EB=8A=94=20=ED=98=84=EC=83=81=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/apache/coyote/http11/request/RequestHandler.java | 4 ++-- .../org/apache/coyote/http11/response/ResponseHandler.java | 4 ++-- .../java/org/apache/coyote/http11/session/SessionManager.java | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java index c5210e8156..fe37dcf1c4 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java @@ -67,8 +67,8 @@ private void login() { User user = InMemoryUserRepository.findByAccount(account) .orElse(new User("guest", "guest", "guest")); if (user.checkPassword(password)) { - SessionManager.createSession(user); - ResponseHandler.redirectWithSetCookie("http://localhost:8080/index.html", outputStream); + String sessionId = SessionManager.createSession(user); + ResponseHandler.redirectWithSetCookie("http://localhost:8080/index.html", sessionId, outputStream); return; } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java index 15712b0d12..915034ffe2 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java @@ -73,11 +73,11 @@ public static void redirect(String location, OutputStream outputStream) { } } - public static void redirectWithSetCookie(String location, OutputStream outputStream) { + public static void redirectWithSetCookie(String location, String sessionId, OutputStream outputStream) { try { String contentType = "text/html"; var response = "HTTP/1.1 302 Found \r\n" + - "Set-Cookie: JSESSIONID" + UUID.randomUUID().toString() + " \r\n" + + "Set-Cookie: JSESSIONID=" + sessionId + " \r\n" + "Location: " + location + " \r\n" + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + "Content-Length: 0"; diff --git a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java index 64961ac4b9..ffeb4618ac 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java @@ -10,9 +10,10 @@ public final class SessionManager { private static final Map sessions = new HashMap<>(); - public static void createSession(User user) { + public static String createSession(User user) { String sessionId = UUID.randomUUID().toString(); sessions.put(sessionId, user); + return sessionId; } public static Optional findUserBySession(String sessionId) { From da09610db7156dd8fe2d1b45ab27a7b76c01ac32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sat, 7 Sep 2024 16:34:53 +0900 Subject: [PATCH 19/29] =?UTF-8?q?refactor:=20=EA=B8=B0=EC=A1=B4=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 161 ------------------ 1 file changed, 161 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 3729cfb2e9..390db0c886 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -59,165 +59,4 @@ public void process(final Socket connection) { log.error(e.getMessage(), e); } } - - // @Override - // public void process(final Socket connection) { - // try (final var inputStream = connection.getInputStream(); - // final var outputStream = connection.getOutputStream()) { - // - // BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - // - // - // String line = reader.readLine(); - // String httpMethod = line.split(" ")[0]; - // String urlPath = line.split(" ")[1]; - // - // while((line = reader.readLine()) != null && !line.isEmpty()) { - // httpRequestHeader.put(line.split(":")[0].trim(), line.split(":")[1].trim()); - // } - // - // if(urlPath.endsWith("html")) { - // printFileResource("static" + urlPath, outputStream); - // return; - // } - // if(urlPath.startsWith("/login")) { - // if(urlPath.equals("/login") && httpMethod.equals("GET")) { - // if(httpRequestHeader.get("Cookie") != null) { - // Optional sessionCookie = Arrays.asList(httpRequestHeader.get("Cookie").split("; ")) - // .stream() - // .filter(cookie -> cookie.startsWith("JSESSIONID")) - // .findAny(); - // if(!sessionCookie.isEmpty()) { - // String sessionId = sessionCookie.get().split("=")[1]; - // Optional userBySession = SessionManager.findUserBySession(sessionId); - // if(userBySession.isPresent()) { - // redirect("http://localhost:8080/index.html", outputStream); - // return; - // } - // } - // } - // printFileResource("static" + urlPath +".html", outputStream); - // return; - // } - // String body = parseBody(reader); - // if (body != null) { - // System.out.println("body = " + body); - // String account = body.split("&")[0].split("=")[1]; - // String password = body.split("&")[1].split("=")[1]; - // - // User user = InMemoryUserRepository.findByAccount(account) - // .orElse(new User("guest", "guest", "guest")); - // if (user.checkPassword(password)) { - // SessionManager.createSession(user); - // redirectWithSetCookie("http://localhost:8080/index.html", outputStream); - // return; - // } - // } - // redirect("http://localhost:8080/401.html", outputStream); - // return; - // } - // if(urlPath.startsWith("/register")) { - // if(httpMethod.equals("GET")) { - // printFileResource("static" + urlPath + ".html", outputStream); - // return; - // } - // String body = parseBody(reader); - // if (body != null) { - // String account = body.split("&")[0].split("=")[1]; - // String mail = body.split("&")[1].split("=")[1]; - // String password = body.split("&")[2].split("=")[1]; - // User user = new User(account, mail, password); - // InMemoryUserRepository.save(user); - // redirect("http://localhost:8080/index.html", outputStream); - // return; - // } - // } - // - // if(urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { - // printFileResource("static" + urlPath, outputStream); - // return; - // } - // - // final var responseBody = "Hello world!"; - // - // final var response = String.join("\r\n", - // "HTTP/1.1 200 OK ", - // "Content-Type: text/html;charset=utf-8 ", - // "Content-Length: " + responseBody.getBytes().length + " ", - // "", - // responseBody); - // - // outputStream.write(response.getBytes()); - // outputStream.flush(); - // } catch (IOException | UncheckedServletException e) { - // log.error(e.getMessage(), e); - // } - // } - // - // private String parseBody(BufferedReader reader) throws IOException { - // int contentLength = Integer.parseInt(httpRequestHeader.get("Content-Length")); - // if(contentLength > 0) { - // char[] body = new char[contentLength]; - // reader.read(body, 0, contentLength); - // return new String(body); - // } - // return null; - // } - // - // private void printFileResource(String fileName, OutputStream outputStream) { - // final URL url = getClass().getClassLoader().getResource(fileName); - // final File file = new File(url.getPath()); - // final Path path = file.toPath(); - // - // try { - // String contentType = "text/html"; - // if(fileName.endsWith("css")) { - // contentType = "text/css"; - // } else if(fileName.endsWith("js")) { - // contentType = "application/javascript"; - // } - // - // final var responseBody = new String(Files.readAllBytes(path)); - // var response = "HTTP/1.1 200 OK \r\n" + - // String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - // "Content-Length: " + responseBody.getBytes().length + " \r\n" + - // "\r\n"+ - // responseBody; - // outputStream.write(response.getBytes()); - // outputStream.flush(); - // } catch (IOException | UncheckedServletException e) { - // log.error(e.getMessage(), e); - // } - // } - // - // private void redirect(String location, OutputStream outputStream) { - // try { - // String contentType = "text/html"; - // var response = "HTTP/1.1 302 Found \r\n" + - // "Location: " + location + "\r\n" + - // String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - // "Content-Length: 0"; - // - // outputStream.write(response.getBytes()); - // outputStream.flush(); - // } catch (IOException | UncheckedServletException e) { - // log.error(e.getMessage(), e); - // } - // } - // - // private void redirectWithSetCookie(String location, OutputStream outputStream) { - // try { - // String contentType = "text/html"; - // var response = "HTTP/1.1 302 Found \r\n" + - // "Set-Cookie: " + sessionId + " \r\n" + - // "Location: " + location + " \r\n" + - // String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + - // "Content-Length: 0"; - // - // outputStream.write(response.getBytes()); - // outputStream.flush(); - // } catch (IOException | UncheckedServletException e) { - // log.error(e.getMessage(), e); - // } - // } } From 85007fd160b5b63fc45de5f69dc51ff347531be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sat, 7 Sep 2024 17:03:46 +0900 Subject: [PATCH 20/29] =?UTF-8?q?style:=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20=EB=B0=8F=20=EC=BB=A8=EB=B2=A4=EC=85=98=20=EB=A7=9E?= =?UTF-8?q?=EC=B6=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- study/src/test/java/study/FileTest.java | 6 ++---- .../apache/coyote/http11/controller/Controller.java | 4 ++++ .../coyote/http11/controller/LoginController.java | 4 ++++ .../apache/coyote/http11/request/RequestHandler.java | 3 ++- .../coyote/http11/servlet/DispatcherServlet.java | 12 ++++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java diff --git a/study/src/test/java/study/FileTest.java b/study/src/test/java/study/FileTest.java index f9ace62522..f8c0359a5e 100644 --- a/study/src/test/java/study/FileTest.java +++ b/study/src/test/java/study/FileTest.java @@ -57,11 +57,9 @@ class FileTest { final String fileName = "nextstep.txt"; // todo - URL url = getClass().getClassLoader().getResource(fileName); - File file = new File(url.getPath()); + final URL url = getClass().getClassLoader().getResource(fileName); + final File file = new File(url.getPath()); final Path path = file.toPath(); - // TODO: 왜 아래와 같이 하면 테스트가 통과하지 않을까? File 클래스를 꼭 써야하는 이유는? - // Path path = Paths.get(url.getPath()); // todo final List actual = Files.readAllLines(path); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java new file mode 100644 index 0000000000..94c71235e4 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java @@ -0,0 +1,4 @@ +package org.apache.coyote.http11.controller; + +public interface Controller { +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java new file mode 100644 index 0000000000..7996c85f18 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java @@ -0,0 +1,4 @@ +package org.apache.coyote.http11.controller; + +public class LoginController { +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java index fe37dcf1c4..94544eff92 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java @@ -12,7 +12,8 @@ import com.techcourse.model.User; public class RequestHandler { - private HttpRequest httpRequest; + + private final HttpRequest httpRequest; private final OutputStream outputStream; public RequestHandler(HttpRequest httpRequest, OutputStream outputStream) { diff --git a/tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java b/tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java new file mode 100644 index 0000000000..5f0e448955 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java @@ -0,0 +1,12 @@ +package org.apache.coyote.http11.servlet; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.coyote.http11.controller.Controller; + +public class DispatcherServlet { + private final Map controllers = new HashMap<>(); + + +} From f1aed308c0d0f4ff918a555d365c5c5407d3cf64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sat, 7 Sep 2024 17:05:50 +0900 Subject: [PATCH 21/29] =?UTF-8?q?refactor:=20=EB=AF=B8=EA=B5=AC=ED=98=84?= =?UTF-8?q?=EB=90=9C=20=ED=8C=8C=EC=9D=BC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/controller/Controller.java | 4 ---- .../coyote/http11/controller/LoginController.java | 4 ---- .../coyote/http11/servlet/DispatcherServlet.java | 12 ------------ 3 files changed, 20 deletions(-) delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java deleted file mode 100644 index 94c71235e4..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/controller/Controller.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.apache.coyote.http11.controller; - -public interface Controller { -} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java b/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java deleted file mode 100644 index 7996c85f18..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/controller/LoginController.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.apache.coyote.http11.controller; - -public class LoginController { -} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java b/tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java deleted file mode 100644 index 5f0e448955..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/servlet/DispatcherServlet.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.apache.coyote.http11.servlet; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.coyote.http11.controller.Controller; - -public class DispatcherServlet { - private final Map controllers = new HashMap<>(); - - -} From 62116299486199548c99c3264b31142c8b7552fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 11:58:02 +0900 Subject: [PATCH 22/29] =?UTF-8?q?refactor:=20=ED=95=84=ED=84=B0=EB=A5=BC?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=97=AC=20ETag=EB=A5=BC=20?= =?UTF-8?q?=EC=A7=80=EC=A0=95=ED=95=98=EB=8A=94=20=EA=B2=83=EC=9C=BC?= =?UTF-8?q?=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 --- study/src/main/java/cache/com/example/GreetingController.java | 3 ++- .../java/cache/com/example/etag/EtagFilterConfiguration.java | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/study/src/main/java/cache/com/example/GreetingController.java b/study/src/main/java/cache/com/example/GreetingController.java index 99aa9aadcc..db99101b4e 100644 --- a/study/src/main/java/cache/com/example/GreetingController.java +++ b/study/src/main/java/cache/com/example/GreetingController.java @@ -5,7 +5,9 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import cache.com.example.etag.Etag; import jakarta.servlet.http.HttpServletResponse; @Controller @@ -36,7 +38,6 @@ public String cacheControl(final HttpServletResponse response) { @GetMapping("/etag") public ResponseEntity etag() { return ResponseEntity.ok() - .eTag(eTag) .body("index"); } diff --git a/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java b/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java index e955da0b2a..2c58520179 100644 --- a/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java +++ b/study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java @@ -11,7 +11,7 @@ public class EtagFilterConfiguration { @Bean public FilterRegistrationBean shallowEtagHeaderFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(new ShallowEtagHeaderFilter()); - filterRegistrationBean.addUrlPatterns("/*"); + filterRegistrationBean.addUrlPatterns("/etag"); return filterRegistrationBean; } -} +} \ No newline at end of file From de9a2efbb68ca6e0fccbdda19ff6f3b5846b3e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 12:07:56 +0900 Subject: [PATCH 23/29] =?UTF-8?q?refactor:=20=EC=9D=B8=ED=84=B0=EC=85=89?= =?UTF-8?q?=ED=84=B0=EC=97=90=EC=84=9C=20CacheControl=EC=9D=84=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/com/example/GreetingController.java | 6 +----- .../cachecontrol/CacheControlInterceptor.java | 15 +++++++++++++++ .../com/example/cachecontrol/CacheWebConfig.java | 2 ++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 study/src/main/java/cache/com/example/cachecontrol/CacheControlInterceptor.java diff --git a/study/src/main/java/cache/com/example/GreetingController.java b/study/src/main/java/cache/com/example/GreetingController.java index db99101b4e..7cd8875e9d 100644 --- a/study/src/main/java/cache/com/example/GreetingController.java +++ b/study/src/main/java/cache/com/example/GreetingController.java @@ -5,20 +5,16 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestHeader; -import cache.com.example.etag.Etag; import jakarta.servlet.http.HttpServletResponse; @Controller public class GreetingController { - private static final String eTag = "33a64df551425fcc55e4d42a148795d9f25f89d4"; @GetMapping("/") public ResponseEntity index() { return ResponseEntity.ok() - .cacheControl(CacheControl.noCache().cachePrivate()) - .header("Content-Encoding", "gzip") + .header("Content-Encoding", "gzip") .body("index"); } diff --git a/study/src/main/java/cache/com/example/cachecontrol/CacheControlInterceptor.java b/study/src/main/java/cache/com/example/cachecontrol/CacheControlInterceptor.java new file mode 100644 index 0000000000..fa967b3edb --- /dev/null +++ b/study/src/main/java/cache/com/example/cachecontrol/CacheControlInterceptor.java @@ -0,0 +1,15 @@ +package cache.com.example.cachecontrol; + +import org.springframework.web.servlet.HandlerInterceptor; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +public class CacheControlInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + response.addHeader("Cache-Control", "no-cache, private"); + return true; + } +} diff --git a/study/src/main/java/cache/com/example/cachecontrol/CacheWebConfig.java b/study/src/main/java/cache/com/example/cachecontrol/CacheWebConfig.java index 305b1f1e1e..ad7f9638a0 100644 --- a/study/src/main/java/cache/com/example/cachecontrol/CacheWebConfig.java +++ b/study/src/main/java/cache/com/example/cachecontrol/CacheWebConfig.java @@ -9,5 +9,7 @@ public class CacheWebConfig implements WebMvcConfigurer { @Override public void addInterceptors(final InterceptorRegistry registry) { + registry.addInterceptor(new CacheControlInterceptor()) + .addPathPatterns("/"); } } From 95abccd524a67cad76098765f6043432ce69114c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 12:10:55 +0900 Subject: [PATCH 24/29] =?UTF-8?q?refactor:=20Content-Length=20=ED=97=A4?= =?UTF-8?q?=EB=8D=94=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=83=81=EC=88=98?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cache/com/example/GreetingController.java | 2 +- .../java/org/apache/coyote/http11/request/HttpRequest.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/study/src/main/java/cache/com/example/GreetingController.java b/study/src/main/java/cache/com/example/GreetingController.java index 7cd8875e9d..af2da3176d 100644 --- a/study/src/main/java/cache/com/example/GreetingController.java +++ b/study/src/main/java/cache/com/example/GreetingController.java @@ -14,7 +14,7 @@ public class GreetingController { @GetMapping("/") public ResponseEntity index() { return ResponseEntity.ok() - .header("Content-Encoding", "gzip") + .header("Content-Encoding", "gzip") .body("index"); } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java index 8c0dfae008..fe7185f508 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java @@ -7,6 +7,8 @@ public class HttpRequest { + private static final String CONTENT_LENGTH = "Content-Length"; + private final String method; private final String path; private final Map headers = new HashMap<>(); @@ -25,10 +27,10 @@ public HttpRequest(BufferedReader reader) throws IOException { } private String parseBody(BufferedReader reader) throws IOException { - if(headers.get("Content-Length") == null) { + if(headers.get(CONTENT_LENGTH) == null) { return null; } - int contentLength = Integer.parseInt(headers.get("Content-Length")); + int contentLength = Integer.parseInt(headers.get(CONTENT_LENGTH)); if(contentLength > 0) { char[] body = new char[contentLength]; reader.read(body, 0, contentLength); From 605727c995f3f2d64777c8d76ec094e7b998208f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 12:20:05 +0900 Subject: [PATCH 25/29] =?UTF-8?q?refactor:=20=EC=A0=95=EC=A0=81=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EC=9D=84=20=ED=95=9C=20=EB=B2=88=EC=97=90=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apache/coyote/http11/request/RequestHandler.java | 7 ++----- .../org/apache/coyote/http11/response/ResponseHandler.java | 2 -- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java index 94544eff92..a15814b5d7 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java @@ -25,15 +25,13 @@ public void handleRequest() throws IOException { String httpMethod = httpRequest.getMethod(); String urlPath = httpRequest.getPath(); - if (urlPath.endsWith("html")) { + if(urlPath.endsWith("html") || urlPath.endsWith("css") || urlPath.endsWith("js")) { ResponseHandler.printFileResource("static" + urlPath, outputStream); } else if (urlPath.startsWith("/login")) { handleLoginRequest(httpMethod, urlPath); } else if (urlPath.startsWith("/register")) { handleRegisterRequest(httpMethod, urlPath); - } else if (urlPath.startsWith("/css") || urlPath.startsWith("/js") || urlPath.startsWith("/assets")) { - ResponseHandler.printFileResource("static" + urlPath, outputStream); - } else { + } else { sendHelloWorldResponse(); } } @@ -91,7 +89,6 @@ private void handleRegisterRequest(String httpMethod, String urlPath) throws IOE User user = new User(account, mail, password); InMemoryUserRepository.save(user); ResponseHandler.redirect("http://localhost:8080/index.html", outputStream); - return; } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java index 915034ffe2..f99dd1d989 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java @@ -5,9 +5,7 @@ import java.io.OutputStream; import java.net.URL; import java.nio.file.Files; -import java.util.UUID; -import org.apache.coyote.http11.Http11Processor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From 539be1cb822c55950db8492e3e66ad2f0fc46999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 13:35:19 +0900 Subject: [PATCH 26/29] =?UTF-8?q?fix:=20Json=20=ED=8C=8C=EC=9D=BC=EC=9D=84?= =?UTF-8?q?=20=EB=B3=B4=EB=82=B4=EB=8A=94=20=EA=B2=BD=EC=9A=B0=EB=A5=BC=20?= =?UTF-8?q?=EA=B3=A0=EB=A0=A4=ED=95=98=EC=97=AC=20MediaType=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coyote/http11/response/ResponseHandler.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java index f99dd1d989..1ec1f2d2c4 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java @@ -39,9 +39,16 @@ public static void printFileResource(String fileName, OutputStream outputStream) } private static String determineContentType(String fileName) { - if (fileName.endsWith("css")) return "text/css"; - if (fileName.endsWith("js")) return "application/javascript"; - return "text/html"; + if (fileName.endsWith("css")) { + return "text/css"; + } + if (fileName.endsWith("js")) { + return "application/javascript"; + } + if(fileName.endsWith("html")) { + return "text/html"; + } + return "application/json"; } private static void sendNotFoundResponse(OutputStream outputStream) { From 407826a877d86f2c45dfecad1ffcc8ba7508296d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 13:59:52 +0900 Subject: [PATCH 27/29] =?UTF-8?q?refactor:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=20=EC=8B=9C=20=EC=BF=A0=ED=82=A4=20=EC=84=B8=EC=85=98=EC=9D=84?= =?UTF-8?q?=20=EA=BA=BC=EB=82=B4=EC=98=A4=EB=8A=94=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EA=B0=80=EB=8F=85=EC=84=B1=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coyote/http11/request/RequestHandler.java | 28 +++++-------- .../apache/coyote/http11/session/Session.java | 39 +++++++++++++++++-- .../coyote/http11/session/SessionManager.java | 14 +++---- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java index a15814b5d7..45278fe87a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java @@ -6,6 +6,7 @@ import java.util.Optional; import org.apache.coyote.http11.response.ResponseHandler; +import org.apache.coyote.http11.session.Session; import org.apache.coyote.http11.session.SessionManager; import com.techcourse.db.InMemoryUserRepository; @@ -37,22 +38,12 @@ public void handleRequest() throws IOException { } private void handleLoginRequest(String httpMethod, String urlPath) { - if(urlPath.equals("/login") && httpMethod.equals("GET")) { - if(httpRequest.getHeader("Cookie") != null) { - Optional sessionCookie = Arrays.asList(httpRequest.getHeader("Cookie").split("; ")) - .stream() - .filter(cookie -> cookie.startsWith("JSESSIONID")) - .findAny(); - if(!sessionCookie.isEmpty()) { - String sessionId = sessionCookie.get().split("=")[1]; - Optional userBySession = SessionManager.findUserBySession(sessionId); - if(userBySession.isPresent()) { - ResponseHandler.redirect("http://localhost:8080/index.html", outputStream); - return; - } - } - } - ResponseHandler.printFileResource("static" + urlPath +".html", outputStream); + if (urlPath.equals("/login") && httpMethod.equals("GET")) { + Session session = new Session(httpRequest); + SessionManager.findUserBySession(session) + .ifPresentOrElse( + user -> ResponseHandler.redirect("http://localhost:8080/index.html", outputStream), + () -> ResponseHandler.printFileResource("static" + urlPath + ".html", outputStream)); } else if (httpMethod.equals("POST")) { login(); } @@ -66,13 +57,12 @@ private void login() { User user = InMemoryUserRepository.findByAccount(account) .orElse(new User("guest", "guest", "guest")); if (user.checkPassword(password)) { - String sessionId = SessionManager.createSession(user); - ResponseHandler.redirectWithSetCookie("http://localhost:8080/index.html", sessionId, outputStream); + Session session = SessionManager.createSession(user); + ResponseHandler.redirectWithSetCookie("http://localhost:8080/index.html", session.getId(), outputStream); return; } } ResponseHandler.redirect("http://localhost:8080/401.html", outputStream); - return; } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java b/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java index 48b4a75b45..0103541904 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java @@ -1,11 +1,42 @@ package org.apache.coyote.http11.session; -import com.techcourse.model.User; +import java.util.Arrays; +import java.util.Objects; +import java.util.UUID; + +import org.apache.coyote.http11.request.HttpRequest; public class Session { - private final String sessionId; + private final String id; + + public Session() { + this.id = UUID.randomUUID().toString(); + } + + public Session(HttpRequest httpRequest) { + this.id = Arrays.asList(httpRequest.getHeader("Cookie").split("; ")) + .stream() + .filter(cookie -> cookie.startsWith("JSESSIONID")) + .findAny() + .orElseGet(null); + } + + public String getId() { + return id; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Session session = (Session)o; + return Objects.equals(id, session.id); + } - public Session(String sessionId) { - this.sessionId = sessionId; + @Override + public int hashCode() { + return Objects.hashCode(id); } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java index ffeb4618ac..c05238b765 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java @@ -8,15 +8,15 @@ import com.techcourse.model.User; public final class SessionManager { - private static final Map sessions = new HashMap<>(); + private static final Map sessions = new HashMap<>(); - public static String createSession(User user) { - String sessionId = UUID.randomUUID().toString(); - sessions.put(sessionId, user); - return sessionId; + public static Session createSession(User user) { + Session session = new Session(); + sessions.put(session, user); + return session; } - public static Optional findUserBySession(String sessionId) { - return Optional.ofNullable(sessions.get(sessionId)); + public static Optional findUserBySession(Session session) { + return Optional.ofNullable(sessions.get(session)); } } From 2439262b4cfa27ec017b80db713a16ce4a2aeb71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 14:11:12 +0900 Subject: [PATCH 28/29] =?UTF-8?q?refactor:=20=EC=9A=94=EC=B2=AD=ED=95=9C?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A0=95=EB=B3=B4=EA=B0=80=20?= =?UTF-8?q?=EC=97=86=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20401=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=EB=A5=BC=20=EB=B3=B4=EC=97=AC=EC=A3=BC?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coyote/http11/request/RequestHandler.java | 28 ++++++++++--------- .../http11/response/ResponseHandler.java | 8 +++--- .../coyote/http11/session/SessionManager.java | 1 - 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java index 45278fe87a..3bd3462836 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHandler.java @@ -2,8 +2,6 @@ import java.io.IOException; import java.io.OutputStream; -import java.util.Arrays; -import java.util.Optional; import org.apache.coyote.http11.response.ResponseHandler; import org.apache.coyote.http11.session.Session; @@ -42,7 +40,7 @@ private void handleLoginRequest(String httpMethod, String urlPath) { Session session = new Session(httpRequest); SessionManager.findUserBySession(session) .ifPresentOrElse( - user -> ResponseHandler.redirect("http://localhost:8080/index.html", outputStream), + user -> ResponseHandler.redirect("/index.html", outputStream), () -> ResponseHandler.printFileResource("static" + urlPath + ".html", outputStream)); } else if (httpMethod.equals("POST")) { login(); @@ -54,19 +52,23 @@ private void login() { if (body != null) { String account = body.split("&")[0].split("=")[1]; String password = body.split("&")[1].split("=")[1]; - User user = InMemoryUserRepository.findByAccount(account) - .orElse(new User("guest", "guest", "guest")); - if (user.checkPassword(password)) { - Session session = SessionManager.createSession(user); - ResponseHandler.redirectWithSetCookie("http://localhost:8080/index.html", session.getId(), outputStream); - return; - } + InMemoryUserRepository.findByAccount(account) + .ifPresentOrElse( + user -> loginUser(user, password), + () -> ResponseHandler.redirect("/401.html", outputStream) + ); } - ResponseHandler.redirect("http://localhost:8080/401.html", outputStream); + ResponseHandler.redirect("/401.html", outputStream); } + private void loginUser(User user, String password) { + if (user.checkPassword(password)) { + Session session = SessionManager.createSession(user); + ResponseHandler.redirectWithSetCookie("/index.html", session.getId(), outputStream); + } + } - private void handleRegisterRequest(String httpMethod, String urlPath) throws IOException { + private void handleRegisterRequest(String httpMethod, String urlPath) { if (httpMethod.equals("GET")) { ResponseHandler.printFileResource("static" + urlPath + ".html", outputStream); return; @@ -78,7 +80,7 @@ private void handleRegisterRequest(String httpMethod, String urlPath) throws IOE String password = body.split("&")[2].split("=")[1]; User user = new User(account, mail, password); InMemoryUserRepository.save(user); - ResponseHandler.redirect("http://localhost:8080/index.html", outputStream); + ResponseHandler.redirect("/index.html", outputStream); } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java index 1ec1f2d2c4..f286e2861e 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHandler.java @@ -63,11 +63,11 @@ private static void sendNotFoundResponse(OutputStream outputStream) { } } - public static void redirect(String location, OutputStream outputStream) { + public static void redirect(String path, OutputStream outputStream) { try { String contentType = "text/html"; var response = "HTTP/1.1 302 Found \r\n" + - "Location: " + location + "\r\n" + + "Location: http://localhost:8080" + path + "\r\n" + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + "Content-Length: 0"; @@ -78,12 +78,12 @@ public static void redirect(String location, OutputStream outputStream) { } } - public static void redirectWithSetCookie(String location, String sessionId, OutputStream outputStream) { + public static void redirectWithSetCookie(String path, String sessionId, OutputStream outputStream) { try { String contentType = "text/html"; var response = "HTTP/1.1 302 Found \r\n" + "Set-Cookie: JSESSIONID=" + sessionId + " \r\n" + - "Location: " + location + " \r\n" + + "Location: http://localhost:8080" + path + " \r\n" + String.format("Content-Type: %s;charset=utf-8 \r\n", contentType) + "Content-Length: 0"; diff --git a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java index c05238b765..040a07fa0b 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java @@ -3,7 +3,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; -import java.util.UUID; import com.techcourse.model.User; From 0bb26a7e7430566dc66ca00a39b6384cae7d83c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EA=B2=B8?= Date: Sun, 8 Sep 2024 14:52:14 +0900 Subject: [PATCH 29/29] =?UTF-8?q?refactor:=20=EC=BF=A0=ED=82=A4=EC=9D=98?= =?UTF-8?q?=20=ED=82=A4=EA=B0=92=EC=9D=84=20=EC=83=81=EC=88=98=ED=99=94?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/apache/coyote/http11/request/HttpRequest.java | 4 ++-- .../main/java/org/apache/coyote/http11/session/Session.java | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java index fe7185f508..aaf2d5f417 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java @@ -47,8 +47,8 @@ public String getPath() { return path; } - public String getHeader(String name) { - return headers.get(name); + public String getCookie() { + return headers.get("Cookie"); } public String getBody() { diff --git a/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java b/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java index 0103541904..ebce4c6da0 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java @@ -7,6 +7,7 @@ import org.apache.coyote.http11.request.HttpRequest; public class Session { + public static final String SESSION_HEADER_KEY = "JSESSIONID"; private final String id; public Session() { @@ -14,9 +15,9 @@ public Session() { } public Session(HttpRequest httpRequest) { - this.id = Arrays.asList(httpRequest.getHeader("Cookie").split("; ")) + this.id = Arrays.asList(httpRequest.getCookie().split("; ")) .stream() - .filter(cookie -> cookie.startsWith("JSESSIONID")) + .filter(cookie -> cookie.startsWith(SESSION_HEADER_KEY)) .findAny() .orElseGet(null); }