Skip to content

Commit

Permalink
oscquery: try a fix for #848
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Dec 18, 2024
1 parent 1a5cf99 commit bbc2492
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/ossia/network/http/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,32 @@ class http_get_request : public std::enable_shared_from_this<http_get_request<Fu
}
}

// Start reading remaining data until EOF.
boost::asio::async_read(
m_socket, m_response, boost::asio::transfer_at_least(1),
[self = shared_from_this()](
const boost::system::error_code& err, std::size_t size) {
self->handle_read_content(err, size);
if(m_contentLength > 0)
{
if(m_contentLength == m_response.size())
{
finish_read(boost::asio::error::eof, size);
}
else
{
boost::asio::async_read(
m_socket, m_response, boost::asio::transfer_exactly(m_contentLength - m_response.size()),
[self = shared_from_this()](
const boost::system::error_code& err, std::size_t size) {
self->handle_read_content(err, size);
});
}
}
else
{
// Start reading remaining data until EOF.
boost::asio::async_read(
m_socket, m_response, boost::asio::transfer_all(),
[self = shared_from_this()](
const boost::system::error_code& err, std::size_t size) {
self->handle_read_content(err, size);
});
}
}
else
{
Expand All @@ -198,30 +217,17 @@ class http_get_request : public std::enable_shared_from_this<http_get_request<Fu

void handle_read_content(const boost::system::error_code& err, std::size_t size)
{
if(!err)
{
// Continue reading remaining data until EOF.
if(m_contentLength < 0 || m_response.size() < m_contentLength)
{
boost::asio::async_read(
m_socket, m_response, boost::asio::transfer_at_least(1),
[self = shared_from_this()](
const boost::system::error_code& err, std::size_t size) {
self->handle_read_content(err, size);
});
return;
}
// else we fallthrough the processing case at the end
}
else if(err != boost::asio::error::eof)
if(!err || err == boost::asio::error::eof)
finish_read(err, size);
else
{
ossia::logger().error("HTTP Error: {}", err.message());
m_err(*this);
return;
}
}

// err == boost::asio::error::eof or we reached content-length
// Write all of the data that has been read so far.
void finish_read(const boost::system::error_code& err, std::size_t size)
{
const auto& dat = m_response.data();
auto begin = boost::asio::buffers_begin(dat);
auto end = boost::asio::buffers_end(dat);
Expand Down

0 comments on commit bbc2492

Please sign in to comment.