6

我的出发点是从 boost http_client_async 的 boost beast http_client_async 示例创建一个简单的下载器代码。在这种情况下,我想将接收到的正文写入文件。

于是我把字符串body换成了file_body,来写接收到的数据:

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

并简单地将 on_write 方法重写为

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));
}

所以但是现在,一些接收到的数据体太大了:

read: body limit exceeded

我试着增加身体极限。

如果使用解析器而不是消息,则可以使用该body_limit()方法更改请求正文的大小限制。

是否也有一种简单的方法可以从消息中增加正文大小限制?

4

1 回答 1

9

Beast 的 HTTP 接口被分组到层中。第一层具有面向消息的流算法,它在 HTTP 消息容器上运行。这些是为简单而设计的,但允许很少的定制。下一层是面向序列化器/解析器的接口。这需要在流操作期间维持序列化器(用于写入)或解析器(用于读取)的生命周期。它有点复杂,但相应地允许更多的定制。

正如您在评论中指出的那样,调整消息正文的最大大小需要使用面向解析器的界面:

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);
于 2018-05-15T22:27:00.297 回答