Remove manually check error and throw because boost asio with use_awaitable throws exceptions

This commit is contained in:
sha512sum 2024-03-19 21:37:48 +00:00
parent f2f6f253ec
commit a78a72d41d
3 changed files with 9 additions and 46 deletions

View file

@ -108,26 +108,16 @@ public:
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(this->taskProcessor.ioContext, this->ctx);
boost::asio::ip::tcp::resolver::iterator endpoint =
co_await this->resolver.async_resolve({request.request.url.host(), request.request.url.has_port() ? request.request.url.port() : request.request.url.scheme()}, boost::asio::use_awaitable);
auto [ec, _] = co_await boost::asio::async_connect(socket.lowest_layer(), endpoint, boost::asio::as_tuple(boost::asio::use_awaitable));
if(ec) {
throw BoostErrorWrapper{ec};
};
co_await socket.async_handshake(boost::asio::ssl::stream_base::client, boost::asio::redirect_error(boost::asio::use_awaitable, ec));
if(ec) {
throw BoostErrorWrapper{ec};
};
co_await boost::asio::async_connect(socket.lowest_layer(), endpoint, boost::asio::use_awaitable);
co_await socket.async_handshake(boost::asio::ssl::stream_base::client, boost::asio::use_awaitable);
std::string req(request.request.ToString());
co_await boost::asio::async_write(socket, boost::asio::buffer(req.data(), req.size()), boost::asio::transfer_all(), boost::asio::redirect_error(boost::asio::use_awaitable, ec));
if(ec) {
throw BoostErrorWrapper{ec};
};
co_await boost::asio::async_write(socket, boost::asio::buffer(req.data(), req.size()), boost::asio::transfer_all(), boost::asio::use_awaitable);
auto response = co_await this->ReadHeaders(socket, flags...);
if constexpr(kUseStreaming) {
co_return clients::http::Response<HttpClient<TaskProcessor>, decltype(socket)>{*this, std::move(socket), std::move(response)};
} else {
if(response.statusCode != 200) {
co_return response;
};
if(response.headers.contains("Content-Length")) {
auto size = std::stoi(response.headers["Content-Length"]);
response.body = co_await ReadBody(std::move(socket), size, flags...);

View file

@ -1,6 +1,5 @@
#pragma once
#include <cserver/engine/coroutine.hpp>
#include <cserver/utils/boost_error_wrapper.hpp>
#include <fmt/format.h>
#include <boost/asio.hpp>
@ -11,17 +10,11 @@ struct HTTPStream {
std::stringstream stream = {};
inline auto SetMethod(std::string method) -> Task<void> {
method += " ";
auto [ec, n] = co_await boost::asio::async_write(this->socket, boost::asio::buffer(method.data(), method.size()), boost::asio::transfer_all(), boost::asio::as_tuple(boost::asio::use_awaitable));
if(ec) {
throw BoostErrorWrapper{ec};
};
co_await boost::asio::async_write(this->socket, boost::asio::buffer(method.data(), method.size()), boost::asio::transfer_all(), boost::asio::use_awaitable);
};
inline auto SetStatus(std::string status) -> Task<void> {
status = fmt::format("HTTP/1.1 {}\r\n", std::move(status));
auto [ec, n] = co_await boost::asio::async_write(this->socket, boost::asio::buffer(status.data(), status.size()), boost::asio::transfer_all(), boost::asio::as_tuple(boost::asio::use_awaitable));
if(ec) {
throw BoostErrorWrapper{ec};
};
co_await boost::asio::async_write(this->socket, boost::asio::buffer(status.data(), status.size()), boost::asio::transfer_all(), boost::asio::use_awaitable);
};
inline auto SetHeader(std::string first, std::string second) -> Task<void> {
@ -31,16 +24,10 @@ struct HTTPStream {
inline auto SetEndOfHeaders() -> Task<void> {
this->stream << "\r\n";
auto str = this->stream.str();
auto [ec, n] = co_await boost::asio::async_write(this->socket, boost::asio::buffer(str.data(), str.size()), boost::asio::transfer_all(), boost::asio::as_tuple(boost::asio::use_awaitable));
if(ec) {
throw BoostErrorWrapper{ec};
};
co_await boost::asio::async_write(this->socket, boost::asio::buffer(str.data(), str.size()), boost::asio::transfer_all(), boost::asio::use_awaitable);
};
inline auto PushBodyChunk(std::string_view chunk) -> Task<void> {
auto [ec, n] = co_await boost::asio::async_write(this->socket, boost::asio::buffer(chunk.data(), chunk.size()), boost::asio::transfer_all(), boost::asio::as_tuple(boost::asio::use_awaitable));
if(ec) {
throw BoostErrorWrapper{ec};
};
co_await boost::asio::async_write(this->socket, boost::asio::buffer(chunk.data(), chunk.size()), boost::asio::transfer_all(), boost::asio::use_awaitable);
};
inline auto Close() -> Task<void> {
this->socket.close();

View file

@ -1,14 +0,0 @@
#pragma once
#include <boost/system/error_code.hpp>
namespace cserver {
struct BoostErrorWrapper : std::exception, boost::system::error_code {
std::string errorMessage;
BoostErrorWrapper(boost::system::error_code ec) : errorMessage(ec.what()) {};
auto what() const noexcept -> const char* override {
return this->errorMessage.c_str();
};
};
} // namespace cserver