-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[톰캣 구현하기 - 3, 4단계] 히이로(문제웅) 미션 제출합니다. #455
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5248e44
refactor: httpRequest 객체 분리
MoonJeWoong e6cf263
refactor: InputStream의 보조 스트림은 request 객체 내부로 캡슐화
MoonJeWoong e9be59d
refactor: httpRequest 에서 cookies 반환 시 방어적 복사 기능 구현
MoonJeWoong 51291fa
refactor: Http11Response 객체 분리
MoonJeWoong c6cf4b2
refactor: HttpRequest 객체명 Http11Request로 수정
MoonJeWoong 6b3ed16
feat: 501 status enum 추가
MoonJeWoong e4c9fb5
feat: Servlet 인터페이스 및 AbstractServlet 클래스 구현
MoonJeWoong 23c3c39
feat: LoginServlet 기능 분리 및 구현
MoonJeWoong 384b755
feat: RegisterServlet 기능 분리 및 구현
MoonJeWoong ae3ac59
feat: 서블릿 컨테이너 Context 클래스 구현 및 DefaultServlet 구현
MoonJeWoong 4aa7b93
feat: 응답 메세지 바디 생성 메서드 공통 Util 클래스로 분리
MoonJeWoong 24ec969
style: catalina, coyote 패키지 역할 별 분리
MoonJeWoong 84bb261
test: thread 관련 학습 테스트 진행
MoonJeWoong 7f81abc
feat: tomcat connector 쓰레드 풀 설정 추가
MoonJeWoong 7d922b6
feat: 로그인 session 동시성 관리를 위한 ConcurrentHashMap 사용
MoonJeWoong 77de1e6
feat: HttpHeader enum 작성
MoonJeWoong 5016190
feat: Connector 상수 분리
MoonJeWoong 055e620
feat: Connector 쓰레드 풀 재설정
MoonJeWoong 881b66d
refactor: Servlet 메서드 순서 변경
MoonJeWoong ad63ded
refactor: AbstractServlet 상수 분리
MoonJeWoong 25a5754
refactor: LoginServlet 객체 static 선언 추가
MoonJeWoong 8c99b09
refactor: LoginServlet 객체 내 SessionManager 전역으로 관리하도록 수정
MoonJeWoong 681e7fb
refactor: context를 Tomcat 객체에서 생성 후 의존성 주입하는 방향으로 수정
MoonJeWoong af65667
refactor: startLine 객체 내 queryString을 queryParams로 수정
MoonJeWoong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
28 changes: 0 additions & 28 deletions
28
tomcat/src/main/java/nextstep/jwp/controller/LoginController.java
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
tomcat/src/main/java/nextstep/jwp/dto/LoginResponseDto.java
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
tomcat/src/main/java/nextstep/jwp/service/LoginService.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
tomcat/src/main/java/nextstep/org/apache/catalina/Context.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,36 @@ | ||
package nextstep.org.apache.catalina; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import nextstep.org.apache.catalina.servlet.DefaultServlet; | ||
import nextstep.org.apache.catalina.servlet.LoginServlet; | ||
import nextstep.org.apache.catalina.servlet.RegisterServlet; | ||
import nextstep.org.apache.catalina.servlet.Servlet; | ||
|
||
public class Context { | ||
|
||
private static final Map<String, Servlet> servletMappings; | ||
private static final String EXTENSION_DELIMITER = "."; | ||
|
||
static { | ||
servletMappings = new HashMap<>(); | ||
servletMappings.put("/login", new LoginServlet()); | ||
servletMappings.put("/register", new RegisterServlet()); | ||
servletMappings.put("default", new DefaultServlet()); | ||
} | ||
|
||
public Servlet getServlet(String pathInfo) { | ||
if (servletMappings.containsKey(removeExtension(pathInfo))) { | ||
return servletMappings.get(removeExtension(pathInfo)); | ||
} | ||
return servletMappings.get("default"); | ||
} | ||
|
||
private String removeExtension(String pathInfo) { | ||
int idx = pathInfo.indexOf(EXTENSION_DELIMITER); | ||
if (idx == -1) { | ||
return pathInfo; | ||
} | ||
return pathInfo.substring(0,idx); | ||
} | ||
} |
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
88 changes: 88 additions & 0 deletions
88
tomcat/src/main/java/nextstep/org/apache/catalina/servlet/AbstractServlet.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,88 @@ | ||
package nextstep.org.apache.catalina.servlet; | ||
|
||
import static nextstep.org.apache.coyote.http11.HttpUtil.selectFirstContentTypeOrDefault; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import nextstep.org.apache.coyote.http11.HttpHeader; | ||
import nextstep.org.apache.coyote.http11.request.Http11Request; | ||
import nextstep.org.apache.coyote.http11.response.Http11Response; | ||
import nextstep.org.apache.coyote.http11.Status; | ||
|
||
public abstract class AbstractServlet implements Servlet{ | ||
|
||
private static final String RESOURCES_PATH_PREFIX = "static"; | ||
protected static final String NOT_FOUND_DEFAULT_MESSAGE = "404 Not Found"; | ||
private static final String CHARSET_UTF_8 = ";charset=utf-8"; | ||
private static final String DEFAULT_EXTENSION = ".html"; | ||
private static final String NOT_FOUND_PAGE = "/404.html"; | ||
private static final String HTTP_GET_METHOD = "GET"; | ||
private static final String HTTP_POST_METHOD = "POST"; | ||
|
||
@Override | ||
public void service(Http11Request request, Http11Response response) throws Exception { | ||
String method = request.getMethod(); | ||
|
||
if (method.equals(HTTP_GET_METHOD)) { | ||
doGet(request, response); | ||
} else if (method.equals(HTTP_POST_METHOD)) { | ||
doPost(request, response); | ||
} else { | ||
response.setStatus(Status.NOT_IMPLEMENTED); | ||
kdkdhoho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
protected abstract void doGet(Http11Request request, Http11Response response) throws Exception; | ||
|
||
protected abstract void doPost(Http11Request request, Http11Response response) throws Exception; | ||
|
||
protected Optional<String> createResponseBody(String requestPath) throws IOException { | ||
if (requestPath.equals("/")) { | ||
return Optional.of("Hello world!"); | ||
} | ||
|
||
String resourceName = RESOURCES_PATH_PREFIX + requestPath; | ||
if (!resourceName.contains(".")) { | ||
resourceName += DEFAULT_EXTENSION; | ||
} | ||
URL resource = getClass().getClassLoader().getResource(resourceName); | ||
|
||
if (Objects.isNull(resource)) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); | ||
} | ||
|
||
protected void responseWithBody(Http11Request request, Http11Response response) throws IOException { | ||
Optional<String> responseBody = createResponseBody(request.getPathInfo()); | ||
String contentType = selectFirstContentTypeOrDefault(request.getHeader(HttpHeader.ACCEPT)); | ||
|
||
if (responseBody.isEmpty()) { | ||
responseWithNotFound(request, response); | ||
return; | ||
} | ||
|
||
response.setStatus(Status.OK) | ||
.setHeader(HttpHeader.CONTENT_TYPE, contentType + CHARSET_UTF_8) | ||
.setHeader(HttpHeader.CONTENT_LENGTH, String.valueOf( | ||
responseBody.get().getBytes(StandardCharsets.UTF_8).length)) | ||
.setBody(responseBody.get()); | ||
} | ||
|
||
private void responseWithNotFound(Http11Request request, Http11Response response) throws IOException { | ||
String notFoundPageBody = createResponseBody(NOT_FOUND_PAGE) | ||
.orElse(NOT_FOUND_DEFAULT_MESSAGE); | ||
String contentType = selectFirstContentTypeOrDefault(request.getHeader(HttpHeader.ACCEPT)); | ||
|
||
response.setStatus(Status.NOT_FOUND) | ||
.setHeader(HttpHeader.CONTENT_TYPE, contentType + CHARSET_UTF_8) | ||
.setHeader(HttpHeader.CONTENT_LENGTH, String.valueOf( | ||
notFoundPageBody.getBytes(StandardCharsets.UTF_8).length)) | ||
.setBody(notFoundPageBody); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ServletContext를 의미하는 클래스명인가요?? 톰켓에서 Servlet과 Context는 약간 다른 의미로 파악을 하고 있거든요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
catalina 패키지 내에서 servlet context 역할을 수행하는 객체 클래스 네이밍을 context라고 사용하더라구요! 그래서 해당 부분을 반영해서 작성해본 네이밍이었습니다 ㅎㅎ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
실제 톰캣 구조도 공부하신 것 같은데, catalina 패키지 내의 Context, Container, ContainerServlet, Servlet 이 네 객체의 차이에 대해 여쭤봐도 될까요?? 정말 간단하게라도 답변해주시면 감사하겠습니다 🙇♂️