Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How copy or reuse boost::asio::streambuf?

I'm implementing http proxy server with some business logic on boost asio.

In point (1) boost::asio::streambuf response_ contains http headers and part of http body.

After parsing with http_response::parse buffer boost::asio::streambuf response_ is empty.

At (2) i check all business logic and read body if there was Content-Length header in headers.

Then if response_ data fits specific condtions i want to send the original response_ buffer to another socket (3).

The problem is that buffer is empty after parsing. Is there a way to copy boost::asio::streambuf to reuse data?

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]);
}
like image 734
e.proydakov Avatar asked Dec 10 '22 13:12

e.proydakov


1 Answers

One can use boost::asio::buffer_copy() to copy the contents of Asio buffers. This can be convenient if, for example, one wishes to copy the contents of one streambuf to another 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);

A similar approach can be used to copy from a streambuf into any type for which Asio supports mutable buffers. For example, copying content into a std::vector<char>:

boost::asio::streambuf source;
...
std::vector<char> target(source.size());
buffer_copy(boost::asio::buffer(target), source.data());

One notable exception is that Asio does not support returning a mutable buffer for std::string. However, one can still accomplish copying into std::string via iterators:

boost::asio::streambuf source;
...
std::string target{
  buffers_begin(source.data()),
  buffers_end(source.data())
};

Here is an example demonstrating copying contents from boost::asio::streambuf into various other types:

#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)));
  }
}
like image 116
Tanner Sansbury Avatar answered Dec 13 '22 02:12

Tanner Sansbury