Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit28ffad1

Browse files
committed
Minor refactoring in request, response, client.
1 parentb7e7aa5 commit28ffad1

File tree

6 files changed

+138
-55
lines changed

6 files changed

+138
-55
lines changed

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

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

6+
#include<future>
67
#include<network/http/v2/client/client.hpp>
78
#include<network/http/v2/method.hpp>
9+
#include<network/http/v2/client/request.hpp>
10+
#include<network/http/v2/client/response.hpp>
811
#include<network/http/v2/client/connection/async_resolver.hpp>
9-
#include<thread>
12+
#include<network/http/v2/client/connection/normal_connection.hpp>
1013

1114
namespacenetwork {
1215
namespacehttp {
1316
namespacev2 {
17+
using boost::asio::ip::tcp;
18+
19+
structrequest_helper {
20+
21+
request_helper(boost::asio::io_service &io_service) {
22+
23+
}
24+
25+
boost::asio::streambuf request_;
26+
std::promise<response> response_promise_;
27+
boost::asio::streambuf response_;
28+
29+
};
30+
1431
structclient::impl {
1532

1633
explicitimpl(client_options options);
1734

1835
~impl()noexcept;
1936

37+
voidconnect(const boost::system::error_code &ec);
38+
39+
voidwrite_request(const boost::system::error_code &ec, std::size_t bytes_written);
40+
41+
voidread_response_status(const boost::system::error_code &ec, std::size_t bytes_written);
42+
43+
voidread_response_headers(const boost::system::error_code &ec, std::size_t bytes_written);
44+
2045
std::future<response>do_request(method method_, request request_, request_options options);
2146

2247
client_options options_;
2348
boost::asio::io_service io_service_;
2449
std::unique_ptr<boost::asio::io_service::work> sentinel_;
2550
std::thread lifetime_thread_;
2651
async_resolver resolver_;
52+
normal_connection connection_;
53+
54+
boost::asio::streambuf request_;
55+
std::promise<response> response_promise_;
56+
boost::asio::streambuf response_;
57+
58+
// promise
59+
// future response
2760

2861
};
2962

3063
client::impl::impl(client_options options)
3164
: options_(options)
3265
, sentinel_(new boost::asio::io_service::work(io_service_))
3366
, lifetime_thread_([=] () { io_service_.run(); })
34-
, resolver_(io_service_, options.cache_resolved()) {
67+
, resolver_(io_service_, options.cache_resolved())
68+
, connection_(io_service_) {
3569

3670
}
3771

@@ -40,13 +74,66 @@ namespace network {
4074
lifetime_thread_.join();
4175
}
4276

43-
std::future<response>client::impl::do_request(method method_,
44-
request request_,
77+
voidclient::impl::connect(const boost::system::error_code &ec) {
78+
if (ec) {
79+
return;
80+
}
81+
82+
// endpoint!
83+
// host!
84+
85+
//connection_.async_connect(endpoint, reques
86+
}
87+
88+
voidclient::impl::write_request(const boost::system::error_code &ec,
89+
std::size_t bytes_written) {
90+
if (ec) {
91+
return;
92+
}
93+
94+
// request!
95+
}
96+
97+
voidclient::impl::read_response_status(const boost::system::error_code &ec,
98+
std::size_t bytes_read) {
99+
if (ec) {
100+
return;
101+
}
102+
103+
// response
104+
}
105+
106+
voidclient::impl::read_response_headers(const boost::system::error_code &ec,
107+
std::size_t bytes_read) {
108+
if (ec) {
109+
return;
110+
}
111+
112+
// response
113+
}
114+
115+
std::future<response>client::impl::do_request(method met,
116+
request req,
45117
request_options options) {
46-
request_.method(method_);
118+
std::future<response> response = response_promise_.get_future();
119+
120+
req.method(met);
121+
std::ostreamrequest_stream(&request_);
122+
request_stream << req;
123+
if (!request_stream) {
124+
// set error
125+
return response;
126+
}
127+
128+
//auto endpoints = resolver_.async_resolve(req.host(), req.port(),
129+
// [=](const boost::system::error_code &ec) {
130+
// if (ec) {
131+
// // promise set error
132+
// return;
133+
// }
134+
// this->connect(ec);
135+
// });
47136

48-
std::future<response> response;
49-
//auto endpoints = resolver_.resolve(request_.host(), request_.port());
50137
return response;
51138
}
52139

@@ -59,28 +146,28 @@ namespace network {
59146
delete pimpl_;
60147
}
61148

62-
std::future<response>client::get(requestrequest_, request_options options) {
63-
return pimpl_->do_request(method::GET,request_, options);
149+
std::future<response>client::get(requestreq, request_options options) {
150+
return pimpl_->do_request(method::GET,req, options);
64151
}
65152

66-
std::future<response>client::post(requestrequest_, request_options options) {
67-
return pimpl_->do_request(method::POST,request_, options);
153+
std::future<response>client::post(requestreq, request_options options) {
154+
return pimpl_->do_request(method::POST,req, options);
68155
}
69156

70-
std::future<response>client::put(requestrequest_, request_options options) {
71-
return pimpl_->do_request(method::PUT,request_, options);
157+
std::future<response>client::put(requestreq, request_options options) {
158+
return pimpl_->do_request(method::PUT,req, options);
72159
}
73160

74-
std::future<response>client::delete_(requestrequest_, request_options options) {
75-
return pimpl_->do_request(method::DELETE,request_, options);
161+
std::future<response>client::delete_(requestreq, request_options options) {
162+
return pimpl_->do_request(method::DELETE,req, options);
76163
}
77164

78-
std::future<response>client::head(requestrequest_, request_options options) {
79-
return pimpl_->do_request(method::HEAD,request_, options);
165+
std::future<response>client::head(requestreq, request_options options) {
166+
return pimpl_->do_request(method::HEAD,req, options);
80167
}
81168

82-
std::future<response>client::options(requestrequest_, request_options options) {
83-
return pimpl_->do_request(method::OPTIONS,request_, options);
169+
std::future<response>client::options(requestreq, request_options options) {
170+
return pimpl_->do_request(method::OPTIONS,req, options);
84171
}
85172
}// namespace v2
86173
}// namespace network

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ namespace network {
109109
* follow redirects, if \c false it doesn't.
110110
* \returns \c *this
111111
*/
112-
client_options &follow_redirects(bool follow_redirects)noexcept{
112+
client_options &follow_redirects(bool follow_redirects) {
113113
follow_redirects_ = follow_redirects;
114114
return *this;
115115
}
@@ -119,7 +119,7 @@ namespace network {
119119
* \returns \c true if the client follows redirects, \c false
120120
* otherwise.
121121
*/
122-
boolfollow_redirects()constnoexcept{
122+
boolfollow_redirects()const {
123123
return follow_redirects_;
124124
}
125125

@@ -130,7 +130,7 @@ namespace network {
130130
* doesn't.
131131
* \returns \c *this
132132
*/
133-
client_options &cache_resolved(bool cache_resolved)noexcept{
133+
client_options &cache_resolved(bool cache_resolved) {
134134
cache_resolved_ = cache_resolved;
135135
return *this;
136136
}
@@ -140,7 +140,7 @@ namespace network {
140140
* \returns \c true if the client caches resolved connections,
141141
* \c false otherwise.
142142
*/
143-
boolcache_resolved()constnoexcept{
143+
boolcache_resolved()const {
144144
return cache_resolved_;
145145
}
146146

@@ -149,7 +149,7 @@ namespace network {
149149
* \param use_proxy If \c true, then the client must use a
150150
* proxy, if \c false it doesn't.
151151
*/
152-
client_options &use_proxy(bool use_proxy)noexcept{
152+
client_options &use_proxy(bool use_proxy) {
153153
use_proxy_ = use_proxy;
154154
return *this;
155155
}
@@ -159,15 +159,15 @@ namespace network {
159159
* \returns \c true if the client uses a proxy, \c false
160160
* otherwise.
161161
*/
162-
booluse_proxy()constnoexcept{
162+
booluse_proxy()const {
163163
return use_proxy_;
164164
}
165165

166166
/**
167167
* \brief Sets the client timeout in milliseconds.
168168
* \param timeout The timeout value in milliseconds.
169169
*/
170-
client_options &timeout(std::chrono::milliseconds timeout)noexcept{
170+
client_options &timeout(std::chrono::milliseconds timeout) {
171171
timeout_ = timeout;
172172
return *this;
173173
}
@@ -176,7 +176,7 @@ namespace network {
176176
* \brief Gets the current timeout value.
177177
* \returns The timeout value in milliseconds.
178178
*/
179-
std::chrono::millisecondstimeout()constnoexcept{
179+
std::chrono::millisecondstimeout()const {
180180
return timeout_;
181181
}
182182

@@ -231,9 +231,6 @@ namespace network {
231231
lhs.swap(rhs);
232232
}
233233

234-
template<typename Handler,typename Signature>
235-
structhandler_type { };
236-
237234
/**
238235
* \ingroup http_client
239236
* \class client network/http/v2/client/client.hpp
@@ -265,45 +262,45 @@ namespace network {
265262

266263
/**
267264
* \brief Makes an HTTP GET request.
268-
* \paramrequest_ The request object.
265+
* \paramreq The request object.
269266
* \param options The request options.
270267
*/
271-
std::future<response>get(requestrequest_, request_options options = request_options());
268+
std::future<response>get(requestreq, request_options options = request_options());
272269

273270
/**
274271
* \brief Makes an HTTP POST request.
275-
* \paramrequest_ The request object.
272+
* \paramreq The request object.
276273
* \param options The request options.
277274
*/
278-
std::future<response>post(requestrequest_, request_options options = request_options());
275+
std::future<response>post(requestreq, request_options options = request_options());
279276

280277
/**
281278
* \brief Makes an HTTP PUT request.
282-
* \paramrequest_ The request object.
279+
* \paramreq The request object.
283280
* \param options The request options.
284281
*/
285-
std::future<response>put(requestrequest_, request_options options = request_options());
282+
std::future<response>put(requestreq, request_options options = request_options());
286283

287284
/**
288285
* \brief Makes an HTTP DELETE request.
289-
* \paramrequest_ The request object.
286+
* \paramreq The request object.
290287
* \param options The request options.
291288
*/
292-
std::future<response>delete_(requestrequest_, request_options options = request_options());
289+
std::future<response>delete_(requestreq, request_options options = request_options());
293290

294291
/**
295292
* \brief Makes an HTTP HEAD request.
296-
* \paramrequest_ The request object.
293+
* \paramreq The request object.
297294
* \param options The request options.
298295
*/
299-
std::future<response>head(requestrequest_, request_options options = request_options());
296+
std::future<response>head(requestreq, request_options options = request_options());
300297

301298
/**
302299
* \brief Makes an HTTP OPTIONS request.
303-
* \paramrequest_ The request object.
300+
* \paramreq The request object.
304301
* \param options The request options.
305302
*/
306-
std::future<response>options(requestrequest_, request_options options = request_options());
303+
std::future<response>options(requestreq, request_options options = request_options());
307304

308305
private:
309306

‎http/src/network/http/v2/client/connection/async_resolver.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace network {
6666
* \param callback A callback handler.
6767
*/
6868
template<classHandler>
69-
voidasync_resolve(const std::string &host, std::uint16_t port, Handler&&handler) {
69+
voidasync_resolve(const std::string &host, std::uint16_t port, Handler &&handler) {
7070
if (cache_resolved_) {
7171
endpoint_cache::iterator it = endpoint_cache_.find(boost::to_lower_copy(host));
7272
if (it != endpoint_cache_.end()) {

‎http/src/network/http/v2/client/connection/normal_connection.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef __NETWORK_HTTP_V2_CLIENT_CONNECTION_NORMAL_CONNECTION_INC__
77
#define__NETWORK_HTTP_V2_CLIENT_CONNECTION_NORMAL_CONNECTION_INC__
88

9+
#include<boost/asio/write.hpp>
10+
#include<boost/asio/read_until.hpp>
911
#include<boost/asio/ip/tcp.hpp>
1012
#include<boost/asio/io_service.hpp>
1113
#include<network/http/v2/client/connection/connection.hpp>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,39 @@ namespace network {
8282
swap(total_timeout_, other.total_timeout_);
8383
}
8484

85-
request_options &resolve_timeout(std::uint64_t resolve_timeout)noexcept{
85+
request_options &resolve_timeout(std::uint64_t resolve_timeout) {
8686
resolve_timeout_ = resolve_timeout;
8787
return *this;
8888
}
8989

90-
std::uint64_tresolve_timeout()constnoexcept{
90+
std::uint64_tresolve_timeout()const {
9191
return resolve_timeout_;
9292
}
9393

94-
request_options &read_timeout(std::uint64_t read_timeout)noexcept{
94+
request_options &read_timeout(std::uint64_t read_timeout) {
9595
read_timeout_ = read_timeout;
9696
return *this;
9797
}
9898

99-
std::uint64_tread_timeout()constnoexcept{
99+
std::uint64_tread_timeout()const {
100100
return read_timeout_;
101101
}
102102

103-
request_options &total_timeout(std::uint64_t total_timeout)noexcept{
103+
request_options &total_timeout(std::uint64_t total_timeout) {
104104
total_timeout_ = total_timeout;
105105
return *this;
106106
}
107107

108-
std::uint64_ttotal_timeout()constnoexcept{
108+
std::uint64_ttotal_timeout()const {
109109
return total_timeout_;
110110
}
111111

112-
request_options &max_redirects(int max_redirects)noexcept{
112+
request_options &max_redirects(int max_redirects) {
113113
max_redirects_ = max_redirects;
114114
return *this;
115115
}
116116

117-
intmax_redirects()constnoexcept{
117+
intmax_redirects()const {
118118
return max_redirects_;
119119
}
120120

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp