title: How copy or reuse boost::asio::streambuf?
body: <p>I'm implementing http proxy server with some business logic on boost asio.</p>
  
  <p>In point (1) <strong>boost::asio::streambuf response_</strong> contains http headers and part of http body.</p>
  
  <p>After parsing with <strong>http_response::parse</strong> buffer <strong>boost::asio::streambuf response_</strong> is empty. </p>
  
  <p>At (2) i check all business logic and read body if there was <strong>Content-Length</strong> header in headers.</p>
  
  <p>Then if <strong>response_</strong> data fits specific condtions i want to send the original <strong>response_</strong> buffer to another socket (3).</p>
  
  <p>The problem is that buffer is empty after parsing. 
  Is there a way to copy <strong>boost::asio::streambuf</strong> to reuse data?</p>
  
  <pre><code>void http_response::parse(boost::asio::streambuf&amp; buffer)
  {
      std::istream response_stream(&amp;buffer);
      response_stream &gt;&gt; version_;
      response_stream &gt;&gt; status_code_;
      response_stream &gt;&gt; status_message_;
      std::string key;
      std::string value;
      std::string header;
      std::getline(response_stream, header);
      while (std::getline(response_stream, header) &amp;&amp; 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&amp; hostname, const std::string&amp; path,
      const std::string&amp; server, int port,
      boost::asio::io_service::strand&amp; strand,
      boost::asio::yield_context&amp; yield)
  {
      ...
      http_response response;
      boost::asio::streambuf response_;
      // async read http header from socket
      std::clog &lt;&lt; "&lt;- " &lt;&lt; sequence_ &lt;&lt; " schedule async_read_until head" &lt;&lt; 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 &gt; 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() &amp;&amp; content_length &gt; response_.size())
      {
          std::clog &lt;&lt; "&lt;- " &lt;&lt; sequence_ &lt;&lt; " schedule async_read body" &lt;&lt; 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]);
  }
  </code></pre>