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주차 미션 / 서버 2조 황정안 #17

Open
wants to merge 3 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
3 changes: 2 additions & 1 deletion .idea/gradle.xml

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.

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

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

2 changes: 2 additions & 0 deletions src/main/java/HttpRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class HttpRequest {
}
4 changes: 4 additions & 0 deletions src/main/java/db/MemoryUserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ public class MemoryUserRepository implements Repository{
private final Map<String, User> users = new HashMap<>();
private static MemoryUserRepository memoryUserRepository;


private MemoryUserRepository() {
}

public static MemoryUserRepository getInstance() {
if (memoryUserRepository == null) {
memoryUserRepository = new MemoryUserRepository();
//새로 유저 레포 생성해서 넣어줌
return memoryUserRepository;
}
return memoryUserRepository;
}

//해쉬맵에 유저 추가
public void addUser(User user) {
users.put(user.getUserId(), user);
}
Expand All @@ -29,6 +32,7 @@ public User findUserById(String userId) {
return users.get(userId);
}

//맵에 있는 모든 유저 검색
public Collection<User> findAll() {
return users.values();
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/enumPackage/HttpHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package enumPackage;

public enum HttpHeader {
HTTP_VERSION("HTTP/1.1"),
CONTENT_TYPE("Content-Type"),
CONTENT_LENGTH("Content-Length"),
LOCATION("Location"),
COOKIE("Cookie"),
SET_COOKIE("Set-Cookie"),
ACCEPT("Accept"),
TEXT_HTML("text/html;charset=utf-8"),
TEXT_CSS("text/css;charset=utf-8");

private final String header;
HttpHeader(String header) {
this.header = header;
}
public String getHeader() {
return header;
}
}
18 changes: 18 additions & 0 deletions src/main/java/enumPackage/HttpMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package enumPackage;

public enum HttpMethod {
GET("GET"),
POST("POST");

private final String method;

HttpMethod(String method) {
this.method = method;
}
public String getMethod() {
return method;
}
public boolean isEqual(String method) {
return this.method.equals(method);
}
}
18 changes: 18 additions & 0 deletions src/main/java/enumPackage/QueryKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package enumPackage;

public enum QueryKey {
USER_ID("userId"),
PASSWORD("password"),
NAME("name"),
EMAIL("email");

private final String key;

QueryKey(String key) {
this.key = key;
}

public String getKey() {
return key;
}
}
19 changes: 19 additions & 0 deletions src/main/java/enumPackage/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package enumPackage;

public enum Status {
STATUS200("200"),
STATUS302("302"),
OK("OK"),
FOUND("Found")
;

private final String status;

Status(String status) {
this.status = status;
}

public String getStatus() {
return status;
}
}
23 changes: 23 additions & 0 deletions src/main/java/enumPackage/URL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package enumPackage;

public enum URL {
WEB_APP("./webapp"),

INDEX_URL("/index.html"),
LOGIN_URL("/user/login.html"),
LIST_URL("/user/list.html"),
USER_LIST_URL("/user/userList"),
FORM_URL("/user/form.html"),
LOGIN_FAILED_URL("/user/login_failed.html"),
;

private final String url;

URL(String url) {
this.url = url;
}

public String getUrl() {
return url;
}
}
20 changes: 19 additions & 1 deletion src/main/java/http/util/HttpRequestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@ public class HttpRequestUtils {
public static Map<String, String> parseQueryParameter(String queryString) {
try {
String[] queryStrings = queryString.split("&");

//streamㅇ로 변환
//=으로 나눠서 분리
//collect(Collectors.toMap())는 이 스트림을 Map<String, String>으로 변환합니다.
//queries[0]: key에 해당하는 부분 (userId).
//queries[1]: value에 해당하는 부분 (admin).
//이런식으로 분리해서 MAP형태로 넘김
//userid=1
return Arrays.stream(queryStrings)
.map(q -> q.split("="))
.collect(Collectors.toMap(queries -> queries[0], queries -> queries[1]));
} catch (Exception e) {
return new HashMap<>();
}
}
public static Map<String, String> parseCookies(String cookieHeader) {
Map<String, String> cookies = new HashMap<>();
String[] cookiePairs = cookieHeader.split("; "); // 각 쿠키를 분리
for (String cookie : cookiePairs) {
String[] keyValue = cookie.split("="); // "key=value"를 분리
if (keyValue.length == 2) {
cookies.put(keyValue[0], keyValue[1]); // 쿠키 이름과 값을 Map에 저장
}
}
return cookies;
}

}
1 change: 1 addition & 0 deletions src/main/java/http/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class IOUtils {
* 헤더의 Content-Length의 값이 들어와야한다.
*
*/

public static String readData(BufferedReader br, int contentLength) throws IOException {
char[] body = new char[contentLength];
br.read(body, 0, contentLength);
Expand Down
Loading