From fed02f6f5f4308400e55c160d9495cad010f5bfb Mon Sep 17 00:00:00 2001 From: Gyeongho Yang Date: Thu, 5 Sep 2024 11:11:09 +0900 Subject: [PATCH 01/28] 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 7e9135698878932274ddc1f523ba817ed9c56c70 Mon Sep 17 00:00:00 2001 From: Gyeongho Yang Date: Thu, 5 Sep 2024 13:51:07 +0900 Subject: [PATCH 02/28] 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 4a259b5258ca24ce4198eea30dd0f090d104b482 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Wed, 4 Sep 2024 15:37:34 +0900 Subject: [PATCH 03/28] =?UTF-8?q?test:=20FileTest=20=EB=B0=8F=20IOStreamTe?= =?UTF-8?q?st=20=ED=86=B5=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- study/src/test/java/study/FileTest.java | 36 +++--- study/src/test/java/study/IOStreamTest.java | 127 ++++++++++---------- 2 files changed, 81 insertions(+), 82 deletions(-) diff --git a/study/src/test/java/study/FileTest.java b/study/src/test/java/study/FileTest.java index e1b6cca042..c67fc44faf 100644 --- a/study/src/test/java/study/FileTest.java +++ b/study/src/test/java/study/FileTest.java @@ -1,53 +1,53 @@ package study; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collections; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; /** - * 웹서버는 사용자가 요청한 html 파일을 제공 할 수 있어야 한다. - * File 클래스를 사용해서 파일을 읽어오고, 사용자에게 전달한다. + * 웹서버는 사용자가 요청한 html 파일을 제공 할 수 있어야 한다. File 클래스를 사용해서 파일을 읽어오고, 사용자에게 전달한다. */ @DisplayName("File 클래스 학습 테스트") class FileTest { /** * resource 디렉터리 경로 찾기 - * - * File 객체를 생성하려면 파일의 경로를 알아야 한다. - * 자바 애플리케이션은 resource 디렉터리에 HTML, CSS 같은 정적 파일을 저장한다. - * resource 디렉터리의 경로는 어떻게 알아낼 수 있을까? + *

+ * File 객체를 생성하려면 파일의 경로를 알아야 한다. 자바 애플리케이션은 resource 디렉터리에 HTML, CSS 같은 정적 파일을 저장한다. resource 디렉터리의 경로는 어떻게 알아낼 수 + * 있을까? */ @Test void resource_디렉터리에_있는_파일의_경로를_찾는다() { final String fileName = "nextstep.txt"; // todo - final String actual = ""; + ClassPathResource resource = new ClassPathResource(fileName); + final String actual = resource.getFilename(); assertThat(actual).endsWith(fileName); } /** * 파일 내용 읽기 - * - * 읽어온 파일의 내용을 I/O Stream을 사용해서 사용자에게 전달 해야 한다. - * File, Files 클래스를 사용하여 파일의 내용을 읽어보자. + *

+ * 읽어온 파일의 내용을 I/O Stream을 사용해서 사용자에게 전달 해야 한다. File, Files 클래스를 사용하여 파일의 내용을 읽어보자. */ @Test - void 파일의_내용을_읽는다() { + void 파일의_내용을_읽는다() throws IOException { final String fileName = "nextstep.txt"; // todo - final Path path = null; + ClassPathResource resource = new ClassPathResource(fileName); + final Path path = resource.getFile().toPath(); // 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..958e5d1a2f 100644 --- a/study/src/test/java/study/IOStreamTest.java +++ b/study/src/test/java/study/IOStreamTest.java @@ -1,45 +1,49 @@ package study; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import java.io.*; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.*; - /** - * 자바는 스트림(Stream)으로부터 I/O를 사용한다. - * 입출력(I/O)은 하나의 시스템에서 다른 시스템으로 데이터를 이동 시킬 때 사용한다. - * - * InputStream은 데이터를 읽고, OutputStream은 데이터를 쓴다. - * FilterStream은 InputStream이나 OutputStream에 연결될 수 있다. - * FilterStream은 읽거나 쓰는 데이터를 수정할 때 사용한다. (e.g. 암호화, 압축, 포맷 변환) - * - * Stream은 데이터를 바이트로 읽고 쓴다. - * 바이트가 아닌 텍스트(문자)를 읽고 쓰려면 Reader와 Writer 클래스를 연결한다. - * Reader, Writer는 다양한 문자 인코딩(e.g. UTF-8)을 처리할 수 있다. + * 자바는 스트림(Stream)으로부터 I/O를 사용한다. 입출력(I/O)은 하나의 시스템에서 다른 시스템으로 데이터를 이동 시킬 때 사용한다. + *

+ * InputStream은 데이터를 읽고, OutputStream은 데이터를 쓴다. FilterStream은 InputStream이나 OutputStream에 연결될 수 있다. FilterStream은 읽거나 쓰는 + * 데이터를 수정할 때 사용한다. (e.g. 암호화, 압축, 포맷 변환) + *

+ * Stream은 데이터를 바이트로 읽고 쓴다. 바이트가 아닌 텍스트(문자)를 읽고 쓰려면 Reader와 Writer 클래스를 연결한다. Reader, Writer는 다양한 문자 인코딩(e.g. UTF-8)을 + * 처리할 수 있다. */ @DisplayName("Java I/O Stream 클래스 학습 테스트") class IOStreamTest { /** * OutputStream 학습하기 - * - * 자바의 기본 출력 클래스는 java.io.OutputStream이다. - * OutputStream의 write(int b) 메서드는 기반 메서드이다. + *

+ * 자바의 기본 출력 클래스는 java.io.OutputStream이다. OutputStream의 write(int b) 메서드는 기반 메서드이다. * public abstract void write(int b) throws IOException; */ @Nested class OutputStream_학습_테스트 { /** - * OutputStream은 다른 매체에 바이트로 데이터를 쓸 때 사용한다. - * OutputStream의 서브 클래스(subclass)는 특정 매체에 데이터를 쓰기 위해 write(int b) 메서드를 사용한다. - * 예를 들어, FilterOutputStream은 파일로 데이터를 쓸 때, - * 또는 DataOutputStream은 자바의 primitive type data를 다른 매체로 데이터를 쓸 때 사용한다. - * + * OutputStream은 다른 매체에 바이트로 데이터를 쓸 때 사용한다. OutputStream의 서브 클래스(subclass)는 특정 매체에 데이터를 쓰기 위해 write(int b) 메서드를 + * 사용한다. 예를 들어, FilterOutputStream은 파일로 데이터를 쓸 때, 또는 DataOutputStream은 자바의 primitive type data를 다른 매체로 데이터를 쓸 때 + * 사용한다. + *

* write 메서드는 데이터를 바이트로 출력하기 때문에 비효율적이다. * write(byte[] data)write(byte b[], int off, int len) 메서드는 * 1바이트 이상을 한 번에 전송 할 수 있어 훨씬 효율적이다. @@ -53,7 +57,7 @@ class OutputStream_학습_테스트 { * todo * OutputStream 객체의 write 메서드를 사용해서 테스트를 통과시킨다 */ - + outputStream.write(bytes); final String actual = outputStream.toString(); assertThat(actual).isEqualTo("nextstep"); @@ -61,13 +65,10 @@ class OutputStream_학습_테스트 { } /** - * 효율적인 전송을 위해 스트림에서 버퍼링을 사용 할 수 있다. - * BufferedOutputStream 필터를 연결하면 버퍼링이 가능하다. - * - * 버퍼링을 사용하면 OutputStream을 사용할 때 flush를 사용하자. - * flush() 메서드는 버퍼가 아직 가득 차지 않은 상황에서 강제로 버퍼의 내용을 전송한다. - * Stream은 동기(synchronous)로 동작하기 때문에 버퍼가 찰 때까지 기다리면 - * 데드락(deadlock) 상태가 되기 때문에 flush로 해제해야 한다. + * 효율적인 전송을 위해 스트림에서 버퍼링을 사용 할 수 있다. BufferedOutputStream 필터를 연결하면 버퍼링이 가능하다. + *

+ * 버퍼링을 사용하면 OutputStream을 사용할 때 flush를 사용하자. flush() 메서드는 버퍼가 아직 가득 차지 않은 상황에서 강제로 버퍼의 내용을 전송한다. Stream은 + * 동기(synchronous)로 동작하기 때문에 버퍼가 찰 때까지 기다리면 데드락(deadlock) 상태가 되기 때문에 flush로 해제해야 한다. */ @Test void BufferedOutputStream을_사용하면_버퍼링이_가능하다() throws IOException { @@ -78,14 +79,14 @@ class OutputStream_학습_테스트 { * flush를 사용해서 테스트를 통과시킨다. * ByteArrayOutputStream과 어떤 차이가 있을까? */ + outputStream.flush(); verify(outputStream, atLeastOnce()).flush(); outputStream.close(); } /** - * 스트림 사용이 끝나면 항상 close() 메서드를 호출하여 스트림을 닫는다. - * 장시간 스트림을 닫지 않으면 파일, 포트 등 다양한 리소스에서 누수(leak)가 발생한다. + * 스트림 사용이 끝나면 항상 close() 메서드를 호출하여 스트림을 닫는다. 장시간 스트림을 닫지 않으면 파일, 포트 등 다양한 리소스에서 누수(leak)가 발생한다. */ @Test void OutputStream은_사용하고_나서_close_처리를_해준다() throws IOException { @@ -97,26 +98,26 @@ class OutputStream_학습_테스트 { * java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다. */ + try (outputStream) { + } verify(outputStream, atLeastOnce()).close(); } } /** * InputStream 학습하기 - * - * 자바의 기본 입력 클래스는 java.io.InputStream이다. - * InputStream은 다른 매체로부터 바이트로 데이터를 읽을 때 사용한다. - * InputStream의 read() 메서드는 기반 메서드이다. + *

+ * 자바의 기본 입력 클래스는 java.io.InputStream이다. InputStream은 다른 매체로부터 바이트로 데이터를 읽을 때 사용한다. InputStream의 read() 메서드는 기반 + * 메서드이다. * public abstract int read() throws IOException; - * + *

* InputStream의 서브 클래스(subclass)는 특정 매체에 데이터를 읽기 위해 read() 메서드를 사용한다. */ @Nested class InputStream_학습_테스트 { /** - * read() 메서드는 매체로부터 단일 바이트를 읽는데, 0부터 255 사이의 값을 int 타입으로 반환한다. - * int 값을 byte 타입으로 변환하면 -128부터 127 사이의 값으로 변환된다. + * read() 메서드는 매체로부터 단일 바이트를 읽는데, 0부터 255 사이의 값을 int 타입으로 반환한다. int 값을 byte 타입으로 변환하면 -128부터 127 사이의 값으로 변환된다. * 그리고 Stream 끝에 도달하면 -1을 반환한다. */ @Test @@ -128,7 +129,7 @@ class InputStream_학습_테스트 { * todo * inputStream에서 바이트로 반환한 값을 문자열로 어떻게 바꿀까? */ - final String actual = ""; + final String actual = new String(inputStream.readAllBytes()); assertThat(actual).isEqualTo("🤩"); assertThat(inputStream.read()).isEqualTo(-1); @@ -136,8 +137,7 @@ class InputStream_학습_테스트 { } /** - * 스트림 사용이 끝나면 항상 close() 메서드를 호출하여 스트림을 닫는다. - * 장시간 스트림을 닫지 않으면 파일, 포트 등 다양한 리소스에서 누수(leak)가 발생한다. + * 스트림 사용이 끝나면 항상 close() 메서드를 호출하여 스트림을 닫는다. 장시간 스트림을 닫지 않으면 파일, 포트 등 다양한 리소스에서 누수(leak)가 발생한다. */ @Test void InputStream은_사용하고_나서_close_처리를_해준다() throws IOException { @@ -148,33 +148,32 @@ class InputStream_학습_테스트 { * try-with-resources를 사용한다. * java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다. */ - + try (inputStream) { + } verify(inputStream, atLeastOnce()).close(); } } /** * FilterStream 학습하기 - * - * 필터는 필터 스트림, reader, writer로 나뉜다. - * 필터는 바이트를 다른 데이터 형식으로 변환 할 때 사용한다. - * reader, writer는 UTF-8, ISO 8859-1 같은 형식으로 인코딩된 텍스트를 처리하는 데 사용된다. + *

+ * 필터는 필터 스트림, reader, writer로 나뉜다. 필터는 바이트를 다른 데이터 형식으로 변환 할 때 사용한다. reader, writer는 UTF-8, ISO 8859-1 같은 형식으로 인코딩된 + * 텍스트를 처리하는 데 사용된다. */ @Nested class FilterStream_학습_테스트 { /** - * BufferedInputStream은 데이터 처리 속도를 높이기 위해 데이터를 버퍼에 저장한다. - * InputStream 객체를 생성하고 필터 생성자에 전달하면 필터에 연결된다. - * 버퍼 크기를 지정하지 않으면 버퍼의 기본 사이즈는 얼마일까? + * BufferedInputStream은 데이터 처리 속도를 높이기 위해 데이터를 버퍼에 저장한다. InputStream 객체를 생성하고 필터 생성자에 전달하면 필터에 연결된다. 버퍼 크기를 지정하지 + * 않으면 버퍼의 기본 사이즈는 얼마일까? */ @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()); @@ -182,31 +181,31 @@ class FilterStream_학습_테스트 { } /** - * 자바의 기본 문자열은 UTF-16 유니코드 인코딩을 사용한다. - * 문자열이 아닌 바이트 단위로 처리하려니 불편하다. - * 그리고 바이트를 문자(char)로 처리하려면 인코딩을 신경 써야 한다. - * reader, writer를 사용하면 입출력 스트림을 바이트가 아닌 문자 단위로 데이터를 처리하게 된다. - * 그리고 InputStreamReader를 사용하면 지정된 인코딩에 따라 유니코드 문자로 변환할 수 있다. + * 자바의 기본 문자열은 UTF-16 유니코드 인코딩을 사용한다. 문자열이 아닌 바이트 단위로 처리하려니 불편하다. 그리고 바이트를 문자(char)로 처리하려면 인코딩을 신경 써야 한다. reader, + * writer를 사용하면 입출력 스트림을 바이트가 아닌 문자 단위로 데이터를 처리하게 된다. 그리고 InputStreamReader를 사용하면 지정된 인코딩에 따라 유니코드 문자로 변환할 수 있다. */ @Nested class InputStreamReader_학습_테스트 { /** - * InputStreamReader를 사용해서 바이트를 문자(char)로 읽어온다. - * 읽어온 문자(char)를 문자열(String)로 처리하자. - * 필터인 BufferedReader를 사용하면 readLine 메서드를 사용해서 문자열(String)을 한 줄 씩 읽어올 수 있다. + * InputStreamReader를 사용해서 바이트를 문자(char)로 읽어온다. 읽어온 문자(char)를 문자열(String)로 처리하자. 필터인 BufferedReader를 사용하면 + * readLine 메서드를 사용해서 문자열(String)을 한 줄 씩 읽어올 수 있다. */ @Test - void BufferedReader를_사용하여_문자열을_읽어온다() { + void BufferedReader를_사용하여_문자열을_읽어온다() throws IOException { final String emoji = String.join("\r\n", "😀😃😄😁😆😅😂🤣🥲☺️😊", "😇🙂🙃😉😌😍🥰😘😗😙😚", "😋😛😝😜🤪🤨🧐🤓😎🥸🤩", ""); final InputStream inputStream = new ByteArrayInputStream(emoji.getBytes()); - + final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); final StringBuilder actual = new StringBuilder(); + while (inputStreamReader.ready()) { + actual.append((char) inputStreamReader.read()); + } + assertThat(actual).hasToString(emoji); } } From fddfdc620bff62900cd45528bfe0ee2875fc716b Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 10:46:58 +0900 Subject: [PATCH 04/28] =?UTF-8?q?chore:=20lombok=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tomcat/build.gradle | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tomcat/build.gradle b/tomcat/build.gradle index 21063b298f..b4fec846f4 100644 --- a/tomcat/build.gradle +++ b/tomcat/build.gradle @@ -2,7 +2,6 @@ plugins { id 'java' } - java { sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 @@ -12,12 +11,21 @@ repositories { mavenCentral() } +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + dependencies { implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0' implementation 'org.reflections:reflections:0.10.2' implementation 'ch.qos.logback:logback-classic:1.5.7' implementation 'org.apache.commons:commons-lang3:3.14.0' + compileOnly 'org.projectlombok:lombok:1.18.30' + annotationProcessor 'org.projectlombok:lombok:1.18.30' + testImplementation 'org.assertj:assertj-core:3.26.0' testImplementation 'org.mockito:mockito-core:5.12.0' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2' From 1d089609d784433b98dc3c674df16c19acef67ef Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 12:59:27 +0900 Subject: [PATCH 05/28] =?UTF-8?q?feat:=201-1=20'GET=20/index.html=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=ED=95=98=EA=B8=B0'=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/http/HttpRequest.java | 19 +++++++ .../techcourse/http/HttpRequestParser.java | 50 +++++++++++++++++++ .../apache/coyote/http11/Http11Processor.java | 50 +++++++++++++++---- .../coyote/http11/Http11ProcessorTest.java | 11 ++-- 4 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 tomcat/src/main/java/com/techcourse/http/HttpRequest.java create mode 100644 tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java new file mode 100644 index 0000000000..83ce35fe23 --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java @@ -0,0 +1,19 @@ +package com.techcourse.http; + +import java.util.HashMap; +import java.util.Map; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class HttpRequest { + private String method; + private String uri; + private Map headers; + private String body; + + public HttpRequest() { + this.headers = new HashMap<>(); + } +} diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java new file mode 100644 index 0000000000..693a2d6194 --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java @@ -0,0 +1,50 @@ +package com.techcourse.http; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class HttpRequestParser { + + public static HttpRequest parse(InputStream inputStream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + HttpRequest request = new HttpRequest(); + + parseStartLine(reader, request); + parseHeaders(reader, request); + parseBody(reader, request); + + return request; + } + + private static void parseStartLine(BufferedReader reader, HttpRequest request) throws IOException { + String startLine = reader.readLine(); + if (startLine != null) { + String[] startLineParts = startLine.split(" "); + request.setMethod(startLineParts[0]); + request.setUri(startLineParts[1]); + } + } + + private static void parseHeaders(BufferedReader reader, HttpRequest request) throws IOException { + String line; + while (!(line = reader.readLine()).isEmpty()) { + String[] headerParts = line.split(": "); + request.getHeaders().put(headerParts[0], headerParts[1]); + } + } + + private static void parseBody(BufferedReader reader, HttpRequest request) throws IOException { + StringBuilder body = new StringBuilder(); + String line; + while (reader.ready()) { + line = reader.readLine(); + if (line == null) { + break; + } + body.append(line).append("\n"); + } + request.setBody(body.toString().trim()); + } +} 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..f37c0ac8ea 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -1,16 +1,21 @@ package org.apache.coyote.http11; import com.techcourse.exception.UncheckedServletException; +import com.techcourse.http.HttpRequest; +import com.techcourse.http.HttpRequestParser; +import java.io.IOException; +import java.net.Socket; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import org.apache.coyote.Processor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.net.Socket; - public class Http11Processor implements Runnable, Processor { private static final Logger log = LoggerFactory.getLogger(Http11Processor.class); + private static final String CRLF = "\r\n"; private final Socket connection; @@ -27,21 +32,46 @@ public void run() { @Override public void process(final Socket connection) { try (final var inputStream = connection.getInputStream(); - final var outputStream = connection.getOutputStream()) { + final var outputStream = connection.getOutputStream() + ) { + final HttpRequest request = HttpRequestParser.parse(inputStream); - final var responseBody = "Hello world!"; + final String response = getResponse(request); - final var response = String.join("\r\n", + outputStream.write(response.getBytes()); + outputStream.flush(); + } catch (IOException | UncheckedServletException e) { + log.error(e.getMessage(), e); + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } + } + + private String getResponse(HttpRequest request) throws NoSuchFieldException, IOException { + if ("/".equals(request.getUri())) { + final String responseBody = "Hello world!"; + return String.join( + CRLF, "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); + URL url = getClass().getClassLoader().getResource("static" + request.getUri()); + if (url == null) { + throw new NoSuchFieldException(); } + final Path path = Path.of(url.getPath()); + final byte[] responseBody = Files.readAllBytes(path); + return String.join( + CRLF, + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: " + responseBody.length + " ", + "", + new String(responseBody) + ); } } diff --git a/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java b/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java index 2aba8c56e0..8af591bcf0 100644 --- a/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java +++ b/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java @@ -1,14 +1,13 @@ package org.apache.coyote.http11; -import org.junit.jupiter.api.Test; -import support.StubSocket; +import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import support.StubSocket; class Http11ProcessorTest { @@ -35,7 +34,7 @@ void process() { @Test void index() throws IOException { // given - final String httpRequest= String.join("\r\n", + final String httpRequest = String.join("\r\n", "GET /index.html HTTP/1.1 ", "Host: localhost:8080 ", "Connection: keep-alive ", @@ -53,7 +52,7 @@ void index() throws IOException { var expected = "HTTP/1.1 200 OK \r\n" + "Content-Type: text/html;charset=utf-8 \r\n" + "Content-Length: 5564 \r\n" + - "\r\n"+ + "\r\n" + new String(Files.readAllBytes(new File(resource.getFile()).toPath())); assertThat(socket.output()).isEqualTo(expected); From 90931d9eee53ee844cab35ebaa1b5c9f812788d4 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 14:13:58 +0900 Subject: [PATCH 06/28] =?UTF-8?q?feat:=201-2=20CSS=20=EC=A7=80=EC=9B=90?= =?UTF-8?q?=ED=95=98=EA=B8=B0=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 - MimeType 열거형 추가 - 정적 URI를 못찾을 시 404 반환 --- .../java/com/techcourse/http/MimeType.java | 30 +++++++++++++++++++ .../apache/coyote/http11/Http11Processor.java | 16 +++++----- 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 tomcat/src/main/java/com/techcourse/http/MimeType.java diff --git a/tomcat/src/main/java/com/techcourse/http/MimeType.java b/tomcat/src/main/java/com/techcourse/http/MimeType.java new file mode 100644 index 0000000000..f83f640fc9 --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/http/MimeType.java @@ -0,0 +1,30 @@ +package com.techcourse.http; + +public enum MimeType { + HTML(".html", "text/html;charset=utf-8"), + CSS(".css", "text/css"), + JS(".js", "application/javascript"), + JSON(".json", "application/json"), + PNG(".png", "image/png"), + JPG(".jpg", "image/jpeg"), + JPEG(".jpeg", "image/jpeg"), + GIF(".gif", "image/gif"), + DEFAULT("", "application/octet-stream"); + + private final String extension; + private final String mimeType; + + MimeType(String extension, String mimeType) { + this.extension = extension; + this.mimeType = mimeType; + } + + public static String getMimeType(String fileName) { + for (MimeType type : values()) { + if (fileName.endsWith(type.extension)) { + return type.mimeType; + } + } + return DEFAULT.mimeType; + } +} 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 f37c0ac8ea..0fb74be72f 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -1,8 +1,8 @@ package org.apache.coyote.http11; -import com.techcourse.exception.UncheckedServletException; import com.techcourse.http.HttpRequest; import com.techcourse.http.HttpRequestParser; +import com.techcourse.http.MimeType; import java.io.IOException; import java.net.Socket; import java.net.URL; @@ -40,14 +40,12 @@ public void process(final Socket connection) { outputStream.write(response.getBytes()); outputStream.flush(); - } catch (IOException | UncheckedServletException e) { + } catch (IOException e) { log.error(e.getMessage(), e); - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); } } - private String getResponse(HttpRequest request) throws NoSuchFieldException, IOException { + private String getResponse(HttpRequest request) throws IOException { if ("/".equals(request.getUri())) { final String responseBody = "Hello world!"; return String.join( @@ -61,14 +59,18 @@ private String getResponse(HttpRequest request) throws NoSuchFieldException, IOE URL url = getClass().getClassLoader().getResource("static" + request.getUri()); if (url == null) { - throw new NoSuchFieldException(); + return "HTTP/1.1 404 Not Found "; } + final Path path = Path.of(url.getPath()); final byte[] responseBody = Files.readAllBytes(path); + String uri = request.getUri(); + String endUri = uri.substring(uri.lastIndexOf("/") + 1); + return String.join( CRLF, "HTTP/1.1 200 OK ", - "Content-Type: text/html;charset=utf-8 ", + "Content-Type: " + MimeType.getMimeType(endUri) + " ", "Content-Length: " + responseBody.length + " ", "", new String(responseBody) From 731928357b272015b2bc7385fac565821444608a Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 14:20:36 +0900 Subject: [PATCH 07/28] =?UTF-8?q?refactor:=20getResponse=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 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 0fb74be72f..a75174fbf3 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -35,7 +35,6 @@ public void process(final Socket connection) { final var outputStream = connection.getOutputStream() ) { final HttpRequest request = HttpRequestParser.parse(inputStream); - final String response = getResponse(request); outputStream.write(response.getBytes()); @@ -47,33 +46,38 @@ public void process(final Socket connection) { private String getResponse(HttpRequest request) throws IOException { if ("/".equals(request.getUri())) { - final String responseBody = "Hello world!"; - return String.join( - CRLF, - "HTTP/1.1 200 OK ", - "Content-Type: text/html;charset=utf-8 ", - "Content-Length: " + responseBody.getBytes().length + " ", - "", - responseBody); + return rootPage(); } - URL url = getClass().getClassLoader().getResource("static" + request.getUri()); - if (url == null) { + return getStaticResource(request.getUri()); + } + + private String rootPage() { + return """ + HTTP/1.1 200 OK \r + Content-Type: text/html;charset=utf-8 \r + Content-Length: 12 \r + \r + Hello world!"""; + } + + private String getStaticResource(String requestUri) throws IOException { + URL resource = getClass().getClassLoader().getResource("static" + requestUri); + if (resource == null) { return "HTTP/1.1 404 Not Found "; } - final Path path = Path.of(url.getPath()); - final byte[] responseBody = Files.readAllBytes(path); - String uri = request.getUri(); - String endUri = uri.substring(uri.lastIndexOf("/") + 1); + final Path path = Path.of(resource.getPath()); + final String responseBody = new String(Files.readAllBytes(path)); + String endUri = requestUri.substring(requestUri.lastIndexOf("/") + 1); return String.join( CRLF, "HTTP/1.1 200 OK ", "Content-Type: " + MimeType.getMimeType(endUri) + " ", - "Content-Length: " + responseBody.length + " ", + "Content-Length: " + responseBody.getBytes().length + " ", "", - new String(responseBody) + responseBody ); } } From ba5010d61e7b423b6192e73d469ba5da2346002e Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 14:49:25 +0900 Subject: [PATCH 08/28] =?UTF-8?q?refactor:=20=EA=B0=84=EB=8B=A8=ED=95=9C?= =?UTF-8?q?=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/coyote/http11/Http11Processor.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 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 a75174fbf3..7d3d6bd205 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -35,7 +35,7 @@ public void process(final Socket connection) { final var outputStream = connection.getOutputStream() ) { final HttpRequest request = HttpRequestParser.parse(inputStream); - final String response = getResponse(request); + final String response = generateResponse(request); outputStream.write(response.getBytes()); outputStream.flush(); @@ -44,12 +44,20 @@ public void process(final Socket connection) { } } - private String getResponse(HttpRequest request) throws IOException { - if ("/".equals(request.getUri())) { - return rootPage(); + private String generateResponse(HttpRequest request) { + try { + String uri = request.getUri(); + if ("/".equals(uri)) { + return rootPage(); + } + return getStaticResource(uri); + } catch (IllegalArgumentException e) { + log.error(e.getMessage(), e); + return "HTTP/1.1 404 Not Found "; + } catch (Exception e) { + log.error(e.getMessage(), e); + return "HTTP/1.1 500 Internal Server Error "; } - - return getStaticResource(request.getUri()); } private String rootPage() { @@ -64,7 +72,7 @@ private String rootPage() { private String getStaticResource(String requestUri) throws IOException { URL resource = getClass().getClassLoader().getResource("static" + requestUri); if (resource == null) { - return "HTTP/1.1 404 Not Found "; + throw new IllegalArgumentException("Resource not found"); } final Path path = Path.of(resource.getPath()); From c0378701aac4ab347a0d09b70822a820d242995d Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 15:52:18 +0900 Subject: [PATCH 09/28] =?UTF-8?q?refactor(HttpRequest):=20path=EC=99=80=20?= =?UTF-8?q?parameters=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=ED=8C=8C?= =?UTF-8?q?=EC=8B=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/http/HttpRequest.java | 11 +++++++ .../techcourse/http/HttpRequestParser.java | 31 ++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java index 83ce35fe23..983b6957e2 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java @@ -10,10 +10,21 @@ public class HttpRequest { private String method; private String uri; + private String path; private Map headers; + private Map parameters; private String body; public HttpRequest() { this.headers = new HashMap<>(); + this.parameters = new HashMap<>(); + } + + public String getParameter(String key) { + return parameters.get(key); + } + + public void setParameter(String key, String value) { + parameters.put(key, value); } } diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java index 693a2d6194..09a5f6ddcb 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java @@ -20,10 +20,33 @@ public static HttpRequest parse(InputStream inputStream) throws IOException { private static void parseStartLine(BufferedReader reader, HttpRequest request) throws IOException { String startLine = reader.readLine(); - if (startLine != null) { - String[] startLineParts = startLine.split(" "); - request.setMethod(startLineParts[0]); - request.setUri(startLineParts[1]); + String[] startLineParts = startLine.split(" "); + String method = startLineParts[0]; + String uri = startLineParts[1]; + + request.setMethod(method); + request.setUri(uri); + + int queryIndex = uri.indexOf("?"); + if (queryIndex == -1) { + request.setPath(uri); + return; + } + request.setPath(uri.substring(0, queryIndex)); + String parameterString = uri.substring(queryIndex + 1); + parseParameters(request, parameterString); + } + + private static void parseParameters(HttpRequest request, String parameterString) { + if (parameterString.isBlank()) { + return; + } + + String[] parameters = parameterString.split("&"); + + for (String parameter : parameters) { + String[] keyValue = parameter.split("="); + request.setParameter(keyValue[0], keyValue[1]); } } From 7d4f988d107c6658c687157b55956fcb4fd371bb Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 16:52:39 +0900 Subject: [PATCH 10/28] =?UTF-8?q?feat:=201-3=20Query=20String=20=ED=8C=8C?= =?UTF-8?q?=EC=8B=B1=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/model/User.java | 16 ++----- .../apache/coyote/http11/Http11Processor.java | 48 +++++++++++++++---- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/tomcat/src/main/java/com/techcourse/model/User.java b/tomcat/src/main/java/com/techcourse/model/User.java index e8cf4c8e68..b471634e3f 100644 --- a/tomcat/src/main/java/com/techcourse/model/User.java +++ b/tomcat/src/main/java/com/techcourse/model/User.java @@ -1,5 +1,10 @@ package com.techcourse.model; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor public class User { private final Long id; @@ -7,13 +12,6 @@ public class User { private final String password; private final String email; - public User(Long id, String account, String password, String email) { - this.id = id; - this.account = account; - this.password = password; - this.email = email; - } - public User(String account, String password, String email) { this(null, account, password, email); } @@ -22,10 +20,6 @@ public boolean checkPassword(String password) { return this.password.equals(password); } - public String getAccount() { - return account; - } - @Override public String toString() { return "User{" + 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 7d3d6bd205..b01ad9bae4 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -1,8 +1,10 @@ package org.apache.coyote.http11; +import com.techcourse.db.InMemoryUserRepository; import com.techcourse.http.HttpRequest; import com.techcourse.http.HttpRequestParser; import com.techcourse.http.MimeType; +import com.techcourse.model.User; import java.io.IOException; import java.net.Socket; import java.net.URL; @@ -46,11 +48,15 @@ public void process(final Socket connection) { private String generateResponse(HttpRequest request) { try { - String uri = request.getUri(); - if ("/".equals(uri)) { + String path = request.getPath(); + String method = request.getMethod(); + if ("/".equals(path) && method.equals("GET")) { return rootPage(); } - return getStaticResource(uri); + if (path.equals("/login") && method.equals("GET")) { + return login(request); + } + return getStaticResource(request); } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); return "HTTP/1.1 404 Not Found "; @@ -69,20 +75,44 @@ private String rootPage() { Hello world!"""; } - private String getStaticResource(String requestUri) throws IOException { - URL resource = getClass().getClassLoader().getResource("static" + requestUri); + private String login(HttpRequest request) throws IOException { + String account = request.getParameter("account"); + User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(() -> new IllegalArgumentException("User not found")); + + if (!user.checkPassword(request.getParameter("password"))) { + throw new IllegalArgumentException("Password or email is not correct"); + } + + log.info("user : {}", user); + + URL resource = getClass().getClassLoader().getResource("static/login.html"); + final String responseBody = new String(Files.readAllBytes(Path.of(resource.getPath()))); + return String.join( + CRLF, + "HTTP/1.1 200 OK ", + "Content-Type: text/html;charset=utf-8 ", + "Content-Length: " + responseBody.getBytes().length + " ", + "", + responseBody + ); + } + + private String getStaticResource(HttpRequest request) throws IOException { + String requestPath = request.getPath(); + URL resource = getClass().getClassLoader().getResource("static" + requestPath); if (resource == null) { throw new IllegalArgumentException("Resource not found"); } - final Path path = Path.of(resource.getPath()); - final String responseBody = new String(Files.readAllBytes(path)); - String endUri = requestUri.substring(requestUri.lastIndexOf("/") + 1); + final String responseBody = new String(Files.readAllBytes(Path.of(resource.getPath()))); + String endPath = requestPath.substring(requestPath.lastIndexOf("/") + 1); + String mimeType = MimeType.getMimeType(endPath); return String.join( CRLF, "HTTP/1.1 200 OK ", - "Content-Type: " + MimeType.getMimeType(endUri) + " ", + "Content-Type: " + mimeType + " ", "Content-Length: " + responseBody.getBytes().length + " ", "", responseBody From 38a85a10ad861c94e20ddf9fffb6b10bcf5d9b71 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Thu, 5 Sep 2024 17:17:49 +0900 Subject: [PATCH 11/28] =?UTF-8?q?feat:=202-1=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=20=EC=97=AC=EB=B6=80=EC=97=90=20=EB=94=B0=EB=9D=BC=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=9D=B4=EB=8F=99=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 | 29 +++++++++---------- 1 file changed, 13 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 b01ad9bae4..7f5df36961 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -10,6 +10,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Optional; import org.apache.coyote.Processor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,27 +76,23 @@ private String rootPage() { Hello world!"""; } - private String login(HttpRequest request) throws IOException { + private String login(HttpRequest request) { String account = request.getParameter("account"); - User user = InMemoryUserRepository.findByAccount(account) - .orElseThrow(() -> new IllegalArgumentException("User not found")); + Optional user = InMemoryUserRepository.findByAccount(account); - if (!user.checkPassword(request.getParameter("password"))) { - throw new IllegalArgumentException("Password or email is not correct"); + if (user.isEmpty() || !user.get().checkPassword(request.getParameter("password"))) { + return """ + HTTP/1.1 302 Found \r + Location: /401.html \r + """; } - log.info("user : {}", user); + log.info("user : {}", user.get()); - URL resource = getClass().getClassLoader().getResource("static/login.html"); - final String responseBody = new String(Files.readAllBytes(Path.of(resource.getPath()))); - return String.join( - CRLF, - "HTTP/1.1 200 OK ", - "Content-Type: text/html;charset=utf-8 ", - "Content-Length: " + responseBody.getBytes().length + " ", - "", - responseBody - ); + return """ + HTTP/1.1 302 Found \r + Location: /index.html \r + """; } private String getStaticResource(HttpRequest request) throws IOException { From 721b7e98a6592a58e1f0b98ac293e730a8f0a6d0 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 10:12:12 +0900 Subject: [PATCH 12/28] =?UTF-8?q?refactor:=20HttpResponse=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/http/HttpRequest.java | 1 + .../com/techcourse/http/HttpResponse.java | 87 +++++++++++++++++++ .../java/com/techcourse/http/MimeType.java | 5 +- .../apache/coyote/http11/Http11Processor.java | 79 +++++++---------- .../coyote/http11/Http11ProcessorTest.java | 4 +- 5 files changed, 126 insertions(+), 50 deletions(-) create mode 100644 tomcat/src/main/java/com/techcourse/http/HttpResponse.java diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java index 983b6957e2..03ee823909 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java @@ -8,6 +8,7 @@ @Getter @Setter public class HttpRequest { + private String method; private String uri; private String path; diff --git a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java new file mode 100644 index 0000000000..e07b18689f --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java @@ -0,0 +1,87 @@ +package com.techcourse.http; + +import java.util.HashMap; +import java.util.Map; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class HttpResponse { + + private static final String HTTP_VERSION = "HTTP/1.1"; + private static final String CRLF = "\r\n"; + private static final String HEADER_SEPARATOR = ": "; + + private int statusCode; + private String statusMessage; + private Map headers; + private String body; + + public static HttpResponse ok(String body) { + return new HttpResponse(200, "OK", new HashMap<>(), body); + } + + public static HttpResponse found(String location) { + return new HttpResponse(302, "Found", Map.of( + "Location", location + ), ""); + } + + public static HttpResponse notFound() { + return new HttpResponse(404, "Not Found", new HashMap<>(), ""); + } + + public static HttpResponse internalServerError() { + return new HttpResponse(500, "Internal Server Error", new HashMap<>(), ""); + } + + public String build() { + if (body.isBlank()) { + return "%s %d %s \r\n%s\r\n" + .formatted(HTTP_VERSION, statusCode, statusMessage, getHeadersString()); + } + + headers.put("Content-Length", String.valueOf(body.getBytes().length)); + return "%s %d %s \r\n%s\r\n%s" + .formatted(HTTP_VERSION, statusCode, statusMessage, getHeadersString(), body); + } + + private String getHeadersString() { + StringBuilder headersString = new StringBuilder(); + for (Map.Entry entry : headers.entrySet()) { + headersString.append(entry.getKey()) + .append(HEADER_SEPARATOR) + .append(entry.getValue()) + .append(" ") + .append(CRLF); + } + return headersString.toString(); + } + + public HttpResponse setStatusCode(int statusCode) { + this.statusCode = statusCode; + return this; + } + + public HttpResponse setStatusMessage(String statusMessage) { + this.statusMessage = statusMessage; + return this; + } + + public HttpResponse setHeader(String key, String value) { + headers.put(key, value); + return this; + } + + public HttpResponse setContentType(String contentType) { + headers.put("Content-Type", contentType); + return this; + } + + public HttpResponse setBody(String body) { + this.body = body; + return this; + } +} diff --git a/tomcat/src/main/java/com/techcourse/http/MimeType.java b/tomcat/src/main/java/com/techcourse/http/MimeType.java index f83f640fc9..f2b37ff4eb 100644 --- a/tomcat/src/main/java/com/techcourse/http/MimeType.java +++ b/tomcat/src/main/java/com/techcourse/http/MimeType.java @@ -1,5 +1,8 @@ package com.techcourse.http; +import lombok.Getter; + +@Getter public enum MimeType { HTML(".html", "text/html;charset=utf-8"), CSS(".css", "text/css"), @@ -19,7 +22,7 @@ public enum MimeType { this.mimeType = mimeType; } - public static String getMimeType(String fileName) { + public static String from(String fileName) { for (MimeType type : values()) { if (fileName.endsWith(type.extension)) { return type.mimeType; 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 7f5df36961..6bbf625ba1 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -3,29 +3,29 @@ import com.techcourse.db.InMemoryUserRepository; import com.techcourse.http.HttpRequest; import com.techcourse.http.HttpRequestParser; +import com.techcourse.http.HttpResponse; import com.techcourse.http.MimeType; import com.techcourse.model.User; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.Socket; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.Optional; +import lombok.AllArgsConstructor; import org.apache.coyote.Processor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@AllArgsConstructor public class Http11Processor implements Runnable, Processor { private static final Logger log = LoggerFactory.getLogger(Http11Processor.class); - private static final String CRLF = "\r\n"; private final Socket connection; - public Http11Processor(final Socket connection) { - this.connection = connection; - } - @Override public void run() { log.info("connect host: {}, port: {}", connection.getInetAddress(), connection.getPort()); @@ -34,11 +34,11 @@ public void run() { @Override public void process(final Socket connection) { - try (final var inputStream = connection.getInputStream(); - final var outputStream = connection.getOutputStream() + try (InputStream inputStream = connection.getInputStream(); + OutputStream outputStream = connection.getOutputStream() ) { - final HttpRequest request = HttpRequestParser.parse(inputStream); - final String response = generateResponse(request); + HttpRequest request = HttpRequestParser.parse(inputStream); + String response = generateResponse(request); outputStream.write(response.getBytes()); outputStream.flush(); @@ -52,67 +52,52 @@ private String generateResponse(HttpRequest request) { String path = request.getPath(); String method = request.getMethod(); if ("/".equals(path) && method.equals("GET")) { - return rootPage(); + return HttpResponse.ok("Hello world!") + .setContentType(MimeType.HTML.getMimeType()) + .build(); } if (path.equals("/login") && method.equals("GET")) { - return login(request); + return login(request).build(); } - return getStaticResource(request); + return getStaticResource(request).build(); } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); - return "HTTP/1.1 404 Not Found "; + return HttpResponse.notFound().build(); } catch (Exception e) { log.error(e.getMessage(), e); - return "HTTP/1.1 500 Internal Server Error "; + return HttpResponse.internalServerError().build(); } } - private String rootPage() { - return """ - HTTP/1.1 200 OK \r - Content-Type: text/html;charset=utf-8 \r - Content-Length: 12 \r - \r - Hello world!"""; - } - - private String login(HttpRequest request) { + private HttpResponse login(HttpRequest request) { String account = request.getParameter("account"); Optional user = InMemoryUserRepository.findByAccount(account); if (user.isEmpty() || !user.get().checkPassword(request.getParameter("password"))) { - return """ - HTTP/1.1 302 Found \r - Location: /401.html \r - """; + return HttpResponse.found("/401.html"); } log.info("user : {}", user.get()); - return """ - HTTP/1.1 302 Found \r - Location: /index.html \r - """; + return HttpResponse.found("/index.html"); } - private String getStaticResource(HttpRequest request) throws IOException { + private HttpResponse getStaticResource(HttpRequest request) throws IOException { String requestPath = request.getPath(); - URL resource = getClass().getClassLoader().getResource("static" + requestPath); + + final String responseBody = readResource("static" + requestPath); + String endPath = requestPath.substring(requestPath.lastIndexOf("/") + 1); + String mimeType = MimeType.from(endPath); + + return HttpResponse.ok(responseBody) + .setContentType(mimeType); + } + + private String readResource(String path) throws IOException { + URL resource = getClass().getClassLoader().getResource(path); if (resource == null) { throw new IllegalArgumentException("Resource not found"); } - - final String responseBody = new String(Files.readAllBytes(Path.of(resource.getPath()))); - String endPath = requestPath.substring(requestPath.lastIndexOf("/") + 1); - String mimeType = MimeType.getMimeType(endPath); - - return String.join( - CRLF, - "HTTP/1.1 200 OK ", - "Content-Type: " + mimeType + " ", - "Content-Length: " + responseBody.getBytes().length + " ", - "", - responseBody - ); + return new String(Files.readAllBytes(Path.of(resource.getPath()))); } } diff --git a/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java b/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java index 8af591bcf0..9159ee0c6f 100644 --- a/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java +++ b/tomcat/src/test/java/org/apache/coyote/http11/Http11ProcessorTest.java @@ -23,8 +23,8 @@ void process() { // then var expected = String.join("\r\n", "HTTP/1.1 200 OK ", - "Content-Type: text/html;charset=utf-8 ", "Content-Length: 12 ", + "Content-Type: text/html;charset=utf-8 ", "", "Hello world!"); @@ -50,8 +50,8 @@ void index() throws IOException { // then final URL resource = getClass().getClassLoader().getResource("static/index.html"); var expected = "HTTP/1.1 200 OK \r\n" + - "Content-Type: text/html;charset=utf-8 \r\n" + "Content-Length: 5564 \r\n" + + "Content-Type: text/html;charset=utf-8 \r\n" + "\r\n" + new String(Files.readAllBytes(new File(resource.getFile()).toPath())); From 6f53481aadf9215828df539ba837ba833334f8ca Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 10:24:31 +0900 Subject: [PATCH 13/28] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EC=9D=B8=EC=9E=90=20=EB=B0=8F=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - getStaticResource(HttpRequest request) -> getStaticResourceResponse(String requestPath) --- .../main/java/org/apache/coyote/http11/Http11Processor.java | 6 ++---- 1 file changed, 2 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 6bbf625ba1..cc582dd7a9 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -59,7 +59,7 @@ private String generateResponse(HttpRequest request) { if (path.equals("/login") && method.equals("GET")) { return login(request).build(); } - return getStaticResource(request).build(); + return getStaticResourceResponse(request.getPath()).build(); } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); return HttpResponse.notFound().build(); @@ -82,9 +82,7 @@ private HttpResponse login(HttpRequest request) { return HttpResponse.found("/index.html"); } - private HttpResponse getStaticResource(HttpRequest request) throws IOException { - String requestPath = request.getPath(); - + private HttpResponse getStaticResourceResponse(String requestPath) throws IOException { final String responseBody = readResource("static" + requestPath); String endPath = requestPath.substring(requestPath.lastIndexOf("/") + 1); String mimeType = MimeType.from(endPath); From db007a25001f8d0ccc4f74b5d724b4a8b90947c0 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 14:17:55 +0900 Subject: [PATCH 14/28] =?UTF-8?q?fix:=20=ED=85=9C=ED=94=8C=EB=A6=BF=20?= =?UTF-8?q?=EC=97=94=EC=A7=84=20=EB=AF=B8=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- study/src/main/resources/{templates => static}/index.html | 0 .../main/resources/{templates => static}/resource-versioning.html | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename study/src/main/resources/{templates => static}/index.html (100%) rename study/src/main/resources/{templates => static}/resource-versioning.html (100%) diff --git a/study/src/main/resources/templates/index.html b/study/src/main/resources/static/index.html similarity index 100% rename from study/src/main/resources/templates/index.html rename to study/src/main/resources/static/index.html diff --git a/study/src/main/resources/templates/resource-versioning.html b/study/src/main/resources/static/resource-versioning.html similarity index 100% rename from study/src/main/resources/templates/resource-versioning.html rename to study/src/main/resources/static/resource-versioning.html From 36c22be49c15acb00880b79dccf7d72bcf0aab87 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 14:18:48 +0900 Subject: [PATCH 15/28] =?UTF-8?q?test:=20=ED=9C=B4=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8B=B1=20=EC=BA=90=EC=8B=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CacheControlInterceptor 추가 --- .../cachecontrol/CacheControlInterceptor.java | 21 +++++++++++++++++++ .../example/cachecontrol/CacheWebConfig.java | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 study/src/main/java/cache/com/example/cachecontrol/CacheControlInterceptor.java 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..12d92b20b7 --- /dev/null +++ b/study/src/main/java/cache/com/example/cachecontrol/CacheControlInterceptor.java @@ -0,0 +1,21 @@ +package cache.com.example.cachecontrol; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.http.CacheControl; +import org.springframework.http.HttpHeaders; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +public class CacheControlInterceptor implements HandlerInterceptor { + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, + ModelAndView modelAndView) throws Exception { + final String cacheControl = CacheControl + .noCache() + .cachePrivate() + .getHeaderValue(); + response.addHeader(HttpHeaders.CACHE_CONTROL, cacheControl); + } +} 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..8d39c2e2bd 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) { + CacheControlInterceptor interceptor = new CacheControlInterceptor(); + registry.addInterceptor(interceptor); } } From add93cd5cc9eb194d73248684bd673eaf3e9b267 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 14:19:06 +0900 Subject: [PATCH 16/28] =?UTF-8?q?test:=20HTTP=20Compression=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- study/src/main/resources/application.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/study/src/main/resources/application.yml b/study/src/main/resources/application.yml index e3503a5fb9..8b74bdfd88 100644 --- a/study/src/main/resources/application.yml +++ b/study/src/main/resources/application.yml @@ -8,3 +8,6 @@ server: threads: min-spare: 2 max: 2 + compression: + enabled: true + min-response-size: 10 From 7f37783bec7e6b67a2551c35ea5b0253e62ea79a Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 14:20:39 +0900 Subject: [PATCH 17/28] =?UTF-8?q?fix:=20=EC=95=8C=EB=A7=9E=EC=9D=80=20reso?= =?UTF-8?q?urce=20=EC=9D=B4=EB=A6=84=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cache/com/example/GreetingController.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 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..ef130677be 100644 --- a/study/src/main/java/cache/com/example/GreetingController.java +++ b/study/src/main/java/cache/com/example/GreetingController.java @@ -12,7 +12,7 @@ public class GreetingController { @GetMapping("/") public String index() { - return "index"; + return "index.html"; } /** @@ -25,16 +25,16 @@ public String cacheControl(final HttpServletResponse response) { .cachePrivate() .getHeaderValue(); response.addHeader(HttpHeaders.CACHE_CONTROL, cacheControl); - return "index"; + return "index.html"; } @GetMapping("/etag") public String etag() { - return "index"; + return "index.html"; } @GetMapping("/resource-versioning") public String resourceVersioning() { - return "resource-versioning"; + return "resource-versioning.html"; } } From 7497b66d12bb3eef5b4cca4b9dd2e0791c439d51 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 14:21:06 +0900 Subject: [PATCH 18/28] =?UTF-8?q?test:=20ETag/If-None-Match=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ShallowEtagHeaderFilter 사용 --- .../com/example/etag/EtagFilterConfiguration.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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..4fb1d0460a 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,18 @@ 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() { + final var filter = new ShallowEtagHeaderFilter(); + final var registrationBean = new FilterRegistrationBean<>(filter); + registrationBean.addUrlPatterns("/etag"); + return registrationBean; + } } From a633a27d46ecaa6ef4e1effdf0fa188bf4a5cfe4 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 14:58:52 +0900 Subject: [PATCH 19/28] =?UTF-8?q?test:=20=EC=BA=90=EC=8B=9C=20=EB=AC=B4?= =?UTF-8?q?=ED=9A=A8=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 캐시 max-age 1년 설정 - ETag 적용 - url에 버전 적용 --- .../cache/com/example/version/CacheBustingWebConfig.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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..2ec64e8fb0 100644 --- a/study/src/main/java/cache/com/example/version/CacheBustingWebConfig.java +++ b/study/src/main/java/cache/com/example/version/CacheBustingWebConfig.java @@ -1,7 +1,9 @@ package cache.com.example.version; +import java.time.Duration; 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; @@ -20,6 +22,8 @@ public CacheBustingWebConfig(ResourceVersion version) { @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler(PREFIX_STATIC_RESOURCES + "/" + version.getVersion() + "/**") - .addResourceLocations("classpath:/static/"); + .addResourceLocations("classpath:/static/") + .setEtagGenerator(request -> version.getVersion()) + .setCacheControl(CacheControl.maxAge(Duration.ofDays(365)).cachePublic()); } } From 34d44a6b2993e857d95d161a61c806872c0412fd Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 15:29:38 +0900 Subject: [PATCH 20/28] =?UTF-8?q?feat:=20POST=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 요청 본문 파싱 로직 수정 --- .../java/com/techcourse/http/HttpRequest.java | 8 ++++++ .../techcourse/http/HttpRequestParser.java | 24 ++++++++++-------- .../apache/coyote/http11/Http11Processor.java | 25 +++++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java index 03ee823909..40c0e95af7 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java @@ -25,7 +25,15 @@ public String getParameter(String key) { return parameters.get(key); } + public String getHeader(String key) { + return headers.get(key); + } + public void setParameter(String key, String value) { parameters.put(key, value); } + + public void setHeader(String key, String value) { + headers.put(key, value); + } } diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java index 09a5f6ddcb..a12c901492 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java @@ -54,20 +54,24 @@ private static void parseHeaders(BufferedReader reader, HttpRequest request) thr String line; while (!(line = reader.readLine()).isEmpty()) { String[] headerParts = line.split(": "); - request.getHeaders().put(headerParts[0], headerParts[1]); + request.setHeader(headerParts[0], headerParts[1]); } } private static void parseBody(BufferedReader reader, HttpRequest request) throws IOException { - StringBuilder body = new StringBuilder(); - String line; - while (reader.ready()) { - line = reader.readLine(); - if (line == null) { - break; - } - body.append(line).append("\n"); + String contentLengthHeader = request.getHeader("Content-Length"); + if (contentLengthHeader == null) { + return; + } + + int contentLength = Integer.parseInt(contentLengthHeader); + char[] bodyChars = new char[contentLength]; + int readChars = reader.read(bodyChars, 0, contentLength); + + if (readChars != contentLength) { + throw new IOException("Failed to read the entire request body"); } - request.setBody(body.toString().trim()); + + request.setBody(new String(bodyChars)); } } 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 cc582dd7a9..e9ce76c86d 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -13,6 +13,8 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import lombok.AllArgsConstructor; import org.apache.coyote.Processor; @@ -59,6 +61,12 @@ private String generateResponse(HttpRequest request) { if (path.equals("/login") && method.equals("GET")) { return login(request).build(); } + if (path.equals("/register") && method.equals("GET")) { + return getStaticResourceResponse("/register.html").build(); + } + if (path.equals("/register") && method.equals("POST")) { + return register(request).build(); + } return getStaticResourceResponse(request.getPath()).build(); } catch (IllegalArgumentException e) { log.error(e.getMessage(), e); @@ -82,6 +90,23 @@ private HttpResponse login(HttpRequest request) { return HttpResponse.found("/index.html"); } + private HttpResponse register(HttpRequest request) { + Map parameters = new HashMap<>(); + String body = request.getBody(); + for (String data : body.split("&")) { + String[] keyValue = data.split("="); + parameters.put(keyValue[0], keyValue[1]); + } + + InMemoryUserRepository.save(new User( + parameters.get("account"), + parameters.get("password"), + parameters.get("email") + )); + + return HttpResponse.found("/index.html"); + } + private HttpResponse getStaticResourceResponse(String requestPath) throws IOException { final String responseBody = readResource("static" + requestPath); String endPath = requestPath.substring(requestPath.lastIndexOf("/") + 1); From 837933e3ed29d716e387fa9c950e20641aee39de Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 15:38:18 +0900 Subject: [PATCH 21/28] =?UTF-8?q?fix:=20=ED=8C=8C=EB=9D=BC=EB=AF=B8?= =?UTF-8?q?=ED=84=B0=20=EC=97=86=EB=8A=94=20/login=20=EC=A0=91=EC=86=8D=20?= =?UTF-8?q?=EB=B6=88=EA=B0=80=EB=8A=A5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/apache/coyote/http11/Http11Processor.java | 6 +++++- 1 file changed, 5 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 e9ce76c86d..401aa6f7c9 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -77,7 +77,11 @@ private String generateResponse(HttpRequest request) { } } - private HttpResponse login(HttpRequest request) { + private HttpResponse login(HttpRequest request) throws IOException { + if (request.getParameters().isEmpty()) { + return getStaticResourceResponse("/login.html"); + } + String account = request.getParameter("account"); Optional user = InMemoryUserRepository.findByAccount(account); From 1b6cb981045f80d4bc0c70db88009bed7f187bf6 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 16:13:24 +0900 Subject: [PATCH 22/28] =?UTF-8?q?fix:=20=EC=9A=94=EC=B2=AD=20=EB=B3=B8?= =?UTF-8?q?=EB=AC=B8=20URLDecode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/techcourse/http/HttpRequestParser.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java index a12c901492..1b6bf9c009 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; public class HttpRequestParser { @@ -72,6 +74,6 @@ private static void parseBody(BufferedReader reader, HttpRequest request) throws throw new IOException("Failed to read the entire request body"); } - request.setBody(new String(bodyChars)); + request.setBody(URLDecoder.decode(new String(bodyChars), StandardCharsets.UTF_8)); } } From 0125570cc4b6185fef7b79432271853dd70d2a99 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 16:38:47 +0900 Subject: [PATCH 23/28] =?UTF-8?q?fix:=20Cookie=EC=97=90=20JSESSIONID=20?= =?UTF-8?q?=EA=B0=92=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - HttpCookie 객체 구현 및 요청, 응답 객체에 추가 --- .../java/com/techcourse/http/HttpCookie.java | 34 +++++++++++++++++++ .../java/com/techcourse/http/HttpRequest.java | 6 ++++ .../techcourse/http/HttpRequestParser.java | 18 ++++++++++ .../com/techcourse/http/HttpResponse.java | 29 ++++++++++++---- .../apache/coyote/http11/Http11Processor.java | 8 ++++- 5 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 tomcat/src/main/java/com/techcourse/http/HttpCookie.java diff --git a/tomcat/src/main/java/com/techcourse/http/HttpCookie.java b/tomcat/src/main/java/com/techcourse/http/HttpCookie.java new file mode 100644 index 0000000000..76c819a0a8 --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/http/HttpCookie.java @@ -0,0 +1,34 @@ +package com.techcourse.http; + +import java.util.HashMap; +import java.util.Map; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class HttpCookie { + + private final Map cookies; + + public HttpCookie() { + this.cookies = new HashMap<>(); + } + + public String serialize() { + StringBuilder cookieString = new StringBuilder(); + for (Map.Entry entry : cookies.entrySet()) { + cookieString.append(entry.getKey()) + .append("=") + .append(entry.getValue()) + .append("; "); + } + return cookieString.toString(); + } + + public boolean isExist() { + return !cookies.isEmpty(); + } + + public String getCookie(String key) { + return cookies.get(key); + } +} diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java index 40c0e95af7..9bcfe396e6 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequest.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequest.java @@ -13,12 +13,14 @@ public class HttpRequest { private String uri; private String path; private Map headers; + private HttpCookie cookie; private Map parameters; private String body; public HttpRequest() { this.headers = new HashMap<>(); this.parameters = new HashMap<>(); + this.cookie = new HttpCookie(); } public String getParameter(String key) { @@ -29,6 +31,10 @@ public String getHeader(String key) { return headers.get(key); } + public String getCookie(String key) { + return cookie.getCookie(key); + } + public void setParameter(String key, String value) { parameters.put(key, value); } diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java index 1b6bf9c009..919455de55 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java @@ -6,6 +6,8 @@ import java.io.InputStreamReader; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; public class HttpRequestParser { @@ -58,6 +60,22 @@ private static void parseHeaders(BufferedReader reader, HttpRequest request) thr String[] headerParts = line.split(": "); request.setHeader(headerParts[0], headerParts[1]); } + + if (request.getHeader("Cookie") != null) { + HttpCookie cookie = parseCookie(request.getHeader("Cookie")); + request.setCookie(cookie); + } + } + + private static HttpCookie parseCookie(String cookieString) { + Map cookies = new HashMap<>(); + + String[] cookieArray = cookieString.split("; "); + for (String cookiePair : cookieArray) { + String[] pair = cookiePair.split("="); + cookies.put(pair[0], pair[1]); + } + return new HttpCookie(cookies); } private static void parseBody(BufferedReader reader, HttpRequest request) throws IOException { diff --git a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java index e07b18689f..b4395c3620 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java @@ -17,24 +17,33 @@ public class HttpResponse { private int statusCode; private String statusMessage; private Map headers; + private HttpCookie cookie; private String body; + private HttpResponse(int statusCode, String statusMessage, String body) { + this.statusCode = statusCode; + this.statusMessage = statusMessage; + this.headers = new HashMap<>(); + this.cookie = new HttpCookie(); + this.body = body; + } + public static HttpResponse ok(String body) { - return new HttpResponse(200, "OK", new HashMap<>(), body); + return new HttpResponse(200, "OK", body); } public static HttpResponse found(String location) { - return new HttpResponse(302, "Found", Map.of( - "Location", location - ), ""); + return new HttpResponse( + 302, "Found", new HashMap<>(Map.of("Location", location)), new HttpCookie(), "" + ); } public static HttpResponse notFound() { - return new HttpResponse(404, "Not Found", new HashMap<>(), ""); + return new HttpResponse(404, "Not Found", ""); } public static HttpResponse internalServerError() { - return new HttpResponse(500, "Internal Server Error", new HashMap<>(), ""); + return new HttpResponse(500, "Internal Server Error", ""); } public String build() { @@ -42,6 +51,9 @@ public String build() { return "%s %d %s \r\n%s\r\n" .formatted(HTTP_VERSION, statusCode, statusMessage, getHeadersString()); } + if (cookie.isExist()) { + headers.put("Set-Cookie", cookie.serialize()); + } headers.put("Content-Length", String.valueOf(body.getBytes().length)); return "%s %d %s \r\n%s\r\n%s" @@ -80,6 +92,11 @@ public HttpResponse setContentType(String contentType) { return this; } + public HttpResponse setCookie(String key, String value) { + headers.put("Set-Cookie", "%s=%s".formatted(key, value)); + return this; + } + public HttpResponse setBody(String body) { this.body = body; return this; 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 401aa6f7c9..e9ff57daf9 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.UUID; import lombok.AllArgsConstructor; import org.apache.coyote.Processor; import org.slf4j.Logger; @@ -25,6 +26,7 @@ public class Http11Processor implements Runnable, Processor { private static final Logger log = LoggerFactory.getLogger(Http11Processor.class); + private static final String JSESSIONID = "JSESSIONID"; private final Socket connection; @@ -91,7 +93,11 @@ private HttpResponse login(HttpRequest request) throws IOException { log.info("user : {}", user.get()); - return HttpResponse.found("/index.html"); + HttpResponse redirect = HttpResponse.found("/index.html"); + if (request.getCookie(JSESSIONID) == null) { + redirect.setCookie(JSESSIONID, UUID.randomUUID().toString()); + } + return redirect; } private HttpResponse register(HttpRequest request) { From 410976033209c5c1fdce151323a2b5005ca6a5e9 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Fri, 6 Sep 2024 17:51:23 +0900 Subject: [PATCH 24/28] =?UTF-8?q?feat:=20Session=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/auth/Session.java | 37 ++++++++++++++++++ .../com/techcourse/auth/SessionManager.java | 29 ++++++++++++++ .../com/techcourse/http/HttpResponse.java | 4 ++ .../apache/coyote/http11/Http11Processor.java | 39 +++++++++++++++---- 4 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 tomcat/src/main/java/com/techcourse/auth/Session.java create mode 100644 tomcat/src/main/java/com/techcourse/auth/SessionManager.java diff --git a/tomcat/src/main/java/com/techcourse/auth/Session.java b/tomcat/src/main/java/com/techcourse/auth/Session.java new file mode 100644 index 0000000000..670eeddb03 --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/auth/Session.java @@ -0,0 +1,37 @@ +package com.techcourse.auth; + +import com.techcourse.model.User; +import java.util.HashMap; +import java.util.Map; +import lombok.Getter; + +public class Session { + + @Getter + private final String id; + private final Map values = new HashMap<>(); + + public Session(String id) { + this.id = id; + } + + public Object getAttribute(String name) { + return values.get(name); + } + + public void setAttribute(String name, Object value) { + values.put(name, value); + } + + public void removeAttribute(String name) { + values.remove(name); + } + + public void invalidate() { + values.clear(); + } + + private User getUser(Session session) { + return (User) session.getAttribute("user"); + } +} diff --git a/tomcat/src/main/java/com/techcourse/auth/SessionManager.java b/tomcat/src/main/java/com/techcourse/auth/SessionManager.java new file mode 100644 index 0000000000..91f0340a16 --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/auth/SessionManager.java @@ -0,0 +1,29 @@ +package com.techcourse.auth; + +import java.util.HashMap; +import java.util.Map; + +public class SessionManager { + + private static final SessionManager INSTANCE = new SessionManager(); + private static final Map SESSIONS = new HashMap<>(); + + private SessionManager() { + } + + public static SessionManager getInstance() { + return INSTANCE; + } + + public void add(Session session) { + SESSIONS.put(session.getId(), session); + } + + public Session findSession(String id) { + return SESSIONS.get(id); + } + + public void remove(String id) { + SESSIONS.remove(id); + } +} diff --git a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java index b4395c3620..914bf235c8 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java @@ -93,6 +93,10 @@ public HttpResponse setContentType(String contentType) { } public HttpResponse setCookie(String key, String value) { + if (headers.get("Set-Cookie") != null) { + headers.put("Set-Cookie", "%s; %s=%s".formatted(headers.get("Set-Cookie"), key, value)); + return this; + } headers.put("Set-Cookie", "%s=%s".formatted(key, value)); return this; } 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 e9ff57daf9..6b6ba65e7c 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,7 @@ package org.apache.coyote.http11; +import com.techcourse.auth.Session; +import com.techcourse.auth.SessionManager; import com.techcourse.db.InMemoryUserRepository; import com.techcourse.http.HttpRequest; import com.techcourse.http.HttpRequestParser; @@ -26,6 +28,7 @@ public class Http11Processor implements Runnable, Processor { private static final Logger log = LoggerFactory.getLogger(Http11Processor.class); + private static final SessionManager sessionManager = SessionManager.getInstance(); private static final String JSESSIONID = "JSESSIONID"; private final Socket connection; @@ -53,6 +56,13 @@ public void process(final Socket connection) { private String generateResponse(HttpRequest request) { try { + String jSession = request.getCookie(JSESSIONID); + if (isInvalidJSession(jSession)) { + return HttpResponse.found("/login.html") + .setCookie(JSESSIONID, jSession).setCookie("Max-Age", "0") + .build(); + } + String path = request.getPath(); String method = request.getMethod(); if ("/".equals(path) && method.equals("GET")) { @@ -79,25 +89,38 @@ private String generateResponse(HttpRequest request) { } } + private boolean isInvalidJSession(String jSession) { + return jSession != null && sessionManager.findSession(jSession) == null; + } + private HttpResponse login(HttpRequest request) throws IOException { - if (request.getParameters().isEmpty()) { + String jSession = request.getCookie(JSESSIONID); + if (request.getParameters().isEmpty() && jSession == null) { return getStaticResourceResponse("/login.html"); } + if (request.getParameters().isEmpty()) { + return HttpResponse.found("/index.html"); + } String account = request.getParameter("account"); - Optional user = InMemoryUserRepository.findByAccount(account); + Optional userOpt = InMemoryUserRepository.findByAccount(account); - if (user.isEmpty() || !user.get().checkPassword(request.getParameter("password"))) { + if (userOpt.isEmpty() || !userOpt.get().checkPassword(request.getParameter("password"))) { return HttpResponse.found("/401.html"); } - log.info("user : {}", user.get()); + User user = userOpt.get(); + log.info("user : {}", user); + + HttpResponse response = HttpResponse.found("/index.html"); + if (jSession == null) { + Session session = new Session(UUID.randomUUID().toString()); + session.setAttribute("user", user); - HttpResponse redirect = HttpResponse.found("/index.html"); - if (request.getCookie(JSESSIONID) == null) { - redirect.setCookie(JSESSIONID, UUID.randomUUID().toString()); + sessionManager.add(session); + response.setCookie(JSESSIONID, session.getId()); } - return redirect; + return response; } private HttpResponse register(HttpRequest request) { From b3de68a0e8f3f87a1728c5e343d9637dc838659f Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Mon, 9 Sep 2024 13:44:02 +0900 Subject: [PATCH 25/28] =?UTF-8?q?refactor:=20=EC=9A=94=EC=B2=AD=20URL=20?= =?UTF-8?q?=EB=94=94=EC=BD=94=EB=94=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java index 919455de55..8d806ed71e 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java @@ -26,7 +26,7 @@ private static void parseStartLine(BufferedReader reader, HttpRequest request) t String startLine = reader.readLine(); String[] startLineParts = startLine.split(" "); String method = startLineParts[0]; - String uri = startLineParts[1]; + String uri = URLDecoder.decode(startLineParts[1], StandardCharsets.UTF_8); request.setMethod(method); request.setUri(uri); From ac157e20b02cae15e6ac8908769ea708e1585b6c Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Mon, 9 Sep 2024 13:54:29 +0900 Subject: [PATCH 26/28] =?UTF-8?q?refactor:=20application/x-www-form-urlenc?= =?UTF-8?q?oded=20MIME=20=ED=83=80=EC=9E=85=20=EC=9A=94=EC=B2=AD=20?= =?UTF-8?q?=EB=B3=B8=EB=AC=B8=20=EA=B0=92=EC=9D=84=20=ED=8C=8C=EB=9D=BC?= =?UTF-8?q?=EB=AF=B8=ED=84=B0=EB=A1=9C=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/http/HttpRequestParser.java | 11 ++++++++--- .../org/apache/coyote/http11/Http11Processor.java | 15 +++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java index 8d806ed71e..ebc91e1a8c 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpRequestParser.java @@ -86,12 +86,17 @@ private static void parseBody(BufferedReader reader, HttpRequest request) throws int contentLength = Integer.parseInt(contentLengthHeader); char[] bodyChars = new char[contentLength]; - int readChars = reader.read(bodyChars, 0, contentLength); - if (readChars != contentLength) { + if (reader.read(bodyChars, 0, contentLength) != contentLength) { throw new IOException("Failed to read the entire request body"); } - request.setBody(URLDecoder.decode(new String(bodyChars), StandardCharsets.UTF_8)); + String contentTypeHeader = request.getHeader("Content-Type"); + if ("application/x-www-form-urlencoded".equals(contentTypeHeader)) { + parseParameters(request, new String(bodyChars)); + return; + } + + request.setBody(new String(bodyChars)); } } 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 6b6ba65e7c..034ed87c0c 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -15,8 +15,6 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; import java.util.UUID; import lombok.AllArgsConstructor; @@ -124,17 +122,10 @@ private HttpResponse login(HttpRequest request) throws IOException { } private HttpResponse register(HttpRequest request) { - Map parameters = new HashMap<>(); - String body = request.getBody(); - for (String data : body.split("&")) { - String[] keyValue = data.split("="); - parameters.put(keyValue[0], keyValue[1]); - } - InMemoryUserRepository.save(new User( - parameters.get("account"), - parameters.get("password"), - parameters.get("email") + request.getParameter("account"), + request.getParameter("password"), + request.getParameter("email") )); return HttpResponse.found("/index.html"); From c3c3791cf5cdb8b4943a5c8633306ac9a4369592 Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Mon, 9 Sep 2024 14:08:40 +0900 Subject: [PATCH 27/28] =?UTF-8?q?refactor(HttpResponse):=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9E=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/http/HttpResponse.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java index 914bf235c8..aedc62687f 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java @@ -21,11 +21,11 @@ public class HttpResponse { private String body; private HttpResponse(int statusCode, String statusMessage, String body) { - this.statusCode = statusCode; - this.statusMessage = statusMessage; - this.headers = new HashMap<>(); - this.cookie = new HttpCookie(); - this.body = body; + this(statusCode, statusMessage, new HashMap<>(), new HttpCookie(), body); + } + + private HttpResponse(int statusCode, String statusMessage) { + this(statusCode, statusMessage, ""); } public static HttpResponse ok(String body) { @@ -33,29 +33,25 @@ public static HttpResponse ok(String body) { } public static HttpResponse found(String location) { - return new HttpResponse( - 302, "Found", new HashMap<>(Map.of("Location", location)), new HttpCookie(), "" - ); + return new HttpResponse(302, "Found").setHeader("Location", location); } public static HttpResponse notFound() { - return new HttpResponse(404, "Not Found", ""); + return new HttpResponse(404, "Not Found"); } public static HttpResponse internalServerError() { - return new HttpResponse(500, "Internal Server Error", ""); + return new HttpResponse(500, "Internal Server Error"); } public String build() { - if (body.isBlank()) { - return "%s %d %s \r\n%s\r\n" - .formatted(HTTP_VERSION, statusCode, statusMessage, getHeadersString()); - } if (cookie.isExist()) { headers.put("Set-Cookie", cookie.serialize()); } + if (!body.isBlank()) { + headers.put("Content-Length", String.valueOf(body.getBytes().length)); + } - headers.put("Content-Length", String.valueOf(body.getBytes().length)); return "%s %d %s \r\n%s\r\n%s" .formatted(HTTP_VERSION, statusCode, statusMessage, getHeadersString(), body); } From 1ceb56d9c4e9353ec1118a5f434cca1213e4f6bf Mon Sep 17 00:00:00 2001 From: seunghye218 Date: Mon, 9 Sep 2024 14:30:52 +0900 Subject: [PATCH 28/28] =?UTF-8?q?refactor(HttpResponse):=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=20=EC=BD=94=EB=93=9C=20=EB=B0=8F=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EC=97=B4=EA=B1=B0=ED=98=95=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - enum HttpStatusCode --- .../com/techcourse/http/HttpResponse.java | 35 ++++++++----------- .../com/techcourse/http/HttpStatusCode.java | 29 +++++++++++++++ 2 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 tomcat/src/main/java/com/techcourse/http/HttpStatusCode.java diff --git a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java index aedc62687f..aababdf7ec 100644 --- a/tomcat/src/main/java/com/techcourse/http/HttpResponse.java +++ b/tomcat/src/main/java/com/techcourse/http/HttpResponse.java @@ -14,34 +14,37 @@ public class HttpResponse { private static final String CRLF = "\r\n"; private static final String HEADER_SEPARATOR = ": "; - private int statusCode; - private String statusMessage; + private HttpStatusCode httpStatusCode; private Map headers; private HttpCookie cookie; private String body; - private HttpResponse(int statusCode, String statusMessage, String body) { - this(statusCode, statusMessage, new HashMap<>(), new HttpCookie(), body); + private HttpResponse(HttpStatusCode statusCode, String body) { + this(statusCode, new HashMap<>(), new HttpCookie(), body); } - private HttpResponse(int statusCode, String statusMessage) { - this(statusCode, statusMessage, ""); + private HttpResponse(int statusCode, String body) { + this(HttpStatusCode.from(statusCode), new HashMap<>(), new HttpCookie(), body); + } + + private HttpResponse(HttpStatusCode statusCode) { + this(statusCode, ""); } public static HttpResponse ok(String body) { - return new HttpResponse(200, "OK", body); + return new HttpResponse(HttpStatusCode.OK, body); } public static HttpResponse found(String location) { - return new HttpResponse(302, "Found").setHeader("Location", location); + return new HttpResponse(HttpStatusCode.FOUND).setHeader("Location", location); } public static HttpResponse notFound() { - return new HttpResponse(404, "Not Found"); + return new HttpResponse(HttpStatusCode.NOT_FOUND); } public static HttpResponse internalServerError() { - return new HttpResponse(500, "Internal Server Error"); + return new HttpResponse(HttpStatusCode.INTERNAL_SERVER_ERROR); } public String build() { @@ -53,7 +56,7 @@ public String build() { } return "%s %d %s \r\n%s\r\n%s" - .formatted(HTTP_VERSION, statusCode, statusMessage, getHeadersString(), body); + .formatted(HTTP_VERSION, httpStatusCode.getCode(), httpStatusCode.getMessage(), getHeadersString(), body); } private String getHeadersString() { @@ -68,16 +71,6 @@ private String getHeadersString() { return headersString.toString(); } - public HttpResponse setStatusCode(int statusCode) { - this.statusCode = statusCode; - return this; - } - - public HttpResponse setStatusMessage(String statusMessage) { - this.statusMessage = statusMessage; - return this; - } - public HttpResponse setHeader(String key, String value) { headers.put(key, value); return this; diff --git a/tomcat/src/main/java/com/techcourse/http/HttpStatusCode.java b/tomcat/src/main/java/com/techcourse/http/HttpStatusCode.java new file mode 100644 index 0000000000..94d8da06ad --- /dev/null +++ b/tomcat/src/main/java/com/techcourse/http/HttpStatusCode.java @@ -0,0 +1,29 @@ +package com.techcourse.http; + +import lombok.Getter; + +@Getter +public enum HttpStatusCode { + + OK(200, "OK"), + FOUND(302, "Found"), + NOT_FOUND(404, "Not Found"), + INTERNAL_SERVER_ERROR(500, "Internal Server Error"); + + private final int code; + private final String message; + + HttpStatusCode(int code, String message) { + this.code = code; + this.message = message; + } + + public static HttpStatusCode from(int code) { + for (HttpStatusCode status : values()) { + if (status.code == code) { + return status; + } + } + return null; + } +}