Skip to content

Commit b99bb94

Browse files
committed
slipp#6 로그인 후 쿠키저장까지 완료
1 parent dd0ab72 commit b99bb94

File tree

2 files changed

+104
-11
lines changed

2 files changed

+104
-11
lines changed

src/main/java/webserver/RequestHandler.java

Lines changed: 103 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package webserver;
22

3-
import java.io.DataOutputStream;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.OutputStream;
7-
import java.net.Socket;
8-
3+
import db.DataBase;
4+
import model.User;
95
import org.slf4j.Logger;
106
import org.slf4j.LoggerFactory;
7+
import util.IOUtils;
8+
9+
import java.io.*;
10+
import java.net.Socket;
11+
import java.nio.file.Files;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static util.HttpRequestUtils.parseQueryString;
1116

1217
public class RequestHandler extends Thread {
1318
private static final Logger log = LoggerFactory.getLogger(RequestHandler.class);
@@ -24,15 +29,103 @@ public void run() {
2429

2530
try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) {
2631
// TODO 사용자 요청에 대한 처리는 이 곳에 구현하면 된다.
27-
DataOutputStream dos = new DataOutputStream(out);
28-
byte[] body = "Hello World".getBytes();
29-
response200Header(dos, body.length);
30-
responseBody(dos, body);
32+
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
33+
String line = br.readLine();
34+
35+
if (line == null) {
36+
return ;
37+
}
38+
39+
String path = getRequestPath(line);
40+
41+
Map<String, String> headerMap = new HashMap<String, String>();
42+
43+
while (!"".equals(line)) {
44+
line = br.readLine();
45+
String[] headers = line.split(": ");
46+
if (headers.length == 2) {
47+
headerMap.put(headers[0], headers[1]);
48+
}
49+
}
50+
51+
if ("/user/create".equals(path)) {
52+
log.debug("회원가입");
53+
String bodyData = IOUtils.readData(br, Integer.parseInt(headerMap.get("Content-Length")) + 1);
54+
Map<String, String> paramMap = parseQueryString(bodyData);
55+
User user = new User(paramMap.get("userId"), paramMap.get("password"), paramMap.get("name"), paramMap.get("email"));
56+
log.debug("user={}", user.toString());
57+
58+
DataBase.addUser(user);
59+
60+
DataOutputStream dos = new DataOutputStream(out);
61+
response302Header(dos);
62+
63+
} else if ("/user/login".equals(path)) {
64+
log.debug("회원로그인");
65+
String bodyData = IOUtils.readData(br, Integer.parseInt(headerMap.get("Content-Length")) + 1);
66+
Map<String, String> paramMap = parseQueryString(bodyData);
67+
String userId = paramMap.get("userId");
68+
String password = paramMap.get("password");
69+
70+
User user = DataBase.findUserById(userId);
71+
if (user == null || !password.equals(user.getPassword())) {
72+
log.debug("로그인 실패");
73+
DataOutputStream dos = new DataOutputStream(out);
74+
responseLoginFail(dos);
75+
} else {
76+
log.debug("로그인 성공");
77+
DataOutputStream dos = new DataOutputStream(out);
78+
response302LoginSuccessHeader(dos);
79+
}
80+
81+
}else {
82+
DataOutputStream dos = new DataOutputStream(out);
83+
byte[] body = Files.readAllBytes(new File("./webapp" + path).toPath());
84+
response200Header(dos, body.length);
85+
responseBody(dos, body);
86+
}
3187
} catch (IOException e) {
3288
log.error(e.getMessage());
3389
}
3490
}
3591

92+
private String getRequestPath(String line) {
93+
String[] tokens = line.split(" ");
94+
return tokens[1];
95+
}
96+
97+
private void response302LoginSuccessHeader(DataOutputStream dos) {
98+
try {
99+
dos.writeBytes("HTTP/1.1 302 Found\r\n");
100+
dos.writeBytes("Location: /index.html\n \r\n");
101+
dos.writeBytes("Set-Cookie : logined=true; Path=/ \n \r\n");
102+
dos.writeBytes("\r\n");
103+
} catch (IOException e) {
104+
log.error(e.getMessage());
105+
}
106+
}
107+
108+
private void responseLoginFail(DataOutputStream dos) {
109+
try {
110+
dos.writeBytes("HTTP/1.1 302 Found\r\n");
111+
dos.writeBytes("Location: /user/login_failed.html\n \r\n");
112+
dos.writeBytes("\r\n");
113+
} catch (IOException e) {
114+
log.error(e.getMessage());
115+
}
116+
}
117+
118+
private void response302Header(DataOutputStream dos) {
119+
try {
120+
dos.writeBytes("HTTP/1.1 302 Found \r\n");
121+
dos.writeBytes("Location: /index.html\n \r\n");
122+
dos.writeBytes("\r\n");
123+
} catch (IOException e) {
124+
log.error(e.getMessage());
125+
}
126+
}
127+
128+
36129
private void response200Header(DataOutputStream dos, int lengthOfBodyContent) {
37130
try {
38131
dos.writeBytes("HTTP/1.1 200 OK \r\n");

webapp/user/form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<div class="container" id="main">
7676
<div class="col-md-6 col-md-offset-3">
7777
<div class="panel panel-default content-main">
78-
<form name="question" method="get" action="/user/create">
78+
<form name="question" method="post" action="/user/create">
7979
<div class="form-group">
8080
<label for="userId">사용자 아이디</label>
8181
<input class="form-control" id="userId" name="userId" placeholder="User ID">

0 commit comments

Comments
 (0)