-
Notifications
You must be signed in to change notification settings - Fork 11
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
[Lee] Step 3: HTTP 웹서버 구현 #4
Open
street62
wants to merge
3
commits into
Next-Squad:street62
Choose a base branch
from
street62:street62
base: street62
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package webserver.logicexecutor; | ||
|
||
import webserver.request.Request; | ||
import webserver.response.Response; | ||
|
||
public interface LogicExecutor { | ||
Response run(Request request); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/webserver/logicexecutor/LoginLogicExecutor.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,22 @@ | ||
package webserver.logicexecutor; | ||
|
||
import model.User; | ||
import webserver.request.Request; | ||
import webserver.response.Response; | ||
import webserver.response.StatusCode; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
|
||
public class LoginLogicExecutor implements LogicExecutor{ | ||
@Override | ||
public Response run(Request request) { | ||
Map<String, String> params = request.getParams(); | ||
String userId = params.get("userId"); | ||
String password = params.get("password"); | ||
String name = params.get("name"); | ||
String email = params.get("email"); | ||
User user = new User(userId, password, name, email); | ||
return new Response(StatusCode.OK, user.toString().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |
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,32 @@ | ||
package webserver.request; | ||
|
||
import java.io.BufferedReader; | ||
import java.util.Map; | ||
|
||
public class Request { | ||
private String method; | ||
private String uri; | ||
private Map<String, String> params; | ||
|
||
public Request(String firstline) { | ||
String[] splitFirstLine = firstline.split(" "); | ||
method = splitFirstLine[0]; | ||
uri = splitFirstLine[1]; | ||
} | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public String getUri() { | ||
return uri; | ||
} | ||
|
||
public void setParams(Map<String, String> params) { | ||
this.params = params; | ||
} | ||
|
||
public Map<String, String> getParams() { | ||
return params; | ||
} | ||
} |
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 webserver.response; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
public class Response { | ||
private StatusCode statusCode; | ||
private byte[] body; | ||
|
||
public Response(StatusCode statusCode, byte[] body) { | ||
this.statusCode = statusCode; | ||
this.body = body; | ||
} | ||
|
||
public byte[] getHeader() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(String.format("HTTP/1.1 %d %s \r\n", statusCode.getCodeNumber(), statusCode)); | ||
sb.append("Content-Type: text/html;charset=utf-8\r\n"); | ||
sb.append("Content-Length: " + body.length + "\r\n"); | ||
sb.append("\r\n"); | ||
return sb.toString().getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
public byte[] getBody() { | ||
return body.clone(); | ||
} | ||
} |
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,33 @@ | ||
package webserver.response; | ||
|
||
import webserver.logicexecutor.LogicExecutor; | ||
import webserver.logicexecutor.LoginLogicExecutor; | ||
import webserver.request.Request; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ResponseMaker { | ||
private final Map<String, LogicExecutor> mapper = new HashMap<>(); | ||
|
||
public ResponseMaker() { | ||
mapper.put("/user/create", new LoginLogicExecutor()); | ||
} | ||
|
||
public Response getResponse(Request request) { | ||
if (mapper.containsKey(request.getUri())) { | ||
LogicExecutor logicExecutor = mapper.get(request.getUri()); | ||
return logicExecutor.run(request); | ||
} | ||
try { | ||
byte[] body = Files.readAllBytes(new File("./webapp" + request.getUri()).toPath()); | ||
return new Response(StatusCode.OK, body); | ||
} catch (IOException e) { | ||
return new Response(StatusCode.OK, "헬로우 월드!".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} | ||
} |
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,19 @@ | ||
package webserver.response; | ||
|
||
public enum StatusCode { | ||
OK(200), | ||
REDIRECT(302); | ||
|
||
|
||
private int codeNumber; | ||
|
||
private StatusCode(int codeNumber) { | ||
this.codeNumber = codeNumber; | ||
} | ||
|
||
public int getCodeNumber() { | ||
return codeNumber; | ||
} | ||
|
||
|
||
} |
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
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.
메서드 단위로 로직을 구분하셨군요.!