我在boost asio上使用一些业务逻辑来实现http代理服务器。
在要点(1)中,boost::asio::streambuf response_包含http头和http的一部分。
使用http_response::parse缓冲区解析后,boost::asio::streambuf response_为空。
在(2)处,我检查所有业务逻辑,并读取头部中是否有Content-Length头的正文。
然后,如果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]);
}发布于 2016-12-19 15:44:37
可以使用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())
};下面是一个示范将内容从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)));
  }
}发布于 2016-12-19 14:10:09
首先,不能复制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修改后失效。
https://stackoverflow.com/questions/41220792
复制相似问题