@@ -142,6 +142,7 @@ std::future<response> client::impl::execute(std::shared_ptr<request_context> con
142142 context->request_ .append_header (" User-Agent" , options_.user_agent ());
143143 }
144144
145+ // Get the host and port from the request and resolve
145146auto url = context->request_ .url ();
146147auto host = url.host ()?
147148uri::string_type (std::begin (*url.host ()),std::end (*url.host ())) :uri::string_type ();
@@ -164,23 +165,25 @@ void client::impl::connect(const boost::system::error_code &ec,
164165return ;
165166 }
166167
168+ // make a connection to an endpoint
167169auto host = context->request_ .url ().host ();
168170 tcp::endpointendpoint (*endpoint_iterator);
169171 context->connection_ ->async_connect (endpoint,
170- std::string (std::begin (*host),std::end (*host)),
171- strand_.wrap ([=] (const boost::system::error_code &ec) {
172- if (ec && endpoint_iterator !=tcp::resolver::iterator ()) {
173- // copy iterator because it is const after the lambda
174- // capture
175- auto it = endpoint_iterator;
176- boost::system::error_code ignore;
177- connect (ignore, ++it, context);
178- return ;
179- }
180-
181- write_request (ec, context);
182- }));
183- }
172+ std::string (std::begin (*host),std::end (*host)),
173+ strand_.wrap ([=] (const boost::system::error_code &ec) {
174+ // If there is no connection, try again on another endpoint
175+ if (ec && endpoint_iterator !=tcp::resolver::iterator ()) {
176+ // copy iterator because it is const after the lambda
177+ // capture
178+ auto it = endpoint_iterator;
179+ boost::system::error_code ignore;
180+ connect (ignore, ++it, context);
181+ return ;
182+ }
183+
184+ write_request (ec, context);
185+ }));
186+ }
184187
185188void client::impl::write_request (const boost::system::error_code &ec,
186189 std::shared_ptr<request_context> context) {
@@ -189,6 +192,7 @@ void client::impl::write_request(const boost::system::error_code &ec,
189192return ;
190193 }
191194
195+ // write the request to an I/O stream.
192196 std::ostreamrequest_stream (&context->request_buffer_ );
193197 request_stream << context->request_ ;
194198if (!request_stream) {
@@ -210,11 +214,13 @@ void client::impl::write_body(const boost::system::error_code &ec,
210214return ;
211215 }
212216
217+ // update progress
213218 context->total_bytes_written_ += bytes_written;
214219if (auto progress = context->options_ .progress ()) {
215220progress (client_message::transfer_direction::bytes_written, context->total_bytes_written_ );
216221 }
217222
223+ // write the body to an I/O stream
218224 std::ostreamrequest_stream (&context->request_buffer_ );
219225// TODO write payload to request_buffer_
220226if (!request_stream) {
@@ -236,11 +242,13 @@ void client::impl::read_response(const boost::system::error_code &ec,
236242return ;
237243 }
238244
245+ // update progress.
239246 context->total_bytes_written_ += bytes_written;
240247if (auto progress = context->options_ .progress ()) {
241248progress (client_message::transfer_direction::bytes_written, context->total_bytes_written_ );
242249 }
243250
251+ // Create a response object and fill it with the status from the server.
244252 std::shared_ptr<response>res (new response{});
245253 context->connection_ ->async_read_until (context->response_buffer_ ,
246254" \r\n " ,
@@ -259,6 +267,7 @@ void client::impl::read_response_status(const boost::system::error_code &ec,
259267return ;
260268 }
261269
270+ // Update the reponse status.
262271 std::istreamis (&context->response_buffer_ );
263272 string_type version;
264273 is >> version;
@@ -271,6 +280,7 @@ void client::impl::read_response_status(const boost::system::error_code &ec,
271280 res->set_status (network::http::v2::status::code (status));
272281 res->set_status_message (boost::trim_copy (message));
273282
283+ // Read the response headers.
274284 context->connection_ ->async_read_until (context->response_buffer_ ,
275285" \r\n\r\n " ,
276286 strand_.wrap ([=] (const boost::system::error_code &ec,
@@ -299,6 +309,7 @@ void client::impl::read_response_headers(const boost::system::error_code &ec,
299309 res->add_header (key, value);
300310 }
301311
312+ // read the response body.
302313 context->connection_ ->async_read (context->response_buffer_ ,
303314 strand_.wrap ([=] (const boost::system::error_code &ec,
304315 std::size_t bytes_read) {
@@ -307,6 +318,8 @@ void client::impl::read_response_headers(const boost::system::error_code &ec,
307318}
308319
309320namespace {
321+ // I don't want to to delimit with newlines when using the input
322+ // stream operator, so that's why I wrote this function.
310323std::istream &getline_with_newline (std::istream &is, std::string &line) {
311324 line.clear ();
312325
@@ -332,11 +345,13 @@ void client::impl::read_response_body(const boost::system::error_code &ec,
332345 std::size_t bytes_read,
333346 std::shared_ptr<request_context> context,
334347 std::shared_ptr<response> res) {
348+ // update progress.
335349 context->total_bytes_read_ += bytes_read;
336350if (auto progress = context->options_ .progress ()) {
337351progress (client_message::transfer_direction::bytes_read, context->total_bytes_read_ );
338352 }
339353
354+ // If there's no data else to read, then set the response and exit.
340355if (bytes_read ==0 ) {
341356 context->response_promise_ .set_value (*res);
342357return ;
@@ -348,11 +363,12 @@ void client::impl::read_response_body(const boost::system::error_code &ec,
348363 res->append_body (line);
349364 }
350365
366+ // Keep reading the response body until we have nothing else to read.
351367 context->connection_ ->async_read (context->response_buffer_ ,
352- strand_.wrap ([=] (const boost::system::error_code &ec,
353- std::size_t bytes_read) {
354- read_response_body (ec, bytes_read, context, res);
355- }));
368+ strand_.wrap ([=] (const boost::system::error_code &ec,
369+ std::size_t bytes_read) {
370+ read_response_body (ec, bytes_read, context, res);
371+ }));
356372}
357373
358374client::client (client_options options)