Skip to content

Commit 2b1c0e2

Browse files
committed
Added transfer direction callback.
1 parent 1bc435b commit 2b1c0e2

File tree

9 files changed

+484
-486
lines changed

9 files changed

+484
-486
lines changed

contrib/http_examples/simple_wget.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
namespace http = network::http;
2222

2323
namespace {
24-
std::string get_filename(const std::string& path) {
25-
auto index = path.find_last_of('/');
26-
auto filename = path.substr(index + 1);
27-
return filename.empty() ? "index.html" : filename;
28-
}
24+
std::string get_filename(const std::string& path) {
25+
auto index = path.find_last_of('/');
26+
auto filename = path.substr(index + 1);
27+
return filename.empty() ? "index.html" : filename;
28+
}
2929
} // namespace
3030

3131
int main(int argc, char* argv[]) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2013 by Glyn Matthews
1+
// Copyright (C) 2013, 2014 by Glyn Matthews
22
// Distributed under the Boost Software License, Version 1.0.
33
// (See accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
@@ -206,7 +206,7 @@ void client::impl::read_response(const boost::system::error_code &ec,
206206

207207
helper->total_bytes_written_ += bytes_written;
208208
if (auto progress = helper->options_.progress()) {
209-
progress(client_message::message_direction::bytes_written, helper->total_bytes_written_);
209+
progress(client_message::transfer_direction::bytes_written, helper->total_bytes_written_);
210210
}
211211

212212
std::shared_ptr<response> res(new response{});
@@ -302,7 +302,7 @@ void client::impl::read_response_body(const boost::system::error_code &ec,
302302
std::shared_ptr<response> res) {
303303
helper->total_bytes_read_ += bytes_read;
304304
if (auto progress = helper->options_.progress()) {
305-
progress(client_message::message_direction::bytes_read, helper->total_bytes_read_);
305+
progress(client_message::transfer_direction::bytes_read, helper->total_bytes_read_);
306306
}
307307

308308
if (bytes_read == 0) {

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

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,70 @@
88
#include <string>
99

1010
namespace network {
11-
namespace http {
12-
namespace v2 {
11+
namespace http {
12+
namespace v2 {
13+
class client_category_impl : public std::error_category {
1314

14-
class client_category_impl : public std::error_category {
15+
public:
1516

16-
public:
17+
client_category_impl() = default;
1718

18-
client_category_impl() = default;
19+
virtual ~client_category_impl() noexcept;
1920

20-
virtual ~client_category_impl() noexcept;
21+
virtual const char *name() const noexcept;
2122

22-
virtual const char *name() const noexcept;
23+
virtual std::string message(int ev) const;
2324

24-
virtual std::string message(int ev) const;
25+
};
2526

26-
};
27+
client_category_impl::~client_category_impl() noexcept {
2728

28-
client_category_impl::~client_category_impl() noexcept {
29+
}
2930

30-
}
31+
const char *client_category_impl::name() const noexcept {
32+
static const char name[] = "client_error";
33+
return name;
34+
}
3135

32-
const char *client_category_impl::name() const noexcept {
33-
static const char name[] = "client_error";
34-
return name;
35-
}
36+
std::string client_category_impl::message(int ev) const {
37+
switch (client_error(ev)) {
3638

37-
std::string client_category_impl::message(int ev) const {
38-
switch (client_error(ev)) {
39+
case client_error::invalid_request:
40+
return "Invalid HTTP request.";
41+
case client_error::invalid_response:
42+
return "Invalid HTTP response.";
43+
default:
44+
break;
45+
}
46+
return "Unknown client error.";
47+
}
3948

40-
case client_error::invalid_request:
41-
return "Invalid HTTP request.";
42-
case client_error::invalid_response:
43-
return "Invalid HTTP response.";
44-
default:
45-
break;
46-
}
47-
return "Unknown client error.";
48-
}
49+
const std::error_category &client_category() {
50+
static client_category_impl client_category;
51+
return client_category;
52+
}
4953

50-
const std::error_category &client_category() {
51-
static client_category_impl client_category;
52-
return client_category;
53-
}
54+
std::error_code make_error_code(client_error e) {
55+
return std::error_code(static_cast<int>(e), client_category());
56+
}
5457

55-
std::error_code make_error_code(client_error e) {
56-
return std::error_code(static_cast<int>(e), client_category());
57-
}
58+
invalid_url::invalid_url()
59+
: std::invalid_argument("Requires HTTP or HTTPS URL.") {
5860

59-
invalid_url::invalid_url()
60-
: std::invalid_argument("Requires HTTP or HTTPS URL.") {
61+
}
6162

62-
}
63+
invalid_url::~invalid_url() noexcept {
6364

64-
invalid_url::~invalid_url() noexcept {
65+
}
6566

66-
}
67+
client_exception::client_exception(client_error error)
68+
: std::system_error(make_error_code(error)) {
6769

68-
client_exception::client_exception(client_error error)
69-
: std::system_error(make_error_code(error)) {
70+
}
7071

71-
}
72+
client_exception::~client_exception() noexcept {
7273

73-
client_exception::~client_exception() noexcept {
74-
75-
}
76-
77-
} // namespace v2
78-
} // namespace network
74+
}
75+
} // namespace v2
76+
} // namespace http
7977
} // namespace network

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

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,75 +17,75 @@
1717
#include <network/config.hpp>
1818

1919
namespace network {
20-
namespace http {
21-
namespace v2 {
22-
/**
23-
* \ingroup http_client
24-
* \enum client_error network/http/v2/client/client_errors.hpp network/http/v2/client.hpp
25-
* \brief An enumeration of all types of client error.
26-
*/
27-
enum class client_error {
28-
// request
29-
invalid_request,
30-
31-
// response
32-
invalid_response,
33-
};
34-
35-
/**
36-
* \brief Gets the error category for HTTP client errors.
37-
*/
38-
const std::error_category &client_category();
39-
40-
/**
41-
* \brief Makes an error code object from a client_error enum.
42-
*/
43-
std::error_code make_error_code(client_error e);
44-
45-
/**
46-
* \ingroup http_client
47-
* \class invalid_url network/http/v2/client/client_errors.hpp network/http/v2/client.hpp
48-
* \brief An exception thrown if the URL provides is invalid.
49-
*/
50-
class invalid_url : public std::invalid_argument {
51-
52-
public:
53-
54-
/**
55-
* \brief Constructor.
56-
*/
57-
explicit invalid_url();
58-
59-
/**
60-
* \brief Destructor.
61-
*/
62-
virtual ~invalid_url() noexcept;
63-
64-
};
65-
66-
/**
67-
* \ingroup http_client
68-
* \class client_exception network/http/v2/client/client_errors.hpp network/http/v2/client.hpp
69-
* \brief An exception thrown when there is a client error.
70-
*/
71-
class client_exception : public std::system_error {
72-
73-
public:
74-
75-
/**
76-
* \brief Constructor.
77-
*/
78-
explicit client_exception(client_error error);
79-
80-
/**
81-
* \brief Destructor.
82-
*/
83-
virtual ~client_exception() noexcept;
84-
85-
};
86-
87-
} // namespace v2
88-
} // namespace http
20+
namespace http {
21+
namespace v2 {
22+
/**
23+
* \ingroup http_client
24+
* \enum client_error network/http/v2/client/client_errors.hpp network/http/v2/client.hpp
25+
* \brief An enumeration of all types of client error.
26+
*/
27+
enum class client_error {
28+
// request
29+
invalid_request,
30+
31+
// response
32+
invalid_response,
33+
};
34+
35+
/**
36+
* \brief Gets the error category for HTTP client errors.
37+
*/
38+
const std::error_category &client_category();
39+
40+
/**
41+
* \brief Makes an error code object from a client_error enum.
42+
*/
43+
std::error_code make_error_code(client_error e);
44+
45+
/**
46+
* \ingroup http_client
47+
* \class invalid_url network/http/v2/client/client_errors.hpp network/http/v2/client.hpp
48+
* \brief An exception thrown if the URL provides is invalid.
49+
*/
50+
class invalid_url : public std::invalid_argument {
51+
52+
public:
53+
54+
/**
55+
* \brief Constructor.
56+
*/
57+
explicit invalid_url();
58+
59+
/**
60+
* \brief Destructor.
61+
*/
62+
virtual ~invalid_url() noexcept;
63+
64+
};
65+
66+
/**
67+
* \ingroup http_client
68+
* \class client_exception network/http/v2/client/client_errors.hpp network/http/v2/client.hpp
69+
* \brief An exception thrown when there is a client error.
70+
*/
71+
class client_exception : public std::system_error {
72+
73+
public:
74+
75+
/**
76+
* \brief Constructor.
77+
*/
78+
explicit client_exception(client_error error);
79+
80+
/**
81+
* \brief Destructor.
82+
*/
83+
virtual ~client_exception() noexcept;
84+
85+
};
86+
87+
} // namespace v2
88+
} // namespace http
8989
} // namespace network
9090

9191
#endif // NETWORK_HTTP_V2_CLIENT_CLIENT_ERRORS_INC

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace network {
3737
namespace http {
3838
namespace v2 {
3939
namespace client_message {
40-
enum class message_direction {
40+
enum class transfer_direction {
4141
bytes_written, bytes_read,
4242
};
4343

@@ -135,12 +135,12 @@ class request_options {
135135
return max_redirects_;
136136
}
137137

138-
request_options &progress(std::function<void (message_direction, std::uint64_t)> handler) {
138+
request_options &progress(std::function<void (transfer_direction, std::uint64_t)> handler) {
139139
progress_handler_ = handler;
140140
return *this;
141141
}
142142

143-
std::function<void (message_direction, std::uint64_t)> progress() const {
143+
std::function<void (transfer_direction, std::uint64_t)> progress() const {
144144
return progress_handler_;
145145
}
146146

@@ -150,7 +150,7 @@ class request_options {
150150
std::uint64_t read_timeout_;
151151
std::uint64_t total_timeout_;
152152
int max_redirects_;
153-
std::function<void (message_direction, std::uint64_t)> progress_handler_;
153+
std::function<void (transfer_direction, std::uint64_t)> progress_handler_;
154154

155155
};
156156

0 commit comments

Comments
 (0)