Skip to content

Commit ad273ec

Browse files
committed
Added some comments; fixed tests so that they compile for Boost.Optional in 1.56.
1 parent ccebaeb commit ad273ec

File tree

4 files changed

+111
-95
lines changed

4 files changed

+111
-95
lines changed

http/src/http/v2/client/client.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
145146
auto url = context->request_.url();
146147
auto host = url.host()?
147148
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,
164165
return;
165166
}
166167

168+
// make a connection to an endpoint
167169
auto host = context->request_.url().host();
168170
tcp::endpoint endpoint(*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

185188
void 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,
189192
return;
190193
}
191194

195+
// write the request to an I/O stream.
192196
std::ostream request_stream(&context->request_buffer_);
193197
request_stream << context->request_;
194198
if (!request_stream) {
@@ -210,11 +214,13 @@ void client::impl::write_body(const boost::system::error_code &ec,
210214
return;
211215
}
212216

217+
// update progress
213218
context->total_bytes_written_ += bytes_written;
214219
if (auto progress = context->options_.progress()) {
215220
progress(client_message::transfer_direction::bytes_written, context->total_bytes_written_);
216221
}
217222

223+
// write the body to an I/O stream
218224
std::ostream request_stream(&context->request_buffer_);
219225
// TODO write payload to request_buffer_
220226
if (!request_stream) {
@@ -236,11 +242,13 @@ void client::impl::read_response(const boost::system::error_code &ec,
236242
return;
237243
}
238244

245+
// update progress.
239246
context->total_bytes_written_ += bytes_written;
240247
if (auto progress = context->options_.progress()) {
241248
progress(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,
259267
return;
260268
}
261269

270+
// Update the reponse status.
262271
std::istream is(&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

309320
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.
310323
std::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;
336350
if (auto progress = context->options_.progress()) {
337351
progress(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.
340355
if (bytes_read == 0) {
341356
context->response_promise_.set_value(*res);
342357
return;
@@ -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

358374
client::client(client_options options)

http/src/network/http/v2/client/connection/tcp_resolver.hpp

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,81 +24,81 @@
2424
#include <network/http/v2/client/connection/endpoint_cache.hpp>
2525

2626
namespace network {
27-
namespace http {
28-
inline namespace v2 {
29-
namespace client_connection {
30-
/**
31-
* \class tcp_resolver network/http/v2/client/connection/tcp_resolver.hpp
32-
* \brief Resolves and maintains a cache of hosts.
33-
*/
34-
class tcp_resolver : public async_resolver {
35-
36-
tcp_resolver(const tcp_resolver &) = delete;
37-
tcp_resolver &operator = (const tcp_resolver &) = delete;
38-
39-
public:
40-
41-
using async_resolver::resolver;
42-
using async_resolver::resolver_iterator;
43-
using async_resolver::resolve_callback;
44-
45-
/**
46-
* \brief Constructor.
47-
*/
48-
tcp_resolver(boost::asio::io_service &service, bool cache_resolved = false)
49-
: resolver_(service)
50-
, cache_resolved_(cache_resolved) {
51-
52-
}
53-
54-
/**
55-
* \brief Destructor.
56-
*/
57-
virtual ~tcp_resolver() noexcept {
58-
59-
}
60-
61-
virtual void async_resolve(const std::string &host, std::uint16_t port,
62-
resolve_callback handler) {
63-
if (cache_resolved_) {
64-
auto it = endpoint_cache_.find(host);
65-
if (it != endpoint_cache_.end()) {
66-
boost::system::error_code ec;
67-
handler(ec, it->second);
68-
return;
69-
}
70-
}
71-
72-
resolver::query query(host, std::to_string(port));
73-
resolver_.async_resolve(query,
74-
[host, handler, this](const boost::system::error_code &ec,
75-
resolver_iterator endpoint_iterator) {
76-
if (ec) {
77-
handler(ec, resolver_iterator());
78-
}
79-
else {
80-
if (cache_resolved_) {
81-
endpoint_cache_.insert(host, endpoint_iterator);
82-
}
83-
handler(ec, endpoint_iterator);
84-
}
85-
});
86-
}
87-
88-
virtual void clear_resolved_cache() {
89-
endpoint_cache_.clear();
90-
}
91-
92-
private:
93-
94-
resolver resolver_;
95-
bool cache_resolved_;
96-
endpoint_cache endpoint_cache_;
97-
98-
};
99-
} // namespace client_connection
100-
} // namespace v2
101-
} // namespace http
27+
namespace http {
28+
inline namespace v2 {
29+
namespace client_connection {
30+
/**
31+
* \class tcp_resolver network/http/v2/client/connection/tcp_resolver.hpp
32+
* \brief Resolves and maintains a cache of hosts.
33+
*/
34+
class tcp_resolver : public async_resolver {
35+
36+
tcp_resolver(const tcp_resolver &) = delete;
37+
tcp_resolver &operator = (const tcp_resolver &) = delete;
38+
39+
public:
40+
41+
using async_resolver::resolver;
42+
using async_resolver::resolver_iterator;
43+
using async_resolver::resolve_callback;
44+
45+
/**
46+
* \brief Constructor.
47+
*/
48+
tcp_resolver(boost::asio::io_service &service, bool cache_resolved = false)
49+
: resolver_(service)
50+
, cache_resolved_(cache_resolved) {
51+
52+
}
53+
54+
/**
55+
* \brief Destructor.
56+
*/
57+
virtual ~tcp_resolver() noexcept {
58+
59+
}
60+
61+
virtual void async_resolve(const std::string &host, std::uint16_t port,
62+
resolve_callback handler) {
63+
if (cache_resolved_) {
64+
auto it = endpoint_cache_.find(host);
65+
if (it != endpoint_cache_.end()) {
66+
boost::system::error_code ec;
67+
handler(ec, it->second);
68+
return;
69+
}
70+
}
71+
72+
resolver::query query(host, std::to_string(port));
73+
resolver_.async_resolve(query,
74+
[host, handler, this](const boost::system::error_code &ec,
75+
resolver_iterator endpoint_iterator) {
76+
if (ec) {
77+
handler(ec, resolver_iterator());
78+
}
79+
else {
80+
if (cache_resolved_) {
81+
endpoint_cache_.insert(host, endpoint_iterator);
82+
}
83+
handler(ec, endpoint_iterator);
84+
}
85+
});
86+
}
87+
88+
virtual void clear_resolved_cache() {
89+
endpoint_cache_.clear();
90+
}
91+
92+
private:
93+
94+
resolver resolver_;
95+
bool cache_resolved_;
96+
endpoint_cache endpoint_cache_;
97+
98+
};
99+
} // namespace client_connection
100+
} // namespace v2
101+
} // namespace http
102102
} // namespace network
103103

104104
#endif // NETWORK_HTTP_V2_CLIENT_CONNECTION_TCP_RESOLVER_INC

http/test/v2/client/units/request_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ TEST(request_test, get_header) {
186186
;
187187

188188
auto header = instance.header("User-Agent");
189-
ASSERT_TRUE(header);
189+
ASSERT_TRUE(static_cast<bool>(header));
190190
ASSERT_EQ("request_test", *header);
191191
}
192192

uri

Submodule uri updated from 56143ae to 20aad3f

0 commit comments

Comments
 (0)