如何复制或重用boost::asio::streambuf?
How copy or reuse boost::asio::streambuf?
我正在使用 boost asio 上的一些业务逻辑实现 http 代理服务器。
在第 (1) 点 boost::asio::streambuf response_ 包含 http headers 和部分 http body.
使用 http_response::parse 解析后,缓冲区 boost::asio::streambuf response_ 为空。
在 (2) 我检查所有业务逻辑并读取 body 如果 Content-Length header 在 header s.
然后,如果 response_ 数据符合特定条件,我想将原始 response_ 缓冲区发送到另一个套接字 (3)。
问题是解析后缓冲区为空。
有没有办法复制 boost::asio::streambuf 以重用数据?
void http_response::parse(boost::asio::streambuf& buffer)
{
std::istream response_stream(&buffer);
response_stream >> version_;
response_stream >> status_code_;
response_stream >> status_message_;
std::string key;
std::string value;
std::string header;
std::getline(response_stream, header);
while (std::getline(response_stream, header) && header != "\r") {
header.resize(header.size() - 1);
std::size_t found = header.find(':');
if (found != std::string::npos) {
key = header.substr(0, found);
value = header.substr(found + 2);
headers_[key] = value;
}
}
}
bool go(const std::string& hostname, const std::string& path,
const std::string& server, int port,
boost::asio::io_service::strand& strand,
boost::asio::yield_context& yield)
{
...
http_response response;
boost::asio::streambuf response_;
// async read http header from socket
std::clog << "<- " << sequence_ << " schedule async_read_until head" << std::endl;
boost::asio::async_read_until(socket_, response_, "\r\n\r\n", yield[err]);
check_error_and_timeout(err, timeout_);
// 1. response_.size() == 512 here
response.parse(response_);
// 2. response_.size() == 0 empty here
// using headers for business logic check
...
// read http body if Content-Length > 0
const std::string str_content_length = response.get_header("Content-Length", "");
const size_t content_length = std::stoi(str_content_length);
if(!str_content_length.empty() && content_length > response_.size())
{
std::clog << "<- " << sequence_ << " schedule async_read body" << std::endl;
boost::asio::async_read(socket_, response_,
boost::asio::transfer_at_least(content_length - response_.size()),
yield[err]);
check_error_and_timeout(err, timeout_);
}
// 3. after read all header and body write all data to server sock
boost::asio::async_write(server_socket_, response_, yield[err]);
}
首先,你不能复制 streambuf - 它已经删除了复制构造函数。
我建议使用 asio::buffer 使用您自己的缓冲区并解析它。但是有了这个你将无法使用async_read_until。此外,您可以将消费复制到您自己的缓冲区中,然后在需要时发送它。
Lifehack:您可以使用 buffer_cast 获取数据而不消耗它,但请注意它不是安全的方式,因为它没有记录并且可能会损坏(但它对我有用很多次并且多次升级):
// req_buf is a streambuf
const char *req_data = boost::asio::buffer_cast<const char *>( req_buf.data() );
auto sz = req_buf.size();
再次注意:这个转换是有效的,因为 streambuf 实现了一些概念,但它没有直接记录。另外,请记住 req_data 可以在任何 req_buf 修改后失效。
可以使用boost::asio::buffer_copy()
复制Asio缓冲区的内容。例如,如果希望将一个 streambuf
的内容复制到另一个 streambuf
.
,这会很方便
boost::asio::streambuf source, target;
...
std::size_t bytes_copied = buffer_copy(
target.prepare(source.size()), // target's output sequence
source.data()); // source's input sequence
// Explicitly move target's output sequence to its input sequence.
target.commit(bytes_copied);
可以使用类似的方法从 streambuf
复制到 Asio 支持可变缓冲区的任何类型。例如,将内容复制到 std::vector<char>
:
boost::asio::streambuf source;
...
std::vector<char> target(source.size());
buffer_copy(boost::asio::buffer(target), source.data());
一个值得注意的例外是 Asio 不支持为 std::string
返回可变缓冲区。但是,仍然可以通过迭代器完成复制到 std::string
:
boost::asio::streambuf source;
...
std::string target{
buffers_begin(source.data()),
buffers_end(source.data())
};
这里是一个例子demonstrating将内容从boost::asio::streambuf
复制到其他各种类型:
#include <iostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>
int main()
{
const std::string expected = "This is a demo";
// Populate source's input sequence.
boost::asio::streambuf source;
std::ostream ostream(&source);
ostream << expected;
// streambuf example
{
boost::asio::streambuf target;
target.commit(buffer_copy(
target.prepare(source.size()), // target's output sequence
source.data())); // source's input sequence
// Verify the contents are equal.
assert(std::equal(
buffers_begin(target.data()),
buffers_end(target.data()),
begin(expected)
));
}
// std::vector example
{
std::vector<char> target(source.size());
buffer_copy(boost::asio::buffer(target), source.data());
// Verify the contents are equal.
assert(std::equal(begin(target), end(target), begin(expected)));
}
// std::string example
{
// Copy directly into std::string. Asio does not support
// returning a mutable buffer for std::string.
std::string target{
buffers_begin(source.data()),
buffers_end(source.data())
};
// Verify the contents are equal.
assert(std::equal(begin(target), end(target), begin(expected)));
}
}
我正在使用 boost asio 上的一些业务逻辑实现 http 代理服务器。
在第 (1) 点 boost::asio::streambuf response_ 包含 http headers 和部分 http body.
使用 http_response::parse 解析后,缓冲区 boost::asio::streambuf response_ 为空。
在 (2) 我检查所有业务逻辑并读取 body 如果 Content-Length header 在 header s.
然后,如果 response_ 数据符合特定条件,我想将原始 response_ 缓冲区发送到另一个套接字 (3)。
问题是解析后缓冲区为空。 有没有办法复制 boost::asio::streambuf 以重用数据?
void http_response::parse(boost::asio::streambuf& buffer)
{
std::istream response_stream(&buffer);
response_stream >> version_;
response_stream >> status_code_;
response_stream >> status_message_;
std::string key;
std::string value;
std::string header;
std::getline(response_stream, header);
while (std::getline(response_stream, header) && header != "\r") {
header.resize(header.size() - 1);
std::size_t found = header.find(':');
if (found != std::string::npos) {
key = header.substr(0, found);
value = header.substr(found + 2);
headers_[key] = value;
}
}
}
bool go(const std::string& hostname, const std::string& path,
const std::string& server, int port,
boost::asio::io_service::strand& strand,
boost::asio::yield_context& yield)
{
...
http_response response;
boost::asio::streambuf response_;
// async read http header from socket
std::clog << "<- " << sequence_ << " schedule async_read_until head" << std::endl;
boost::asio::async_read_until(socket_, response_, "\r\n\r\n", yield[err]);
check_error_and_timeout(err, timeout_);
// 1. response_.size() == 512 here
response.parse(response_);
// 2. response_.size() == 0 empty here
// using headers for business logic check
...
// read http body if Content-Length > 0
const std::string str_content_length = response.get_header("Content-Length", "");
const size_t content_length = std::stoi(str_content_length);
if(!str_content_length.empty() && content_length > response_.size())
{
std::clog << "<- " << sequence_ << " schedule async_read body" << std::endl;
boost::asio::async_read(socket_, response_,
boost::asio::transfer_at_least(content_length - response_.size()),
yield[err]);
check_error_and_timeout(err, timeout_);
}
// 3. after read all header and body write all data to server sock
boost::asio::async_write(server_socket_, response_, yield[err]);
}
首先,你不能复制 streambuf - 它已经删除了复制构造函数。
我建议使用 asio::buffer 使用您自己的缓冲区并解析它。但是有了这个你将无法使用async_read_until。此外,您可以将消费复制到您自己的缓冲区中,然后在需要时发送它。
Lifehack:您可以使用 buffer_cast 获取数据而不消耗它,但请注意它不是安全的方式,因为它没有记录并且可能会损坏(但它对我有用很多次并且多次升级):
// req_buf is a streambuf
const char *req_data = boost::asio::buffer_cast<const char *>( req_buf.data() );
auto sz = req_buf.size();
再次注意:这个转换是有效的,因为 streambuf 实现了一些概念,但它没有直接记录。另外,请记住 req_data 可以在任何 req_buf 修改后失效。
可以使用boost::asio::buffer_copy()
复制Asio缓冲区的内容。例如,如果希望将一个 streambuf
的内容复制到另一个 streambuf
.
boost::asio::streambuf source, target;
...
std::size_t bytes_copied = buffer_copy(
target.prepare(source.size()), // target's output sequence
source.data()); // source's input sequence
// Explicitly move target's output sequence to its input sequence.
target.commit(bytes_copied);
可以使用类似的方法从 streambuf
复制到 Asio 支持可变缓冲区的任何类型。例如,将内容复制到 std::vector<char>
:
boost::asio::streambuf source;
...
std::vector<char> target(source.size());
buffer_copy(boost::asio::buffer(target), source.data());
一个值得注意的例外是 Asio 不支持为 std::string
返回可变缓冲区。但是,仍然可以通过迭代器完成复制到 std::string
:
boost::asio::streambuf source;
...
std::string target{
buffers_begin(source.data()),
buffers_end(source.data())
};
这里是一个例子demonstrating将内容从boost::asio::streambuf
复制到其他各种类型:
#include <iostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>
int main()
{
const std::string expected = "This is a demo";
// Populate source's input sequence.
boost::asio::streambuf source;
std::ostream ostream(&source);
ostream << expected;
// streambuf example
{
boost::asio::streambuf target;
target.commit(buffer_copy(
target.prepare(source.size()), // target's output sequence
source.data())); // source's input sequence
// Verify the contents are equal.
assert(std::equal(
buffers_begin(target.data()),
buffers_end(target.data()),
begin(expected)
));
}
// std::vector example
{
std::vector<char> target(source.size());
buffer_copy(boost::asio::buffer(target), source.data());
// Verify the contents are equal.
assert(std::equal(begin(target), end(target), begin(expected)));
}
// std::string example
{
// Copy directly into std::string. Asio does not support
// returning a mutable buffer for std::string.
std::string target{
buffers_begin(source.data()),
buffers_end(source.data())
};
// Verify the contents are equal.
assert(std::equal(begin(target), end(target), begin(expected)));
}
}