{"id":13340473,"url":"https://github.com/willsaibott/LibHTTP","last_synced_at":"2025-03-11T18:31:20.158Z","repository":{"id":56691664,"uuid":"302170679","full_name":"willsaibott/LibHTTP","owner":"willsaibott","description":"LibHTTP that is similar to ExpressJS in usage, but it's built using Boost Beast Framework, and it's written in modern C++17. Please, take a look:","archived":false,"fork":false,"pushed_at":"2021-02-22T20:50:16.000Z","size":1757,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-24T06:27:48.719Z","etag":null,"topics":["boost","boost-asio","boost-beast","expressjs","http","https-server","httpserver","qmake","visual-studio","websocket","websocket-server"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willsaibott.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-07T22:02:24.000Z","updated_at":"2023-01-09T14:56:29.000Z","dependencies_parsed_at":"2022-08-15T23:21:00.301Z","dependency_job_id":null,"html_url":"https://github.com/willsaibott/LibHTTP","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsaibott%2FLibHTTP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsaibott%2FLibHTTP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsaibott%2FLibHTTP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsaibott%2FLibHTTP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willsaibott","download_url":"https://codeload.github.com/willsaibott/LibHTTP/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243090309,"owners_count":20234766,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boost","boost-asio","boost-beast","expressjs","http","https-server","httpserver","qmake","visual-studio","websocket","websocket-server"],"created_at":"2024-07-29T19:23:00.671Z","updated_at":"2025-03-11T18:31:19.707Z","avatar_url":"https://github.com/willsaibott.png","language":"C++","readme":"# Build Status:\n\nMaster:\n\n![Windows Build](https://github.com/willsaibott/LibHTTP/workflows/Windows%20Build/badge.svg?branch=master)\n![Linux_Build](https://github.com/willsaibott/LibHTTP/workflows/Linux_Build/badge.svg?branch=master)\n\nDev:\n\n![Windows Build](https://github.com/willsaibott/LibHTTP/workflows/Windows%20Build/badge.svg?branch=dev)\n![Linux_Build](https://github.com/willsaibott/LibHTTP/workflows/Linux_Build/badge.svg?branch=dev)\n\n# LibHTTP\nLibHTTP is a C++ library similar to NodeJS Express in usage, and is based on boost beast. [In development]\n\n## Main Classes:\n   * **http_server\u003c Router, Listener \u003e:** \n    \n     Base class that represents a HTTP Server that is associted with a boost::asio::io_context\n    \n     To modify how the routes are handled, it's just necessary to specialize the **Router** template argument\n    \n     To modify how the connection should be open, binded and accepted, it's just necessary to specialize the **Listener**  template argument\n     #### Declaration:\n     ```C++\n       template \u003cclass Router   = http_regex_router,\n                 class Listener = http_listener\u003chttp_session\u003chttp_request_handler\u003cRouter\u003e\u003e\u003e\u003e\n       class  http_server\n     ``` \n    \n  * **https_server\u003c Router \u003e:**\n    \n    This class is a specialization of the base class http_server that has a http_listener with a https_session (inherits http_section) associated with.\n    \n    To modify how the routes are handled, it's just necessary to specialize the **Router** template argument\n    #### Declaration:\n    ```C++\n      template \u003cclass Router\u003e\n      using https_server =\n          http_server\u003cRouter, http_listener\u003chttps_session\u003chttp_request_handler\u003cRouter\u003e\u003e\u003e\u003e;\n    ```\n    \n  * **websocket_server\u003c MessageHandler \u003e:**\n    \n    This class is a specialization of the base class http_server that has a http_listener with a websocket_session associated with.\n    \n    To modify how the routes are handled, it's just necessary to specialize the **MessageHandler** template argument.\n    \n    **Note**: Currently, the behaviour of the websocket_server is reactive, it calls the MessageHandler when receives a message. It'll be modifiable in the future.\n    #### Declaration:\n    ```C++\n      template \u003cclass MessageHandler\u003e\n      using websocket_server =\n          http_server\u003cMessageHandler, http_listener\u003cwebsocket_section\u003cMessageHandler\u003e, http_error_handler\u003e\u003e;\n    ```\n    \n  * **websocket_ssl_server\u003c MessageHandler \u003e:**\n    \n    This class is a specialization of the base class http_server that has a http_listener with a websocket_ssl_session (inherits websocket_section) associated with.\n    \n    To modify how the routes are handled, it's just necessary to specialize the **MessageHandler** template argument\n    \n    **Note**: Currently, the behaviour of the websocket_ssl_server is reactive, it calls the MessageHandler when receives a message. It'll be modifiable in the future.\n    #### Declaration:\n    ```C++\n      template \u003cclass MessageHandler\u003e\n      using websocket_ssl_server =\n          http_server\u003cMessageHandler, http_listener\u003cwebsocket_ssl_section\u003cMessageHandler\u003e, http_error_handler\u003e\u003e;\n    ```\n  \n\n\n## HTTP Router Usage Example:\n\n### router.h:\n```C++\n#include \u003cLibHttp\\http_server.h\u003e\n\nclass http_custom_router : public http::http_regex_router {\npublic:\n  http_custom_router();\n};\n\n```\n\n### router.cpp:\n```C++\n#include \"router.h\"\n\nhttp_custom_router::\nhttp_custom_router() {\n\n  get(\"/\", [](const auto\u0026 root,\n              const auto\u0026 request,\n              const auto\u0026 matches,\n              auto\u0026       sender)\n  {\n    namespace http = boost::beast::http;\n    http::response\u003chttp::string_body\u003e res{ http::status::ok, request.version() };\n    res.set(http::field::server, BOOST_BEAST_VERSION_STRING);\n    res.set(http::field::content_type, helper::mime_type(\".json\"));\n    res.keep_alive(request.keep_alive());\n    res.body() = \"{ \\\"Root\\\": \\\"reached\\\" }\";\n    res.prepare_payload();\n    sender.async_send(std::move(res));\n\n    boost::ignore_unused(root);\n    boost::ignore_unused(matches);\n    return true;\n  });\n\n  get(\"/print(.*)/?\", [](const auto\u0026 root,\n                         const auto\u0026 request,\n                         const auto\u0026 matches,\n                         auto\u0026       sender)\n  {\n    namespace http = boost::beast::http;\n    http::response\u003chttp::string_body\u003e res{http::status::ok, request.version()};\n    res.set(http::field::server, BOOST_BEAST_VERSION_STRING);\n    res.set(http::field::content_type, \"text/html; charset=utf-8\");\n    res.keep_alive(request.keep_alive());\n    res.body() = \"\u003c!DOCTYPE html\u003e\u003chtml\u003e\"\n                 \"\u003chead\u003e\"\n                   \"\u003ctitle\u003eExpress C++\u003c/title\u003e\"\n                   \"\u003clink rel=\\\"stylesheet\\\" href=\\\"/stylesheets/style.css\\\"\u003e\"\n                 \"\u003c/head\u003e\"\n                 \"\u003cbody\u003e\"\n                   \"\u003ch1\u003eExpress\u003c/h1\u003e\"\n                   \"\u003cp\u003eWelcome to Express C++ using Boost Beast (Asio)\u003c/p\u003e\"\n                   \"\u003cp\u003eParameter: \u003cb\u003e\" + matches[1] + \"\u003c/b\u003e\u003c/p\u003e\"\n                 \"\u003c/body\u003e\"\n                 \"\u003c/html\u003e\";\n    res.prepare_payload();\n    sender.async_send(std::move(res));\n\n    boost::ignore_unused(root);\n    return true;\n  });\n  \n  post(\"/post(.*)\", [](const auto\u0026 root,\n                       const auto\u0026 request,\n                       const auto\u0026 matches,\n                       auto\u0026       sender)\n  {\n    namespace http = boost::beast::http;\n    http::response\u003chttp::string_body\u003e res{http::status::ok, request.version()};\n    res.set(http::field::server, BOOST_BEAST_VERSION_STRING);\n    res.set(http::field::content_type, helper::mime_type(\".json\"));\n    res.keep_alive(request.keep_alive());\n    res.body() = std::string{ \"{ \\\"print\\\": \\\"\" } +\n                 std::string{ matches[static_cast\u003cint\u003e(matches.size() - 1ull)] } +\n                 \"\\\"}\";\n    res.prepare_payload();\n    sender.async_send(std::move(res));\n\n    boost::ignore_unused(root);\n    boost::ignore_unused(matches);\n    return true;\n  });\n}\n\n```\n\n### http_server.cpp:\n```C++\n#include \u003cthread\u003e\n#include \u003cstring\u003e\n#include \u003cfilesystem\u003e\n#include \u003cLibHttp/http_server.h\u003e\n#include \"router.h\"\n\nint main() {\n  const std::string interface_address{ \"127.0.0.1\" };\n  http::http_server\u003chttp_custom_router\u003e\n        http_server { interface_address, 18080, std::filesystem::current_path() };\n        \n  std::thread thread {\n    [\u0026http_server]() {\n      http_server.start(std::thread::hardware_concurrency());\n      if (http_server.last_error()) {\n        std::cerr \u003c\u003c \"Error starting HTTP server: \"\n                  \u003c\u003c http_server.last_error().message()\n                  \u003c\u003c std::endl;\n      }\n    }\n  };\n  std::cout \u003c\u003c \"HTTP server running at http://\" \u003c\u003c interface_address \u003c\u003c \":18080...\" \u003c\u003c  std::endl;\n  std::this_thread::sleep_for(std::chrono::minutes{ 1000 });\n  http_server.stop();\n  thread.join();\n  return 0;\n}\n\n```\n## HTTPS Router Usage Example:\n### https_server.cpp:\n```C++\n#include \u003cthread\u003e\n#include \u003cstring\u003e\n#include \u003cfilesystem\u003e\n#include \u003cLibHttp/http_server.h\u003e\n#include \"router.h\"\n\nint main() {\n  const std::string interface_address{ \"127.0.0.1\" };\n  std::string const cert =\n          \"-----BEGIN CERTIFICATE-----\\n\"\n          \"MIIDaDCCAlCgAwIBAgIJAO8vBu8i8exWMA0GCSqGSIb3DQEBCwUAMEkxCzAJBgNV\\n\"\n          \"BAYTAlVTMQswCQYDVQQIDAJDQTEtMCsGA1UEBwwkTG9zIEFuZ2VsZXNPPUJlYXN0\\n\"\n          \"Q049d3d3LmV4YW1wbGUuY29tMB4XDTE3MDUwMzE4MzkxMloXDTQ0MDkxODE4Mzkx\\n\"\n          \"MlowSTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMS0wKwYDVQQHDCRMb3MgQW5n\\n\"\n          \"ZWxlc089QmVhc3RDTj13d3cuZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUA\\n\"\n          \"A4IBDwAwggEKAoIBAQDJ7BRKFO8fqmsEXw8v9YOVXyrQVsVbjSSGEs4Vzs4cJgcF\\n\"\n          \"xqGitbnLIrOgiJpRAPLy5MNcAXE1strVGfdEf7xMYSZ/4wOrxUyVw/Ltgsft8m7b\\n\"\n          \"Fu8TsCzO6XrxpnVtWk506YZ7ToTa5UjHfBi2+pWTxbpN12UhiZNUcrRsqTFW+6fO\\n\"\n          \"9d7xm5wlaZG8cMdg0cO1bhkz45JSl3wWKIES7t3EfKePZbNlQ5hPy7Pd5JTmdGBp\\n\"\n          \"yY8anC8u4LPbmgW0/U31PH0rRVfGcBbZsAoQw5Tc5dnb6N2GEIbq3ehSfdDHGnrv\\n\"\n          \"enu2tOK9Qx6GEzXh3sekZkxcgh+NlIxCNxu//Dk9AgMBAAGjUzBRMB0GA1UdDgQW\\n\"\n          \"BBTZh0N9Ne1OD7GBGJYz4PNESHuXezAfBgNVHSMEGDAWgBTZh0N9Ne1OD7GBGJYz\\n\"\n          \"4PNESHuXezAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCmTJVT\\n\"\n          \"LH5Cru1vXtzb3N9dyolcVH82xFVwPewArchgq+CEkajOU9bnzCqvhM4CryBb4cUs\\n\"\n          \"gqXWp85hAh55uBOqXb2yyESEleMCJEiVTwm/m26FdONvEGptsiCmF5Gxi0YRtn8N\\n\"\n          \"V+KhrQaAyLrLdPYI7TrwAOisq2I1cD0mt+xgwuv/654Rl3IhOMx+fKWKJ9qLAiaE\\n\"\n          \"fQyshjlPP9mYVxWOxqctUdQ8UnsUKKGEUcVrA08i1OAnVKlPFjKBvk+r7jpsTPcr\\n\"\n          \"9pWXTO9JrYMML7d+XRSZA1n3856OqZDX4403+9FnXCvfcLZLLKTBvwwFgEFGpzjK\\n\"\n          \"UEVbkhd5qstF6qWK\\n\"\n          \"-----END CERTIFICATE-----\\n\";\n\n  std::string const key =\n          \"-----BEGIN PRIVATE KEY-----\\n\"\n          \"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDJ7BRKFO8fqmsE\\n\"\n          \"Xw8v9YOVXyrQVsVbjSSGEs4Vzs4cJgcFxqGitbnLIrOgiJpRAPLy5MNcAXE1strV\\n\"\n          \"GfdEf7xMYSZ/4wOrxUyVw/Ltgsft8m7bFu8TsCzO6XrxpnVtWk506YZ7ToTa5UjH\\n\"\n          \"fBi2+pWTxbpN12UhiZNUcrRsqTFW+6fO9d7xm5wlaZG8cMdg0cO1bhkz45JSl3wW\\n\"\n          \"KIES7t3EfKePZbNlQ5hPy7Pd5JTmdGBpyY8anC8u4LPbmgW0/U31PH0rRVfGcBbZ\\n\"\n          \"sAoQw5Tc5dnb6N2GEIbq3ehSfdDHGnrvenu2tOK9Qx6GEzXh3sekZkxcgh+NlIxC\\n\"\n          \"Nxu//Dk9AgMBAAECggEBAK1gV8uETg4SdfE67f9v/5uyK0DYQH1ro4C7hNiUycTB\\n\"\n          \"oiYDd6YOA4m4MiQVJuuGtRR5+IR3eI1zFRMFSJs4UqYChNwqQGys7CVsKpplQOW+\\n\"\n          \"1BCqkH2HN/Ix5662Dv3mHJemLCKUON77IJKoq0/xuZ04mc9csykox6grFWB3pjXY\\n\"\n          \"OEn9U8pt5KNldWfpfAZ7xu9WfyvthGXlhfwKEetOuHfAQv7FF6s25UIEU6Hmnwp9\\n\"\n          \"VmYp2twfMGdztz/gfFjKOGxf92RG+FMSkyAPq/vhyB7oQWxa+vdBn6BSdsfn27Qs\\n\"\n          \"bTvXrGe4FYcbuw4WkAKTljZX7TUegkXiwFoSps0jegECgYEA7o5AcRTZVUmmSs8W\\n\"\n          \"PUHn89UEuDAMFVk7grG1bg8exLQSpugCykcqXt1WNrqB7x6nB+dbVANWNhSmhgCg\\n\"\n          \"VrV941vbx8ketqZ9YInSbGPWIU/tss3r8Yx2Ct3mQpvpGC6iGHzEc/NHJP8Efvh/\\n\"\n          \"CcUWmLjLGJYYeP5oNu5cncC3fXUCgYEA2LANATm0A6sFVGe3sSLO9un1brA4zlZE\\n\"\n          \"Hjd3KOZnMPt73B426qUOcw5B2wIS8GJsUES0P94pKg83oyzmoUV9vJpJLjHA4qmL\\n\"\n          \"CDAd6CjAmE5ea4dFdZwDDS8F9FntJMdPQJA9vq+JaeS+k7ds3+7oiNe+RUIHR1Sz\\n\"\n          \"VEAKh3Xw66kCgYB7KO/2Mchesu5qku2tZJhHF4QfP5cNcos511uO3bmJ3ln+16uR\\n\"\n          \"GRqz7Vu0V6f7dvzPJM/O2QYqV5D9f9dHzN2YgvU9+QSlUeFK9PyxPv3vJt/WP1//\\n\"\n          \"zf+nbpaRbwLxnCnNsKSQJFpnrE166/pSZfFbmZQpNlyeIuJU8czZGQTifQKBgHXe\\n\"\n          \"/pQGEZhVNab+bHwdFTxXdDzr+1qyrodJYLaM7uFES9InVXQ6qSuJO+WosSi2QXlA\\n\"\n          \"hlSfwwCwGnHXAPYFWSp5Owm34tbpp0mi8wHQ+UNgjhgsE2qwnTBUvgZ3zHpPORtD\\n\"\n          \"23KZBkTmO40bIEyIJ1IZGdWO32q79nkEBTY+v/lRAoGBAI1rbouFYPBrTYQ9kcjt\\n\"\n          \"1yfu4JF5MvO9JrHQ9tOwkqDmNCWx9xWXbgydsn/eFtuUMULWsG3lNjfst/Esb8ch\\n\"\n          \"k5cZd6pdJZa4/vhEwrYYSuEjMCnRb0lUsm7TsHxQrUd6Fi/mUuFU/haC0o0chLq7\\n\"\n          \"pVOUFq5mW8p0zbtfHbjkgxyF\\n\"\n          \"-----END PRIVATE KEY-----\\n\";\n\n  std::string const dh =\n          \"-----BEGIN DH PARAMETERS-----\\n\"\n          \"MIIBCAKCAQEArzQc5mpm0Fs8yahDeySj31JZlwEphUdZ9StM2D8+Fo7TMduGtSi+\\n\"\n          \"/HRWVwHcTFAgrxVdm+dl474mOUqqaz4MpzIb6+6OVfWHbQJmXPepZKyu4LgUPvY/\\n\"\n          \"4q3/iDMjIS0fLOu/bLuObwU5ccZmDgfhmz1GanRlTQOiYRty3FiOATWZBRh6uv4u\\n\"\n          \"tff4A9Bm3V9tLx9S6djq31w31Gl7OQhryodW28kc16t9TvO1BzcV3HjRPwpe701X\\n\"\n          \"oEEZdnZWANkkpR/m/pfgdmGPU66S2sXMHgsliViQWpDCYeehrvFRHEdR9NV+XJfC\\n\"\n          \"QMUk26jPTIVTLfXmmwU0u8vUkpR7LQKkwwIBAg==\\n\"\n          \"-----END DH PARAMETERS-----\\n\";\n          \n  http::https_server\u003chttp_custom_router\u003e\n        https_server { interface_address, 18045, std::filesystem::current_path() };\n        \n  https_server.set_ssl_context(http::ssl_dh_context_creator{}(cert, key, dh, \"test\"));\n  \n  std::thread thread {\n    [\u0026https_server]() {\n      https_server.start(std::thread::hardware_concurrency());\n      if (httsp_server.last_error()) {\n        std::cerr \u003c\u003c \"Error starting HTTPS server: \"\n                  \u003c\u003c https_server.last_error().message()\n                  \u003c\u003c std::endl;\n      }\n    }\n  };\n  \n  std::cout \u003c\u003c \"HTTPS server running at https://\" \u003c\u003c interface_address \u003c\u003c \":18045...\" \u003c\u003c  std::endl;\n  std::this_thread::sleep_for(std::chrono::minutes{ 1000 });\n  https_server.stop();\n  thread.join();\n  return 0;\n}\n\n```\n## WebSocket server Usage Example:\n```C++\n\nint main() {\n  const std::string interface_address{ \"127.0.0.1\" };\n  http::websocket_server\u003chttp::echo_message_handler\u003e\n        websocket_server{ interface_address, 18090, std::filesystem::current_path() };\n        \n  std::thread thread {\n    [\u0026websocket_server]() {\n      websocket_server.start(std::thread::hardware_concurrency());\n      if (websocket_server.last_error()) {\n        std::cerr \u003c\u003c \"Error starting WebSocket server: \"\n                  \u003c\u003c websocket_server.last_error().message()\n                  \u003c\u003c std::endl;\n      }\n    }\n  };\n  \n  std::cout \u003c\u003c \"WebSocket server running at ws://\" \u003c\u003c interface_address \u003c\u003c \":18090...\" \u003c\u003c  std::endl;\n  std::this_thread::sleep_for(std::chrono::minutes{ 1000 });\n  websocket_server.stop();\n  thread.join();\n  return 0;\n}\n\n```\n\n## WebSocket SSL server Usage Example:\n```C++\n\nint main() {\n  ... declarations of std::string cert, key, dh...\n  \n  const std::string interface_address{ \"127.0.0.1\" };\n  http::websocket_ssl_server\u003chttp::echo_message_handler\u003e\n        websocket_ssl_server{ interface_address, 18095, std::filesystem::current_path() };\n  \n  ... Similar to HTTPS:\n  websocket_ssl_server.set_ssl_context(http::ssl_dh_context_creator{}(cert, key, dh, \"test\"));\n  \n  std::thread thread {\n    [\u0026websocket_ssl_server]() {\n      websocket_ssl_server.start(std::thread::hardware_concurrency());\n      if (websocket_ssl_server.last_error()) {\n        std::cerr \u003c\u003c \"Error starting WebSocket server: \"\n                  \u003c\u003c websocket_ssl_server.last_error().message()\n                  \u003c\u003c std::endl;\n      }\n    }\n  };\n  \n  std::cout \u003c\u003c \"WebSocket Secure Server running at wss://\" \u003c\u003c interface_address \u003c\u003c \":18095...\" \u003c\u003c  std::endl;\n  std::this_thread::sleep_for(std::chrono::minutes{ 1000 });\n  websocket_ssl_server.stop();\n  thread.join();\n  return 0;\n}\n\n```\n\n### WebSocket::MessageHandler\n```C++\n  struct  echo_message_handler {\n\n    inline websocket_message\n    operator()(const websocket_message\u0026 message) const {\n      websocket_message response = message;\n      return response.write(\" \\\"[echo]\\\"\");\n    }\n    \n  };\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillsaibott%2FLibHTTP","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillsaibott%2FLibHTTP","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillsaibott%2FLibHTTP/lists"}