4
4
import model .User ;
5
5
import org .slf4j .Logger ;
6
6
import org .slf4j .LoggerFactory ;
7
+ import util .HttpRequestUtils ;
7
8
import util .IOUtils ;
8
9
9
10
import java .io .*;
@@ -58,7 +59,7 @@ public void run() {
58
59
DataBase .addUser (user );
59
60
60
61
DataOutputStream dos = new DataOutputStream (out );
61
- response302Header (dos );
62
+ sendRedirectUrl (dos , "/index.html" );
62
63
63
64
} else if ("/user/login" .equals (path )) {
64
65
log .debug ("회원로그인" );
@@ -71,17 +72,37 @@ public void run() {
71
72
if (user == null || !password .equals (user .getPassword ())) {
72
73
log .debug ("로그인 실패" );
73
74
DataOutputStream dos = new DataOutputStream (out );
74
- responseLoginFail (dos );
75
+ dos .writeBytes ("Set-Cookie : logined=false; Path=/ \n \r \n " );
76
+ sendRedirectUrl (dos , "/index.html" );
75
77
} else {
76
78
log .debug ("로그인 성공" );
77
79
DataOutputStream dos = new DataOutputStream (out );
78
- response302LoginSuccessHeader (dos );
80
+ dos .writeBytes ("Set-Cookie : logined=true; Path=/ \n \r \n " );
81
+ sendRedirectUrl (dos , "/index.html" );
79
82
}
80
83
81
- }else {
84
+ } else if ("/user/list" .equals (path )) {
85
+
86
+ Map <String , String > cookieMaps = HttpRequestUtils .parseCookies (headerMap .get ("Cookie" ));
87
+ String logined = cookieMaps .get ("logined" );
88
+ if ("true" .equals (logined )) {
89
+ StringBuilder builder = new StringBuilder ();
90
+ builder .append ("sssss" );
91
+ } else {
92
+ DataOutputStream dos = new DataOutputStream (out );
93
+ sendRedirectUrl (dos , "/user/login.html" );
94
+ }
95
+
96
+ } else {
82
97
DataOutputStream dos = new DataOutputStream (out );
83
98
byte [] body = Files .readAllBytes (new File ("./webapp" + path ).toPath ());
84
- response200Header (dos , body .length );
99
+
100
+ if (path .contains (".css" )) {
101
+ responseCSSHeader (dos , body .length );
102
+ } else {
103
+ response200Header (dos , body .length );
104
+ }
105
+
85
106
responseBody (dos , body );
86
107
}
87
108
} catch (IOException e ) {
@@ -94,31 +115,20 @@ private String getRequestPath(String line) {
94
115
return tokens [1 ];
95
116
}
96
117
97
- private void response302LoginSuccessHeader (DataOutputStream dos ) {
118
+ private void sendRedirectUrl (DataOutputStream dos , String url ) {
98
119
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 " );
120
+ dos .writeBytes ("HTTP/1.1 302 Found \r \n " );
121
+ dos .writeBytes ("Location: " +url +"\n \r \n " );
112
122
dos .writeBytes ("\r \n " );
113
123
} catch (IOException e ) {
114
124
log .error (e .getMessage ());
115
125
}
116
126
}
117
127
118
- private void response302Header (DataOutputStream dos ) {
128
+ private void responseCSSHeader (DataOutputStream dos , int lengthOfBodyContent ) {
119
129
try {
120
- dos .writeBytes ("HTTP/1.1 302 Found \r \n " );
121
- dos .writeBytes ("Location: /index.html \n \r \n " );
130
+ dos .writeBytes ("HTTP/1.1 200 OK \r \n " );
131
+ dos .writeBytes ("Content-Type: text/css;charset=utf-8 \r \n " );
122
132
dos .writeBytes ("\r \n " );
123
133
} catch (IOException e ) {
124
134
log .error (e .getMessage ());
0 commit comments