Support receiving a body as well when handling chunks individually
This commit is contained in:
parent
f5d3bd41d1
commit
80c99cb179
12
cli/main.cpp
12
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<std::string>();
|
||||
if (auto *const emptyResponse = std::get_if<LibRepoMgr::WebClient::EmptyResponse>(&session.response)) {
|
||||
result = emptyResponse->get().result();
|
||||
if (auto *const responseParser = std::get_if<LibRepoMgr::WebClient::StringResponse>(&session.response)) {
|
||||
result = responseParser->get().result();
|
||||
body = std::move(responseParser->get().body());
|
||||
} else if (auto *const response = std::get_if<LibRepoMgr::WebClient::Response>(&session.response)) {
|
||||
result = response->result();
|
||||
body = std::move(response->body());
|
||||
|
|
|
@ -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<EmptyResponse>();
|
||||
auto &emptyResponse = response.emplace<StringResponse>();
|
||||
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<std::decay_t<decltype(response)>, EmptyResponse>) {
|
||||
if constexpr (std::is_same_v<std::decay_t<decltype(response)>, 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<EmptyResponse>(response);
|
||||
auto &parser = std::get<StringResponse>(response);
|
||||
if (parser.is_done()) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -43,8 +43,8 @@ inline LibRepoMgr::WebClient::HttpClientError::operator bool() const
|
|||
|
||||
using Response = WebAPI::Response;
|
||||
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 MultiResponse = std::variant<Response, FileResponse, EmptyResponse>;
|
||||
using StringResponse = boost::beast::http::response_parser<boost::beast::http::string_body>;
|
||||
using MultiResponse = std::variant<Response, FileResponse, StringResponse>;
|
||||
using Request = boost::beast::http::request<boost::beast::http::empty_body>;
|
||||
struct ChunkProcessing;
|
||||
|
||||
|
|
Loading…
Reference in New Issue