From 80c99cb179b968ddc6888f228daceea5e2e85a01 Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 14 Apr 2021 17:55:54 +0200 Subject: [PATCH] Support receiving a body as well when handling chunks individually --- cli/main.cpp | 12 ++++++------ librepomgr/webclient/session.cpp | 6 +++--- librepomgr/webclient/session.h | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cli/main.cpp b/cli/main.cpp index 5b76d21..5360e58 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -311,17 +311,17 @@ static void printRawDataForErrorHandling(const LibRepoMgr::WebClient::Response:: 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, - const LibRepoMgr::WebClient::HttpClientError &error, void (*printer)(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData), - int &returnCode) +static void handleResponse(const std::string &url, LibRepoMgr::WebClient::Session &session, const LibRepoMgr::WebClient::HttpClientError &error, + void (*printer)(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData), int &returnCode) { auto result = boost::beast::http::status::ok; auto body = std::optional(); - if (auto *const emptyResponse = std::get_if(&session.response)) { - result = emptyResponse->get().result(); + if (auto *const responseParser = std::get_if(&session.response)) { + result = responseParser->get().result(); + body = std::move(responseParser->get().body()); } else if (auto *const response = std::get_if(&session.response)) { result = response->result(); body = std::move(response->body()); diff --git a/librepomgr/webclient/session.cpp b/librepomgr/webclient/session.cpp index 892ecb9..f8fa24c 100644 --- a/librepomgr/webclient/session.cpp +++ b/librepomgr/webclient/session.cpp @@ -72,7 +72,7 @@ void Session::run(const char *host, const char *port, http::verb verb, const cha return; } } else if (m_chunkProcessing) { - auto &emptyResponse = response.emplace(); + auto &emptyResponse = response.emplace(); emptyResponse.on_chunk_header(m_chunkProcessing->onChunkHeader); 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 std::visit( [this](auto &stream, auto &&response) { - if constexpr (std::is_same_v, EmptyResponse>) { + if constexpr (std::is_same_v, StringResponse>) { http::async_read_header( stream, m_buffer, response, std::bind(&Session::chunkReceived, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); } else { @@ -208,7 +208,7 @@ void Session::chunkReceived(boost::beast::error_code ec, std::size_t bytesTransf bool Session::continueReadingChunks() { - auto &parser = std::get(response); + auto &parser = std::get(response); if (parser.is_done()) { return false; } diff --git a/librepomgr/webclient/session.h b/librepomgr/webclient/session.h index efe451e..1c19ad5 100644 --- a/librepomgr/webclient/session.h +++ b/librepomgr/webclient/session.h @@ -43,8 +43,8 @@ inline LibRepoMgr::WebClient::HttpClientError::operator bool() const using Response = WebAPI::Response; using FileResponse = boost::beast::http::response_parser; -using EmptyResponse = boost::beast::http::response_parser; -using MultiResponse = std::variant; +using StringResponse = boost::beast::http::response_parser; +using MultiResponse = std::variant; using Request = boost::beast::http::request; struct ChunkProcessing;