1
1
package webserver ;
2
2
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 ;
9
5
import org .slf4j .Logger ;
10
6
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 ;
11
16
12
17
public class RequestHandler extends Thread {
13
18
private static final Logger log = LoggerFactory .getLogger (RequestHandler .class );
@@ -24,15 +29,103 @@ public void run() {
24
29
25
30
try (InputStream in = connection .getInputStream (); OutputStream out = connection .getOutputStream ()) {
26
31
// 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
+ }
31
87
} catch (IOException e ) {
32
88
log .error (e .getMessage ());
33
89
}
34
90
}
35
91
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
+
36
129
private void response200Header (DataOutputStream dos , int lengthOfBodyContent ) {
37
130
try {
38
131
dos .writeBytes ("HTTP/1.1 200 OK \r \n " );
0 commit comments