[Webserver]miniWebserver-(8)

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

Leave a Reply