Skip to content

Commit 619255a

Browse files
committed
Updated the client and response classes in order to make a successful HEAD request.
1 parent a8bad17 commit 619255a

File tree

6 files changed

+112
-52
lines changed

6 files changed

+112
-52
lines changed

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

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
#include <future>
7+
#include <boost/asio/strand.hpp>
8+
#include <boost/algorithm/string/trim.hpp>
79
#include <boost/algorithm/string/predicate.hpp>
810
#include <network/uri.hpp>
911
#include <network/config.hpp>
@@ -34,22 +36,25 @@ namespace network {
3436
std::size_t bytes_written);
3537

3638
void read_response_status(const boost::system::error_code &ec,
37-
std::size_t bytes_written);
39+
std::size_t bytes_written,
40+
std::shared_ptr<response> res);
3841

3942
void read_response_headers(const boost::system::error_code &ec,
40-
std::size_t bytes_read);
43+
std::size_t bytes_read,
44+
std::shared_ptr<response> res);
4145

4246
std::future<response> do_request(method method_, request request_, request_options options);
4347

4448
client_options options_;
4549
boost::asio::io_service io_service_;
50+
boost::asio::io_service::strand strand_;
4651
std::unique_ptr<async_resolver> resolver_;
4752
std::unique_ptr<async_connection> connection_;
4853
std::unique_ptr<boost::asio::io_service::work> sentinel_;
4954
std::thread lifetime_thread_;
5055

51-
boost::asio::streambuf request_;
5256
std::promise<response> response_promise_;
57+
boost::asio::streambuf request_;
5358
boost::asio::streambuf response_;
5459

5560
// promise
@@ -59,6 +64,7 @@ namespace network {
5964

6065
client::impl::impl(client_options options)
6166
: options_(options)
67+
, strand_(io_service_)
6268
, resolver_(new tcp_resolver(io_service_, options.cache_resolved()))
6369
, connection_(new normal_connection(io_service_))
6470
, sentinel_(new boost::asio::io_service::work(io_service_))
@@ -87,9 +93,10 @@ namespace network {
8793

8894
tcp::endpoint endpoint(*endpoint_iterator);
8995
connection_->async_connect(endpoint,
90-
[=] (const boost::system::error_code &ec) {
91-
write_request(ec);
92-
});
96+
strand_.wrap(
97+
[=] (const boost::system::error_code &ec) {
98+
write_request(ec);
99+
}));
93100
}
94101

95102
void client::impl::write_request(const boost::system::error_code &ec) {
@@ -100,10 +107,11 @@ namespace network {
100107
}
101108

102109
connection_->async_write(request_,
103-
[=] (const boost::system::error_code &ec,
104-
std::size_t bytes_written) {
105-
read_response(ec, bytes_written);
106-
});
110+
strand_.wrap(
111+
[=] (const boost::system::error_code &ec,
112+
std::size_t bytes_written) {
113+
read_response(ec, bytes_written);
114+
}));
107115
}
108116

109117
void client::impl::read_response(const boost::system::error_code &ec, std::size_t) {
@@ -113,36 +121,49 @@ namespace network {
113121
return;
114122
}
115123

124+
std::shared_ptr<response> res(new response{});
116125
connection_->async_read_until(response_,
117126
"\r\n",
118-
[=] (const boost::system::error_code &ec,
119-
std::size_t bytes_read) {
120-
read_response_status(ec, bytes_read);
121-
});
127+
strand_.wrap(
128+
[=] (const boost::system::error_code &ec,
129+
std::size_t bytes_read) {
130+
read_response_status(ec, bytes_read, res);
131+
}));
122132
}
123133

124134
void client::impl::read_response_status(const boost::system::error_code &ec,
125-
std::size_t) {
135+
std::size_t,
136+
std::shared_ptr<response> res) {
126137
if (ec) {
127138
response_promise_.set_exception(std::make_exception_ptr(
128139
std::system_error(ec.value(), std::system_category())));
129140
return;
130141
}
131142

132143
std::istream is(&response_);
133-
std::string status;
134-
std::getline(is, status);
144+
std::string version;
145+
is >> version;
146+
unsigned int status;
147+
is >> status;
148+
std::string message;
149+
std::getline(is, message);
150+
151+
res->set_version(version);
152+
res->set_status(network::http::v2::status::code(status));
153+
res->set_status_message(boost::trim_copy(message));
135154

136155
connection_->async_read_until(response_,
137156
"\r\n",
138-
[=] (const boost::system::error_code &ec,
139-
std::size_t bytes_read) {
140-
read_response_headers(ec, bytes_read);
141-
});
157+
strand_.wrap(
158+
[=] (const boost::system::error_code &ec,
159+
std::size_t bytes_read) {
160+
read_response_headers(ec, bytes_read, res);
161+
}));
142162
}
143163

144164
void client::impl::read_response_headers(const boost::system::error_code &ec,
145-
std::size_t) {
165+
std::size_t,
166+
std::shared_ptr<response> res) {
146167
if (ec) {
147168
response_promise_.set_exception(std::make_exception_ptr(
148169
std::system_error(ec.value(), std::system_category())));
@@ -152,11 +173,12 @@ namespace network {
152173
// fill headers
153174
connection_->async_read_until(response_,
154175
"\r\n\r\n",
155-
[=] (const boost::system::error_code &ec,
156-
std::size_t bytes_read) {
157-
// um...
158-
response_promise_.set_value(response());
159-
});
176+
strand_.wrap(
177+
[=] (const boost::system::error_code &ec,
178+
std::size_t bytes_read) {
179+
// um...
180+
response_promise_.set_value(*res);
181+
}));
160182
}
161183

162184
std::future<response> client::impl::do_request(method met,
@@ -193,10 +215,11 @@ namespace network {
193215
auto port = auth.port<std::uint16_t>()? *auth.port<std::uint16_t>() : 80;
194216

195217
resolver_->async_resolve(host, port,
196-
[=](const boost::system::error_code &ec,
197-
tcp::resolver::iterator endpoint_iterator) {
198-
connect(ec, endpoint_iterator);
199-
});
218+
strand_.wrap(
219+
[=](const boost::system::error_code &ec,
220+
tcp::resolver::iterator endpoint_iterator) {
221+
connect(ec, endpoint_iterator);
222+
}));
200223

201224
return res;
202225
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace network {
3131
namespace v2 {
3232
/**
3333
* \ingroup http_client
34-
* \class client_options network/http/v2/client/client.hpp
34+
* \class client_options network/http/v2/client/client.hpp network/http/v2/client.hpp
3535
* \brief A set of options to configure an HTTP client.
3636
*/
3737
class client_options {
@@ -234,7 +234,7 @@ namespace network {
234234

235235
/**
236236
* \ingroup http_client
237-
* \class client network/http/v2/client/client.hpp
237+
* \class client network/http/v2/client/client.hpp network/http/v2/client.hpp
238238
* \brief A class that encapsulates the operations and methods
239239
* for communicating with an HTTP server.
240240
*/
@@ -246,18 +246,19 @@ namespace network {
246246
public:
247247

248248
/**
249-
* \typedef The client's string_type.
249+
* \typedef string_type
250+
* \brief The client string_type.
250251
*/
251252
typedef request::string_type string_type;
252253

253254
/**
254-
* Constructor.
255-
* \params options Client options.
255+
* \brief Constructor.
256+
* \param options Client options.
256257
*/
257258
explicit client(client_options options = client_options());
258259

259260
/**
260-
* Destructor.
261+
* \brief Destructor.
261262
*/
262263
~client() noexcept;
263264

http/src/network/http/v2/client/request.hpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace network {
3535
namespace v2 {
3636
/**
3737
* \ingroup http_client
38-
* \class request_options network/http/v2/client/request.hpp
38+
* \class request_options network/http/v2/client/request.hpp network/http/v2/client.hpp
3939
* \brief A class used to configure an HTTP request.
4040
*/
4141
class request_options {
@@ -135,7 +135,7 @@ namespace network {
135135

136136
/**
137137
* \ingroup http_client
138-
* \class byte_source network/http/v2/client/request.hpp
138+
* \class byte_source network/http/v2/client/request.hpp network/http/v2/client.hpp
139139
* \brief An abstract class that allows a request object to read
140140
* data from any source.
141141
*/
@@ -170,7 +170,7 @@ namespace network {
170170

171171
/**
172172
* \ingroup http_client
173-
* \class string_byte_source network/http/v2/client/request.hpp
173+
* \class string_byte_source network/http/v2/client/request.hpp network/http/v2/client.hpp
174174
* \brief A class that wraps a string as a byte source.
175175
*/
176176
class string_byte_source : public byte_source {
@@ -197,7 +197,7 @@ namespace network {
197197

198198
/**
199199
* \ingroup http_client
200-
* \class request network/http/v2/client/request.hpp
200+
* \class request network/http/v2/client/request.hpp network/http/v2/client.hpp
201201
* \brief A class that models an HTTP request.
202202
*/
203203
class request {
@@ -209,16 +209,19 @@ namespace network {
209209
* \brief The request string type.
210210
*/
211211
typedef byte_source::string_type string_type;
212+
212213
/**
213214
* \typedef headers_type
214215
* \brief The request headers type.
215216
*/
216217
typedef std::vector<std::pair<string_type, string_type>> headers_type;
218+
217219
/**
218220
* \typedef headers_iterator
219221
* \brief The request headers iterator.
220222
*/
221223
typedef headers_type::iterator headers_iterator;
224+
222225
/**
223226
* \typedef const_headers_iterator
224227
* \brief The request headers const_iterator.
@@ -307,7 +310,8 @@ namespace network {
307310

308311
/**
309312
* \brief Sets the HTTP request method.
310-
* \param method THe HTTP request method.
313+
* \param method The HTTP request method.
314+
* \returns *this
311315
*/
312316
request &method(network::http::v2::method method) {
313317
method_ = method;
@@ -322,11 +326,20 @@ namespace network {
322326
return method_;
323327
}
324328

325-
request &path(std::string path) {
329+
/**
330+
* \brief Sets the HTTP path.
331+
* \param path The HTTP path.
332+
* \returns *this
333+
*/
334+
request &path(string_type path) {
326335
path_ = path;
327336
return *this;
328337
}
329338

339+
/**
340+
* \brief Gets the HTTP path.
341+
* \returns The HTTP path.
342+
*/
330343
string_type path() const {
331344
return path_;
332345
}

http/src/network/http/v2/client/response.hpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,25 @@ namespace network {
3232

3333
/**
3434
* \typedef string_type
35-
* \brief The responses string_type.
35+
* \brief The response string_type.
3636
*/
3737
typedef std::string string_type;
3838

3939
/**
4040
* \typedef headers_type
41-
* \brief
41+
* \brief The response headers type.
4242
*/
4343
typedef std::vector<std::pair<string_type, string_type>> headers_type;
4444

4545
/**
4646
* \typedef headers_iterator
47+
* \brief The response headers iterator.
4748
*/
4849
typedef headers_type::iterator headers_iterator;
4950

5051
/**
5152
* \typedef const_headers_iterator
53+
* \brief The response headers const_iterator.
5254
*/
5355
typedef headers_type::const_iterator const_headers_iterator;
5456

@@ -102,6 +104,14 @@ namespace network {
102104
swap(headers_, other.headers_);
103105
}
104106

107+
/**
108+
* \brief Sets the HTTP version.
109+
* \param version The HTTP version (1.0 or 1.1).
110+
*/
111+
void set_version(const string_type &version) {
112+
version_ = version;
113+
}
114+
105115
/**
106116
* \brief Returns the HTTP version.
107117
* \returns The HTTP version.
@@ -110,6 +120,14 @@ namespace network {
110120
return version_;
111121
}
112122

123+
/**
124+
* \brief Sets the HTTP response status code.
125+
* \param status The HTTP response status code.
126+
*/
127+
void set_status(network::http::v2::status::code status) {
128+
status_ = status;
129+
}
130+
113131
/**
114132
* \brief Returns the HTTP response status.
115133
* \returns The status code.
@@ -118,6 +136,14 @@ namespace network {
118136
return status_;
119137
}
120138

139+
/**
140+
* \brief Sets the HTTP response status message.
141+
* \param status The HTTP response status message.
142+
*/
143+
void set_status_message(const string_type &status_message) {
144+
status_message_ = status_message;
145+
}
146+
121147
/**
122148
* \brief Returns the HTTP response status message.
123149
* \returns The status message.
@@ -135,9 +161,6 @@ namespace network {
135161

136162
std::future<string_type> read_body(std::size_t length) const;
137163

138-
// set_version
139-
// set_status
140-
// set_status_message
141164
// add_header
142165
// set_body
143166
// append_body

0 commit comments

Comments
 (0)