-
Notifications
You must be signed in to change notification settings - Fork 23
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
3주차 미션 / 서버 4조 조동현 #1
Open
mr8356
wants to merge
10
commits into
Konkuk-KUIT:main
Choose a base branch
from
mr8356:mr8356
base: 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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0501a67
requirement 1 complete
mr8356 2174324
requirement 2~4 complete
mr8356 736ee43
requirement 6 complete
mr8356 7477871
implementaion complete
mr8356 3bcf43f
refactoring 1~3 complete
mr8356 447b18b
refactoring 4 complete few errors..
mr8356 9211004
refactoring 4 complete and DI
mr8356 1aa2d67
MISSION COMPLETE
mr8356 563258a
Apply code review feedback and refactor
mr8356 f4c3626
refactor and change DI to single tone static
mr8356 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
package enums; | ||
public enum HttpHeader { | ||
CONTENT_TYPE("Content-Type"), | ||
CONTENT_LENGTH("Content-Length"), | ||
COOKIE("Cookie"), | ||
SET_COOKIE("Set-Cookie"); | ||
|
||
private final String headerName; | ||
|
||
HttpHeader(String headerName) { | ||
this.headerName = headerName; | ||
} | ||
|
||
public String getHeaderName() { | ||
return headerName; | ||
} | ||
} |
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,12 @@ | ||
package enums; | ||
|
||
public enum HttpMethod { | ||
GET, | ||
POST, | ||
PUT, | ||
DELETE; | ||
public String getMethod() { | ||
return this.toString(); | ||
} | ||
} | ||
|
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,37 @@ | ||
package enums; | ||
|
||
public enum StatusCode { | ||
OK(200, "OK"), | ||
FOUND(302, "Found"), | ||
BAD_REQUEST(400, "Bad Request"), | ||
UNAUTHORIZED(401, "Unauthorized"), | ||
NOT_FOUND(404, "Not Found"), | ||
METHOD_NOT_ALLOWED(405, "Method Not Allowed"), | ||
CONFLICT(409, "Conflict"), | ||
LENGTH_REQUIRED(411, "Length Required"); | ||
|
||
private final int code; | ||
private final String phrase; | ||
|
||
StatusCode(int code, String phrase) { | ||
this.code = code; | ||
this.phrase = phrase; | ||
} | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public String getPhrase() { | ||
return phrase; | ||
} | ||
|
||
public static StatusCode fromCode(int code) { | ||
for (StatusCode sc : StatusCode.values()) { | ||
if (sc.getCode() == code) { | ||
return sc; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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 enums; | ||
|
||
|
||
public enum URLPath { | ||
INDEX("/index.html"), | ||
LOGIN("/user/login"), | ||
SIGNUP("/user/signup"), | ||
LIST("/user/list.html"), | ||
LOGINFAIL("/user/login_failed.html"); | ||
|
||
|
||
private final String path; | ||
|
||
URLPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
} |
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 enums; | ||
|
||
public enum UserQueryKey { | ||
USER_ID("userId"), | ||
PASSWORD("password"), | ||
NAME("name"), | ||
EMAIL("email"); | ||
|
||
private final String key; | ||
|
||
UserQueryKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public static String getValue(UserQueryKey key) { | ||
return key.getKey(); | ||
} | ||
} |
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,99 @@ | ||
package webserver; | ||
|
||
import http.util.HttpRequestUtils; | ||
import http.util.IOUtils; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HttpRequest { | ||
private String method; | ||
private String path; | ||
private String version; | ||
private Map<String, String> headers; | ||
private Map<String, String> queryParams; | ||
private Map<String, String> bodyParams; | ||
|
||
// 생성자 | ||
private HttpRequest(String method, String path, String version, Map<String, String> headers, | ||
Map<String, String> queryParams, Map<String, String> bodyParams) { | ||
this.method = method; | ||
this.path = path; | ||
this.version = version; | ||
this.headers = headers; | ||
this.queryParams = queryParams; | ||
this.bodyParams = bodyParams; | ||
} | ||
|
||
// 정적 팩토리 메서드: BufferedReader를 통해 HttpRequest 객체 생성 | ||
public static HttpRequest from(BufferedReader br) throws IOException { | ||
// 1. 요청 라인 파싱 | ||
String requestLine = br.readLine(); | ||
if (requestLine == null || requestLine.isEmpty()) { | ||
throw new IllegalArgumentException("Invalid request line"); | ||
} | ||
String[] requestTokens = requestLine.split(" "); | ||
String method = requestTokens[0]; | ||
String url = requestTokens[1]; | ||
String protocol = requestTokens[2]; | ||
|
||
// 2. 헤더 파싱 | ||
Map<String, String> headers = new HashMap<>(); | ||
String line; | ||
while (!(line = br.readLine()).isEmpty()) { | ||
String[] headerTokens = line.split(": ", 2); | ||
if (headerTokens.length == 2) { | ||
headers.put(headerTokens[0], headerTokens[1]); | ||
} | ||
} | ||
|
||
// 3. URL에서 경로와 쿼리 스트링 분리 | ||
String path = url.split("\\?")[0]; | ||
Map<String, String> queryParams = new HashMap<>(); | ||
if (url.contains("?")) { | ||
String queryString = url.split("\\?")[1]; | ||
queryParams = HttpRequestUtils.parseQueryParameter(queryString); | ||
} | ||
|
||
// 4. 바디 파싱 (Content-Length가 있을 때만) | ||
Map<String, String> bodyParams = new HashMap<>(); | ||
if (headers.containsKey("Content-Length")) { | ||
int contentLength = Integer.parseInt(headers.get("Content-Length")); | ||
String bodyContent = IOUtils.readData(br, contentLength); | ||
bodyParams = HttpRequestUtils.parseQueryParameter(bodyContent); | ||
} | ||
|
||
return new HttpRequest(method, path, protocol, headers, queryParams, bodyParams); | ||
} | ||
|
||
// Getters | ||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public Map<String, String> getHeaders() { | ||
return headers; | ||
} | ||
|
||
public String getHeader(String key) { | ||
return headers.get(key); | ||
} | ||
|
||
public Map<String, String> getQueryParams() { | ||
return queryParams; | ||
} | ||
|
||
public Map<String, String> getBodyParams() { | ||
return bodyParams; | ||
} | ||
} |
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.
HttpRequest에서 StartLine, Header, Body를 인스턴스 변수로 가지고 있는게 직관적으로 코드 구조의 이해가 쉬울 것 같아요.