2

Sometimes, boost http client async read response get an error code 5, message is "need buffer", I can't find any infomation about this error code, what's this error code mean?

enter image description here

void
    do_read()
{
    // Make the request empty before reading,
    // otherwise the operation behavior is undefined.
    std::shared_ptr<boost::beast::http::response<boost::beast::http::buffer_body>> p_read_remote_server_response =  std::make_shared<boost::beast::http::response<boost::beast::http::buffer_body>>(); ;

    std::shared_ptr<boost::beast::flat_buffer> p_read_remote_server_buffer = std::make_shared<boost::beast::flat_buffer>(15000000);
    // Read a response
    boost::beast::http::async_read(*pClientToServerStream, *p_read_remote_server_buffer, *p_read_remote_server_response,
        boost::bind(&DCClient::on_read_remote_server,
            shared_from_this(), p_read_remote_server_buffer, p_read_remote_server_response, _1, _2));
}

void
    on_read_remote_server(
        std::shared_ptr<boost::beast::flat_buffer> p_read_remote_server_buffer, 
        std::shared_ptr<boost::beast::http::response<boost::beast::http::buffer_body>> p_read_remote_server_response, 
        boost::beast::error_code ec,
        std::size_t bytes_transferred)
{
    boost::ignore_unused(bytes_transferred);

    std::stringstream strstm;
    strstm.write((const char *)((*p_read_remote_server_buffer).data().data()), bytes_transferred);
    std::string s = strstm.str();

    // This means they closed the connection
    if (ec == boost::beast::http::error::end_of_stream)
        return do_close();

    if (ec)
        throw boost::system::system_error{ ec };
}

And the response is wrong, method on_read_remote_server parameter bytes_transferred is 124, I convert response to a string, this string missing http headers, only body:

std::stringstream strstm;
    strstm.write((const char *)((*p_read_remote_server_buffer).data().data()), bytes_transferred);
    std::string s = strstm.str();
walnut
  • 21,629
  • 4
  • 23
  • 59
Meow
  • 41
  • 4
  • You must check the ec parameter of on_read_remote_server before doing anything with the other parameters. – Jean Davy Nov 26 '19 at 10:06
  • I changed the std::shared_ptr> p_read_remote_server_response to std::shared_ptr> p_read_remote_server_response , change buffer_body to dynamic_body, and this error never throw. – Meow Nov 27 '19 at 06:46
  • Yes, I know it, that code before check error_code, just to convert http response to string, and display it, that code is test code, not final version. – Meow Nov 27 '19 at 06:53

1 Answers1

-1

Stumbled onto this just now. You need to do request.body().more = false; and then it should work.

Marco Merlini
  • 875
  • 7
  • 29