Skip to content

Commit 0610d08

Browse files
committed
Updated errors and the HTTP request.
1 parent 88f6faf commit 0610d08

File tree

12 files changed

+113
-62
lines changed

12 files changed

+113
-62
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ namespace network {
1919

2020
std::string client_category_impl::message(int ev) const {
2121
switch (client_error(ev)) {
22-
case client_error::invalid_scheme:
23-
return "Requires HTTP or HTTPS scheme.";
22+
case client_error::invalid_url:
23+
return "Requires HTTP or HTTPS URL.";
24+
case client_error::resolver_error:
25+
return "Unable to resolve host";
26+
case client_error::connection_timeout:
27+
return "Connection timeout.";
28+
case client_error::https_not_supported:
29+
return "HTTPS is not supported.";
2430
default:
2531
break;
2632
}
@@ -36,30 +42,30 @@ namespace network {
3642
return std::error_code(static_cast<int>(e), client_category());
3743
}
3844

39-
invalid_scheme::invalid_scheme(const std::string &scheme)
40-
: std::system_error(make_error_code(client_error::invalid_scheme), "Invalid scheme: " + scheme) {
45+
invalid_url::invalid_url()
46+
: std::system_error(make_error_code(client_error::invalid_url)) {
4147

4248
}
4349

44-
invalid_scheme::~invalid_scheme() noexcept {
50+
invalid_url::~invalid_url() noexcept {
4551

4652
}
4753

48-
resolver_error::resolver_error(const std::string &msg)
49-
: std::system_error(make_error_code(client_error::resolver_error), msg) {
54+
resolver_error::resolver_error()
55+
: std::system_error(make_error_code(client_error::resolver_error)) {
5056

5157
}
5258

5359
resolver_error::~resolver_error() {
5460

5561
}
5662

57-
connection_timeout::connection_timeout()
58-
: std::system_error(make_error_code(client_error::connection_timeout)) {
63+
connection_error::connection_error(client_error error)
64+
: std::system_error(make_error_code(error)) {
5965

6066
}
6167

62-
connection_timeout::~connection_timeout() {
68+
connection_error::~connection_error() {
6369

6470
}
6571

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ namespace network {
1313
namespace http {
1414
namespace v2 {
1515
enum class client_error {
16-
// scheme
17-
invalid_scheme,
16+
// url
17+
invalid_url,
1818

1919
// resolution
2020
resolver_error,
2121

2222
// connection
2323
connection_timeout,
24+
https_not_supported,
2425
};
2526

2627
class client_category_impl : public std::error_category {
@@ -41,13 +42,13 @@ namespace network {
4142

4243
std::error_code make_error_code(client_error e);
4344

44-
class invalid_scheme : public std::system_error {
45+
class invalid_url : public std::system_error {
4546

4647
public:
4748

48-
explicit invalid_scheme(const std::string &scheme);
49+
explicit invalid_url();
4950

50-
virtual ~invalid_scheme() noexcept;
51+
virtual ~invalid_url() noexcept;
5152

5253
};
5354

@@ -56,21 +57,22 @@ namespace network {
5657

5758
public:
5859

59-
explicit resolver_error(const std::string &msg);
60+
explicit resolver_error();
6061

6162
virtual ~resolver_error() noexcept;
6263

6364
};
6465

65-
class connection_timeout : public std::system_error {
66+
class connection_error : public std::system_error {
6667

6768
public:
6869

69-
connection_timeout();
70+
explicit connection_error(client_error error);
7071

71-
virtual ~connection_timeout() noexcept;
72+
virtual ~connection_error() noexcept;
7273

7374
};
75+
7476
} // namespace v2
7577
} // namespace http
7678
} // namespace network

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ namespace network {
4242

4343
}
4444

45-
4645
/**
4746
* \brief Resolves a host asynchronously.
4847
*/

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ namespace network {
3939
virtual void connect(boost::asio::ip::tcp::endpoint &endpoint,
4040
const std::string &host,
4141
std::function<void (const boost::system::error_code &)> handler) {
42-
context_.reset(
43-
new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
42+
context_.reset(new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
4443
std::vector<std::string> const& certificate_paths =
4544
options_.openssl_certificate_paths();
4645
std::vector<std::string> const& verifier_paths =
4746
options_.openssl_verify_paths();
48-
bool use_default_verification = certificate_paths.empty() &&
49-
verifier_paths.empty();
47+
bool use_default_verification = certificate_paths.empty() && verifier_paths.empty();
5048
if (!use_default_verification) {
5149
for (auto path : certificate_paths) {
5250
context_->load_verify_file(path);
@@ -56,7 +54,8 @@ namespace network {
5654
}
5755
context_->set_verify_mode(boost::asio::ssl::context::verify_peer);
5856
context_->set_verify_callback(boost::asio::ssl::rfc2818_verification(host));
59-
} else {
57+
}
58+
else {
6059
context_->set_default_verify_paths();
6160
context_->set_verify_mode(boost::asio::ssl::context::verify_none);
6261
}

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
#include <string>
1111
#include <vector>
1212
#include <utility>
13-
#include <network/http/v2/constants.hpp>
14-
#include <network/http/v2/message_base.hpp>
15-
#include <network/http/v2/client/client_errors.hpp>
13+
#include "network/http/v2/constants.hpp"
14+
#include "network/http/v2/method.hpp"
15+
#include "network/http/v2/client/client_errors.hpp"
16+
#include "network/uri.hpp"
1617
#include <boost/range/iterator_range.hpp>
1718
#include <boost/range/algorithm/equal.hpp>
1819
#include <boost/range/as_literal.hpp>
19-
#include <network/uri.hpp>
2020

2121
namespace network {
2222
namespace http {
@@ -30,8 +30,8 @@ namespace network {
3030

3131
public:
3232

33-
typedef message_base::string_type string_type;
34-
typedef message_base::size_type size_type;
33+
typedef std::string string_type;
34+
typedef std::size_t size_type;
3535

3636
/**
3737
* \brief Destructor.
@@ -82,11 +82,11 @@ namespace network {
8282
if (auto scheme = destination.scheme()) {
8383
if ((!boost::equal(*scheme, boost::as_literal("http"))) &&
8484
(!boost::equal(*scheme, boost::as_literal("https")))) {
85-
throw invalid_scheme(std::string(std::begin(*scheme), std::end(*scheme)));
85+
throw invalid_url();
8686
}
8787
}
8888
else {
89-
throw invalid_scheme("<none>");
89+
throw invalid_url();
9090
}
9191
}
9292

@@ -151,11 +151,11 @@ namespace network {
151151
headers_.clear();
152152
}
153153

154-
void set_method(string_type method) {
155-
method_ = std::move(method);
154+
void set_method(network::http::v2::method method) {
155+
method_ = method;
156156
}
157157

158-
string_type method() const {
158+
network::http::v2::method method() const {
159159
return method_;
160160
}
161161

@@ -170,21 +170,18 @@ namespace network {
170170
private:
171171

172172
uri destination_;
173-
string_type method_, version_;
173+
network::http::v2::method method_;
174+
string_type version_;
174175
headers_type headers_;
175176
std::shared_ptr<byte_source> byte_source_;
176177

177178
friend std::ostream &operator << (std::ostream &os, const request &req) {
178-
request::string_type path{std::begin(*req.destination_.path()), std::end(*req.destination_.path())};
179-
request::string_type host;
180-
host.append(request::string_type{std::begin(*req.destination_.scheme()),
181-
std::end(*req.destination_.scheme())});
182-
host.append("://");
183-
host.append(request::string_type{std::begin(*req.destination_.authority()),
184-
std::end(*req.destination_.authority())});
185-
186-
os << req.method_ << " " << path << " HTTP/" << req.version_ << "\r\n";
187-
os << "Host: " << host << "\r\n";
179+
os << req.method_ << " " << *req.destination_.path() << " HTTP/" << req.version_ << "\r\n";
180+
os << "Host: ";
181+
os << *req.destination_.scheme();
182+
os << "://";
183+
os << *req.destination_.authority();
184+
os << "\r\n";
188185
for (auto header : req.headers_) {
189186
os << header.first << ": " << header.second << "\r\n";
190187
}

http/src/network/http/v2/method.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef __NETWORK_HTTP_V2_METHOD_INC__
7+
#define __NETWORK_HTTP_V2_METHOD_INC__
8+
9+
#include <string>
10+
#include <ostream>
11+
12+
namespace network {
13+
namespace http {
14+
namespace v2 {
15+
enum class method { GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE, CONNECT, MERGE, PATCH, };
16+
17+
inline
18+
std::ostream &operator << (std::ostream &os, method m) {
19+
switch (m) {
20+
case method::GET:
21+
return os << "GET";
22+
case method::POST:
23+
return os << "POST";
24+
case method::PUT:
25+
return os << "PUT";
26+
case method::DELETE:
27+
return os << "DELETE";
28+
case method::HEAD:
29+
return os << "HEAD";
30+
case method::OPTIONS:
31+
return os << "OPTIONS";
32+
case method::TRACE:
33+
return os << "TRACE";
34+
case method::CONNECT:
35+
return os << "CONNECT";
36+
case method::MERGE:
37+
return os << "MERGE";
38+
case method::PATCH:
39+
return os << "PATCH";
40+
}
41+
return os;
42+
}
43+
} // namespace v2
44+
} // namespace http
45+
} // namespace network
46+
47+
48+
#endif // __NETWORK_HTTP_V2_METHOD_INC__

http/test/v2/client/byte_source_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
#include <gtest/gtest.h>
7-
#include <network/http/v2/client/request.hpp>
7+
#include "network/http/v2/client/request.hpp"
88

99

http/test/v2/client/client_options_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
#include <gtest/gtest.h>
7-
#include <network/http/v2/client/client_options.hpp>
7+
#include "network/http/v2/client/client_options.hpp"
88

99
TEST(client_options_test, default_options_follow_redirects) {
1010
network::http::v2::client_options opts;

http/test/v2/client/client_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
#include <gtest/gtest.h>
7-
#include <network/http/v2/client.hpp>
7+
#include "network/http/v2/client.hpp"
88

99
namespace http = network::http::v2;
1010

http/test/v2/client/request_options_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
#include <gtest/gtest.h>
7-
#include <network/http/v2/client/request_options.hpp>
7+
#include "network/http/v2/client/request_options.hpp"
88

99
TEST(request_options_test, default_options_resolve_timeout) {
1010
network::http::v2::request_options opts;

0 commit comments

Comments
 (0)