#19 was : HttpRequest, HttpResponse
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
package org.example;
|
package org.example;
|
||||||
|
|
||||||
|
import org.example.calculator.domain.Calculator;
|
||||||
|
import org.example.calculator.domain.PositiveNumber;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class CustomWebApplicationServer {
|
public class CustomWebApplicationServer {
|
||||||
|
|
||||||
@@ -26,6 +29,32 @@ public class CustomWebApplicationServer {
|
|||||||
|
|
||||||
while ((clientSocket = serverSocket.accept()) != null) {
|
while ((clientSocket = serverSocket.accept()) != null) {
|
||||||
logger.info("[CustomWebApplicationServer] client connected!");
|
logger.info("[CustomWebApplicationServer] client connected!");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Step1 - 사용자 요청을 메인 Thread 가 처리하도록 한다.
|
||||||
|
*/
|
||||||
|
|
||||||
|
try (InputStream in = clientSocket.getInputStream(); OutputStream out = clientSocket.getOutputStream()) {
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
|
||||||
|
DataOutputStream dos = new DataOutputStream(out);
|
||||||
|
|
||||||
|
HttpRequest httpRequest = new HttpRequest(br);
|
||||||
|
|
||||||
|
if (httpRequest.isGetRequest() && httpRequest.matchPath("/calculate")) {
|
||||||
|
QueryStrings queryStrings = httpRequest.getQueryStrings();
|
||||||
|
|
||||||
|
int operand1 = Integer.parseInt(queryStrings.getValue("operand1"));
|
||||||
|
String operator = queryStrings.getValue("operator");
|
||||||
|
int operand2 = Integer.parseInt(queryStrings.getValue("operand2"));
|
||||||
|
|
||||||
|
int result = Calculator.calculate(new PositiveNumber(operand1), operator, new PositiveNumber(operand2));
|
||||||
|
byte[] body = String.valueOf(result).getBytes();
|
||||||
|
|
||||||
|
HttpResponse response = new HttpResponse(dos);
|
||||||
|
response.response200Header("application/json", body.length);
|
||||||
|
response.responseBody(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class HttpRequest {
|
||||||
|
|
||||||
|
private final RequestLine requestLine;
|
||||||
|
|
||||||
|
public HttpRequest(BufferedReader br) throws IOException {
|
||||||
|
this.requestLine = new RequestLine(br.readLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueryStrings getQueryStrings() {
|
||||||
|
return requestLine.getQueryStrings();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isGetRequest() {
|
||||||
|
return requestLine.isGetRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean matchPath(String requestPath) {
|
||||||
|
return requestLine.matchPath(requestPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class HttpResponse {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(HttpResponse.class);
|
||||||
|
|
||||||
|
private final DataOutputStream dos;
|
||||||
|
|
||||||
|
public HttpResponse(DataOutputStream dos) {
|
||||||
|
this.dos = dos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void response200Header(String contentType, int lengthOfBodyContent) {
|
||||||
|
try {
|
||||||
|
dos.writeBytes("HTTP/1.1 200 OK \r\n");
|
||||||
|
dos.writeBytes("Content-Type: " + contentType + ";charset=utf-8\r\n");
|
||||||
|
dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n");
|
||||||
|
dos.writeBytes("\r\n");
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void responseBody(byte[] body) {
|
||||||
|
try {
|
||||||
|
dos.write(body, 0, body.length);
|
||||||
|
dos.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1 @@
|
|||||||
GET http://localhost:8080
|
GET http://localhost:8080/calculate?operand1=10&operator=-&operand2=20
|
||||||
Reference in New Issue
Block a user