Skip to content

Commit

Permalink
Merge branch 'eunbii0213' into step3
Browse files Browse the repository at this point in the history
  • Loading branch information
eunbii0213 authored Sep 12, 2023
2 parents a89311c + cb889b7 commit c54a99d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 361 deletions.
18 changes: 1 addition & 17 deletions study/src/main/java/cache/com/example/GreetingController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package cache.com.example;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

@Controller
public class GreetingController {
Expand All @@ -22,16 +16,6 @@ public String index() {
return "index";
}

@GetMapping("/index.html")
public String indexHtml() {
return "index";
}

@GetMapping("/login")
public String login() {
return "login";
}

/**
* 인터셉터를 쓰지 않고 response에 직접 헤더값을 지정할 수도 있다.
*/
Expand Down
34 changes: 0 additions & 34 deletions tomcat/src/main/java/org/apache/coyote/http11/Response.java
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);
}
}
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();
}
}
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);
}
}
}
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;
}
}
29 changes: 0 additions & 29 deletions tomcat/src/main/java/org/apache/coyote/http11/enums/Path.java
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;
}
}
Loading

0 comments on commit c54a99d

Please sign in to comment.