👉🏻 주요변경사항은 다음과 같습니다.
The major changes are as follows.
✔️ GET,POST,PUT,DELETE,FETCH 메소드를 사용 할 수 있습니다.
You can use GET, POST, PUT, DELETE, and FETCH methods.
✔️ 주요 변경 사항은 기존의 함수에서 method 부분이 추가되었습니다.
The main change is that a method part has been added to the existing function.
✔️ 테스트는 GET,POST만 테스트 했습니다.
Only GET and POST were tested.
✔️ 메소드 추출하는 부분은 아래 부분입니다.
The part for extracting the method is the section below.
std::istringstream req_stream(req);
std::string method, path, http_version;
req_stream >> method >> path >> http_version;
✔️ 다른 변경사항은 깃허브의 코드에서 Router.h와 Router.cpp파일을 참조하세요
For other changes, please refer to the Router.h and Router.cpp files in the code on GitHub.
https://github.com/gideonslife01/flm-Cpp
✔️ ❗️ 표시된 부분이 변경사항입니다.
The parts marked with ❗️ are the changes.
✔️ main.cpp
// -- 라우트 설정 변경 / Change route settings -- //❗️
Router server1_router("../public1");
server1_router.add_route("GET","/test1", "test1.html");
server1_router.add_route("POST","/test2", "test2.html");
Router server2_router("../public2");
server2_router.add_route("GET","/test1", "test1.html");
server2_router.add_route("POST","/test2", "test2.html");
// --- 서버 1 설정 (5080 포트) --- // ❗️
// --- Server 1 Configuration (Port 5080) --- //
Server server1("127.0.0.1", 5080);
server1.set_handler([&](const std::string& req) {
// method 검사
std::istringstream req_stream(req);
std::string method, path, http_version;
req_stream >> method >> path >> http_version;
std::pair<std::string, std::string> result;
if (method == "GET" || method == "POST" || method == "PUT" || method == "DELETE" || method == "FETCH") {
result = server1_router.router(method, path);
}
else {
return std::string(
"HTTP/1.1 405 Method Not Allowed\r\n"
"Allow: GET, POST, PUT, DELETE, FETCH\r\n"
"Connection: close\r\n\r\n"
"Method Not Allowed"
);
}
// 라우터로부터 마임 타입과 본문을 받음
// Receive MIME type and body from router
//auto [mime, body] = server1_router.router(req); // 이전방식❗️
const auto& [mime, body] = result;
// HTTP 응답 헤더 생성
// Generate HTTP response headers
std::string response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: " + mime + "\r\n";
response += "Content-Length: " + std::to_string(body.size()) + "\r\n";
response += "Connection: close\r\n";
response += "\r\n"; // 헤더와 본문 구분자 / Header and body separator
// 본문 추가 / Add body
response += body;
// 최종 응답 반환 / Return final response
return response;
});
👉🏻 빌드 / Build
cd build
cmake ..
make
👉🏻실행 / Run
MacBookAir build % ./main
--- Starting Server Application ---
Server listening on port 5080...
Server listening on port 6080...
CMD>
👉🏻 브라우저 접속 / Access Browser
✔️ 브라우저에는 5080,6080 포트로 접속합니다.
Connect to ports 5080 and 6080 in the browser.
http://localhost:5080
http://localhost:6080
✔️ POST는 아래의 test1.htm에서 테스트 할 수 있습니다.
You can test the POST in test1.htm below.
— 만약에 폼으로 데이터를 전송하지않고 링크로 test2.html에 접근하면 404오류(404 Not Found)가 발생합니다.
If you access test2.html via a link without submitting data through the form, a 404 error (404 Not Found) occurs.
http://localhost:5080/test1.html
http://localhost:6080/test1.html