refactor: Coroutine based webserver (#1699)

Code of new coroutine-based web server. The new server is not connected
to Clio and not ready to use yet.
For #919.
This commit is contained in:
Sergey Kuznetsov
2024-10-24 16:50:26 +01:00
committed by Alex Kremer
parent 5c77e59374
commit b8f1deb90f
58 changed files with 5581 additions and 488 deletions

View File

@@ -19,6 +19,8 @@
#include "util/TestHttpServer.hpp"
#include "util/Assert.hpp"
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/address.hpp>
@@ -36,6 +38,7 @@
#include <gtest/gtest.h>
#include <chrono>
#include <expected>
#include <string>
#include <utility>
@@ -107,13 +110,27 @@ doSession(
TestHttpServer::TestHttpServer(boost::asio::io_context& context, std::string host) : acceptor_(context)
{
boost::asio::ip::tcp::endpoint const endpoint(boost::asio::ip::make_address(host), 0);
boost::asio::ip::tcp::resolver resolver{context};
auto const results = resolver.resolve(host, "0");
ASSERT(!results.empty(), "Failed to resolve host");
boost::asio::ip::tcp::endpoint const& endpoint = results.begin()->endpoint();
acceptor_.open(endpoint.protocol());
acceptor_.set_option(asio::socket_base::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen(asio::socket_base::max_listen_connections);
}
std::expected<boost::asio::ip::tcp::socket, boost::system::error_code>
TestHttpServer::accept(boost::asio::yield_context yield)
{
boost::beast::error_code errorCode;
tcp::socket socket(this->acceptor_.get_executor());
acceptor_.async_accept(socket, yield[errorCode]);
if (errorCode)
return std::unexpected{errorCode};
return socket;
}
void
TestHttpServer::handleRequest(TestHttpServer::RequestHandler handler, bool const allowToFail)
{