0

我已经使用 boost beast 库实现了一个代理,其中我使用了 boost 组织的异步 https 服务器和客户端示例。在我的代理中,我使用了http::request<http::string_body>http::response<http::string_body>类型的消息,如示例中使用的那样。

此代理运行良好,只是它无法接收(下载)大文件和流。

因此,为此目的,我决定通过结合 https://www.boost.org/doc/libs/1_66_0/libs/beast/example/doc/http_examples.hpp中的两个示例来重新设计我的传输机制。提到的示例是“示例:增量读取”和“示例:发送子进程输出”。

此示例(以下)部分有效,但存在一些问题。

很多时候,当我在一个连接上执行一系列请求时,我未能读取成功写入请求的第二个或第三个响应(标头),因此连接中断,客户端(浏览器)重新连接并尝试在不同的会话中执行它们。这使得交通非常缓慢和烦人。

虽然代理应用程序是异步编写的,但此方法是以同步(阻塞)方式编写的,仅用于以块的形式接收来自主机(上游)的响应,并将接收到的数据块在收到时直接写入原始数据块客户端(下游)。

问题是我做错了什么?

我相信经验丰富的 Beast boost 用户可以通过阅读示例来解决问题。

std::shared_ptr<beast::ssl_stream<beast::tcp_stream>> m_sslDownStream;
std::shared_ptr<beast::ssl_stream<beast::tcp_stream>> m_sslUpStream;
beast::flat_buffer m_upBuffer;

void Session::do_read_upstream_buffered_response()
{
    beast::error_code ec;
    size_t bytes_transferred = 0
    beast::flat_buffer m_upBuffer;
    http::response_parser<http::buffer_body> resPar;
    resPar.body_limit(ULONG_MAX);

    bytes_transferred = http::read_header(*m_sslUpStream.get(), m_upBuffer, resPar, ec);

    if (ec)
    {
        return fail(ec, "read header");
    }

    http::response<http::buffer_body> bufRes;

    bufRes.result(resPar.get().result_int());
    bufRes.version(resPar.get().version());

    int field_count = 0;
    for (auto const& field : resPar.get())
    {
        bufRes.insert(field.name_string().to_string(), field.value().to_string());
    }

    // No data yet, but we set more = true to indicate
    // that it might be coming later. Otherwise the
    // serializer::is_done would return true right after
    // sending the header.
    bufRes.body().data = nullptr;
    bufRes.body().more = true;

    http::response_serializer<http::buffer_body, http::fields> resSer { bufRes };

    bytes_transferred = http::write_header(*(m_sslDownStream.get()), resSer, ec);

    if (ec)
    {
        LSPROXY_LOGD(LOG_MITM_PROXY, "Session[%d]::do_read_upstream_buffered_response(%s) Failed to write header to the original client (downstream) with error: %s", this, m_sessionStateStr, ec.message().c_str());
        return fail(ec, "write header");
    }

    // Read the rest of the response body upstream and send it downstream 
    while (!resPar.is_done())
    {
        char buf[8192];

        resPar.get().body().data = buf;
        resPar.get().body().size = sizeof(buf);

        bytes_transferred = http::read(*m_sslUpStream.get(), m_upBuffer, resPar, ec);

        if (ec == http::error::need_buffer)
        {
            ec.message().c_str());
            ec.assign(0, ec.category());
        }

        if (ec)
        {
            return fail(ec, "read body");
        }

        // Point to our buffer with the bytes that
        // we received, and indicate that there may
        // be some more data coming
        bufRes.body().data = buf;
        bufRes.body().size = sizeof(buf) - resPar.get().body().size;
        bufRes.body().more = true;

        bytes_transferred = http::write(*(m_sslDownStream.get()), resSer, ec);

        // This error is returned by body_buffer during
        // serialization when it is done sending the data
        // provided and needs another buffer.
        if (ec == http::error::need_buffer)
        {
            ec.message().c_str());
            ec = {};
            //continue;
        }

        if (ec)
        {
            return fail(ec, "write body");
        }

    } //while (!resPar.is_done())

    // `nullptr` indicates there is no buffer
    bufRes.body().data = nullptr;
    // `false` means no more data is coming
    bufRes.body().more = false;

    // Send the response header to the original client (downstream).
    bytes_transferred = http::write(*(m_sslDownStream.get()), resSer, ec);

    // Read another request from the original client (downstream)
    do_read_downstream();
}
4

1 回答 1

0

只是为了其他人有相同或相似的问题,我想发布我的问题的解决方案。答案一直在问题中。更准确地说,在https://www.boost.org/doc/libs/1_66_0/libs/beast/example/doc/http_examples.hpp中有一个示例:HTTP Relay 这正是我首先需要的. 此示例与我将自己与其他两个示例(在原始帖子中提到)相结合的示例中存在细微差别。最重要的一个,示例:HTTP 中继不使用缓冲的正文响应:

http::response<http::buffer_body> bufRes;

构造序列化器:

http::response_serializer<http::buffer_body, http::fields> resSer { bufRes };

它直接使用接收解析器来构造序列化器:

// Create a parser with a buffer body to read from the input.
parser<isRequest, buffer_body> p;

// Create a serializer from the message contained in the parser.
serializer<isRequest, buffer_body, fields> sr{p.get()};

稍加修改后,示例:HTTP 中继对于我的代理所有类型的请求、小正文请求、大文件下载和数据流也适用。

于 2020-09-07T11:20:07.763 回答