{"id":5592,"date":"2026-05-07T21:27:00","date_gmt":"2026-05-07T12:27:00","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5592"},"modified":"2026-05-07T21:33:02","modified_gmt":"2026-05-07T12:33:02","slug":"miniwebserver-6","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/07\/miniwebserver-6\/","title":{"rendered":"[Websersver]miniWebserver-(6)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc8fc\uc694\ubcc0\uacbd \uc0ac\ud56d \/ Major changes<\/p>\n\n\n\n<p>\u2714\ufe0f \ucf54\ub4dc\uc591\uc774 \ub9ce\uc544\uc838\uc11c  \ubc14\ub010 \ubd80\ubd84\ub9cc \uc124\uba85\ud569\ub2c8\ub2e4.<br>Since the amount of code has increased, I will only explain the parts that have changed.<\/p>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c\uc744 cmake\ub85c \ubcc0\uacbd<br>Change compilation to cmake<\/p>\n\n\n\n<p>&#8212; g++ \ub85c \ucef4\ud30c\uc77c \ud558\ub294 \ubc29\uc2dd\uc5d0\uc11c cmake\ub97c \ud1b5\ud574\uc11c \ucef4\ud30c\uc77c \ud558\ub3c4\ub85d \ubcc0\uacbd\ud588\uc2b5\ub2c8\ub2e4.<br>I changed the compilation method from using g++ to using cmake.<\/p>\n\n\n\n<p>&#8212; \ucef4\ud30c\uc77c \ud574\uc57c\ud558\ub294 \ud30c\uc77c\uc758 \uc591\uc774 \ub9ce\uace0 \uc678\ubd80\ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc0ac\uc6a9\ud574\uc57c\ud55c\ub2e4\uba74 cmake\uac00 \ub354 \ud6a8\uc728\uc801\uc785\ub2c8\ub2e4.<br>If you have a large amount of files to compile and need to use external libraries, CMake is more efficient.<\/p>\n\n\n\n<p>&#8212; cmake\ub97c \uba3c\uc800 \uc124\uce58\ud569\ub2c8\ub2e4.(MacOS)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install cmake<\/code><\/pre>\n\n\n\n<p>&#8212; CMakeLists.txt \ud30c\uc77c\uc744 \ub9cc\ub4e4\uace0 \uc544\ub798\uc758 \ub0b4\uc6a9\uc744 \uc791\uc131\ud569\ub2c8\ub2e4.<br>Create a CMakeLists.txt file and write the following content.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># cmake\ucd5c\uc18c\ubc84\uc804\uc9c0\uc815 \/ Specify minimum CMake version\ncmake_minimum_required(VERSION 3.10)\n\n# \ud504\ub85c\uc81d\ud2b8 \uc774\ub984\uacfc \ubc84\uc804 \uc124\uc815 \/ Set project name and version\nproject(mini_webserver_6 VERSION 1.0)\n\n# C++ \ud45c\uc900 \ubc84\uc804 \uc9c0\uc815(c++20) \/ C++ Standard Version Specification (C++20)\nset(CMAKE_CXX_STANDARD 20)\nset(CMAKE_CXX_STANDARD_REQUIRED True)\n\n# \uc2e4\ud589 \ud30c\uc77c \uc774\ub984, \uc18c\uc2a4 \ud30c\uc77c \/ Executable file name, source file\nadd_executable(main\n    Main.cpp\n    Router.cpp\n    Server.cpp\n)<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ud5e4\ub354\uc640 \ubc14\ub514 \ub9ac\ud134 \uae30\ub2a5 \ucd94\uac00<br>Added header and body return functionality<\/p>\n\n\n\n<p>&#8212; server.cpp\uc5d0\uc11c \ucc98\ub9ac\ud558\ub358 \ud5e4\ub354\ub97c main.cpp\uc5d0\uc11c body\uc640 header\ub97c \uac19\uc774 \ub118\uaca8 \uc90d\ub2c8\ub2e4.<br>The header that was handled in server.cpp is passed to main.cpp along with the body.<\/p>\n\n\n\n<p>&#8212; main.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    my_server.set_handler(&#91;&amp;](const std::string&amp; req) {\n\n        \/\/ \ub77c\uc6b0\ud130\ub85c\ubd80\ud130 \ub9c8\uc784 \ud0c0\uc785\uacfc \ubcf8\ubb38\uc744 \ubc1b\uc74c\n        \/\/ Receive MIME type and body from router\n        auto &#91;mime, body] = my_router.router(req);\n\n        \/\/ HTTP \uc751\ub2f5 \ud5e4\ub354 \uc0dd\uc131\n        \/\/ Generate HTTP response headers\n        std::string response = \"HTTP\/1.1 200 OK\\r\\n\";\n        response += \"Content-Type: \" + mime + \"\\r\\n\";\n        response += \"Content-Length: \" + std::to_string(body.size()) + \"\\r\\n\";\n        response += \"Connection: close\\r\\n\";\n        response += \"\\r\\n\"; \/\/ \ud5e4\ub354\uc640 \ubcf8\ubb38 \uad6c\ubd84\uc790 \/ Header and body separator\n\n        \/\/ \ubcf8\ubb38 \ucd94\uac00 \/ Add body\n        response += body;\n\n        \/\/ \ucd5c\uc885 \uc751\ub2f5 \ubc18\ud658 \/ Return final response\n        return response;\n    });<\/code><\/pre>\n\n\n\n<p>&#8212; server.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nvoid Server::handle_client(int client_socket) {\n\n \/\/ ... \uc911\ub7b5 \/ Omitted ...\n\n        \/\/ HTTP \ud3ec\ub9f7 \uc751\ub2f5\n        \/\/ std::string response = \"HTTP\/1.1 200 OK\\r\\n\"\n        \/\/                        \"Content-Type: text\/html; charset=utf-8\\r\\n\"\n        \/\/                        \"Content-Length: \" + std::to_string(html_content.size()) + \"\\r\\n\"\n        \/\/                        \"\\r\\n\" + html_content;\n        \/\/\n        \/\/ main.cpp\uc5d0\uc11c \ub118\uc5b4\uc628 \ud5e4\ub354 \uc801\uc6a9\u2757\ufe0f\n        \/\/ Apply header passed from main.cpp\n        <strong>std::string response = html_content;<\/strong>\n\n\/\/ ... \uc911\ub7b5 \/ Omitted ...\n\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc815\uc801\ud30c\uc77c \uc11c\ube59 \uae30\ub2a5 \ucd94\uac00<br>Added static file serving feature<\/p>\n\n\n\n<p>&#8212; public\ub514\ub809\ud1a0\ub9ac \uc548\uc5d0 html \ud30c\uc77c\uc744 \uc0ac\uc6a9 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. <br>You can use HTML files inside the public directory.<\/p>\n\n\n\n<p>&#8212; Router.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::pair&lt;std::string, std::string> Router::router(const std::string&amp; request) {\n\n\/\/ ... \uc911\ub7b5 \/ omitted ...\n\n   <strong> std::string doc_root = \"..\/public\";<\/strong>\n    std::string full_path = doc_root + path;\n\n    \/\/ \uacbd\ub85c\uac00 \/ \uc774\uba74 index.html\ub85c \ub9e4\ud551\n    \/\/ If the path is \/, map to index.html\n\n    if (path == \"\/\") {\n        full_path = doc_root + \"\/index.html\";\n    }else if(path == \"\/test1\"){\n        full_path = doc_root + \"\/test1.html\";\n    }else if(path == \"\/test2\"){\n        full_path = doc_root + \"\/test2.html\";\n    }else{\n        full_path = doc_root + \"\/index.html\";\n    }\n    if (!std::filesystem::exists(full_path)) {\n        return {\"text\/html\", \"&lt;h1 style='color:red;'>404 Not Found&lt;\/h1>\"};\n    }\n\/\/ ... \uc911\ub7b5 \/ omitted ...\n\n    \/\/ \ud30c\uc77c \ud655\uc7a5\uc790\uc5d0 \ub530\ub978 \ub9c8\uc784\ud0c0\uc785 \uc9c0\uc815\n    \/\/ Specify MIME type based on file extension\n    std::string mime_type = \"text\/html; charset=UTF-8\";\n    if (full_path.ends_with(\".css\")) mime_type = \"text\/css\";\n    else if (full_path.ends_with(\".js\")) mime_type = \"application\/javascript\";\n    else if (full_path.ends_with(\".json\")) mime_type = \"application\/json\";\n    else if (full_path.ends_with(\".png\")) mime_type = \"image\/png\";\n    else if (full_path.ends_with(\".jpg\")) mime_type = \"image\/jpg\";\n\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ube4c\ub4dc \/ build<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MacBookAir build % cd ..  \nMacBookAir mini_webserver_6 % cd build\nMacBookAir build % cmake ..\n-- Configuring done (0.1s)\n-- Generating done (0.0s)\n-- Build files have been written to: \/Users\/...\/mini_webserver_6\/build\nMacBookAir build % make\n&#91; 25%] Building CXX object CMakeFiles\/main.dir\/Main.cpp.o\n&#91; 50%] Building CXX object CMakeFiles\/main.dir\/Router.cpp.o\n&#91; 75%] Building CXX object CMakeFiles\/main.dir\/Server.cpp.o\n&#91;100%] Linking CXX executable main\n&#91;100%] Built target main\nMacBookAir build % \n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc2e4\ud589 \/ Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MacBookAir build % .\/maiin<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ube0c\ub77c\uc6b0\uc800 \uc811\uc18d \/ Access Browser<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;localhost:5080<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc8fc\uc694\ubcc0\uacbd \uc0ac\ud56d \/ Major changes \u2714\ufe0f \ucf54\ub4dc\uc591\uc774 \ub9ce\uc544\uc838\uc11c \ubc14\ub010 \ubd80\ubd84\ub9cc \uc124\uba85\ud569\ub2c8\ub2e4.Since the amount of code has increased, I will only explain the parts that have changed. \u2714\ufe0f \ucef4\ud30c\uc77c\uc744 cmake\ub85c \ubcc0\uacbdChange compilation to cmake &#8212; g++ \ub85c \ucef4\ud30c\uc77c \ud558\ub294 \ubc29\uc2dd\uc5d0\uc11c cmake\ub97c \ud1b5\ud574\uc11c \ucef4\ud30c\uc77c \ud558\ub3c4\ub85d \ubcc0\uacbd\ud588\uc2b5\ub2c8\ub2e4.I changed the compilation method from using g++ to using cmake. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5592","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5592"}],"version-history":[{"count":5,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5592\/revisions"}],"predecessor-version":[{"id":5597,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5592\/revisions\/5597"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}