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주차 미션 / 서버 1조 - 정소민 #8

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
18 changes: 18 additions & 0 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controller;

import http.util.HttpRequest;
import http.util.HttpResponse;

import java.io.IOException;

public interface Controller {
static final String ROOT_URL = "./webapp";
static final String HOME_URL = "/index.html";
static final String LOGIN_FAILED_URL = "/user/login_failed.html";
static final String LIST_URL = "/user/list.html";
static final String LOGIN_URL = "/user/login.html";
static final String STYLE_CSS_URL = "/css/styles.css";
static final String HTML_TYPE = "text/html";
static final String CSS_TYPE = "text/css";
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException;
}
19 changes: 19 additions & 0 deletions src/main/java/controller/ForwardController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package controller;

import enumclass.EnumURL;
import http.util.HttpRequest;
import http.util.HttpResponse;

import javax.swing.text.html.CSS;
import java.io.IOException;

public class ForwardController implements Controller{
@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
if(httpRequest.getURL().endsWith(EnumURL.CSS.getUrl())){
httpResponse.forward(ROOT_URL+STYLE_CSS_URL, CSS_TYPE);
return;
}
httpResponse.forward(ROOT_URL + httpRequest.getURL(), HTML_TYPE);
}
}
13 changes: 13 additions & 0 deletions src/main/java/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package controller;

import http.util.HttpRequest;
import http.util.HttpResponse;

import java.io.IOException;

public class HomeController implements Controller{
@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
httpResponse.forward(ROOT_URL+HOME_URL, HTML_TYPE);
}
}
17 changes: 17 additions & 0 deletions src/main/java/controller/ListController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package controller;

import http.util.HttpRequest;
import http.util.HttpResponse;

import java.io.IOException;

public class ListController implements Controller{
@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
if(!httpRequest.getCookie().equals("logined=true")){
httpResponse.redirect(LOGIN_URL, false);
return;
}
httpResponse.forward(ROOT_URL+LIST_URL, HTML_TYPE);
}
}
35 changes: 35 additions & 0 deletions src/main/java/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package controller;

import db.MemoryUserRepository;
import db.Repository;
import http.util.HttpRequest;
import http.util.HttpResponse;
import model.User;

import java.io.IOException;
import java.util.Map;

import static enumclass.EnumUserQueryKey.PASSWORD;
import static enumclass.EnumUserQueryKey.USERID;
import static http.util.HttpRequestUtils.parseQueryParameter;

public class LoginController implements Controller{
private Repository repository = MemoryUserRepository.getInstance();

@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
String queryString = httpRequest.getBody();
Map<String, String> queryParameter = parseQueryParameter(queryString);
String userid = queryParameter.get(USERID.getKey());
User user = repository.findUserById(userid);
login(httpResponse, queryParameter, user);
}

private void login(HttpResponse httpResponse, Map<String, String> queryParameter, User user) throws IOException {
if(user != null && queryParameter.get(PASSWORD.getKey()).equals(user.getPassword())){
httpResponse.redirect(HOME_URL, true);
return;
}
httpResponse.redirect(LOGIN_FAILED_URL, false);
}
}
34 changes: 34 additions & 0 deletions src/main/java/controller/SignUpController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package controller;

import db.MemoryUserRepository;
import db.Repository;
import http.util.HttpRequest;
import http.util.HttpResponse;
import model.User;

import java.io.IOException;
import java.util.Map;

import static enumclass.EnumUserQueryKey.*;
import static enumclass.EnumUserQueryKey.EMAIL;
import static http.util.HttpRequestUtils.parseQueryParameter;

public class SignUpController implements Controller{
private Repository repository = MemoryUserRepository.getInstance();

@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException {
if(httpRequest.getMethod().equals("GET")){
Map<String, String> queryParameter = parseQueryParameter(httpRequest.getURL().split("\\?")[1]);
User user = new User(queryParameter.get(USERID.getKey()), queryParameter.get(PASSWORD.getKey()), queryParameter.get(NAME.getKey()), queryParameter.get(EMAIL.getKey()));
repository.addUser(user);
httpResponse.redirect(HOME_URL, false);
return;
}
String queryString = httpRequest.getBody();
Map<String, String> queryParameter = parseQueryParameter(queryString);
User user = new User(queryParameter.get(USERID.getKey()), queryParameter.get(PASSWORD.getKey()), queryParameter.get(NAME.getKey()), queryParameter.get(EMAIL.getKey()));
repository.addUser(user);
httpResponse.redirect(HOME_URL, false);
}
}
15 changes: 15 additions & 0 deletions src/main/java/enumclass/EnumHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package enumclass;

public enum EnumHeader {
contentLength("Content-Length"), contentType("Content-Type"), cookie("Cookie"), location("Location"), setCookie("Set-Cookie");

private final String header;

EnumHeader(String header){
this.header = header;
}

public String getHeader(){
return header;
}
}
16 changes: 16 additions & 0 deletions src/main/java/enumclass/EnumMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package enumclass;

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

private final String method;

EnumMethod(String method){
this.method = method;
}

public String getMethod(){
return method;
}

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

public enum EnumStatusCode {
response302("302"), response200("200");

private final String code;

EnumStatusCode(String code){
this.code = code;
}

public String getCode(){
return code;
}
}
15 changes: 15 additions & 0 deletions src/main/java/enumclass/EnumURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package enumclass;

public enum EnumURL {
SIGNUP("/user/signup"), LOGIN("/user/login"), USERLIST("/user/userList"), HTML(".html"), CSS(".css");

private final String url;

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

public String getUrl() {
return url;
}
}
15 changes: 15 additions & 0 deletions src/main/java/enumclass/EnumUserQueryKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package enumclass;

public enum EnumUserQueryKey {
USERID("userId"), PASSWORD("password"), NAME("name"), EMAIL("email");

private final String key;

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

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

import enumclass.EnumHeader;
import webserver.RequestHandler;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Logger;

import static enumclass.EnumHeader.contentLength;
import static enumclass.EnumHeader.cookie;

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

String startLine;
String header;
String body;
String cookie;
public HttpRequest(String startLine, String header, String body, String cookie) {
this.startLine = startLine;
this.header = header;
this.body = body;
this.cookie = cookie;
}
public static HttpRequest from(BufferedReader br) throws IOException {
String startLine = br.readLine();

int requestContentLength = 0;
String cookie = "";
String header = "";

while (true){
final String line = br.readLine();

if(line.equals("")){
break;
}

// Header info
if(line.startsWith(contentLength.getHeader())){
requestContentLength = Integer.parseInt(line.split(": ")[1]);
}

if(line.startsWith(EnumHeader.cookie.getHeader())){
cookie = line.split(": ")[1];
}
}
String body = IOUtils.readData(br, requestContentLength);

return new HttpRequest(startLine, header, body, cookie);
}
public String getCookie(){
return cookie;
}
public String getURL(){
return startLine.split(" ")[1];
}
public String getMethod(){
return startLine.split(" ")[0];
}
public String getBody(){
return body;
}
}
67 changes: 67 additions & 0 deletions src/main/java/http/util/HttpResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package http.util;

import webserver.RequestHandler;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

import static enumclass.EnumHeader.*;

public class HttpResponse {
DataOutputStream dos;
byte[] body = new byte[0];
private static final Logger log = Logger.getLogger(RequestHandler.class.getName());

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

public void forward(String path, String type) throws IOException {
body = Files.readAllBytes(Paths.get(path));
response200Header(body.length, type);
responseBody(body);
}

public void redirect(String path, Boolean cookie) throws IOException{
response302Header(path, cookie);
}

private void response302Header(String path, Boolean cookie) {
try {
dos.writeBytes("HTTP/1.1 302 Redirect \r\n");
dos.writeBytes(location.getHeader() + ": " + path + "\r\n");
if(cookie){
log.warning("true");
dos.writeBytes(setCookie.getHeader() + ": logined=true \r\n");
}
dos.writeBytes("\r\n");
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}

private void response200Header(int lengthOfBodyContent, String type) {
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
dos.writeBytes(contentType.getHeader() + ": "+ type + ";charset=utf-8\r\n");
dos.writeBytes(contentLength.getHeader() + ": " + lengthOfBodyContent + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}

private void responseBody(byte[] body) {
try {
dos.write(body, 0, body.length);
dos.flush();
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}
}
Loading