Skip to content

Commit

Permalink
feat(RequestHandler) : POST와 GET 요청에 대해 동적으로 응답할 수 있도록 핸들러 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
mjj111 committed May 14, 2024
1 parent d41ed3a commit b2e7dba
Showing 1 changed file with 69 additions and 8 deletions.
77 changes: 69 additions & 8 deletions src/main/java/RequestHandler.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import model.HttpRequestMethod;
import model.HttpRequestUtils;
import model.IOUtils;
import model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class RequestHandler extends Thread{
private static final Logger log = LoggerFactory.getLogger(RequestHandler.class);
private Socket connect;

public RequestHandler(Socket connect) {
public RequestHandler(final Socket connect) {
this.connect = connect;
}

Expand All @@ -18,15 +23,71 @@ public void run() {

try(InputStream in = connect.getInputStream();
OutputStream out = connect.getOutputStream()) {

BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = br.readLine();
log.info("request Line : {}", line);

if(line == null) {
return;
}

String[] tokens = line.split(" ");

int contentLength = 0;
while(!line.equals("")) {
log.debug("header : {}", line);
line = br.readLine();
if(line.contains("Content-Length")) {
contentLength = getContentLength(line);
}
}

String method = tokens[0];
String url = tokens[1];
DataOutputStream dos = new DataOutputStream(out);
byte[] body = "안녕하세요!".getBytes(StandardCharsets.UTF_8);
response200Header(dos, body.length);
responseBody(dos, body);

//POST
if(HttpRequestMethod.POST.equals(method) && url.equals("/users/create")) {
String requestBody = IOUtils.readData(br, contentLength);
Map<String, String> params = HttpRequestUtils.parseQueryString(requestBody);

User user = new User(params.get("userId"), params.get("password"), params.get("name"), params.get("email"));
String response = user.toString();

byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
response200Header(dos, responseBytes.length);
responseBody(dos, responseBytes);
}

//GET
else if(HttpRequestMethod.GET.equals(method) && url.startsWith("/users")) {
int index = url.indexOf("?");
String query = url.substring(index + 1);

Map<String, String> params = HttpRequestUtils.parseQueryString(query);

int wannaSize = Integer.parseInt(params.get("size"));
String response = wannaSize + "만큼의 유저 정보를 읽기 원합니다.";

byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
response200Header(dos, responseBytes.length);
responseBody(dos, responseBytes);
}

else {
byte[] body = "안녕하세요!".getBytes(StandardCharsets.UTF_8);
response200Header(dos, body.length);
responseBody(dos, body);
}
} catch (IOException e) {
log.error(e.getMessage());
}
}


private int getContentLength(final String line) {
String[] headerTokens = line.split(":");
return Integer.parseInt(headerTokens[1].trim());
}

private void responseBody(final DataOutputStream dos, final byte[] body) {
Expand All @@ -41,9 +102,9 @@ private void responseBody(final DataOutputStream dos, final byte[] body) {

private void response200Header(final DataOutputStream dos, final int length) {
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" + length + "\r\n");
dos.writeBytes("HTTP/1.1 200 OK\r\n");
dos.writeBytes("Content-Type: text/html; charset=utf-8\r\n");
dos.writeBytes("Content-Length: " + length + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.error(e.getMessage());
Expand Down

0 comments on commit b2e7dba

Please sign in to comment.