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

Ch3/require 2 #54

Open
wants to merge 3 commits into
base: master
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 src/main/java/db/DataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public static User findUserById(String userId) {
public static Collection<User> findAll() {
return users.values();
}

}
47 changes: 45 additions & 2 deletions src/main/java/util/HttpRequestUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package util;

import model.User;
import db.DataBase;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -8,6 +14,44 @@
import com.google.common.collect.Maps;

public class HttpRequestUtils {

public static String headerRequest(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();

if (line == null) { return null; }

String url = line.split(" ")[1];
System.out.println(line);

//if (!url.endsWith(".html")) {return null;}

/*while (!"".equals(line)) {
if (line == null) { return null; }
System.out.println(line);
line = reader.readLine();
}*/
return url;
}

public static String parseUrl(String url) {
int index = url.indexOf("?");
String requestPath = url.substring(0, index);
return url.substring(index+1);

}

public static void signUpRequest(String url) {
String params = parseUrl(url);
Map<String, String> map = parseQueryString(params);
User user = new User(
map.get("userId"),
map.get("password"),
map.get("name"),
map.get("email")
);
}

/**
* @param queryString은
* URL에서 ? 이후에 전달되는 field1=value1&field2=value2 형식임
Expand All @@ -18,8 +62,7 @@ public static Map<String, String> parseQueryString(String queryString) {
}

/**
* @param 쿠키
* 값은 name1=value1; name2=value2 형식임
* @param 쿠키 값은 name1=value1; name2=value2 형식임
* @return
*/
public static Map<String, String> parseCookies(String cookies) {
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/webserver/RequestHandler.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package webserver;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.net.Socket;
import java.nio.file.Files;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.HttpRequestUtils;
import db.DataBase;

public class RequestHandler extends Thread {
private static final Logger log = LoggerFactory.getLogger(RequestHandler.class);
Expand All @@ -23,16 +23,26 @@ public void run() {
connection.getPort());

try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) {
// TODO 사용자 요청에 대한 처리는 이 곳에 구현하면 된다.

DataOutputStream dos = new DataOutputStream(out);
byte[] body = "Hello World".getBytes();
String url = HttpRequestUtils.headerRequest(in);

if (url.startsWith("/user/create")) {
HttpRequestUtils.signUpRequest(url);
}

byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath());
response200Header(dos, body.length);
responseBody(dos, body);


} catch (IOException e) {
log.error(e.getMessage());
}
}

// private void stringSplit

private void response200Header(DataOutputStream dos, int lengthOfBodyContent) {
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/util/HttpRequestUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,49 @@
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

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

import model.User;
import org.junit.Test;

import util.HttpRequestUtils.Pair;
import db.DataBase;

public class HttpRequestUtilsTest {

@Test
public void headerRequest() throws IOException {
String httpRequest = "GET /path/resource HTTP/1.1\n" +
"Host: localhost\n" +
"Accept: */*\n" +
"\n";

InputStream inputStream = new ByteArrayInputStream(httpRequest.getBytes());
String url = HttpRequestUtils.headerRequest(inputStream);

assertEquals("/path/resource", url);
}

@Test
public void parseUrl() {
String url = "/?data=234";
String params = HttpRequestUtils.parseUrl(url);
assertEquals("data=234", params);
}

@Test
public void signUpRequest() {
String url = "/user/create?userId=user&password=pass&name=user&email=user%40naver.com";
Map<String, String> map = HttpRequestUtils.parseQueryString(url);
DataBase.addUser(new User(map.get("userId"), map.get("password"),
map.get("name"), map.get("email")));

assertEquals(1, DataBase.findAll().size());
}

@Test
public void parseQueryString() {
String queryString = "userId=javajigi";
Expand Down