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조 정윤아 #12

Open
wants to merge 5 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/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.

21 changes: 21 additions & 0 deletions src/main/java/strings/FilePath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package strings;

import java.nio.file.Path;

public enum FilePath {
INDEX_HTML(Path.of("webapp/index.html")),
FORM_HTML(Path.of("webapp/user/form.html")),
LOGIN_HTML(Path.of("webapp/user/login.html")),
LOGIN_FAILED_HTML(Path.of("webapp/user/login_failed.html")),
STYLES_CSS(Path.of("css/styles.css"));
Comment on lines +6 to +10

Choose a reason for hiding this comment

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

File객체 자체를 을 enum으로 관리하셨군요 👍 👍 👍


private final Path path;

FilePath(Path path) {
this.path = path;
}

public Path getPath() {
return path;
}
}
13 changes: 13 additions & 0 deletions src/main/java/webserver/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package webserver;

import java.io.IOException;

public class Controller {
HttpRequest httpRequest;
HttpResponse httpResponse;
Comment on lines +6 to +7

Choose a reason for hiding this comment

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

Controller의 request, response를 member 변수로 선언하신 이유가 있나요?


public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
this.httpRequest = httpRequest;
this.httpResponse = httpResponse;
}
}
8 changes: 8 additions & 0 deletions src/main/java/webserver/ForwardController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package webserver;

public class ForwardController extends Controller {

public void execute(HttpRequest httpRequest, HttpResponse httpResponse) {

}
}
8 changes: 8 additions & 0 deletions src/main/java/webserver/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package webserver;

public class HomeController extends Controller {

public void execute(HttpRequest httpRequest, HttpResponse httpResponse) {

}
}
149 changes: 149 additions & 0 deletions src/main/java/webserver/HttpRequest.java

Choose a reason for hiding this comment

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

HttpRequest가 너무 많은 책임을 가지고 있는거 같습니다.
HttpRequest의 멤버 변수로 startline, body, header 객체들을 구성하고, 각자 관리하면 좋을 거 같아요

Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package webserver;

import http.util.IOUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;

import static strings.FilePath.*;
import static strings.FilePath.STYLES_CSS;

public class HttpRequest {
private String startLine;
private String method;
private String url;
private String version;
private String header;
private String body;
Comment on lines +13 to +18

Choose a reason for hiding this comment

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

바뀌지 않아야 하는 항목들이니까 final keyword를 붙이면 좋을 것 같습니다.


private HttpRequest(String startLine, String header, String body) {
this.startLine = startLine;
this.header = header;
this.body = body;
}

public static HttpRequest from(BufferedReader br) throws IOException {
String startLine = extractStartLine(br);
String header = extractRequestHeader(br);
String body = extractRequestBody(br);

if (areNull(startLine, header, body) || areEmpty(startLine, header)) {
throw new IllegalArgumentException("Arguments must not be null or empty");
}
return new HttpRequest(startLine, header, body);
}

public String getStartLine() {
return startLine;
}

public String getHeader() {
return header;
}

public String getBody() {
return body;
}

public String getMethod() {
return method;
}

public String getUrl() {
return url;
}

public String getVersion() {
return version;
}

public boolean cookieLoginTrue() {
if (header.startsWith("Cookie:")) {
return header.contains("true");
}
return false;
}


private static String extractStartLine(BufferedReader br) throws IOException {
try {
String startLine = br.readLine();
if (startLine != null) {
return br.readLine();
}
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String extractRequestHeader(BufferedReader br) throws IOException {
String headers;

while ((headers = br.readLine()) != "") {
headers = br.readLine();
}

return headers;
}

private static String extractRequestBody(BufferedReader br) throws IOException {
int requestContentLength = 0;

while (true) {
final String line;
line = br.readLine();
if (line.equals("")) { break; }

// header info
if (line.startsWith("Content-Length")) {
requestContentLength = Integer.parseInt(line.split(": ")[1]);
}
}

String body = IOUtils.readData(br, requestContentLength);

return body;
}

private void extractStartLineElements() {
if (startLine != null) {
String[] tokens = startLine.split(" ");
if (tokens.length > 1) {
method = tokens[0];
url = tokens[1];
version = tokens[2];
}
}
}

private static boolean areNull(String startLine, String header, String body) {
return startLine == null || header == null || body == null;
}

private static boolean areEmpty(String startLine, String header) {
return startLine.isEmpty() || header.isEmpty();
}

public String[] getValueOfURLQuery(String urlQuery) {
String[] buf = urlQuery.split("\\?");
String queryString = buf[1];

return getValueOfQuery(queryString);
}

public String[] getValueOfQuery(String query) {
String[] keyAndValue = query.split("&");

String[] value = new String[keyAndValue.length];

for (int i = 0; i < keyAndValue.length; i++) {
value[i] = keyAndValue[i].split("=")[1];
System.out.println(value[i]);
}

return value;
}

}
67 changes: 67 additions & 0 deletions src/main/java/webserver/HttpResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package webserver;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HttpResponse {
DataOutputStream dos;
private static final Logger log = Logger.getLogger(RequestHandler.class.getName());

public HttpResponse(DataOutputStream dos) {
this.dos = dos;
}

public void response200Header(int lengthOfBodyContent) {
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
dos.writeBytes("Content-Type: text/html;charset=utf-8\r\n");
dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}

public void response200HeaderCss(int lengthOfBodyContent) {
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
dos.writeBytes("Content-Type: text/css;charset=utf-8\r\n");
dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}

public void response302Redirect(String redirectPath){
try {
dos.writeBytes("HTTP/1.1 302 Found\r\n");
dos.writeBytes("Location: " + redirectPath + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}

public void response302RedirectCookie(String redirectPath){
try {
dos.writeBytes("HTTP/1.1 302 Found\r\n");
dos.writeBytes("Location: " + redirectPath + "\r\n");
dos.writeBytes("Set-Cookie: logined=true" + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}

public void responseBody(byte[] body) {
try {
dos.write(body, 0, body.length);
dos.flush();
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/webserver/ListController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package webserver;

import db.MemoryUserRepository;

import java.io.IOException;

public class ListController extends Controller {
HttpRequest httpRequest;
HttpResponse httpResponse;

private void printList() {
if (httpRequest.cookieLoginTrue()) {
System.out.println(MemoryUserRepository.getInstance().findAll());
}
}

public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
this.httpRequest = httpRequest;
this.httpResponse = httpResponse;
}
}
44 changes: 44 additions & 0 deletions src/main/java/webserver/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package webserver;

import db.MemoryUserRepository;

import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.file.Files;

import static strings.FilePath.INDEX_HTML;
import static strings.FilePath.LOGIN_FAILED_HTML;

public class LoginController extends Controller {
HttpRequest httpRequest;
HttpResponse httpResponse;

private byte[] Login() throws IOException {
byte[] body;
String[] userInfo = null;

if (httpRequest.getMethod().equals("GET")) {
userInfo = httpRequest.getValueOfURLQuery(httpRequest.getUrl());
}
if (httpRequest.getMethod().equals("POST")) {
userInfo = httpRequest.getValueOfQuery(httpRequest.getBody());
}

body = Files.readAllBytes(INDEX_HTML.getPath());
httpResponse.response302RedirectCookie("/index.html");

if (MemoryUserRepository.getInstance().findUserById(userInfo[0]) == null) {
body = Files.readAllBytes(LOGIN_FAILED_HTML.getPath());
}
return body;
}

public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
this.httpRequest = httpRequest;
this.httpResponse = httpResponse;

Login();
}


}
Loading