๐๐ป ์ฃผ์๋ณ๊ฒฝ ์ฌํญ / Major changes
โ๏ธ ์ฝ๋์์ด ๋ง์์ ธ์ ๋ฐ๋ ๋ถ๋ถ๋ง ์ค๋ช
ํฉ๋๋ค.
Since the amount of code has increased, I will only explain the parts that have changed.
โ๏ธ ์ปดํ์ผ์ cmake๋ก ๋ณ๊ฒฝ
Change compilation to cmake
— g++ ๋ก ์ปดํ์ผ ํ๋ ๋ฐฉ์์์ cmake๋ฅผ ํตํด์ ์ปดํ์ผ ํ๋๋ก ๋ณ๊ฒฝํ์ต๋๋ค.
I changed the compilation method from using g++ to using cmake.
— ์ปดํ์ผ ํด์ผํ๋ ํ์ผ์ ์์ด ๋ง๊ณ ์ธ๋ถ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ผํ๋ค๋ฉด cmake๊ฐ ๋ ํจ์จ์ ์
๋๋ค.
If you have a large amount of files to compile and need to use external libraries, CMake is more efficient.
— cmake๋ฅผ ๋จผ์ ์ค์นํฉ๋๋ค.(MacOS)
brew install cmake
— CMakeLists.txt ํ์ผ์ ๋ง๋ค๊ณ ์๋์ ๋ด์ฉ์ ์์ฑํฉ๋๋ค.
Create a CMakeLists.txt file and write the following content.
# cmake์ต์๋ฒ์ ์ง์ / Specify minimum CMake version
cmake_minimum_required(VERSION 3.10)
# ํ๋ก์ ํธ ์ด๋ฆ๊ณผ ๋ฒ์ ์ค์ / Set project name and version
project(mini_webserver_6 VERSION 1.0)
# C++ ํ์ค ๋ฒ์ ์ง์ (c++20) / C++ Standard Version Specification (C++20)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# ์คํ ํ์ผ ์ด๋ฆ, ์์ค ํ์ผ / Executable file name, source file
add_executable(main
Main.cpp
Router.cpp
Server.cpp
)
โ๏ธ ํค๋์ ๋ฐ๋ ๋ฆฌํด ๊ธฐ๋ฅ ์ถ๊ฐ
Added header and body return functionality
— server.cpp์์ ์ฒ๋ฆฌํ๋ ํค๋๋ฅผ main.cpp์์ body์ header๋ฅผ ๊ฐ์ด ๋๊ฒจ ์ค๋๋ค.
The header that was handled in server.cpp is passed to main.cpp along with the body.
— main.cpp
my_server.set_handler([&](const std::string& req) {
// ๋ผ์ฐํฐ๋ก๋ถํฐ ๋ง์ ํ์
๊ณผ ๋ณธ๋ฌธ์ ๋ฐ์
// Receive MIME type and body from router
auto [mime, body] = my_router.router(req);
// 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;
});
— server.cpp
void Server::handle_client(int client_socket) {
// ... ์ค๋ต / Omitted ...
// HTTP ํฌ๋งท ์๋ต
// std::string response = "HTTP/1.1 200 OK\r\n"
// "Content-Type: text/html; charset=utf-8\r\n"
// "Content-Length: " + std::to_string(html_content.size()) + "\r\n"
// "\r\n" + html_content;
//
// main.cpp์์ ๋์ด์จ ํค๋ ์ ์ฉโ๏ธ
// Apply header passed from main.cpp
std::string response = html_content;
// ... ์ค๋ต / Omitted ...
}
โ๏ธ ์ ์ ํ์ผ ์๋น ๊ธฐ๋ฅ ์ถ๊ฐ
Added static file serving feature
— public๋๋ ํ ๋ฆฌ ์์ html ํ์ผ์ ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
You can use HTML files inside the public directory.
— Router.cpp
std::pair<std::string, std::string> Router::router(const std::string& request) {
// ... ์ค๋ต / omitted ...
std::string doc_root = "../public";
std::string full_path = doc_root + path;
// ๊ฒฝ๋ก๊ฐ / ์ด๋ฉด index.html๋ก ๋งคํ
// If the path is /, map to index.html
if (path == "/") {
full_path = doc_root + "/index.html";
}else if(path == "/test1"){
full_path = doc_root + "/test1.html";
}else if(path == "/test2"){
full_path = doc_root + "/test2.html";
}else{
full_path = doc_root + "/index.html";
}
if (!std::filesystem::exists(full_path)) {
return {"text/html", "<h1 style='color:red;'>404 Not Found</h1>"};
}
// ... ์ค๋ต / omitted ...
// ํ์ผ ํ์ฅ์์ ๋ฐ๋ฅธ ๋ง์ํ์
์ง์
// Specify MIME type based on file extension
std::string mime_type = "text/html; charset=UTF-8";
if (full_path.ends_with(".css")) mime_type = "text/css";
else if (full_path.ends_with(".js")) mime_type = "application/javascript";
else if (full_path.ends_with(".json")) mime_type = "application/json";
else if (full_path.ends_with(".png")) mime_type = "image/png";
else if (full_path.ends_with(".jpg")) mime_type = "image/jpg";
}
โ๏ธ ๋น๋ / build
MacBookAir build % cd ..
MacBookAir mini_webserver_6 % cd build
MacBookAir build % cmake ..
-- Configuring done (0.1s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/.../mini_webserver_6/build
MacBookAir build % make
[ 25%] Building CXX object CMakeFiles/main.dir/Main.cpp.o
[ 50%] Building CXX object CMakeFiles/main.dir/Router.cpp.o
[ 75%] Building CXX object CMakeFiles/main.dir/Server.cpp.o
[100%] Linking CXX executable main
[100%] Built target main
MacBookAir build %
โ๏ธ ์คํ / Run
MacBookAir build % ./maiin
โ๏ธ ๋ธ๋ผ์ฐ์ ์ ์ / Access Browser
http://localhost:5080