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

[케이] Step 3: HTTP 웹서버 구현 #3

Open
wants to merge 35 commits into
base: leekm0310
Choose a base branch
from
Open
Changes from 3 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6eb9e0e
1단계: BufferedReader 생성 및 요청정보 읽어 출력
leekm0310 Jul 19, 2022
731db31
2단계: LineParser 생성
leekm0310 Jul 19, 2022
8f10cdc
3단계: webapp 디렉토리에서 요청 URL에 해당하는 파일을 읽어오기
leekm0310 Jul 19, 2022
5de6103
Merge pull request #1 from leekm0310/3.4.3.1
leekm0310 Jul 19, 2022
5bb0f5a
요구사항 2: GET 방식으로 회원가입하기
leekm0310 Jul 20, 2022
52ef241
Merge pull request #2 from leekm0310/3.4.3.2
leekm0310 Jul 20, 2022
fc0418e
요구사항 3: POST 방식으로 회원가입하기
leekm0310 Jul 20, 2022
d1a81ed
Merge pull request #3 from leekm0310/3.4.3.3
leekm0310 Jul 20, 2022
de7a969
요구사항 4: 302 status code 적용
leekm0310 Jul 21, 2022
cb44abe
Merge pull request #4 from leekm0310/3.4.3.4
leekm0310 Jul 21, 2022
e487e54
요구사항 5: 로그인하기
leekm0310 Jul 21, 2022
dbcddab
Merge pull request #5 from leekm0310/3.4.3.5
leekm0310 Jul 21, 2022
c4c8174
요구사항 6: 쿠키 확인 메소드 구현
leekm0310 Jul 22, 2022
d4e31a9
요구사항 6: 사용자 목록 출력하기
leekm0310 Jul 23, 2022
456a2f0
Merge pull request #6 from leekm0310/3.4.3.6
leekm0310 Jul 23, 2022
8a85e62
요구사항 7: CSS 지원하기
leekm0310 Jul 25, 2022
fdb87d3
TODO: 한글 깨짐 해결 완료
leekm0310 Jul 25, 2022
6e18154
Merge pull request #7 from leekm0310/3.4.3.7
leekm0310 Jul 25, 2022
1290d6d
리팩토링 1단계: 요청 데이터를 처리하는 로직을 별도의 클래스로 분리
leekm0310 Aug 2, 2022
9327f7c
리팩토링 1단계: method가 POST일 때도 읽어오는 로직 작성
leekm0310 Aug 2, 2022
4e647f2
Fix: HttpRequestTest.request_GET에서 NPE 발생 문제 해결
leekm0310 Aug 3, 2022
16c4360
Merge pull request #8 from leekm0310/5.1.2.1
leekm0310 Aug 3, 2022
937f17e
Test: HttpResponseTest 작성
leekm0310 Aug 5, 2022
72c2506
Feat: HttpResponse 클래스 생성
leekm0310 Aug 5, 2022
d518a8f
Refactor: RequestHandler에 HttpResponse 사용하도록 리팩토링
leekm0310 Aug 6, 2022
b08aaef
Refactor: HttpResponse관련 오탈자, 누락된 로직 작성
leekm0310 Aug 9, 2022
a7c8ecc
Refactor: HttpRequest.parseRequestLine 리팩토링
leekm0310 Aug 9, 2022
f2672b6
Refactor: RequestHandler에 HttpRequest 사용하도록 리팩토링
leekm0310 Aug 9, 2022
65b8e59
Merge pull request #9 from leekm0310/5.1.2.2
leekm0310 Aug 9, 2022
3d8bd7a
fix: socket closed 에러 해결
leekm0310 Aug 17, 2022
fd5a7af
Refactor: RequestMapping 클래스 생성
leekm0310 Sep 17, 2022
47e946e
Refactor: 컨트롤러 인터페이스, 추상 클래스 작성
leekm0310 Sep 17, 2022
97df9a0
Refactor: 요청에 맞는 컨트롤러 생성
leekm0310 Sep 17, 2022
33c0dc4
Refactor: RequestHandler 리팩토링
leekm0310 Sep 17, 2022
a4f4952
Merge pull request #10 from leekm0310/5.1.2.3
leekm0310 Sep 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/main/java/webserver/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.*;
import java.net.Socket;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collection;
Expand Down Expand Up @@ -51,11 +52,16 @@ public void run() {
cookies = HttpRequestUtils.parseCookies(line);
log.debug("쿠키: {}", cookies);
}
if(line.contains("Accept: text/css")){
responseCss(out, url);
}
log.debug("header: {}", line);
}

if (url.startsWith("/user/create")){
String body = IOUtils.readData(br, contentLength);

String body = URLDecoder.decode(IOUtils.readData(br, contentLength), StandardCharsets.UTF_8);
log.debug("바디: {}", body);
Map<String, String> params = HttpRequestUtils.parseQueryString(body);
User user = new User(params.get("userId"), params.get("password"), params.get("name"),
params.get("email"));
Expand Down Expand Up @@ -88,7 +94,6 @@ public void run() {
}

} else if (url.equals("/user/list")){
//TODO: 한글 깨짐 해결하기
if (cookies.get("logined").equals("true")) {
log.debug("쿠키확인: {}" , cookies.get("logined"));
Collection<User> users = DataBase.findAll();
Expand All @@ -98,9 +103,10 @@ public void run() {
sb.append("<tbody>");
for (User u : users) {
sb.append("<tr>");
sb.append("<td>" + u.getUserId() + "<td>");
sb.append("<td>" + u.getName() + "<td>");
sb.append("<td>" + u.getEmail() + "<td>");
sb.append("<td>#</td>");
sb.append("<td>" + u.getUserId() + "</td>");
sb.append("<td>" + u.getName() + "</td>");
sb.append("<td>" + u.getEmail() + "</td>");
sb.append("</tr>");
}
sb.append("</tbody>");
Expand Down Expand Up @@ -130,6 +136,15 @@ private void response200Header(DataOutputStream dos, int lengthOfBodyContent) {
} catch (IOException e) {
log.error(e.getMessage());
}
}private void responseCss200Header(DataOutputStream dos, int lengthOfBodyContent) {
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
dos.writeBytes("Content-Type: text/css;charset=utf-8\r\n");
dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.error(e.getMessage());
}
}

private void responseLogin302Header(DataOutputStream dos, String cookie, String url) {
Expand Down Expand Up @@ -169,6 +184,13 @@ private void response(OutputStream out, String url) throws IOException {
responseBody(dos, body);
}

private void responseCss(OutputStream out, String url) throws IOException {
DataOutputStream dos = new DataOutputStream(out);
byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath());
responseCss200Header(dos, body.length);
responseBody(dos, body);
}

private int getContentLength(String line) {
String[] split = line.split(":");
return Integer.parseInt(split[1].trim());
Expand Down