Skip to content
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
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/main/java/enums/HttpHeader.java
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;
}
}
12 changes: 12 additions & 0 deletions src/main/java/enums/HttpMethod.java
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();
}
}

37 changes: 37 additions & 0 deletions src/main/java/enums/StatusCode.java
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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/enums/URLPath.java
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;
}

}
22 changes: 22 additions & 0 deletions src/main/java/enums/UserQueryKey.java
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();
}
}
99 changes: 99 additions & 0 deletions src/main/java/webserver/HttpRequest.java
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpRequest에서 StartLine, Header, Body를 인스턴스 변수로 가지고 있는게 직관적으로 코드 구조의 이해가 쉬울 것 같아요.


// 생성자
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;
}
}
Loading