-
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단계] 에단(김석호) 미션 제출합니다. #372
Changes from 15 commits
92cba7e
d115a52
2a7c45c
4507b9d
cdd1155
63eb5f4
159001a
0a2fbfd
c1a1e25
59d8f91
8e5ff25
45180a3
7720677
e3be3bc
3f4bb9e
96e89a9
0e56b55
df69091
ded667a
c839e59
b406c5e
d0380db
cff2f3b
cf9b8bc
fae548b
3b0c080
01ef6dd
59b8ca5
b332054
aa5d8ec
8bf5593
5f5c2cb
8db321f
feb7a82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package org.apache.coyote.handler.mapping; | ||
|
||
import org.apache.coyote.http.HttpHeaders; | ||
import org.apache.coyote.http.HttpMethod; | ||
import org.apache.coyote.http.request.HttpRequest; | ||
import org.apache.coyote.http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public interface HandlerMapping { | ||
|
||
boolean supports(final HttpMethod httpMethod, final String requestUri); | ||
boolean supports(final HttpRequest httpRequest); | ||
|
||
String handle(final String requestUri, final HttpHeaders httpHeaders, final String requestBody) throws IOException; | ||
HttpResponse handle(final HttpRequest httpRequest) throws IOException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.apache.coyote.handler.mapping; | ||
|
||
import org.apache.coyote.http.common.HttpBody; | ||
import org.apache.coyote.http.common.HttpHeaders; | ||
import org.apache.coyote.http.request.HttpRequest; | ||
import org.apache.coyote.http.response.ContentType; | ||
import org.apache.coyote.http.response.HttpResponse; | ||
import org.apache.coyote.http.response.StatusCode; | ||
import org.apache.coyote.http.response.StatusLine; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.apache.coyote.http.common.HttpHeader.CONTENT_TYPE; | ||
|
||
public class HomePageMapping implements HandlerMapping { | ||
|
||
@Override | ||
public boolean supports(final HttpRequest httpRequest) { | ||
return httpRequest.isGetRequest() && "/".equals(httpRequest.getRequestUri().getRequestUri()); | ||
} | ||
|
||
@Override | ||
public HttpResponse handle(final HttpRequest httpRequest) throws IOException { | ||
return HttpResponse.builder() | ||
.statusLine(StatusLine.from(StatusCode.OK)) | ||
.httpHeaders(new HttpHeaders(Map.of(CONTENT_TYPE, ContentType.HTML.getValue()))) | ||
.body(new HttpBody("Hello world!")) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,34 +2,33 @@ | |
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http.HttpHeaders; | ||
import org.apache.coyote.http.HttpMethod; | ||
import org.apache.coyote.http.common.HttpBody; | ||
import org.apache.coyote.http.common.HttpHeader; | ||
import org.apache.coyote.http.common.HttpHeaders; | ||
import org.apache.coyote.http.request.HttpRequest; | ||
import org.apache.coyote.http.response.HttpResponse; | ||
import org.apache.coyote.http.response.StatusCode; | ||
import org.apache.coyote.http.response.StatusLine; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.apache.coyote.http.HttpMethod.POST; | ||
|
||
public class LoginMapping extends LoginFilter implements HandlerMapping { | ||
|
||
public static final String TARGET_URI = "login"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 접근제어자 private으로 해주면 더 좋을 것 같아요. |
||
private static final Logger log = LoggerFactory.getLogger(LoginMapping.class); | ||
|
||
@Override | ||
public boolean supports(final HttpMethod httpMethod, final String requestUri) { | ||
return POST == httpMethod && | ||
requestUri.contains("login"); | ||
public boolean supports(final HttpRequest httpRequest) { | ||
return httpRequest.isPostRequest() && httpRequest.containsRequestUri(TARGET_URI); | ||
} | ||
|
||
@Override | ||
public String handle(final String requestUri, final HttpHeaders httpHeaders, final String requestBody) { | ||
final Map<String, String> bodyParams = Arrays.stream(requestBody.split("&")) | ||
.map(param -> param.split("=")) | ||
.collect(Collectors.toMap(param -> param[0], param -> param[1])); | ||
|
||
public HttpResponse handle(final HttpRequest httpRequest) throws IOException { | ||
final Map<String, String> bodyParams = httpRequest.getParsedBody(); | ||
final String account = bodyParams.get("account"); | ||
final String password = bodyParams.get("password"); | ||
|
||
|
@@ -44,17 +43,18 @@ public String handle(final String requestUri, final HttpHeaders httpHeaders, fin | |
log.info("로그인 성공! user = {}", user); | ||
} catch (final IllegalArgumentException e) { | ||
log.warn("login error = {}", e); | ||
return String.join("\r\n", | ||
"HTTP/1.1 302 Found ", | ||
"Location: /401.html "); | ||
return HttpResponse.redirect("/401.html"); | ||
} | ||
|
||
final UUID uuid = UUID.randomUUID(); | ||
setSession(uuid.toString(), Map.of("account", user.getAccount())); | ||
|
||
return String.join("\r\n", | ||
"HTTP/1.1 302 Found ", | ||
"Location: /index.html ", | ||
"Set-Cookie: JSESSIONID=" + uuid + " "); | ||
return HttpResponse.builder() | ||
.statusLine(StatusLine.from(StatusCode.FOUND)) | ||
.httpHeaders(new HttpHeaders( | ||
Map.of(HttpHeader.LOCATION, "/index.html", | ||
HttpHeader.SET_COOKIE, "JSESSIONID=" + uuid))) | ||
.body(HttpBody.empty()) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,46 +2,46 @@ | |
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http.HttpCookie; | ||
import org.apache.coyote.http.HttpHeaders; | ||
import org.apache.coyote.http.HttpMethod; | ||
import org.apache.coyote.http.common.HttpBody; | ||
import org.apache.coyote.http.common.HttpHeaders; | ||
import org.apache.coyote.http.request.HttpCookie; | ||
import org.apache.coyote.http.request.HttpRequest; | ||
import org.apache.coyote.http.response.ContentType; | ||
import org.apache.coyote.http.response.HttpResponse; | ||
import org.apache.coyote.http.response.StatusCode; | ||
import org.apache.coyote.http.response.StatusLine; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.apache.coyote.http.HttpMethod.GET; | ||
import static org.apache.coyote.http.common.HttpHeader.CONTENT_TYPE; | ||
import static org.apache.coyote.http.common.HttpHeader.COOKIE; | ||
|
||
public class LoginPageMapping extends LoginFilter implements HandlerMapping { | ||
|
||
public static final String TARGET_URI = "login"; | ||
private static final Logger log = LoggerFactory.getLogger(LoginPageMapping.class); | ||
|
||
@Override | ||
public boolean supports(final HttpMethod httpMethod, final String requestUri) { | ||
return GET == httpMethod && | ||
requestUri.contains("login"); | ||
public boolean supports(final HttpRequest httpRequest) { | ||
return httpRequest.isGetRequest() && httpRequest.containsRequestUri(TARGET_URI); | ||
} | ||
|
||
@Override | ||
public String handle(final String requestUri, final HttpHeaders httpHeaders, final String requestBody) throws IOException { | ||
if (httpHeaders.containsKey("Cookie")) { | ||
final HttpCookie cookies = HttpCookie.from(httpHeaders.get("Cookie")); | ||
public HttpResponse handle(final HttpRequest httpRequest) throws IOException { | ||
if (httpRequest.containsHeader(COOKIE)) { | ||
final HttpCookie cookies = HttpCookie.from(httpRequest.getHeader(COOKIE)); | ||
if (isAlreadyLogined(cookies.get("JSESSIONID"))) { | ||
return String.join("\r\n", | ||
"HTTP/1.1 302 Found ", | ||
"Location: /index.html "); | ||
return HttpResponse.redirect("/index.html"); | ||
} | ||
} | ||
|
||
final String[] parsedRequestUri = requestUri.split("\\?"); | ||
if (requestUri.contains("?")) { | ||
final String[] parsedRequestUri = httpRequest.getRequestUri().getRequestUri().split("\\?"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parsedRequestUri가 아래 if문 안에서만 사용되는 거 같아서 If문 안에 들어가도 될 것 같아요! |
||
if (httpRequest.getRequestUri().getRequestUri().contains("?")) { | ||
final Map<String, String> queryStrings = Arrays.stream(parsedRequestUri[1].split("&")) | ||
.map(param -> param.split("=")) | ||
.collect(Collectors.toMap(param -> param[0], param -> param[1])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분도 HttpRequest안으로 분리해보는 건 어떨까요? |
||
|
@@ -59,26 +59,17 @@ public String handle(final String requestUri, final HttpHeaders httpHeaders, fin | |
log.info("로그인 성공! user = {}", user); | ||
} catch (final IllegalArgumentException e) { | ||
log.warn("login error = {}", e); | ||
return String.join("\r\n", | ||
"HTTP/1.1 302 Found ", | ||
"Location: /401.html "); | ||
return HttpResponse.redirect("/401.html"); | ||
} | ||
|
||
return String.join("\r\n", | ||
"HTTP/1.1 302 Found ", | ||
"Location: /index.html "); | ||
return HttpResponse.redirect("/index.html"); | ||
} | ||
|
||
final String filePath = "static/login.html"; | ||
final URL fileUrl = getClass().getClassLoader().getResource(filePath); | ||
final Path path = new File(fileUrl.getPath()).toPath(); | ||
final String responseBody = new String(Files.readAllBytes(path)); | ||
|
||
return String.join("\r\n", | ||
"HTTP/1.1 200 OK ", | ||
"Content-Type: text/html;charset=utf-8 ", | ||
"Content-Length: " + responseBody.getBytes().length + " ", | ||
"", | ||
responseBody); | ||
return HttpResponse.builder() | ||
.statusLine(StatusLine.from(StatusCode.OK)) | ||
.httpHeaders(new HttpHeaders(Map.of(CONTENT_TYPE, ContentType.HTML.getValue()))) | ||
.body(HttpBody.file("static/login.html")) | ||
.build(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,28 +2,24 @@ | |
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http.HttpHeaders; | ||
import org.apache.coyote.http.HttpMethod; | ||
import org.apache.coyote.http.request.HttpRequest; | ||
import org.apache.coyote.http.response.HttpResponse; | ||
|
||
import java.util.Arrays; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.apache.coyote.http.HttpMethod.POST; | ||
|
||
public class RegisterMapping implements HandlerMapping { | ||
|
||
public static final String TARGET_URI = "register"; | ||
|
||
@Override | ||
public boolean supports(final HttpMethod httpMethod, final String requestUri) { | ||
return POST == httpMethod && | ||
requestUri.contains("register"); | ||
public boolean supports(final HttpRequest httpRequest) { | ||
return httpRequest.isPostRequest() && httpRequest.containsRequestUri(TARGET_URI); | ||
} | ||
|
||
@Override | ||
public String handle(final String requestUri, final HttpHeaders httpHeaders, final String requestBody) { | ||
final Map<String, String> bodyParams = Arrays.stream(requestBody.split("&")) | ||
.map(param -> param.split("=")) | ||
.collect(Collectors.toMap(param -> param[0], param -> param[1])); | ||
public HttpResponse handle(final HttpRequest httpRequest) throws IOException { | ||
final Map<String, String> bodyParams = httpRequest.getParsedBody(); | ||
|
||
final String account = bodyParams.get("account"); | ||
final String password = bodyParams.get("password"); | ||
|
@@ -32,8 +28,6 @@ public String handle(final String requestUri, final HttpHeaders httpHeaders, fin | |
final User user = new User(account, password, email); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음... 이번 톰켓 미션과 크게 관련있는지는 모르겠는데 똑같은 걸로 계속 가입이 되더라구요. |
||
InMemoryUserRepository.save(user); | ||
|
||
return String.join("\r\n", | ||
"HTTP/1.1 302 Found ", | ||
"Location: /index.html "); | ||
return HttpResponse.redirect("/index.html"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
package org.apache.coyote.handler.mapping; | ||
|
||
import org.apache.coyote.http.HttpHeaders; | ||
import org.apache.coyote.http.HttpMethod; | ||
import org.apache.coyote.http.common.HttpBody; | ||
import org.apache.coyote.http.common.HttpHeaders; | ||
import org.apache.coyote.http.request.HttpRequest; | ||
import org.apache.coyote.http.response.ContentType; | ||
import org.apache.coyote.http.response.HttpResponse; | ||
import org.apache.coyote.http.response.StatusCode; | ||
import org.apache.coyote.http.response.StatusLine; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
import static org.apache.coyote.http.common.HttpHeader.CONTENT_TYPE; | ||
|
||
public class RegisterPageMapping implements HandlerMapping { | ||
|
||
public static final String TARGET_URI = "register"; | ||
|
||
@Override | ||
public boolean supports(final HttpMethod httpMethod, final String requestUri) { | ||
return HttpMethod.GET == httpMethod && | ||
requestUri.contains("register"); | ||
public boolean supports(final HttpRequest httpRequest) { | ||
return httpRequest.isGetRequest() && httpRequest.containsRequestUri(TARGET_URI); | ||
} | ||
|
||
@Override | ||
public String handle(final String requestUri, final HttpHeaders httpHeaders, final String requestBody) throws IOException { | ||
final String filePath = "static/register.html"; | ||
final URL fileUrl = getClass().getClassLoader().getResource(filePath); | ||
final Path path = new File(fileUrl.getPath()).toPath(); | ||
final String responseBody = new String(Files.readAllBytes(path)); | ||
|
||
return String.join("\r\n", | ||
"HTTP/1.1 200 OK ", | ||
"Content-Type: text/html;charset=utf-8 ", | ||
"Content-Length: " + responseBody.getBytes().length + " ", | ||
"", | ||
responseBody); | ||
public HttpResponse handle(final HttpRequest httpRequest) throws IOException { | ||
return HttpResponse.builder() | ||
.statusLine(StatusLine.from(StatusCode.OK)) | ||
.httpHeaders(new HttpHeaders(Map.of(CONTENT_TYPE, ContentType.HTML.getValue()))) | ||
.body(HttpBody.file("static/register.html")) | ||
.build(); | ||
} | ||
} |
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.
혹시 loginFilter를 상속으로 적용하신 이유가 있을까요?
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.
다른 화면이나 기능이 추가되면 인증이 필요한 부분이 자연스럽게 생길것 같다는 생각이 들었습니다. 그래서 공통적인 기능을 추상화 했습니다.