[Webserver]miniWebserver-(11)

๐Ÿ‘‰๐Ÿป ์ฃผ์š” ๋ณ€๊ฒฝ์‚ฌํ•ญ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
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

Leave a Reply