问题描述
我已经使用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。 提到的示例是“示例:增量读取”和“示例: 发送子进程输出”。
下面的示例部分起作用,但是存在一些问题。
通常,当我有一系列要在一个连接上执行的请求时, 未能成功读取第二个或第三个响应(标头) 书面请求,因此连接断开,客户端 (浏览器)重新连接并尝试在其他会话中执行它们。这个 使流量非常缓慢且令人讨厌。
尽管代理应用程序是异步编写的,但此方法是用 同步(阻止)方式,仅用于接收主机的响应 (上游)成块,并直接按原样写入接收到的数据块 接收到原始客户端(下游)。
问题是我做错了什么?
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(),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()),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()),ec);
// Read another request from the original client (downstream)
do_read_downstream();
}
解决方法
为了其他人遇到相同或相似的问题,我想将解决方案发布到我的问题上。 答案一直都是问题。 为了更加精确,在https://www.boost.org/doc/libs/1_66_0/libs/beast/example/doc/http_examples.hpp中有一个示例:HTTP Relay最先实现了我所需要的。 在这个例子中,Theere与我将自己与其他两个例子(在原始文章中提到的)结合在一起所产生的差异很小。 最重要的一个是“示例: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中继就可以很好地适用于我的代理的所有类型的请求,小的正文要求,也可以用于大文件下载和数据流。