Support receiving a body as well when handling chunks individually

This commit is contained in:
Martchus 2021-04-14 17:55:54 +02:00
parent f5d3bd41d1
commit 80c99cb179
3 changed files with 11 additions and 11 deletions

View File

@ -311,17 +311,17 @@ static void printRawDataForErrorHandling(const LibRepoMgr::WebClient::Response::
static void printRawData(const LibRepoMgr::WebClient::Response::body_type::value_type &rawData) static void printRawData(const LibRepoMgr::WebClient::Response::body_type::value_type &rawData)
{ {
std::cout << rawData << '\n'; std::cout << rawData;
} }
static void handleResponse(const std::string &url, LibRepoMgr::WebClient::Session &session, static void handleResponse(const std::string &url, LibRepoMgr::WebClient::Session &session, const LibRepoMgr::WebClient::HttpClientError &error,
const LibRepoMgr::WebClient::HttpClientError &error, void (*printer)(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData), void (*printer)(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData), int &returnCode)
int &returnCode)
{ {
auto result = boost::beast::http::status::ok; auto result = boost::beast::http::status::ok;
auto body = std::optional<std::string>(); auto body = std::optional<std::string>();
if (auto *const emptyResponse = std::get_if<LibRepoMgr::WebClient::EmptyResponse>(&session.response)) { if (auto *const responseParser = std::get_if<LibRepoMgr::WebClient::StringResponse>(&session.response)) {
result = emptyResponse->get().result(); result = responseParser->get().result();
body = std::move(responseParser->get().body());
} else if (auto *const response = std::get_if<LibRepoMgr::WebClient::Response>(&session.response)) { } else if (auto *const response = std::get_if<LibRepoMgr::WebClient::Response>(&session.response)) {
result = response->result(); result = response->result();
body = std::move(response->body()); body = std::move(response->body());

View File

@ -72,7 +72,7 @@ void Session::run(const char *host, const char *port, http::verb verb, const cha
return; return;
} }
} else if (m_chunkProcessing) { } else if (m_chunkProcessing) {
auto &emptyResponse = response.emplace<EmptyResponse>(); auto &emptyResponse = response.emplace<StringResponse>();
emptyResponse.on_chunk_header(m_chunkProcessing->onChunkHeader); emptyResponse.on_chunk_header(m_chunkProcessing->onChunkHeader);
emptyResponse.on_chunk_body(m_chunkProcessing->onChunkBody); emptyResponse.on_chunk_body(m_chunkProcessing->onChunkBody);
} }
@ -149,7 +149,7 @@ void Session::requested(boost::beast::error_code ec, std::size_t bytesTransferre
// receive the HTTP response // receive the HTTP response
std::visit( std::visit(
[this](auto &stream, auto &&response) { [this](auto &stream, auto &&response) {
if constexpr (std::is_same_v<std::decay_t<decltype(response)>, EmptyResponse>) { if constexpr (std::is_same_v<std::decay_t<decltype(response)>, StringResponse>) {
http::async_read_header( http::async_read_header(
stream, m_buffer, response, std::bind(&Session::chunkReceived, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); stream, m_buffer, response, std::bind(&Session::chunkReceived, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
} else { } else {
@ -208,7 +208,7 @@ void Session::chunkReceived(boost::beast::error_code ec, std::size_t bytesTransf
bool Session::continueReadingChunks() bool Session::continueReadingChunks()
{ {
auto &parser = std::get<EmptyResponse>(response); auto &parser = std::get<StringResponse>(response);
if (parser.is_done()) { if (parser.is_done()) {
return false; return false;
} }

View File

@ -43,8 +43,8 @@ inline LibRepoMgr::WebClient::HttpClientError::operator bool() const
using Response = WebAPI::Response; using Response = WebAPI::Response;
using FileResponse = boost::beast::http::response_parser<boost::beast::http::file_body>; using FileResponse = boost::beast::http::response_parser<boost::beast::http::file_body>;
using EmptyResponse = boost::beast::http::response_parser<boost::beast::http::empty_body>; using StringResponse = boost::beast::http::response_parser<boost::beast::http::string_body>;
using MultiResponse = std::variant<Response, FileResponse, EmptyResponse>; using MultiResponse = std::variant<Response, FileResponse, StringResponse>;
using Request = boost::beast::http::request<boost::beast::http::empty_body>; using Request = boost::beast::http::request<boost::beast::http::empty_body>;
struct ChunkProcessing; struct ChunkProcessing;