44import model .User ;
55import org .slf4j .Logger ;
66import org .slf4j .LoggerFactory ;
7+ import util .HttpRequestUtils ;
78import util .IOUtils ;
89
910import java .io .*;
@@ -58,7 +59,7 @@ public void run() {
5859 DataBase .addUser (user );
5960
6061 DataOutputStream dos = new DataOutputStream (out );
61- response302Header (dos );
62+ sendRedirectUrl (dos , "/index.html" );
6263
6364 } else if ("/user/login" .equals (path )) {
6465 log .debug ("회원로그인" );
@@ -71,17 +72,37 @@ public void run() {
7172 if (user == null || !password .equals (user .getPassword ())) {
7273 log .debug ("로그인 실패" );
7374 DataOutputStream dos = new DataOutputStream (out );
74- responseLoginFail (dos );
75+ dos .writeBytes ("Set-Cookie : logined=false; Path=/ \n \r \n " );
76+ sendRedirectUrl (dos , "/index.html" );
7577 } else {
7678 log .debug ("로그인 성공" );
7779 DataOutputStream dos = new DataOutputStream (out );
78- response302LoginSuccessHeader (dos );
80+ dos .writeBytes ("Set-Cookie : logined=true; Path=/ \n \r \n " );
81+ sendRedirectUrl (dos , "/index.html" );
7982 }
8083
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 {
8297 DataOutputStream dos = new DataOutputStream (out );
8398 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+
85106 responseBody (dos , body );
86107 }
87108 } catch (IOException e ) {
@@ -94,31 +115,20 @@ private String getRequestPath(String line) {
94115 return tokens [1 ];
95116 }
96117
97- private void response302LoginSuccessHeader (DataOutputStream dos ) {
118+ private void sendRedirectUrl (DataOutputStream dos , String url ) {
98119 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 " );
112122 dos .writeBytes ("\r \n " );
113123 } catch (IOException e ) {
114124 log .error (e .getMessage ());
115125 }
116126 }
117127
118- private void response302Header (DataOutputStream dos ) {
128+ private void responseCSSHeader (DataOutputStream dos , int lengthOfBodyContent ) {
119129 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 " );
122132 dos .writeBytes ("\r \n " );
123133 } catch (IOException e ) {
124134 log .error (e .getMessage ());
0 commit comments