-
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단계] 다즐(최우창) 미션 제출합니다. (#316)
* test: 학습 테스트 작성 * feat: GET /index.html 응답하기 기능 구현 * feat: HttpException 커스텀 예외 구현 * feat: http 공통 클래스 구현 * feat: http request 클래스 구현 * feat: http response 클래스 구현 * feat: 요청 매핑을 위한 클래스 구현 * refactor: ACCEPT 헤더 처리 수정 * feat: Handler 클래스 기능 구현 * feat: Processor 클래스 기능 구현 * test: GET 요청 본문 제거 * refactor(ViewResolver): 존재하지 않는 페이지 예외 처리 * refactor: 요청 헤더 파싱 기능 수정
- Loading branch information
Showing
68 changed files
with
2,833 additions
and
38 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
64 changes: 64 additions & 0 deletions
64
tomcat/src/main/java/org/apache/coyote/common/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,64 @@ | ||
package org.apache.coyote.common; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.apache.coyote.request.Accept; | ||
|
||
public enum ContentType { | ||
|
||
APPLICATION_XML("application/xml", "xml"), | ||
APPLICATION_JAVASCRIPT("application/javascript", "js"), | ||
APPLICATION_JSON("application/json", "json"), | ||
APPLICATION_OCTET_STREAM("application/octet-stream", ""), | ||
APPLICATION_PDF("application/pdf", "pdf"), | ||
TEXT_PLAIN("text/plain", "text"), | ||
TEXT_HTML("text/html;charset=utf-8", "html"), | ||
TEXT_CSS("text/css", "css"), | ||
TEXT_JAVASCRIPT("text/javascript", "js"), | ||
TEXT_XML("text/xml", "xml"), | ||
IMAGE_JPEG("image/jpeg", "jpeg"), | ||
IMAGE_PNG("image/png", "png"), | ||
IMAGE_GIF("image/gif", "gif"), | ||
IMAGE_SVG("image/svg+xml", "svg"), | ||
AUDIO_MPEG("audio/mpeg", "mpeg"), | ||
AUDIO_OGG("audio/ogg", "ogg"), | ||
VIDEO_MP4("video/mp4", "mp4"), | ||
MULTIPART_FOR_DATA("multipart/form-data", ""); | ||
|
||
private final String type; | ||
private final String extension; | ||
|
||
ContentType(final String type, final String extension) { | ||
this.type = type; | ||
this.extension = extension; | ||
} | ||
|
||
public static String getTypeFrom(final List<Accept> accepts) { | ||
return accepts.stream() | ||
.map(ContentType::from) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.map(ContentType::getType) | ||
.orElse(null); | ||
} | ||
|
||
private static ContentType from(final Accept accept) { | ||
return Arrays.stream(ContentType.values()) | ||
.filter(contentType -> contentType.type.contains(accept.getAcceptType())) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public static String getTypeFrom(final String extension) { | ||
return Arrays.stream(ContentType.values()) | ||
.filter(contentType -> Objects.equals(contentType.extension, extension)) | ||
.findAny() | ||
.map(ContentType::getType) | ||
.orElse(null); | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tomcat/src/main/java/org/apache/coyote/common/HttpMethod.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,25 @@ | ||
package org.apache.coyote.common; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import org.apache.coyote.exception.http.HttpMethodNotMatchException; | ||
|
||
public enum HttpMethod { | ||
|
||
GET, | ||
POST, | ||
PUT, | ||
DELETE, | ||
HEAD, | ||
OPTIONS, | ||
PATCH, | ||
CONNECT, | ||
TRACE; | ||
|
||
public static HttpMethod from(final String method) { | ||
return Arrays.stream(HttpMethod.values()) | ||
.filter(httpMethod -> Objects.equals(httpMethod.name(), method)) | ||
.findFirst() | ||
.orElseThrow(HttpMethodNotMatchException::new); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tomcat/src/main/java/org/apache/coyote/common/HttpStatus.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,40 @@ | ||
package org.apache.coyote.common; | ||
|
||
public enum HttpStatus { | ||
|
||
CONTINUE(100, "Continue"), | ||
SWITCHING_PROTOCOLS(101, "Switching Protocols"), | ||
OK(200, "OK"), | ||
CREATED(201, "Created"), | ||
ACCEPTED(202, "Accepted"), | ||
NO_CONTENT(204, "No Content"), | ||
MULTIPLE_CHOICES(300, "Multiple Choices"), | ||
MOVED_PERMANENTLY(301, "Moved Permanently"), | ||
FOUND(302, "Found"), | ||
NOT_MODIFIED(304, "Not Modified"), | ||
BAD_REQUEST(400, "Bad Request"), | ||
UNAUTHORIZED(401, "Unauthorized"), | ||
FORBIDDEN(403, "Forbidden"), | ||
NOT_FOUND(404, "Not Found"), | ||
METHOD_NOT_ALLOWED(405, "Method Not Allowed"), | ||
INTERNAL_SERVER_ERROR(500, "Internal Server Error"), | ||
NOT_IMPLEMENTED(501, "Not Implemented"), | ||
BAD_GATEWAY(502, "Bad Gateway"), | ||
SERVICE_UNAVAILABLE(503, "Service Unavailable"); | ||
|
||
private final int code; | ||
private final String message; | ||
|
||
HttpStatus(final int code, final String message) { | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tomcat/src/main/java/org/apache/coyote/common/HttpVersion.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,31 @@ | ||
package org.apache.coyote.common; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import org.apache.coyote.exception.http.HttpVersionNotMatchException; | ||
|
||
public enum HttpVersion { | ||
|
||
HTTP_09("HTTP/0.9"), | ||
HTTP_10("HTTP/1.0"), | ||
HTTP_11("HTTP/1.1"), | ||
HTTP_20("HTTP/2"), | ||
HTTP_30("HTTP/3"); | ||
|
||
private final String version; | ||
|
||
HttpVersion(final String version) { | ||
this.version = version; | ||
} | ||
|
||
public static HttpVersion from(final String version) { | ||
return Arrays.stream(HttpVersion.values()) | ||
.filter(httpVersion -> Objects.equals(httpVersion.version, version)) | ||
.findFirst() | ||
.orElseThrow(HttpVersionNotMatchException::new); | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tomcat/src/main/java/org/apache/coyote/common/Session.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,35 @@ | ||
package org.apache.coyote.common; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Session { | ||
|
||
private final String id; | ||
private final Map<String, Object> values = new HashMap<>(); | ||
|
||
public Session(final String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public Object getAttribute(final String name) { | ||
return values.get(name); | ||
} | ||
|
||
public void setAttribute(final String name, final Object value) { | ||
values.put(name, value); | ||
} | ||
|
||
public void removeAttribute(final String name) { | ||
values.remove(name); | ||
} | ||
|
||
public void invalidate() { | ||
values.clear(); | ||
} | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
tomcat/src/main/java/org/apache/coyote/common/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,24 @@ | ||
package org.apache.coyote.common; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SessionManager { | ||
|
||
private static final Map<String, Session> SESSIONS = new HashMap<>(); | ||
|
||
private SessionManager() { | ||
} | ||
|
||
public static void add(final Session session) { | ||
SESSIONS.put(session.getId(), session); | ||
} | ||
|
||
public static Session findSession(final String id) { | ||
return SESSIONS.get(id); | ||
} | ||
|
||
public static void remove(final String id) { | ||
SESSIONS.remove(id); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tomcat/src/main/java/org/apache/coyote/exception/http/HttpException.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,8 @@ | ||
package org.apache.coyote.exception.http; | ||
|
||
public class HttpException extends RuntimeException { | ||
|
||
public HttpException(final String message) { | ||
super(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tomcat/src/main/java/org/apache/coyote/exception/http/HttpMethodNotMatchException.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,8 @@ | ||
package org.apache.coyote.exception.http; | ||
|
||
public class HttpMethodNotMatchException extends HttpException { | ||
|
||
public HttpMethodNotMatchException() { | ||
super("Http Method Not Matched"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tomcat/src/main/java/org/apache/coyote/exception/http/HttpVersionNotMatchException.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,8 @@ | ||
package org.apache.coyote.exception.http; | ||
|
||
public class HttpVersionNotMatchException extends HttpException { | ||
|
||
public HttpVersionNotMatchException() { | ||
super("Http Version Not Matched"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tomcat/src/main/java/org/apache/coyote/exception/http/InvalidHeaderException.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,8 @@ | ||
package org.apache.coyote.exception.http; | ||
|
||
public class InvalidHeaderException extends HttpException { | ||
|
||
public InvalidHeaderException(final String message) { | ||
super(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tomcat/src/main/java/org/apache/coyote/exception/http/InvalidRequestPathException.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,8 @@ | ||
package org.apache.coyote.exception.http; | ||
|
||
public class InvalidRequestPathException extends HttpException { | ||
|
||
public InvalidRequestPathException(final String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.