๐๐ป ์ฃผ์๋ณ๊ฒฝ์ฌํญ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
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