gpt4 book ai didi

c++ - HTTP 内容长度非常大时崩溃

转载 作者:行者123 更新时间:2023-12-05 05:48:12 34 4
gpt4 key购买 nike

我有一个 boost-beast 异步 http 客户端,当内容长度非常大时它会崩溃。我已经使用 ncat http 服务器对其进行了测试

cat response.txt| sudo ncat -lvnp 443 --ssl

其中 response.txt 包含我的回复。奇怪的是,当文件保存在 UNIX 环境中时,代码不会崩溃,但当文件保存在 Windows 环境中时,代码会崩溃。 UNIX 保存文件的内容长度的 hexdump 为 -1234,而 windows hex dump 显示原始长度。文件内容为

HTTP/1.1 200 OK
Connection: close
Cache-control: no-cache, no-store
Pragma: no-cache
X-Frame-Options: DENY
Content-Type: text/html; charset=utf-8
X-Content-Type-Options: nosniff
Content-Length: 214748364716

<html><body>
<h1>Hi</h1>
</body>
</html>

在它之后是我的故障转储

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007f63bcda5921 in __GI_abort () at abort.c:79
#2 0x00007f63bd798957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007f63bd79eae6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007f63bd79eb21 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007f63bd79ed54 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007f63bd79f2dc in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x000000000047276a in __gnu_cxx::new_allocator<char>::allocate (__n=2, this=<optimized out>) at /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/ext/new_allocator.h:111
#8 std::allocator_traits<std::allocator<char> >::allocate (__n=2, __a=...) at /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/alloc_traits.h:436
#9 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create (this=0x7f639c003be8, __old_capacity=<optimized out>, __capacity=<optimized out>)
at /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.tcc:153
#10 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::reserve (this=0x7f639c003be8, __res=<optimized out>) at /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.tcc:293
#11 0x00000000007613b7 in boost::beast::http::basic_string_body<char, std::char_traits<char>, std::allocator<char> >::reader::init (this=0x7f639c003c08, length=..., ec=...) at ../INPUT/boost/include/boost/beast/http/string_body.hpp:102
#12 boost::beast::http::parser<false, boost::beast::http::basic_string_body<char, std::char_traits<char>, std::allocator<char> >, std::allocator<char> >::on_body_init_impl (this=0x7f639c003b38, content_length=..., ec=...)
at ../INPUT/boost/include/boost/beast/http/parser.hpp:448
#13 0x000000000076b130 in boost::beast::http::basic_parser<false>::put (this=0x7f639c003b38, buffer=..., ec=...) at ../INPUT/boost/include/boost/beast/http/impl/basic_parser.ipp:161
#14 0x000000000074bc4f in boost::beast::http::basic_parser<false>::put<boost::asio::mutable_buffer> (this=0x2, buffers=..., ec=...) at ../INPUT/boost/include/boost/beast/http/impl/basic_parser.hpp:41

崩溃的相关代码

void HttpClientSsl::onWrite(const boost::beast::error_code& ec, std::size_t) {

if (ec) {
mResultCallback(ec, "on HttpClientSsl::onWrite", std::nullopt);
return;
}

// make the mRes before reading
// otherwise the operation behavior is undefined.
mRes.emplace();
mRes.value().body_limit(8 * 1024 * 1024);
// Receive the HTTP response
boost::beast::http::async_read(
mStream, mBuffer, mRes.value(),
[shared = shared_from_this()](auto ec, auto bytesTransferred) { shared->onRead(ec, bytesTransferred); });
}

我有以下相关变量的定义

std::optional<boost::beast::http::response_parser<boost::beast::http::string_body>> mRes;
boost::beast::flat_buffer mBuffer;
boost::beast::ssl_stream<boost::beast::tcp_stream> mStream;

我是否读错了内容长度导致崩溃?

最佳答案

我认为您读错了,或者这可能是库的更新版本中已修复的错误。

这里是一个最小化的复制器,在这里工作正常,无论 body_limit 是否被覆盖:

#include <boost/beast.hpp>
#include <iostream>
namespace http = boost::beast::http;

static constexpr std::string_view example =
"HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-Length: 214748364716\r\n"
"\r\n"
"<html><body>Hi</body></html>\r\n";

int main() {
http::response_parser<http::string_body> p;

boost::system::error_code ec;
p.body_limit(8 * 1024 * 1024);
p.put(boost::asio::buffer(example), ec);
// if (!ec) p.put_eof(ec);

std::cout << ec.message() << "\n";
}

用它来看

关于c++ - HTTP 内容长度非常大时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70857076/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com