簡體   English   中英

HTTP 內容長度很大時崩潰

[英]Crash when HTTP content length is very large

我有一個 boost-beast 異步 http 客戶端,當內容長度非常大時它會崩潰。 我已經使用 ncat http 服務器對其進行了測試

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

其中 response.txt 包含我的回復。 奇怪的是,當文件保存在 UNIX 環境中時代碼不會崩潰,但是當文件保存在 Windows 環境中時代碼不會崩潰。 UNIX 保存文件的內容長度的十六進制轉儲為 -1234,而 windows 十六進制轉儲顯示原始長度。 該文件的內容是

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

與它一起看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM