title: slow http client based on boost::asio - (Chunked Transfer)
body: <p>I am using the following code, taken from boost tutorials, to get a json string from the server. </p>
  
  <p>The problem is that it takes some time to execute, ie more than 2 seconds to finish and both client and server are on localhost. If I remove the last 2 lines of the program, ie this while: </p>
  
  <blockquote>
    <p>while (boost::asio::read(socket, response,
                  boost::asio::transfer_at_least(1), error))</p>
  </blockquote>
  
  <p>the program executes extremely fast. What might the problem be?</p>
  
  <pre><code>        boost::asio::streambuf response;
          boost::asio::read_until(socket, response, "\r\n");
  
          std::istream response_stream(&amp;response);
          std::string http_version;
          response_stream &gt;&gt; http_version;
          unsigned int status_code;
          response_stream &gt;&gt; status_code;
          std::string status_message;
          std::getline(response_stream, status_message);
          if (!response_stream || http_version.substr(0, 5) != "HTTP/")
          {
            std::cout &lt;&lt; "Invalid response\n";
            return 1;
          }
          if (status_code != 200)
          {
            std::cout &lt;&lt; "Response returned with status code " &lt;&lt; status_code &lt;&lt; "\n";
            return 1;
          }
  
  
          boost::asio::read_until(socket, response, "\r\n\r\n");
  
          // Process the response headers.
          std::string header;
          while (std::getline(response_stream, header) &amp;&amp; header != "\r");
  
  
          if (response.size() &gt; 0)
            std::cout &lt;&lt; &amp;response;
  
          // Read until EOF, writing data to output as we go.
          boost::system::error_code error;
          while (boost::asio::read(socket, response,
                boost::asio::transfer_at_least(1), error))
            std::cout &lt;&lt; &amp;response;
          if (error != boost::asio::error::eof)
            throw boost::system::system_error(error);
  </code></pre>
  
  <p>a tcpdump to show some data from the server</p>
  
  <pre><code>HTTP/1.1 200 OK
  Connection: close
  Content-Length: 42
  Server: C/1.1
  Date: Thu, 24 Nov 2016 07:47:27 GMT
  
  {"Out":[1],"In":[1,2,3,4,5,6]}
  </code></pre>