Opened 7 years ago

Last modified 6 years ago

#12131 new Bugs

Boost Asio async HTTP Client example source code incorrect

Reported by: Micah Quinn <micah.quinn@…> Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:

Description

The example source code for the async HTTP Client (http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/example/cpp03/http/client/async_client.cpp) does not output the final read bytes inside "handle_read_content" if the parameter to the transfer_at_least(x) algorithm is greater than one. The code prematurely checks for an error code and disregards any already read data.

The current code in question:

void handle_read_content(const boost::system::error_code& err)
  {
    if (!err)
    {
      // Write all of the data that has been read so far.
      std::cout << &response_;

      // Continue reading remaining data until EOF.
      boost::asio::async_read(socket_, response_,
          boost::asio::transfer_at_least(1),
          boost::bind(&client::handle_read_content, this,
            boost::asio::placeholders::error));
    }
    else if (err != boost::asio::error::eof)
    {
      std::cout << "Error: " << err << "\n";
    }
  }

One possible "fix" for the code would be:

 void handle_read_content(const boost::system::error_code& err)
  {

    // Write all of the data that has been read so far.
    if ( response_.size() > 0 )
      std::cout << &response_;

    if (!err)
    {
      // Continue reading remaining data until EOF.
      boost::asio::async_read(socket_, response_,
          boost::asio::transfer_at_least(1024),
          boost::bind(&client::handle_read_content, this,
            boost::asio::placeholders::error));
    }
    else if (err != boost::asio::error::eof)
    {
      std::cout << "Error: " << err << "\n";
    }
  }

Change History (1)

comment:1 by anonymous, 6 years ago

Component: Documentationasio
Owner: changed from Matias Capeletto to chris_kohlhoff
Note: See TracTickets for help on using tickets.