-
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.
[톰캣 구현하기 1,2단계] 호이(이건호) 미션 제출합니다. (#313)
- Loading branch information
Showing
25 changed files
with
972 additions
and
51 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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.apache.catalina; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class Session { | ||
|
||
private final String id; | ||
private final Map<String, Object> values = new HashMap<>(); | ||
|
||
public Session() { | ||
this(UUID.randomUUID().toString()); | ||
} | ||
|
||
public Session(final String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public Object getAttribute(final String name) { | ||
return values.getOrDefault(name, null); | ||
} | ||
|
||
public void setAttribute(final String name, final Object value) { | ||
values.put(name, value); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tomcat/src/main/java/org/apache/catalina/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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.apache.catalina; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SessionManager { | ||
|
||
private SessionManager() { | ||
} | ||
|
||
private static final Map<String, Session> SESSIONS = new HashMap<>(); | ||
|
||
public static void add(final Session session) { | ||
SESSIONS.put(session.getId(), session); | ||
} | ||
|
||
public static Session findSession(final String id) { | ||
return SESSIONS.getOrDefault(id, null); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tomcat/src/main/java/org/apache/coyote/http11/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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.apache.coyote.http11; | ||
|
||
public enum ContentType { | ||
|
||
TEXT_HTML("text/html"), | ||
TEXT_CSS("text/css"), | ||
APPLICATION_JS("application/js"), | ||
WILD_CARD("*/*"); | ||
|
||
private static final String CSS = "css"; | ||
private static final String HTML = "html"; | ||
private static final String JS = "js"; | ||
private static final String ENCODING = ";charset=utf-8"; | ||
|
||
private String value; | ||
|
||
ContentType(final String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value + ENCODING; | ||
} | ||
|
||
public static ContentType from(String extension) { | ||
if (extension.equalsIgnoreCase(CSS)) { | ||
return TEXT_CSS; | ||
} | ||
if (extension.equalsIgnoreCase(HTML)) { | ||
return TEXT_HTML; | ||
} | ||
if (extension.equalsIgnoreCase(JS)) { | ||
return APPLICATION_JS; | ||
} | ||
return WILD_CARD; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tomcat/src/main/java/org/apache/coyote/http11/Controller.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
public interface Controller { | ||
|
||
HttpResponse handle(HttpRequest request); | ||
} |
37 changes: 37 additions & 0 deletions
37
tomcat/src/main/java/org/apache/coyote/http11/ControllerAdapter.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.apache.coyote.http11.controller.ErrorController; | ||
import org.apache.coyote.http11.controller.LoginController; | ||
import org.apache.coyote.http11.controller.RegisterController; | ||
import org.apache.coyote.http11.controller.StaticController; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
|
||
public class ControllerAdapter { | ||
|
||
private static final Map<String, Controller> map = new ConcurrentHashMap<>(); | ||
private static final String STATIC_CONTROLLER = "staticController"; | ||
private static final String ERROR_CONTROLLER = "errorController"; | ||
|
||
public ControllerAdapter() { | ||
init(); | ||
} | ||
|
||
private void init() { | ||
map.put(STATIC_CONTROLLER, new StaticController()); | ||
map.put(ERROR_CONTROLLER, new ErrorController()); | ||
map.put("/login", new LoginController()); | ||
map.put("/register", new RegisterController()); | ||
} | ||
|
||
public Controller findController(HttpRequest httpRequest) { | ||
if (httpRequest.isStaticRequest()) { | ||
return map.get(STATIC_CONTROLLER); | ||
} | ||
if (map.containsKey(httpRequest.getUri())) { | ||
return map.get(httpRequest.getUri()); | ||
} | ||
return map.get(ERROR_CONTROLLER); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
tomcat/src/main/java/org/apache/coyote/http11/Headers.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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import org.apache.coyote.http11.request.HttpCookie; | ||
|
||
public class Headers { | ||
|
||
private static final String HEADER_DELIMITER = ":"; | ||
private Map<String, String> values; | ||
private HttpCookie cookie; | ||
|
||
private Headers(Map<String, String> values, HttpCookie cookie) { | ||
this.values = values; | ||
this.cookie = cookie; | ||
} | ||
|
||
public static Headers from(final BufferedReader bufferedReader) throws IOException { | ||
String line = ""; | ||
final HashMap<String, String> map = new HashMap<>(); | ||
HttpCookie httpCookie = null; | ||
|
||
while (!(line = bufferedReader.readLine()).isBlank()) { | ||
final String[] headerInfo = line.split(HEADER_DELIMITER); | ||
final String headerName = headerInfo[0]; | ||
final String value = headerInfo[1].trim(); | ||
|
||
if (headerName.equals("Cookie")) { | ||
httpCookie = new HttpCookie(value); | ||
} | ||
map.put(headerName, value); | ||
} | ||
|
||
return new Headers(map, httpCookie); | ||
} | ||
|
||
public boolean containsHeader(final String header) { | ||
return values.containsKey(header); | ||
} | ||
|
||
public String get(final String header) { | ||
return values.get(header); | ||
} | ||
|
||
public boolean hasCookie() { | ||
return Objects.nonNull(cookie); | ||
} | ||
|
||
public HttpCookie getCookie() { | ||
return cookie; | ||
} | ||
} |
Oops, something went wrong.