@@ -142,6 +142,7 @@ std::future<response> client::impl::execute(std::shared_ptr<request_context> con
142
142
context->request_ .append_header (" User-Agent" , options_.user_agent ());
143
143
}
144
144
145
+ // Get the host and port from the request and resolve
145
146
auto url = context->request_ .url ();
146
147
auto host = url.host ()?
147
148
uri::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,
164
165
return ;
165
166
}
166
167
168
+ // make a connection to an endpoint
167
169
auto host = context->request_ .url ().host ();
168
170
tcp::endpoint endpoint (*endpoint_iterator);
169
171
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
+ }
184
187
185
188
void client::impl::write_request (const boost::system::error_code &ec,
186
189
std::shared_ptr<request_context> context) {
@@ -189,6 +192,7 @@ void client::impl::write_request(const boost::system::error_code &ec,
189
192
return ;
190
193
}
191
194
195
+ // write the request to an I/O stream.
192
196
std::ostream request_stream (&context->request_buffer_ );
193
197
request_stream << context->request_ ;
194
198
if (!request_stream) {
@@ -210,11 +214,13 @@ void client::impl::write_body(const boost::system::error_code &ec,
210
214
return ;
211
215
}
212
216
217
+ // update progress
213
218
context->total_bytes_written_ += bytes_written;
214
219
if (auto progress = context->options_ .progress ()) {
215
220
progress (client_message::transfer_direction::bytes_written, context->total_bytes_written_ );
216
221
}
217
222
223
+ // write the body to an I/O stream
218
224
std::ostream request_stream (&context->request_buffer_ );
219
225
// TODO write payload to request_buffer_
220
226
if (!request_stream) {
@@ -236,11 +242,13 @@ void client::impl::read_response(const boost::system::error_code &ec,
236
242
return ;
237
243
}
238
244
245
+ // update progress.
239
246
context->total_bytes_written_ += bytes_written;
240
247
if (auto progress = context->options_ .progress ()) {
241
248
progress (client_message::transfer_direction::bytes_written, context->total_bytes_written_ );
242
249
}
243
250
251
+ // Create a response object and fill it with the status from the server.
244
252
std::shared_ptr<response> res (new response{});
245
253
context->connection_ ->async_read_until (context->response_buffer_ ,
246
254
" \r\n " ,
@@ -259,6 +267,7 @@ void client::impl::read_response_status(const boost::system::error_code &ec,
259
267
return ;
260
268
}
261
269
270
+ // Update the reponse status.
262
271
std::istream is (&context->response_buffer_ );
263
272
string_type version;
264
273
is >> version;
@@ -271,6 +280,7 @@ void client::impl::read_response_status(const boost::system::error_code &ec,
271
280
res->set_status (network::http::v2::status::code (status));
272
281
res->set_status_message (boost::trim_copy (message));
273
282
283
+ // Read the response headers.
274
284
context->connection_ ->async_read_until (context->response_buffer_ ,
275
285
" \r\n\r\n " ,
276
286
strand_.wrap ([=] (const boost::system ::error_code &ec,
@@ -299,6 +309,7 @@ void client::impl::read_response_headers(const boost::system::error_code &ec,
299
309
res->add_header (key, value);
300
310
}
301
311
312
+ // read the response body.
302
313
context->connection_ ->async_read (context->response_buffer_ ,
303
314
strand_.wrap ([=] (const boost::system ::error_code &ec,
304
315
std::size_t bytes_read) {
@@ -307,6 +318,8 @@ void client::impl::read_response_headers(const boost::system::error_code &ec,
307
318
}
308
319
309
320
namespace {
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.
310
323
std::istream &getline_with_newline (std::istream &is, std::string &line) {
311
324
line.clear ();
312
325
@@ -332,11 +345,13 @@ void client::impl::read_response_body(const boost::system::error_code &ec,
332
345
std::size_t bytes_read,
333
346
std::shared_ptr<request_context> context,
334
347
std::shared_ptr<response> res) {
348
+ // update progress.
335
349
context->total_bytes_read_ += bytes_read;
336
350
if (auto progress = context->options_ .progress ()) {
337
351
progress (client_message::transfer_direction::bytes_read, context->total_bytes_read_ );
338
352
}
339
353
354
+ // If there's no data else to read, then set the response and exit.
340
355
if (bytes_read == 0 ) {
341
356
context->response_promise_ .set_value (*res);
342
357
return ;
@@ -348,11 +363,12 @@ void client::impl::read_response_body(const boost::system::error_code &ec,
348
363
res->append_body (line);
349
364
}
350
365
366
+ // Keep reading the response body until we have nothing else to read.
351
367
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
+ }));
356
372
}
357
373
358
374
client::client (client_options options)
0 commit comments