A header-only library created to facilitate web server and client development built on top of other boost libraries like boost.beast, boost.asio, boost.url, boost.json, etc.
- boost.taar
Boost.taar is a portable C++ header-only library which provides convenient tools for web server and client development. This library is built on top of other boost libraries, like Boost.ASIO, Boost.Beast, Boost.URL, and Boost.JSON, and Boost.system.
- A C++ compiler supporting C++23 and above.
- Other boost libraries like Boost.ASIO, Boost.Beast, Boost.URL, and Boost.JSON, and Boost.system.
- CMake 3.28 or a more recent version
- Note: Lower versions might also work but I'm not able to test that now.
Below are some short examples showcasing various use-cases for this library. For complete examples, look under the examples directory.
Accepts an HTTP GET request for specific target and respond with a stock text.
taar::session::http http_session;
http_session.register_request_handler(
method == http::verb::get && target == "/api/version",
taar::handler::rest([]{ return "1.0"; }
));
taar::cancellation_signals cancellation_signals;
co_spawn(
io_context,
taar::server::tcp(
"0.0.0.0",
"8090",
http_session,
cancellation_signals),
bind_cancellation_slot(cancellation_signals.slot(), taar::ignore_and_rethrow));
io_context.run();
Accepts an HTTP GET request with a path template, calculates the sum for two of the path arguments and respond with a JSON value.
taar::session::http http_session;
http_session.register_request_handler(
method == http::verb::get && target == "/api/sum/{a}/{b}",
taar::handler::rest([](int a, int b)
{
return boost::json::value {
{"a", a},
{"b", b},
{"result", a + b}
};
},
taar::handler::path_arg("a"),
taar::handler::path_arg("b")
));
taar::cancellation_signals cancellation_signals;
co_spawn(
io_context,
taar::server::tcp(
"0.0.0.0",
"8090",
http_session,
cancellation_signals),
bind_cancellation_slot(cancellation_signals.slot(), taar::ignore_and_rethrow));
io_context.run();
Accepts an HTTP POST request for a specific target, expects that a query parameter called "test_arg" to be present and passes the value of it to the handler lambda.
taar::session::http http_session;
http_session.register_request_handler(
method == http::verb::post && target == "/api/store/",
taar::handler::rest([](const std::string& test_arg)
{
std::cout << "Received test_arg = " << test_arg << '\n';
},
taar::handler::query_arg("test_arg")
));
taar::cancellation_signals cancellation_signals;
co_spawn(
io_context,
taar::server::tcp(
"0.0.0.0",
"8090",
http_session,
cancellation_signals),
bind_cancellation_slot(cancellation_signals.slot(), taar::ignore_and_rethrow));
io_context.run();
Accepts an HTTP GET request for all targets under the specified template and respond with the requested file content with a correct mime-type.
taar::session::http http_session;
http_session.register_request_handler(
method == http::verb::get && target == "/{*}",
taar::handler::htdocs {argv[2]}
);
taar::cancellation_signals cancellation_signals;
co_spawn(
io_context,
taar::server::tcp(
"0.0.0.0",
"8090",
http_session,
cancellation_signals),
bind_cancellation_slot(cancellation_signals.slot(), taar::ignore_and_rethrow));
io_context.run();
Accepts an HTTP PUT request for all targets under the specified template and respond with a custom text message.
taar::session::http http_session;
http_session.register_request_handler(
method == http::verb::put && target == "/special/{*}",
[](
const http::request<http::empty_body>& request,
const taar::matcher::context& context) -> http::message_generator
{
http::response<http::string_body> res {
boost::beast::http::status::ok,
request.version()};
res.set(boost::beast::http::field::content_type, "text/html");
res.keep_alive(request.keep_alive());
res.body() = "Special path is: " + context.path_args.at("*");
res.prepare_payload();
return res;
}
);
taar::cancellation_signals cancellation_signals;
co_spawn(
io_context,
taar::server::tcp(
"0.0.0.0",
"8090",
http_session,
cancellation_signals),
bind_cancellation_slot(cancellation_signals.slot(), taar::ignore_and_rethrow));
io_context.run();
Execute the following commands from the project's root directory to configure it for the Debug configuration.
This requires the Boost libraries to be installed on the host.
cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug
cmake \
-B build \
-S . \
-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=cmake/conan_provider.cmake \
-DCMAKE_BUILD_TYPE=Debug
Assuming it is invoked from the project's root directory.
cmake --build build
build/test/boost-taar-test
cmake --install build --prefix out
cmake --build build -j --target http_server && build/examples/http_server 8082 ./
version=$(git describe --abbrev=0 | grep -o '[0-9]\+.[0-9]\+.[0-9]\+')
conan create . --version "$version" --build=missing
conan upload "boost-taar/$version" --only-recipe --remote your_conan_remote
- GCC 14.1.1 on Arch Linux
- GCC 13.2.0 on Ubuntu 24.04
- GCC 11.2.0 on Ubuntu 22.04
- Note: The required CMake version is 3.28 which is newer than 3.22 shipped with Ubuntu 22.04
Copyright © 2021, 2024 Reza Jahanbakhshi
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
- http session needs full unit-test coverage.
- htdocs handler needs full unit-test-coverage.
- curl -v 127.0.0.1:8082/api/sum/1/2 doesn't match
- rest should only handle its own internal errors and leave the rest to the http handler.
- rest_arg content-type interpretation could be wrong.
- encoded and decoded option for rest arg providers.
- metadata support for HTTP request handlers including name and other requirements like if it needs cookies or other things to be preparsed and put into contexts.
- support for generating above metadata in rest handlers.
- parse cookies only when it is requested in the metadata (currently rest handler reparse cookies everytime)