Skip to content

Commit 184d278

Browse files
committed
Updated some HTTP client errors.
1 parent 0739a4a commit 184d278

File tree

2 files changed

+72
-52
lines changed

2 files changed

+72
-52
lines changed

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

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

66
#include <future>
7+
#include <boost/algorithm/string/predicate.hpp>
78
#include <network/uri.hpp>
89
#include <network/config.hpp>
910
#include <network/http/v2/client/client.hpp>
@@ -110,6 +111,8 @@ namespace network {
110111

111112
void client::impl::write_request(const boost::system::error_code &ec) {
112113
if (ec) {
114+
response_promise_.set_exception(std::make_exception_ptr(
115+
boost::system::system_error(ec)));
113116
return;
114117
}
115118

@@ -123,6 +126,8 @@ namespace network {
123126
void client::impl::read_response_status(const boost::system::error_code &ec,
124127
std::size_t) {
125128
if (ec) {
129+
response_promise_.set_exception(std::make_exception_ptr(
130+
boost::system::system_error(ec)));
126131
return;
127132
}
128133

@@ -138,6 +143,8 @@ namespace network {
138143
void client::impl::read_response_headers(const boost::system::error_code &ec,
139144
std::size_t) {
140145
if (ec) {
146+
response_promise_.set_exception(std::make_exception_ptr(
147+
boost::system::system_error(ec)));
141148
return;
142149
}
143150

@@ -152,27 +159,32 @@ namespace network {
152159
std::future<response> client::impl::do_request(method met,
153160
request req,
154161
request_options options) {
155-
std::future<response> response = response_promise_.get_future();
162+
std::future<response> res = response_promise_.get_future();
156163

157164
req.method(met);
158165
std::ostream request_stream(&request_);
159166
request_stream << req;
160167
if (!request_stream) {
161168
// set error
162-
//response_promise_.set_value(response());
163-
return response;
169+
response_promise_.set_value(response());
170+
return res;
164171
}
165172

166-
uri_builder builder;
167-
for (auto header : req.headers()) {
168-
// boost::iequals(header.first, "host")
169-
if ((header.first == "Host") || (header.first == "host")) {
170-
builder
171-
.authority(header.second)
172-
;
173-
break;
174-
}
173+
auto it = std::find_if(std::begin(req.headers()), std::end(req.headers()),
174+
[] (const std::pair<uri::string_type, uri::string_type> &header) {
175+
return (boost::iequals(header.first, "host"));
176+
});
177+
if (it == std::end(req.headers())) {
178+
// set error
179+
response_promise_.set_value(response());
180+
return res;
175181
}
182+
183+
uri_builder builder;
184+
builder
185+
.authority(it->second)
186+
;
187+
176188
auto auth = builder.uri();
177189
auto host = auth.host()?
178190
uri::string_type(std::begin(*auth.host()), std::end(*auth.host())) : uri::string_type();
@@ -183,15 +195,22 @@ namespace network {
183195
tcp::resolver::iterator endpoint_iterator) {
184196
if (ec) {
185197
if (endpoint_iterator == tcp::resolver::iterator()) {
198+
response_promise_.set_exception(
199+
std::make_exception_ptr(
200+
connection_error(client_error::host_not_found)));
186201
return;
187202
}
203+
204+
response_promise_.set_exception(
205+
std::make_exception_ptr(
206+
boost::system::system_error(ec)));
188207
return;
189208
}
190209

191210
connect(ec, endpoint_iterator);
192211
});
193212

194-
return response;
213+
return res;
195214
}
196215

197216
client::client(client_options options)

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

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ namespace network {
2424
* \brief An enumeration of all types of client error.
2525
*/
2626
enum class client_error {
27-
// url
28-
invalid_url,
27+
// url
28+
invalid_url,
2929

30-
// resolution
31-
resolver_error,
30+
// resolution
31+
resolver_error,
3232

33-
// connection
34-
connection_timeout,
35-
https_not_supported,
33+
// connection
34+
host_not_found,
35+
connection_timeout,
36+
https_not_supported,
3637

3738
// response
3839
invalid_version,
@@ -60,15 +61,15 @@ namespace network {
6061

6162
public:
6263

63-
/**
64-
* \brief Constructor.
65-
*/
66-
explicit invalid_url();
64+
/**
65+
* \brief Constructor.
66+
*/
67+
explicit invalid_url();
6768

68-
/**
69-
* \brief Destructor.
70-
*/
71-
virtual ~invalid_url() noexcept;
69+
/**
70+
* \brief Destructor.
71+
*/
72+
virtual ~invalid_url() noexcept;
7273

7374
};
7475

@@ -81,15 +82,15 @@ namespace network {
8182

8283
public:
8384

84-
/**
85-
* \brief Constructor.
86-
*/
87-
explicit resolver_error();
85+
/**
86+
* \brief Constructor.
87+
*/
88+
explicit resolver_error();
8889

89-
/**
90-
* \brief Destructor.
91-
*/
92-
virtual ~resolver_error() noexcept;
90+
/**
91+
* \brief Destructor.
92+
*/
93+
virtual ~resolver_error() noexcept;
9394

9495
};
9596

@@ -102,16 +103,16 @@ namespace network {
102103

103104
public:
104105

105-
/**
106-
* \brief Constructor.
107-
* \param The client_error code.
108-
*/
109-
explicit connection_error(client_error error);
106+
/**
107+
* \brief Constructor.
108+
* \param The client_error code.
109+
*/
110+
explicit connection_error(client_error error);
110111

111-
/**
112-
* \brief Destructor.
113-
*/
114-
virtual ~connection_error() noexcept;
112+
/**
113+
* \brief Destructor.
114+
*/
115+
virtual ~connection_error() noexcept;
115116

116117
};
117118

@@ -123,15 +124,15 @@ namespace network {
123124

124125
public:
125126

126-
/**
127-
* \brief Constructor.
128-
* \param The client_error code.
129-
*/
127+
/**
128+
* \brief Constructor.
129+
* \param The client_error code.
130+
*/
130131
explicit response_error(client_error error);
131132

132-
/**
133-
* \brief Destructor.
134-
*/
133+
/**
134+
* \brief Destructor.
135+
*/
135136
virtual ~response_error() noexcept;
136137

137138
};

0 commit comments

Comments
 (0)