-
Notifications
You must be signed in to change notification settings - Fork 388
[1 -2단계 - Tomcat 구현하기] 카피(김상혁) 미션 제출합니다 #536
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fed02f6
fix: remove implementation logback-classic on gradle (#501)
geoje 7e91356
fix: add threads min-spare configuration on properties (#502)
geoje 7b81c9c
feat: /index.html 응답하는 기능 구현
tkdgur0906 f15f373
feat: css 지원하는 기능 구현
tkdgur0906 927cc60
feat: Query String 파싱 기능 구현
tkdgur0906 d310bcb
feat: 로그인 여부에 따라 다른 페이지로 리다이렉트 하는 기능 구현
tkdgur0906 a3c3177
feat: Post 방식으로 회원가입하는 기능 구현
tkdgur0906 2d0e5bd
feat: 로그인 Post로 전송하도록 변경
tkdgur0906 77c6b42
feat: 로그인 시 Cookie에 JSESSIONID 값 저장하는 기능 구현
tkdgur0906 41b82f7
feat: 세션을 통해 로그인 여부 체크하는 기능 구현
tkdgur0906 0974368
feat: httpRequest 클래스가 요청 처리하도록 변경
tkdgur0906 553ce12
feat: httpResponse 클래스가 요청 처리하도록 변경
tkdgur0906 39ed564
feat: 리소스 이름으로 리소스 바이트 정보 로딩하는 메서드 적용
tkdgur0906 03057fc
feat: content-type에 charset 정보 추가
tkdgur0906 807d2e1
feat: 0단계 휴리스틱 제거하기
tkdgur0906 faafaa6
feat: 1단계 Http Compression 설정
tkdgur0906 27471e5
feat: 2단계 ETag/If-None-Match 적용
tkdgur0906 ccffb6b
feat: 3단계 캐시 무효화 적용
tkdgur0906 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 hidden or 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 hidden or 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
study/src/main/java/cache/com/example/cachecontrol/CacheWebConfig.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| package cache.com.example.cachecontrol; | ||
|
|
||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.CacheControl; | ||
| import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
| import org.springframework.web.servlet.mvc.WebContentInterceptor; | ||
|
|
||
| @Configuration | ||
| public class CacheWebConfig implements WebMvcConfigurer { | ||
|
|
||
| @Override | ||
| public void addInterceptors(final InterceptorRegistry registry) { | ||
| WebContentInterceptor webContentInterceptor = new WebContentInterceptor(); | ||
| webContentInterceptor.addCacheMapping( | ||
| CacheControl.noCache().cachePrivate(), | ||
| "/**" | ||
| ); | ||
| registry.addInterceptor(webContentInterceptor); | ||
| } | ||
| } |
24 changes: 20 additions & 4 deletions
24
study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,12 +1,28 @@ | ||
| package cache.com.example.etag; | ||
|
|
||
| import cache.com.example.version.ResourceVersion; | ||
| import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.filter.ShallowEtagHeaderFilter; | ||
|
|
||
| import static cache.com.example.version.CacheBustingWebConfig.PREFIX_STATIC_RESOURCES; | ||
|
|
||
| @Configuration | ||
| public class EtagFilterConfiguration { | ||
|
|
||
| // @Bean | ||
| // public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { | ||
| // return null; | ||
| // } | ||
| private final ResourceVersion resourceVersion; | ||
|
|
||
| public EtagFilterConfiguration(ResourceVersion resourceVersion) { | ||
| this.resourceVersion = resourceVersion; | ||
| } | ||
|
|
||
| @Bean | ||
| public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { | ||
| FilterRegistrationBean<ShallowEtagHeaderFilter> filterFilterRegistrationBean | ||
| = new FilterRegistrationBean<>(new ShallowEtagHeaderFilter()); | ||
| filterFilterRegistrationBean.addUrlPatterns("/etag"); | ||
| filterFilterRegistrationBean.addUrlPatterns(PREFIX_STATIC_RESOURCES + "/" + resourceVersion.getVersion() + "/*"); | ||
| return filterFilterRegistrationBean; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
File renamed without changes.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,12 +1,19 @@ | ||
| package org.apache.coyote.http11; | ||
|
|
||
| import com.techcourse.db.InMemoryUserRepository; | ||
| import com.techcourse.exception.UncheckedServletException; | ||
| import com.techcourse.model.User; | ||
| import org.apache.coyote.Processor; | ||
| import org.apache.coyote.http11.controller.ResourceLoader; | ||
| import org.apache.coyote.http11.request.HttpRequest; | ||
| import org.apache.coyote.http11.response.HttpResponse; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.Socket; | ||
| import java.net.URISyntaxException; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| public class Http11Processor implements Runnable, Processor { | ||
|
|
||
|
|
@@ -29,19 +36,88 @@ public void process(final Socket connection) { | |
| try (final var inputStream = connection.getInputStream(); | ||
| final var outputStream = connection.getOutputStream()) { | ||
|
|
||
| final var responseBody = "Hello world!"; | ||
| HttpRequest httpRequest = HttpRequest.from(inputStream); | ||
|
|
||
| String version = httpRequest.getVersion(); | ||
| Map<String, String> httpRequestHeaders = httpRequest.getHeaders(); | ||
| String httpMethod = httpRequest.getHttpMethod(); | ||
| String page = httpRequest.getPath(); | ||
| HttpResponse httpResponse; | ||
|
|
||
| String responseBody; | ||
| if (page.equals("/")) { | ||
| responseBody = "Hello world!"; | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| } else if (page.startsWith("/login") && httpMethod.equals("POST")) { | ||
| String requestBody = httpRequest.getBody(); | ||
| String account = requestBody.split("&")[0].split("=")[1]; | ||
| String password = requestBody.split("&")[1].split("=")[1]; | ||
|
|
||
| User user = InMemoryUserRepository.findByAccount(account).get(); | ||
|
|
||
| if (user.checkPassword(password)) { | ||
| log.info("user : {}", user); | ||
| SessionManager sessionManager = new SessionManager(); | ||
| UUID jSessionId = UUID.randomUUID(); | ||
| Session session = new Session(jSessionId.toString()); | ||
| session.setAttribute("user", user); | ||
| sessionManager.add(session); | ||
|
|
||
| responseBody = new String(ResourceLoader.loadResource("static/index.html")); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| httpResponse.addHeader("Location", "/index.html"); | ||
| httpResponse.addHeader("Set-Cookie", "JSESSIONID=" + jSessionId); | ||
| } else { | ||
| responseBody = new String(ResourceLoader.loadResource("static/401.html")); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| httpResponse.addHeader("Location", "/401.html"); | ||
| } | ||
| } else if (page.startsWith("/login") && httpMethod.equals("GET")) { | ||
| SessionManager sessionManager = new SessionManager(); | ||
|
|
||
| final var response = String.join("\r\n", | ||
| "HTTP/1.1 200 OK ", | ||
| "Content-Type: text/html;charset=utf-8 ", | ||
| "Content-Length: " + responseBody.getBytes().length + " ", | ||
| "", | ||
| responseBody); | ||
| if (httpRequestHeaders.containsKey("Cookie") && | ||
| httpRequestHeaders.get("Cookie").startsWith("JSESSIONID=")) { | ||
| String jSessionId = httpRequestHeaders.get("Cookie").split("=")[1]; | ||
| Session session = sessionManager.findSession(jSessionId); | ||
|
|
||
| outputStream.write(response.getBytes()); | ||
| outputStream.flush(); | ||
| if (session != null && session.getAttribute("user") != null) { | ||
| responseBody = new String(ResourceLoader.loadResource("static/index.html")); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| } else { | ||
| responseBody = new String(ResourceLoader.loadResource("static" + page + ".html")); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| } | ||
| } else { | ||
| responseBody = new String(ResourceLoader.loadResource("static" + page + ".html")); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| } | ||
| } else if (page.equals("/register") && httpMethod.equals("POST")) { | ||
| String requestBody = httpRequest.getBody(); | ||
| String account = requestBody.split("&")[0].split("=")[1]; | ||
| String email = requestBody.split("&")[1].split("=")[1]; | ||
| String password = requestBody.split("&")[2].split("=")[1]; | ||
| InMemoryUserRepository.save(new User(account, email, password)); | ||
|
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. User 생성 파라미터 순서가 틀려 회원가입이 정상적으로 작동하고 있지 않습니다. |
||
| responseBody = new String(ResourceLoader.loadResource("static/index.html")); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| httpResponse.addHeader("Location", "/index.html"); | ||
| } else if (page.startsWith("/css/")) { | ||
| responseBody = new String(ResourceLoader.loadResource("static" + page)); | ||
| httpResponse = HttpResponse.of(version, 200, "text/css", responseBody); | ||
| } else if (page.contains(".js")) { | ||
| responseBody = new String(ResourceLoader.loadResource("static" + page)); | ||
| httpResponse = HttpResponse.of(version, 200, "text/javascript", responseBody); | ||
| } else if (page.endsWith(".html")) { | ||
| responseBody = new String(ResourceLoader.loadResource("static" + page)); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| } else { | ||
| responseBody = new String(ResourceLoader.loadResource("static" + page + ".html")); | ||
| httpResponse = HttpResponse.of(version, 200, "text/html", responseBody); | ||
| } | ||
| httpResponse.send(outputStream); | ||
| } catch (IOException | UncheckedServletException e) { | ||
| log.error(e.getMessage(), e); | ||
| } catch (URISyntaxException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
tomcat/src/main/java/org/apache/coyote/http11/Session.java
This file contains hidden or 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,30 @@ | ||
| package org.apache.coyote.http11; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class Session { | ||
|
|
||
| private static final Map<String, Object> values = new HashMap<>(); | ||
| private final String id; | ||
|
|
||
| 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); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
tomcat/src/main/java/org/apache/coyote/http11/SessionManager.java
This file contains hidden or 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,29 @@ | ||
| package org.apache.coyote.http11; | ||
|
|
||
| import org.apache.catalina.Manager; | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class SessionManager implements Manager { | ||
|
|
||
| private static final Map<String, Session> SESSIONS = new HashMap<>(); | ||
|
|
||
| public SessionManager() { | ||
| } | ||
|
|
||
| @Override | ||
| public void add(Session session) { | ||
| SESSIONS.put(session.getId(), new Session(session.getId())); | ||
| } | ||
|
|
||
| @Override | ||
| public Session findSession(String id) throws IOException { | ||
| return SESSIONS.get(id); | ||
| } | ||
|
|
||
| @Override | ||
| public void remove(Session session) { | ||
| SESSIONS.remove(session.getId()); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
tomcat/src/main/java/org/apache/coyote/http11/controller/ResourceLoader.java
This file contains hidden or 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,20 @@ | ||
| package org.apache.coyote.http11.controller; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class ResourceLoader { | ||
|
|
||
| public static byte[] loadResource(String resourceName) throws URISyntaxException, IOException { | ||
| URL url = ResourceLoader.class.getClassLoader().getResource(resourceName); | ||
| if (url == null) { | ||
| throw new IllegalArgumentException("존재하지 않는 리소스 입니다." + resourceName); | ||
| } | ||
|
|
||
| Path path = Path.of(url.toURI()); | ||
| return Files.readAllBytes(path); | ||
| } | ||
| } |
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.
'비밀번호가 틀릴 때' 외 아이디가 틀릴 때도 고려해주시면 좋겠네요!