-
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
[톰캣] - 1,2 단계 박스터 미션 제출합니다 #303
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a3779e7
feat: content type 구현
drunkenhw 2cf2007
feat: 파비콘 추가
drunkenhw 2d73d3f
feat: 쿠키, 세션 구현
drunkenhw 21c4ec7
feat: http 메서드 구현
drunkenhw d6b75cf
feat: http 요청 클래스 구현
drunkenhw 0d5643d
feat: http 상태코드 구현
drunkenhw 9e37afe
feat: 예외 추가
drunkenhw 63efae6
feat: 요청 객체 분리
drunkenhw 56f8ac8
feat: 응답객체 분리
drunkenhw 6717090
feat: 요청에 따른 응답 구현
drunkenhw c1c43df
test: 테스트 코드 추가
drunkenhw 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
8 changes: 8 additions & 0 deletions
8
tomcat/src/main/java/nextstep/jwp/exception/UserNotFoundException.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 nextstep.jwp.exception; | ||
|
||
public class UserNotFoundException extends RuntimeException { | ||
|
||
public UserNotFoundException() { | ||
super("사용자를 찾을 수 없습니다."); | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
tomcat/src/main/java/org/apache/coyote/http11/RequestReader.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,83 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.exception.UserNotFoundException; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.common.HttpMethod; | ||
import org.apache.coyote.http11.cookie.HttpCookie; | ||
import org.apache.coyote.http11.cookie.Session; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpStatusCode; | ||
import org.apache.coyote.http11.response.ResponseBody; | ||
import org.apache.coyote.http11.response.ResponseEntity; | ||
import org.apache.coyote.http11.response.StaticResource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Pattern; | ||
|
||
public class RequestReader { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(RequestReader.class); | ||
private static final Pattern FILE_EXTENSION_REGEX = Pattern.compile(".css|.js|.ico|.html"); | ||
private static final String INDEX_HTML = "/index.html"; | ||
private static final String LOGIN_HTML = "/login.html"; | ||
private static final String REGISTER_HTML = "/register.html"; | ||
private static final String UNAUTHORIZED_HTML = "/401.html"; | ||
|
||
public ResponseEntity parse(HttpRequest httpRequest) throws IOException { | ||
if (FILE_EXTENSION_REGEX.matcher(httpRequest.getRequestUri()).find() && httpRequest.isMatchMethod(HttpMethod.GET)) { | ||
StaticResource staticResource = StaticResource.of(httpRequest.getRequestUri()); | ||
ResponseBody responseBody = ResponseBody.of(staticResource.fileToString(), staticResource.getFileExtension()); | ||
return ResponseEntity.of(responseBody, HttpStatusCode.OK); | ||
} | ||
|
||
if (httpRequest.getRequestUri().startsWith("/login") && httpRequest.isMatchMethod(HttpMethod.GET)) { | ||
if (httpRequest.existsSession()) { | ||
Session session = httpRequest.getSession(false); | ||
User user = (User) session.getAttribute("user"); | ||
log.info("user: {}", user); | ||
return ResponseEntity.redirect(INDEX_HTML, HttpStatusCode.FOUND); | ||
} | ||
StaticResource staticResource = StaticResource.of(LOGIN_HTML); | ||
ResponseBody responseBody = ResponseBody.of(staticResource.fileToString(), staticResource.getFileExtension()); | ||
return ResponseEntity.of(responseBody, HttpStatusCode.OK); | ||
} | ||
|
||
if (httpRequest.isStartsWith("/login") && httpRequest.isMatchMethod(HttpMethod.POST)) { | ||
String account = httpRequest.getBody("account"); | ||
String password = httpRequest.getBody("password"); | ||
return redirectLogin(httpRequest, account, password); | ||
} | ||
|
||
if (httpRequest.isStartsWith("/register") && httpRequest.isMatchMethod(HttpMethod.GET)) { | ||
StaticResource staticResource = StaticResource.of(REGISTER_HTML); | ||
ResponseBody responseBody = ResponseBody.of(staticResource.fileToString(), staticResource.getFileExtension()); | ||
return ResponseEntity.of(responseBody, HttpStatusCode.OK); | ||
} | ||
|
||
if (httpRequest.isStartsWith("/register") && httpRequest.isMatchMethod(HttpMethod.POST)) { | ||
InMemoryUserRepository.save(new User(httpRequest.getBody("account"), httpRequest.getBody("password"), httpRequest.getBody("email"))); | ||
return ResponseEntity.redirect(INDEX_HTML, HttpStatusCode.FOUND); | ||
} | ||
|
||
return ResponseEntity.of(ResponseBody.of("Hello world!", "html"), HttpStatusCode.OK); | ||
} | ||
|
||
private ResponseEntity redirectLogin(HttpRequest httpRequest, String account, String password) throws IOException { | ||
User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(UserNotFoundException::new); | ||
if (!user.checkPassword(password)) { | ||
StaticResource staticResource = StaticResource.of(UNAUTHORIZED_HTML); | ||
ResponseBody responseBody = ResponseBody.of(staticResource.fileToString(), staticResource.getFileExtension()); | ||
return ResponseEntity.of(responseBody, HttpStatusCode.UNAUTHORIZED); | ||
} | ||
log.info("user: {}", user); | ||
ResponseEntity responseEntity = ResponseEntity.redirect(INDEX_HTML, HttpStatusCode.FOUND); | ||
Session session = httpRequest.getSession(true); | ||
session.setAttribute("user", user); | ||
responseEntity.addCookie(HttpCookie.jSessionId(session.getId())); | ||
return responseEntity; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tomcat/src/main/java/org/apache/coyote/http11/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,31 @@ | ||
package org.apache.coyote.http11.common; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum ContentType { | ||
HTML("html", "text/html;charset=utf-8"), | ||
CSS("css", "text/css"), | ||
JS("js", "application/js"), | ||
ICO("ico", "image/vnd.microsoft.icon"), | ||
SVG("svg", "image/svg+xml"), | ||
; | ||
|
||
private final String fileExtension; | ||
private final String name; | ||
|
||
ContentType(String fileExtension, String name) { | ||
this.fileExtension = fileExtension; | ||
this.name = name; | ||
} | ||
|
||
public static ContentType parseContentType(String fileExtension) { | ||
return Arrays.stream(values()) | ||
.filter(contentType -> contentType.fileExtension.equals(fileExtension)) | ||
.findFirst() | ||
.orElse(HTML); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tomcat/src/main/java/org/apache/coyote/http11/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,24 @@ | ||
package org.apache.coyote.http11.common; | ||
|
||
import org.apache.coyote.http11.exception.NotMatchedHttpMethodException; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum HttpMethod { | ||
GET, | ||
POST, | ||
PUT, | ||
DELETE, | ||
HEAD, | ||
OPTIONS, | ||
TRACE, | ||
CONNECT, | ||
PATCH; | ||
|
||
public static HttpMethod of(String method) { | ||
return Arrays.stream(values()) | ||
.filter(httpMethod -> httpMethod.name().equalsIgnoreCase(method)) | ||
.findFirst() | ||
.orElseThrow(NotMatchedHttpMethodException::new); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tomcat/src/main/java/org/apache/coyote/http11/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,26 @@ | ||
package org.apache.coyote.http11.common; | ||
|
||
import org.apache.coyote.http11.exception.InvalidHttpVersionException; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum HttpVersion { | ||
HTTP_1_1("HTTP/1.1"), | ||
; | ||
private final String version; | ||
|
||
HttpVersion(String version) { | ||
this.version = version; | ||
} | ||
|
||
public static HttpVersion of(String version) { | ||
return Arrays.stream(values()) | ||
.filter(httpVersion -> httpVersion.version.equalsIgnoreCase(version)) | ||
.findFirst() | ||
.orElseThrow(InvalidHttpVersionException::new); | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
} |
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.
enum 분리 잘되어있어서 읽기 편했습니다 !!!!
너무 좋습니다 !!