为什么Boost-Beast给我部分消息异常 [英] Why does Boost-Beast give me a partial message exception

查看:177
本文介绍了为什么Boost-Beast给我部分消息异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将boost beast http库用于HTTP客户端.当我使用模拟服务器时,它可以正常工作,但是当我尝试连接到真实服务器时, boost :: beast :: http :: read 会抛出一个异常,指出部分消息".

I´m trying to use the boost beast http library for an HTTP Client. It´s working without any issues when I´m using a simulated server, however when I try connecting to the real server, boost::beast::http::read throws an exception saying "partial message".

我已经在这个问题上工作了几天,但我不知道为什么.到目前为止,我一直在使用其他http客户端库,并且服务器通信一直在正常工作,而没有任何类似的问题.

I´ve been working on this issue for a couple days now but I can´t figure out why. Until now I´ve been using a different http client library and the server communication has been working without any similar issues.

对于这种想法为什么会发生以及为什么在使用其他库时似乎没有什么问题,我将不胜感激.

I´d be grateful for any kind of idea or hint as to why this is happening and why it doesn't seem to be an issue when using a different library.

推荐答案

boost :: beast :: http :: read引发异常,并显示部分消息".

boost::beast::http::read throws an exception saying "partial message".

发生这种情况是因为所解析的消息不完整.造成这种情况的典型原因是内容长度标头错误,或者发送方过早地放弃了连接.例如:

This happens because the message being parsed wasn't complete. A typical reason for it is when the content-length header is wrong, or the sender abandons the connection prematurely. E.g.:

在编译器资源管理器中实时运行

Live On Compiler Explorer

这是 http :: [async_] read 最终在幕后完成的工作,但没有与网络相关的东西:

This is what http::[async_]read ends up doing under the hood, but without the network related stuff:

#include <iostream>
#include <iomanip>
#include <string_view>
#include <boost/beast/http.hpp>

int main() {
    using namespace boost::beast::http;
    using boost::asio::buffer;

    for (std::string_view buf : {
            "GET / HTTP/1.1\r\n", // incomplete headers
            "GET / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 0\r\n\r\ntrailing data",
            "GET / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 42\r\n\r\nshort",
        })
    {
        //std::cout << std::quoted(test) << "\n";
        std::cout << "---------------------" << "\n";

        request_parser<string_body> parser;
        boost::system::error_code ec;

        size_t n = parser.put(buffer(buf), ec);
        if (n && !ec && !parser.is_done()) {
            buf.remove_prefix(n);
            n = parser.put(buffer(buf), ec); // body
        }
        if (!ec)
            parser.put_eof(ec);
        buf.remove_prefix(n);

        std::cout
            << (parser.is_header_done()?"headers ok":"incomplete headers")
            << " / " << (parser.is_done()?"done":"not done")
            << " / " << ec.message() << "\n";
        if (parser.is_header_done() && !parser.is_done())
            std::cout << parser.content_length_remaining().value_or(0) << " more content bytes expected\n";

        if (!buf.empty())
            std::cout << "Remaining buffer: " << std::quoted(buf) << "\n";
    }
}

打印

---------------------
incomplete headers / not done / need more
---------------------
headers ok / done / Success
Remaining buffer: "trailing data"
---------------------
headers ok / not done / partial message
37 more content bytes expected

如果您没有将 error_code 传递给您的呼叫,他们将抛出具有相同代码的异常 system_error ,这正是您所看到的.

If you're not passing error_code to your calls they will throw the exception system_error with the same code, which is exactly what you see.

如果另一个库没有此问题",则为有两种选择:

If another library doesn't have this "problem" there are two options:

  • 图书馆马虎(不好)
  • 您使用的是错误的(也许您不是在检查错误)

这篇关于为什么Boost-Beast给我部分消息异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆