forked from codesquad-members-2021/java-was
-
Notifications
You must be signed in to change notification settings - Fork 0
Read cookie/noeul #50
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
Open
sanhee
wants to merge
3
commits into
read-cookie/main
Choose a base branch
from
read-cookie/noeul
base: read-cookie/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,73 +31,131 @@ public void run() { | |
| connection.getPort()); | ||
|
|
||
| try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(in)); | ||
| StringJoiner requestMessages = new StringJoiner(System.lineSeparator()); | ||
| Request request = getRequest(in); // Request | ||
|
|
||
| String requestMessage = br.readLine(); | ||
| String responseMessage = requestController(request); | ||
|
|
||
| while (requestMessage != null && requestMessage.length() != 0) { | ||
| requestMessages.add(requestMessage); | ||
| requestMessage = br.readLine(); | ||
| } | ||
| Response response = Response.from(responseMessage); // Response | ||
| response.write(out); | ||
|
|
||
| } catch (IOException e) { | ||
| log.error(e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| private Request getRequest(InputStream in) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(in)); | ||
| StringJoiner requestMessages = new StringJoiner(System.lineSeparator()); | ||
|
|
||
| RequestHeader requestHeader = RequestHeader.from(requestMessages.toString()); | ||
| String requestMessage = br.readLine(); | ||
|
|
||
| int contentLength = Integer.parseInt(requestHeader.getAttributes().getOrDefault("Content-Length", "0")); | ||
| String requestBody = IOUtils.readData(br, contentLength); | ||
| while (requestMessage != null && requestMessage.length() != 0) { | ||
| requestMessages.add(requestMessage); | ||
| requestMessage = br.readLine(); | ||
| } | ||
|
|
||
| requestMessages.add(System.lineSeparator() + requestBody); | ||
| RequestHeader requestHeader = RequestHeader.from(requestMessages.toString()); | ||
|
|
||
| Request request = Request.from(requestMessages.toString()); | ||
| int contentLength = Integer.parseInt(requestHeader.getAttributes().getOrDefault("Content-Length", "0")); | ||
| String requestBody = IOUtils.readData(br, contentLength); | ||
|
|
||
| // TODO: Default Message를 설정할 수 있을 것 같다. | ||
| String responseMessage = "HTTP/1.1 404 NotFound" + System.lineSeparator() + System.lineSeparator(); | ||
| requestMessages.add(System.lineSeparator() + requestBody); | ||
|
|
||
| String path = request.getPath(); | ||
| String extension = request.getPathExtension(); | ||
| if (extension.equals("html")) { | ||
| Request request = Request.from(requestMessages.toString()); | ||
| return request; | ||
| } | ||
|
|
||
| File htmlFile = new File("./webapp" + path); | ||
| private String requestController(Request request) throws IOException { | ||
|
|
||
| if (htmlFile.exists()) { | ||
| byte[] body = Files.readAllBytes(htmlFile.toPath()); | ||
| String responseMessage = ""; | ||
| String path = request.getPath(); | ||
| String extension = request.getPathExtension(); | ||
|
|
||
| responseMessage = "HTTP/1.1 200 OK" + System.lineSeparator() + | ||
| "Content-Type: text/html;charset=utf-8" + System.lineSeparator() + | ||
| "Content-Length: " + body.length + System.lineSeparator() + | ||
| System.lineSeparator() + | ||
| new String(body); | ||
| } | ||
| } | ||
| switch (path) { | ||
| case "/user/create": { | ||
| Map<String, String> parameters = request.getRequestMessage().getParameters(); | ||
|
|
||
| switch (path) { | ||
| case "/user/create": { | ||
| Map<String, String> parameters = request.getRequestMessage().getParameters(); | ||
| User newUser = User.builder() | ||
| .setUserId(parameters.get("userId")) | ||
| .setPassword(parameters.get("password")) | ||
| .setName(parameters.get("name")) | ||
| .setEmail(parameters.get("email")) | ||
| .build(); | ||
|
|
||
| User newUser = User.builder() | ||
| .setUserId(parameters.get("userId")) | ||
| .setPassword(parameters.get("password")) | ||
| .setName(parameters.get("name")) | ||
| .setEmail(parameters.get("email")) | ||
| .build(); | ||
| DataBase.addUser(newUser); | ||
|
|
||
| responseMessage = "HTTP/1.1 302 Found" + System.lineSeparator() + | ||
| "Location: /index.html"; | ||
| break; | ||
| } | ||
| case "/user/login": { | ||
| responseMessage = loginHandler(request.getRequestMessage().getParameters()); | ||
|
|
||
| break; | ||
| } | ||
| case "/user/list": { | ||
| Map<String, String> parameters = request.getRequestMessage().getHeader().getAttributes(); | ||
|
|
||
| DataBase.addUser(newUser); | ||
| String Cookie = parameters.getOrDefault("Cookie", "empty"); | ||
|
|
||
| if (Cookie.equals("logined=true")) { | ||
| responseMessage = findHtmlFrom(path + ".html"); | ||
| } else { | ||
| responseMessage = "HTTP/1.1 302 Found" + System.lineSeparator() + | ||
| "Location: /index.html"; | ||
| break; | ||
| "Location: /user/login.html"; | ||
| } | ||
| case "/user/login": { | ||
| responseMessage = loginHandler(request.getRequestMessage().getParameters()); | ||
| break; | ||
| } | ||
| default: { | ||
| responseMessage = htmlController(request, responseMessage, path, extension); | ||
| } | ||
| } | ||
| return responseMessage; | ||
| } | ||
|
|
||
| private String htmlController(Request request, String responseMessage, String path, String extension) throws IOException { | ||
| if (extension.equals("html")) { | ||
|
|
||
| Map<String, String> parameters = request.getRequestMessage().getHeader().getAttributes(); | ||
| String Cookie = parameters.getOrDefault("Cookie", "empty"); | ||
|
|
||
| if (loginRequired(path)) { | ||
|
Collaborator
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. 접근은 괜찮은 것 같아요 |
||
| switch (Cookie) { | ||
| case "logined=true": | ||
| responseMessage = findHtmlFrom(path); | ||
| break; | ||
| default: | ||
| responseMessage = "HTTP/1.1 302 Found" + System.lineSeparator() + | ||
| "Location: /user/login.html"; | ||
| } | ||
| } else { | ||
| responseMessage = findHtmlFrom(path); | ||
| } | ||
| } | ||
| return responseMessage; | ||
| } | ||
|
|
||
| Response response = Response.from(responseMessage); | ||
| response.write(out); | ||
| private String findHtmlFrom(String path) throws IOException { | ||
| File htmlFile = new File("./webapp" + path); | ||
| String responseMessage = ""; | ||
|
|
||
| } catch (IOException e) { | ||
| log.error(e.getMessage(), e); | ||
| if (htmlFile.exists()) { | ||
| byte[] body = Files.readAllBytes(htmlFile.toPath()); | ||
|
|
||
| responseMessage = "HTTP/1.1 200 OK" + System.lineSeparator() + | ||
| "Content-Type: text/html;charset=utf-8" + System.lineSeparator() + | ||
| "Content-Length: " + body.length + System.lineSeparator() + | ||
| System.lineSeparator() + | ||
| new String(body); | ||
| } else { | ||
| responseMessage = "HTTP/1.1 404 NotFound" + System.lineSeparator() + System.lineSeparator(); | ||
| } | ||
|
|
||
| return responseMessage; | ||
| } | ||
|
|
||
| private boolean loginRequired(String path) { | ||
| return path.equals("/user/list.html"); | ||
| } | ||
|
|
||
| public String loginHandler(Map<String, String> parameters) { | ||
|
|
||
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
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.
이거도 좋네요
나중에 클래스로 분리도 가능할 것 같네요