-
Notifications
You must be signed in to change notification settings - Fork 310
[톰캣 구현하기 - 3, 4단계] 베베(최원용) 미션 제출합니다. #417
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
Changes from all commits
38bce8f
a5ed403
63b77a4
89ebd54
fb57afd
221dae9
9789bb4
2f58f2a
2d40278
cb615f0
ae1289a
353872e
c4e6719
e03eb85
01f3cbe
19edeb8
b2f16b6
00e56ac
c7d1c44
ded5f7a
a871936
ae90174
255ced3
ad61f36
1b8ab94
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 |
---|---|---|
|
@@ -4,6 +4,14 @@ | |
import java.io.UncheckedIOException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.coyote.http11.Http11Processor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -14,8 +22,12 @@ public class Connector implements Runnable { | |
|
||
private static final int DEFAULT_PORT = 8080; | ||
private static final int DEFAULT_ACCEPT_COUNT = 100; | ||
private static final int MAX_THREAD_POOL_SIZE = 250; | ||
private static final int CORE_POOL_SIZE = 10; | ||
private static final Long KEEP_ALIVE_TIME = 60L; | ||
|
||
private final ServerSocket serverSocket; | ||
private final ThreadPoolExecutor threadPoolExecutor; | ||
private boolean stopped; | ||
|
||
public Connector() { | ||
|
@@ -24,6 +36,12 @@ public Connector() { | |
|
||
public Connector(final int port, final int acceptCount) { | ||
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. 리뷰라기보단, 저도 궁금했던 부분이라 베베에게도 여쭤보고 싶은 것이 있습니다!
4단계 미션 요구사항을 보면, 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. Connector 클래스의 생성자에서 파라미터로 받는 Container는 아마 웹 서버에서 사용되는 컨테이너를 나타내는 객체일 것입니다.(서블릿 컨테이너일듯?) Connector 클래스의 생성자에서 받는 Container 객체는 뭔가 컨테이너와 관련된 설정이나 동작을 구성하고 초기화하는 데 사용될 것 같네요. 구체적인 내용은 코드의 나머지 부분이나 설명을 확인해봐야 알 것 같은데 별도로 제공되지는 않나보군요 ㅠ 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. 아하 .. 이 부분에 대해서는 따로 공부가 필요한 것 같네요 ㅋㅋㅋ 저도 잘 몰라서 사용하지 않았습니다.. 의견 알려주셔서 감사합니다~! |
||
this.serverSocket = createServerSocket(port, acceptCount); | ||
this.threadPoolExecutor = new ThreadPoolExecutor( | ||
CORE_POOL_SIZE, | ||
MAX_THREAD_POOL_SIZE, | ||
KEEP_ALIVE_TIME, | ||
TimeUnit.SECONDS, | ||
new LinkedBlockingQueue<>(DEFAULT_ACCEPT_COUNT)); | ||
this.stopped = false; | ||
} | ||
|
||
|
@@ -66,13 +84,14 @@ private void process(final Socket connection) { | |
return; | ||
} | ||
var processor = new Http11Processor(connection); | ||
new Thread(processor).start(); | ||
threadPoolExecutor.execute(processor); | ||
} | ||
|
||
public void stop() { | ||
stopped = true; | ||
try { | ||
serverSocket.close(); | ||
threadPoolExecutor.shutdown(); | ||
} catch (IOException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.apache.coyote.http11.auth; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.request.line.Protocol; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import static org.apache.coyote.http11.response.ResponsePage.INDEX_PAGE; | ||
import static org.apache.coyote.http11.response.ResponsePage.LOGIN_PAGE; | ||
import static org.apache.coyote.http11.response.ResponsePage.UNAUTHORIZED_PAGE; | ||
|
||
public class LoginService { | ||
|
||
public static final String COOKIE_KEY = "JSESSIONID"; | ||
|
||
private final SessionRepository sessionRepository = new SessionRepository(); | ||
|
||
public HttpResponse getLoginViewResponse(Cookie cookie, Protocol protocol) { | ||
Optional<String> cookieOption = cookie.get(COOKIE_KEY); | ||
if (cookieOption.isEmpty()) { | ||
return HttpResponse.getCookieNullResponseEntity(protocol, LOGIN_PAGE); | ||
} | ||
final Optional<Session> session = sessionRepository.getSession(cookieOption.get()); | ||
if (session.isEmpty()) { | ||
return HttpResponse.getCookieNullResponseEntity(protocol, LOGIN_PAGE); | ||
} | ||
return HttpResponse.getCookieNullResponseEntity(protocol, INDEX_PAGE); | ||
} | ||
|
||
public HttpResponse getLoginOrElseUnAuthorizedResponse(Protocol protocol, String account, String password) { | ||
return InMemoryUserRepository.findByAccount(account) | ||
.filter(user -> user.checkPassword(password)) | ||
.map(user -> getSuccessLoginResponse(user, protocol)) | ||
.orElseGet(() -> HttpResponse.getCookieNullResponseEntity(protocol, UNAUTHORIZED_PAGE)); | ||
} | ||
|
||
private HttpResponse getSuccessLoginResponse(final User user, final Protocol protocol) { | ||
final String uuid = UUID.randomUUID().toString(); | ||
final Session session = Session.from(uuid); | ||
session.setAttribute("user", user); | ||
sessionRepository.create(session); | ||
Cookie cookie = new Cookie(); | ||
cookie.setSession(session); | ||
return HttpResponse.getResponseEntity(protocol, cookie, INDEX_PAGE); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.apache.coyote.http11.auth; | ||
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.request.line.Protocol; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import static org.apache.coyote.http11.response.ResponsePage.CONFLICT_PAGE; | ||
import static org.apache.coyote.http11.response.ResponsePage.INDEX_PAGE; | ||
|
||
public class RegisterService { | ||
|
||
public HttpResponse getIndexOrConflictResponse(String account, String password, String email, Protocol protocol) { | ||
if (InMemoryUserRepository.findByAccount(account).isPresent()) { | ||
return HttpResponse.getCookieNullResponseEntity(protocol, CONFLICT_PAGE); | ||
} | ||
|
||
InMemoryUserRepository.save(new User(account, password, email)); | ||
return HttpResponse.getCookieNullResponseEntity(protocol, INDEX_PAGE); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
package org.apache.coyote.http11.auth; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class SessionRepository { | ||
|
||
private static final Map<String, Session> SESSIONS = new HashMap<>(); | ||
private static final Map<String, Session> SESSIONS = new ConcurrentHashMap<>(); | ||
|
||
public static void create(Session session) { | ||
SESSIONS.put(session.getId(), session); | ||
} | ||
|
||
public Session getSession(String id) { | ||
return SESSIONS.get(id); | ||
} | ||
|
||
public static void clearSessions() { | ||
SESSIONS.clear(); | ||
public Optional<Session> getSession(String id) { | ||
if (id == null) { | ||
return Optional.ofNullable(null); | ||
} | ||
return Optional.ofNullable(SESSIONS.get(id)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.apache.coyote.http11.controller; | ||
|
||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.request.line.HttpMethod; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
public abstract class AbstractController implements Controller { | ||
|
||
@Override | ||
public HttpResponse service(final HttpRequest request, final HttpResponse response) { | ||
if (request.methodIsEqualTo(HttpMethod.GET)) { | ||
return doGet(request, response); | ||
} | ||
if (request.methodIsEqualTo(HttpMethod.POST)) { | ||
return doPost(request, response); | ||
} | ||
return null; | ||
} | ||
|
||
protected abstract HttpResponse doPost(final HttpRequest request, final HttpResponse response); | ||
|
||
protected abstract HttpResponse doGet(final HttpRequest request, final HttpResponse response); | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.