Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost beast message with body_limit

My starting point is to create a simple downloader code from the boost beast http_client_async example at boost http_client_async. In this scenario i want to write the received body into a file.

So I exchanged the string body into a file_body, to write the received data:

 http::response_parser<http::file_body> res_;

And simply rewrote the on_write method to

void on_write( boost::system::error_code ec,
               std::size_t bytes_transferred )
{
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail(ec, "write");

    boost::system::error_code ec_file;
    res_.body().open("myTest.txt", boost::beast::file_mode::write, ec_file);

    // test for ec_file missing 

    // Receive the HTTP response
    http::async_read(socket_, buffer_, res_,
        std::bind(
            &session::on_read,
            shared_from_this(),
            std::placeholders::_1,
            std::placeholders::_2));
}

So but now, some of the received data bodies are to big:

read: body limit exceeded

and I try to increase the body limit.

In case of using a parser instead of a message, the size limit of the requested body can be changed with the body_limit() method.

Is there an easy way to increase the body size limit from a message as well?

like image 752
Tom Avatar asked May 15 '18 11:05

Tom


1 Answers

Beast's HTTP interfaces are grouped into layers. The first layer has the message-oriented stream algorithms, which operate on an HTTP message container. These are designed for simplicity, but allow for very little customization. The next layer is the serializer/parser oriented interface. This requires maintaining the lifetime of a serializer (for writing) or a parser (for reading) for the duration of the stream operation. It is a little more complex but correspondingly allows for more customization.

Adjusting the maximum size of the message body requires using the parser-oriented interface, as you have noted in your comment:

namespace http = boost::beast::http;
http::response_parser<http::file_body> parser;

// Allow for an unlimited body size
parser.body_limit((std::numeric_limits<std::uint64_t>::max)());
...
http::read(socket, buffer, parser);
like image 153
Vinnie Falco Avatar answered Sep 17 '22 21:09

Vinnie Falco