Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[๐Ÿฑ Tomcat ๊ตฌํ˜„ํ•˜๊ธฐ 1๋‹จ๊ณ„] ์ œ์šฐ์Šค(์‹ ์žฌ์šฐ) ๋ฏธ์…˜ ์ œ์ถœํ•ฉ๋‹ˆ๋‹ค. #552

Merged
merged 11 commits into from
Sep 9, 2024
Merged
5 changes: 5 additions & 0 deletions docs/requirements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ

- [x] 1\. GET `index.html` ์‘๋‹ตํ•˜๊ธฐ
- [ ] 2\. CSS ์ง€์›ํ•˜๊ธฐ
- [ ] 3\. Query String ํŒŒ์‹ฑ
38 changes: 34 additions & 4 deletions tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.apache.coyote.http11;

import com.techcourse.exception.UncheckedServletException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
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;
import com.techcourse.exception.UncheckedServletException;

public class Http11Processor implements Runnable, Processor {

Expand All @@ -29,8 +34,33 @@ public void process(final Socket connection) {
try (final var inputStream = connection.getInputStream();
final var outputStream = connection.getOutputStream()) {

final var responseBody = "Hello world!";
// 1. Request
final var inputStreamReader = new InputStreamReader(inputStream);
final var bufferedReader = new BufferedReader(inputStreamReader);

// 1.1. Request Line
final var requestLine = bufferedReader.readLine();
final var requestLineSplit = requestLine.split(" ");
final var _method = requestLineSplit[0];
final var path = requestLineSplit[1];
final var _protocolVersion = requestLineSplit[2];

final var filename = path.replace("/", "");

// 1.1.1. Find Static Resource
String responseBody;
if (!filename.isEmpty()) {
final var classLoader = getClass().getClassLoader();
final var url = classLoader.getResource("static/" + filename);
final var resourcePath = Path.of(url.getPath());

responseBody = Files.readString(resourcePath);

} else {
responseBody = "Hello world!";
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

responseBody๋ฅผ "Hello,World!"๋กœ ๋””ํดํŠธ๋กœ ๊ณ ์ •ํ•˜๊ณ , ํŒŒ์ผ ์ด๋ฆ„์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋‹ค๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋ฉดelse` ๊ตฌ๋ฌธ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š” !


// 2. Response
final var response = String.join("\r\n",
"HTTP/1.1 200 OK ",
"Content-Type: text/html;charset=utf-8 ",
Expand Down