Skip to content

Commit 41f4ec2

Browse files
committed
Added a client_errors file and invalid_scheme class.
1 parent 5ecb5c7 commit 41f4ec2

File tree

7 files changed

+181
-57
lines changed

7 files changed

+181
-57
lines changed

http/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ target_link_libraries(network-http-constants-v2
116116

117117
set(CPP-NETLIB_HTTP_CLIENT-V2_SRCS
118118
${CMAKE_CURRENT_SOURCE_DIR}/http/v2/client/client.cpp
119+
${CMAKE_CURRENT_SOURCE_DIR}/http/v2/client/client_errors.cpp
119120
)
120121
add_library(network-http-client-v2 ${CPP-NETLIB_HTTP_CLIENT-V2_SRCS})
121122
add_dependencies(network-http-client-v2
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+
#include <network/http/v2/client/client_errors.hpp>
7+
8+
namespace network {
9+
namespace http {
10+
namespace v2 {
11+
client_category_impl::~client_category_impl() noexcept {
12+
13+
}
14+
15+
const char *client_category_impl::name() const noexcept {
16+
static const char name[] = "client_error";
17+
return name;
18+
}
19+
20+
std::string client_category_impl::message(int ev) const {
21+
switch (client_error(ev)) {
22+
case client_error::invalid_scheme:
23+
return "Requires HTTP or HTTPS scheme.";
24+
default:
25+
break;
26+
}
27+
return "Unknown client error.";
28+
}
29+
30+
const std::error_category &client_category() {
31+
static client_category_impl client_category;
32+
return client_category;
33+
}
34+
35+
std::error_code make_error_code(client_error e) {
36+
return std::error_code(static_cast<int>(e), client_category());
37+
}
38+
39+
invalid_scheme::invalid_scheme(const std::string &scheme) {
40+
41+
}
42+
43+
invalid_scheme::~invalid_scheme() noexcept {
44+
45+
}
46+
} // namespace v2
47+
} // namespace network
48+
} // namespace network

http/src/http/v2/constants.cpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,46 @@
88

99
std::string message(int status_code) {
1010
static std::unordered_map<int, std::string> status_messages{
11-
std::make_pair(100, "Continue"),
12-
std::make_pair(101, "Switching Protocols"),
13-
std::make_pair(200, "OK"),
14-
std::make_pair(201, "Created"),
15-
std::make_pair(202, "Accepted"),
16-
std::make_pair(203, "Non-Authoritative Information"),
17-
std::make_pair(204, "No Content"),
18-
std::make_pair(205, "Reset Content"),
19-
std::make_pair(206, "Partial Content"),
20-
std::make_pair(300, "Multiple Choices"),
21-
std::make_pair(301, "Moved Permanently"),
22-
std::make_pair(302, "Found"),
23-
std::make_pair(303, "See Other"),
24-
std::make_pair(304, "Not Modified"),
25-
std::make_pair(305, "Use Proxy"),
26-
std::make_pair(307, "Temporary Redirect"),
27-
std::make_pair(400, "Bad Request"),
28-
std::make_pair(401, "Unauthorized"),
29-
std::make_pair(402, "Payment Required"),
30-
std::make_pair(403, "Forbidden"),
31-
std::make_pair(404, "Not Found"),
32-
std::make_pair(405, "Method Not Allowed"),
33-
std::make_pair(406, "Not Acceptable"),
34-
std::make_pair(407, "Proxy Authentication Required"),
35-
std::make_pair(408, "Request Timeout"),
36-
std::make_pair(409, "Conflict"),
37-
std::make_pair(410, "Gone"),
38-
std::make_pair(411, "Length Required"),
39-
std::make_pair(412, "Precondition Failed"),
40-
std::make_pair(413, "Request Entity Too Large"),
41-
std::make_pair(414, "Request Uri Too Long"),
42-
std::make_pair(415, "Unsupported Media Type"),
43-
std::make_pair(416, "Request Range Not Satisfiable"),
44-
std::make_pair(417, "Expectation Failed"),
45-
std::make_pair(500, "Internal Error"),
46-
std::make_pair(501, "Not Implemented"),
47-
std::make_pair(502, "Bad Gateway"),
48-
std::make_pair(503, "Service Unavailable"),
49-
std::make_pair(504, "Gateway Timeout"),
50-
std::make_pair(505, "HTTP Version Not Supported"),
11+
{100, "Continue"},
12+
{101, "Switching Protocols"},
13+
{200, "OK"},
14+
{201, "Created"},
15+
{202, "Accepted"},
16+
{203, "Non-Authoritative Information"},
17+
{204, "No Content"},
18+
{205, "Reset Content"},
19+
{206, "Partial Content"},
20+
{300, "Multiple Choices"},
21+
{301, "Moved Permanently"},
22+
{302, "Found"},
23+
{303, "See Other"},
24+
{304, "Not Modified"},
25+
{305, "Use Proxy"},
26+
{307, "Temporary Redirect"},
27+
{400, "Bad Request"},
28+
{401, "Unauthorized"},
29+
{402, "Payment Required"},
30+
{403, "Forbidden"},
31+
{404, "Not Found"},
32+
{405, "Method Not Allowed"},
33+
{406, "Not Acceptable"},
34+
{407, "Proxy Authentication Required"},
35+
{408, "Request Timeout"},
36+
{409, "Conflict"},
37+
{410, "Gone"},
38+
{411, "Length Required"},
39+
{412, "Precondition Failed"},
40+
{413, "Request Entity Too Large"},
41+
{414, "Request Uri Too Long"},
42+
{415, "Unsupported Media Type"},
43+
{416, "Request Range Not Satisfiable"},
44+
{417, "Expectation Failed"},
45+
{500, "Internal Error"},
46+
{501, "Not Implemented"},
47+
{502, "Bad Gateway"},
48+
{503, "Service Unavailable"},
49+
{504, "Gateway Timeout"},
50+
{505, "HTTP Version Not Supported"},
5151
};
5252

5353
auto it = status_messages.find(status_code);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_CLIENT_CLIENT_ERRORS_INC__
7+
#define __NETWORK_HTTP_V2_CLIENT_CLIENT_ERRORS_INC__
8+
9+
#include <network/config.hpp>
10+
#include <system_error>
11+
12+
namespace network {
13+
namespace http {
14+
namespace v2 {
15+
enum class client_error {
16+
// scheme
17+
invalid_scheme,
18+
};
19+
20+
class client_category_impl : public std::error_category {
21+
22+
public:
23+
24+
client_category_impl() = default;
25+
26+
virtual ~client_category_impl() noexcept;
27+
28+
virtual const char *name() const noexcept;
29+
30+
virtual std::string message(int ev) const;
31+
32+
};
33+
34+
const std::error_category &client_category();
35+
36+
std::error_code make_error_code(client_error e);
37+
38+
class invalid_scheme : public std::system_error {
39+
40+
public:
41+
42+
invalid_scheme(const std::string &scheme);
43+
44+
virtual ~invalid_scheme() noexcept;
45+
46+
};
47+
} // namespace v2
48+
} // namespace http
49+
} // namespace network
50+
51+
#endif // __NETWORK_HTTP_V2_CLIENT_CLIENT_ERRORS_INC__

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
// (See accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6-
#ifndef __NETWORK_HTTP_V2_REQUEST_INC__
7-
#define __NETWORK_HTTP_V2_REQUEST_INC__
6+
#ifndef __NETWORK_HTTP_V2_CLIENT_REQUEST_INC__
7+
#define __NETWORK_HTTP_V2_CLIENT_REQUEST_INC__
88

99
#include <cstdint>
1010
#include <memory>
1111
#include <string>
1212
#include <network/http/v2/constants.hpp>
1313
#include <network/http/v2/message_base.hpp>
14+
#include <network/http/v2/client/client_errors.hpp>
15+
#include <boost/range/algorithm/equal.hpp>
16+
#include <boost/range/as_literal.hpp>
1417
#include <network/uri.hpp>
1518

1619
namespace network {
@@ -51,15 +54,35 @@ namespace network {
5154

5255
typedef byte_source::string_type string_type;
5356

54-
request(uri destination, std::shared_ptr<byte_source> source = nullptr)
55-
: destination_(destination), byte_source_(source) { }
57+
request()
58+
: byte_source_(nullptr) { }
59+
60+
explicit request(uri destination, std::shared_ptr<byte_source> source = nullptr)
61+
: destination_(destination), byte_source_(source) {
62+
if (auto scheme = destination.scheme()) {
63+
if ((!boost::equal(*scheme, boost::as_literal("http"))) &&
64+
(!boost::equal(*scheme, boost::as_literal("https")))) {
65+
throw invalid_scheme(std::string(std::begin(*scheme), std::end(*scheme)));
66+
}
67+
}
68+
else {
69+
throw invalid_scheme("<none>");
70+
}
71+
}
5672

5773
request(const request &other)
58-
: destination_(other.destination_), byte_source_(other.byte_source_) { }
74+
: destination_(other.destination_)
75+
, byte_source_(other.byte_source_)
76+
, headers_(other.headers_)
77+
, method_(other.method_)
78+
, version_(other.version_) { }
5979

6080
request(request &&other) noexcept
6181
: destination_(std::move(other.destination_))
62-
, byte_source_(std::move(other.byte_source_)) { }
82+
, byte_source_(std::move(other.byte_source_))
83+
, headers_(std::move(other.headers_))
84+
, method_(std::move(other.method_))
85+
, version_(std::move(other.version_)) { }
6386

6487
request &operator = (request other) {
6588
other.swap(*this);
@@ -74,6 +97,10 @@ namespace network {
7497
std::swap(version_, other.version_);
7598
}
7699

100+
void set_destination(uri destination) {
101+
destination_ = std::move(destination);
102+
}
103+
77104
void set_body(std::shared_ptr<byte_source> byte_source) {
78105
byte_source_ = byte_source;
79106
}
@@ -108,12 +135,14 @@ namespace network {
108135

109136
uri destination_;
110137
std::shared_ptr<byte_source> byte_source_;
111-
std::map<string_type, string_type> headers_;
138+
std::multimap<string_type, string_type> headers_;
112139
string_type method_, version_;
113140

114141
};
142+
143+
std::ostream &operator << (std::ostream &os, const request &req);
115144
} // namespace v2
116145
} // namespace http
117146
} // namespace network
118147

119-
#endif // __NETWORK_HTTP_V2_REQUEST_INC__
148+
#endif // __NETWORK_HTTP_V2_CLIENT_REQUEST_INC__

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ namespace network {
4040
// append_body
4141
// get_body
4242

43-
private:
44-
45-
struct impl;
46-
impl *pimpl_;
47-
4843

4944
};
5045
} // namespace v2

http/test/v2/client/request_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ TEST(request_test, request_constructor_https_url) {
1414
ASSERT_NO_THROW(network::http::v2::request(network::uri("https://www.example.com/")));
1515
}
1616

17-
//TEST(request_test, request_constructor_invalid_url) {
18-
// ASSERT_THROW(network::http::v2::invalid_scheme,
19-
// network::http::v2::request(network::uri("mailto:john.doe@example.com")));
20-
//}
17+
TEST(request_test, request_constructor_invalid_url) {
18+
ASSERT_THROW(network::http::v2::request(network::uri("mailto:john.doe@example.com")),
19+
network::http::v2::invalid_scheme);
20+
}

0 commit comments

Comments
 (0)