๐๐ป ์ฃผ์ ๋ณ๊ฒฝ์ฌํญ์ ์๋์ ๊ฐ์ต๋๋ค.
The major changes are as follows.
โ๏ธ โ๏ธ ํ์๋ ๋ถ๋ถ์ด ๋ณ๊ฒฝ์ฌํญ์
๋๋ค.
The parts marked with โ๏ธ are the changes.
โ๏ธ ๊ฐ์ํธ์คํธ ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ต๋๋ค.
Added virtual host functionality.
โ๏ธ HttpsServer.cpp์ ์ค๋ ๋ ์ ๋ฆฌ์๊ฐ์ ์ข ๋ ์ถ๊ฐํ์ต๋๋ค.
I added some more thread cleanup time to HttpsServer.cpp.
โ๏ธ ์๋ฒ์ข
๋ฃ ๋ถ๋ถ์ try-catch ๋ถ๋ถ์ด ์ถ๊ฐ๋์์ต๋๋ค.
A try-catch block has been added to the server termination section.
๐๐ป์ฝ๋ / Code
โ๏ธ๊ฐ์ํธ์คํธ / Virtual Host
— main.cpp
// virtusl host ๋ผ์ฐํฐ / virtual host routerโ๏ธ
Router virtual_router("../public_virtual");
virtual_router.add_route("GET","/test1", "test1.html");
virtual_router.add_route("POST","/test2", "test2.html");
// virtual host http(5080) server host mapโ๏ธ
static std::map<std::string, Router*> vhost_routers = {
{"yourmainhost.domain.org", &server1_router},
{"yourvirtualhost.domain.org", &virtual_router},
{"localhost", &server1_router},
{"127.0.0.1", &server1_router},
{"192.168.0.100", &server1_router}
};
// virtual host https(6443) server host mapโ๏ธ
static std::map<std::string, Router*> vhost_https_routers = {
{"yourmainhost.domain.org", &server2_router},
{"yourvirtualhost.domain.org"", &virtual_router},
{"localhost", &server2_router},
{"127.0.0.1", &server2_router},
{"192.168.0.100", &server2_router}
};
std::pair<std::string, std::string> result;
// ์์ฒญ ํค๋ ์ ์ฒด ์ฝ๊ธฐ ๋ฐ Host ํค๋ ํ์ฑโ๏ธ
// Read all request headers and parse Host headers
std::string line;
std::string host;
// ํค๋์์ ๊ฐํ๋ฌธ์ ์ ๊ฑฐ์ฉ โ๏ธ
// For removing newline characters from headers
std::string dummy;
std::getline(req_stream, dummy);
// ์์ฒญ ํค๋ ํ์ฑ(req stream์ ๊ฐ์ ์ฝ์ด์ line ๋ณ์์ ํ ์ค์ฉ ๋ณต์ฌ)โ๏ธ
// Parse request headers (read values โโfrom the req stream and copy them line by line to the line variable)
while (std::getline(req_stream, line) && line != "\r" && !line.empty()) {
// "Host:" ์ฐพ๊ธฐ (๋์๋ฌธ์ ๋ฌด๊ดํ๊ฒ ๊ฒ์ฌํ๋ ๊ฒ์ด ์์ ํจ)
if (line.compare(0, 5, "Host:") == 0 || line.compare(0, 5, "host:") == 0) {
host = line.substr(5);
// ๊ณต๋ฐฑ ๋ฐ \r ์ ๊ฑฐ
host.erase(0, host.find_first_not_of(" \t"));
size_t last = host.find_last_not_of(" \t\r\n");
if (last != std::string::npos) host.erase(last + 1);
// std::cout << "[DEBUG] https Host Found: " << host << std::endl << std::flush;
}
}
server2.set_handler([&](const std::string& req) {
... ์ค๋ต / Skipping the middle ...
// ํฌํธ ๋ฒํธ ์ ๊ฑฐํ์ฌ ๋๋ฉ์ธ๋ช
๋ง ์ถ์ถโ๏ธ
// Remove port number to extract only the domain name
std::string host_normalized = host;
size_t pos = host.find(':');
if (pos != std::string::npos) {
host_normalized = host.substr(0, pos);
}
// ๋ผ์ฐํฐ ์ ํ: ํด๋น ํธ์คํธ์ ๋ผ์ฐํฐ ์ฐพ๊ธฐ, ์์ผ๋ฉด ๊ธฐ๋ณธ ๋ผ์ฐํฐโ๏ธ
// Select Router: Find the router for the host, if none exists, use the default router
Router* router_ptr = &server2_router; // ๊ธฐ๋ณธ๊ฐ / default value
auto it = vhost_https_routers.find(host_normalized);
if (it != vhost_https_routers.end()) {
router_ptr = it->second;
}
if (method == "GET" || method == "POST" || method == "PUT" || method == "DELETE" || method == "FETCH") {
// ์ด์ ์ฝ๋ / previous code
//result = server2_router.router(method, path);
//
// virtual host ์ ์ฉ / apply virtual hostโ๏ธ
result = router_ptr->router(method, path);
}
... ์ค๋ต / Skipping the middle ...
}
โ๏ธ ์ค๋ ๋ ์ ๋ฆฌ์๊ฐ ์ถ๊ฐ / Add thread cleanup time
// ์ค๋ ๋ ์ ๋ฆฌ์๊ฐ ์ถ๊ฐโ๏ธ
// Add thread cleanup time
std::this_thread::sleep_for(std::chrono::milliseconds(300));
๐๐ป ๋น๋ / Build
cd build
cmake ..
make
๐๐ป ์คํ / Run
./main
๐๐ป ๋ธ๋ผ์ฐ์ ์ ์ / Access Browser
http://yourmainhost.domain.org:5080
http://yourvirtualhost.domain.org:5080
https://yourmainhost.domain.org:6444
https://yourvirtualhost.domain.org:6444