-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'eunbii0213' into step3
- Loading branch information
Showing
7 changed files
with
55 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +0,0 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import org.apache.coyote.http11.cookie.HttpCookie; | ||
|
||
public class Response { | ||
|
||
public static String of(final String contentType, final String responseBody) { | ||
return String.join("\r\n", | ||
"HTTP/1.1 200 OK ", | ||
"Content-Type: " + contentType + "charset=utf-8 ", | ||
"Content-Length: " + responseBody.getBytes().length + " ", | ||
"", | ||
responseBody); | ||
} | ||
|
||
public static String of(final String contentType, final String responseBody, final String status) { | ||
return String.join("\r\n", | ||
"HTTP/1.1 " + status + " ", | ||
"Content-Type: " + contentType + "charset=utf-8 ", | ||
"Content-Length: " + responseBody.getBytes().length + " ", | ||
"", | ||
responseBody); | ||
} | ||
|
||
public static String of(final String contentType, final String responseBody, final HttpCookie httpCookie) { | ||
return String.join("\r\n", | ||
"HTTP/1.1 200 OK ", | ||
"Set-Cookie: " + "JSESSIONID=" + httpCookie.getJSessionId() + " ", | ||
"Content-Type: " + contentType + "charset=utf-8 ", | ||
"Content-Length: " + responseBody.getBytes().length + " ", | ||
"", | ||
responseBody); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +0,0 @@ | ||
package org.apache.coyote.http11.cookie; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class HttpCookie { | ||
private final Map<String, String> cookies = new ConcurrentHashMap<>(); | ||
|
||
public HttpCookie() { | ||
} | ||
|
||
// JSESSIONID 값 설정 | ||
public void addJSessionId(String sessionId) { | ||
cookies.put("JSESSIONID", sessionId); | ||
} | ||
|
||
// JSESSIONID 값 가져오기 | ||
public String getJSessionId() { | ||
return cookies.get("JSESSIONID"); | ||
} | ||
|
||
// 서버 응답 헤더에 Set-Cookie 추가 | ||
public String addCookieInResponseHeader() { | ||
StringBuilder header = new StringBuilder(); | ||
header.append("HTTP/1.1 200 OK\r\n"); | ||
header.append("Content-Length: 5571\r\n"); | ||
header.append("Content-Type: text/html;charset=utf-8\r\n"); | ||
header.append("Set-Cookie: JSESSIONID=").append(getJSessionId()).append("\r\n"); | ||
return header.toString(); | ||
} | ||
} | ||
29 changes: 8 additions & 21 deletions
29
tomcat/src/main/java/org/apache/coyote/http11/cookie/SessionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,19 @@ | ||
package org.apache.coyote.http11.cookie; | ||
package org.apache.coyote.http; | ||
|
||
import nextstep.jwp.model.User; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class SessionManager { | ||
|
||
private static final Map<User, String> sessions = new HashMap<>(); | ||
private static final Map<String, User> SESSIONS = new ConcurrentHashMap<>(); | ||
|
||
public static void add(final User user, String jsessionId) { | ||
sessions.put(user, jsessionId); | ||
public static void add(String sessionId, User user) { | ||
SESSIONS.put(sessionId, user); | ||
} | ||
|
||
public static boolean validUser(final User user) { | ||
if(sessions.containsKey(user)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static boolean validJsession(final String jsession) { | ||
if(sessions.containsValue(jsession)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
public static boolean isAlreadyLogin(String sessionId) { | ||
return SESSIONS.containsKey(sessionId); | ||
} | ||
} | ||
} |
46 changes: 24 additions & 22 deletions
46
tomcat/src/main/java/org/apache/coyote/http11/enums/ContentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
package org.apache.coyote.http11.enums; | ||
package org.apache.coyote.http; | ||
|
||
public enum ContentType { | ||
import java.util.Arrays; | ||
|
||
JS(".js","Application/javascript;"), | ||
CSS(".css","text/css;"), | ||
HTML(".html","text/html;"); | ||
public enum ContentType { | ||
CSS(".css", "text/css"), | ||
ICO(".ico", "text/css"), | ||
JS(".js", "text/javascript"), | ||
SVG(".svg", "image/svg+xml"), | ||
HTML(".html", "text/html; charset=utf-8"); | ||
|
||
private final String fileType; | ||
private final String path; | ||
private final String extension; | ||
private final String type; | ||
|
||
ContentType(final String fileType, final String path) { | ||
this.fileType = fileType; | ||
this.path = path; | ||
ContentType(String extension, String type) { | ||
this.extension = extension; | ||
this.type = type; | ||
} | ||
|
||
public static String getContentType(final String path) { | ||
if (path.endsWith(JS.getFileType())) { | ||
return JS.getPath(); | ||
} | ||
if (path.endsWith(CSS.getFileType())) { | ||
return CSS.getPath(); | ||
} | ||
return HTML.getPath(); | ||
public static String findType(String path) { | ||
return Arrays.stream(values()) | ||
.filter(contentType -> path.endsWith(contentType.extension)) | ||
.map(contentType -> contentType.type) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException("지원하지 않는 Content Type 입니다.")); | ||
} | ||
|
||
public String getFileType() { | ||
return fileType; | ||
public static boolean isStaticFile(String type) { | ||
return Arrays.stream(values()) | ||
.anyMatch(contentType -> contentType.getType().equals(type)); | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
public String getType() { | ||
return type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +0,0 @@ | ||
package org.apache.coyote.http11.enums; | ||
|
||
public enum Path { | ||
|
||
INDEX_URL("static/index.html"), | ||
LOGIN_URL("/login"), | ||
LOGIN_WITH_PARAM_URL("/login?"), | ||
REGISTER_URL("/register"), | ||
|
||
LOGIN_HTML("static/login.html"), | ||
UNAUTHORIZED_HTML("static/401.html"), | ||
REGISTER_HTML("static/register.html"), | ||
|
||
STATIC("static"); | ||
|
||
private final String value; | ||
|
||
Path(final String value) { | ||
this.value = value; | ||
} | ||
|
||
public static boolean isContainParam(String path) { | ||
return path.contains("?"); | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} | ||
Oops, something went wrong.