4
4
// http://www.boost.org/LICENSE_1_0.txt)
5
5
6
6
#include < future>
7
+ #include < network/uri.hpp>
7
8
#include < network/config.hpp>
8
9
#include < network/http/v2/client/client.hpp>
9
10
#include < network/http/v2/method.hpp>
@@ -23,37 +24,32 @@ namespace network {
23
24
//
24
25
// }
25
26
//
27
+ // void connect(client::string_type host,
28
+ // const boost::system::error_code &ec,
29
+ // tcp::resolver::iterator endpoint_iterator);
30
+ //
31
+ // void write_request(const boost::system::error_code &ec);
32
+ //
33
+ // void read_response_status(const boost::system::error_code &ec,
34
+ // std::size_t bytes_written);
35
+ //
36
+ // void read_response_headers(const boost::system::error_code &ec,
37
+ // std::size_t bytes_read);
38
+ //
26
39
// client::string_type host_;
27
40
// boost::asio::streambuf request_;
28
41
// std::promise<response> response_promise_;
29
42
// boost::asio::streambuf response_;
30
43
//
31
44
// };
32
45
33
- struct resolve_helper {
34
-
35
- };
36
-
37
- struct connect_helper {
38
-
39
- };
40
-
41
- struct request_helper {
42
-
43
- };
44
-
45
- struct response_helper {
46
-
47
- };
48
-
49
46
struct client ::impl {
50
47
51
48
explicit impl (client_options options);
52
49
53
50
~impl () noexcept ;
54
51
55
- void connect (client::string_type host,
56
- const boost::system::error_code &ec,
52
+ void connect (const boost::system::error_code &ec,
57
53
tcp::resolver::iterator endpoint_iterator);
58
54
59
55
void write_request (const boost::system::error_code &ec);
@@ -68,10 +64,10 @@ namespace network {
68
64
69
65
client_options options_;
70
66
boost::asio::io_service io_service_;
71
- std::unique_ptr<boost::asio::io_service::work> sentinel_;
72
- std::thread lifetime_thread_;
73
67
async_resolver resolver_;
74
68
normal_connection connection_;
69
+ std::unique_ptr<boost::asio::io_service::work> sentinel_;
70
+ std::thread lifetime_thread_;
75
71
76
72
boost::asio::streambuf request_;
77
73
std::promise<response> response_promise_;
@@ -84,10 +80,10 @@ namespace network {
84
80
85
81
client::impl::impl (client_options options)
86
82
: options_(options)
87
- , sentinel_(new boost::asio::io_service::work(io_service_))
88
- , lifetime_thread_([=] () { io_service_.run (); })
89
83
, resolver_(io_service_, options.cache_resolved())
90
- , connection_(io_service_) {
84
+ , connection_(io_service_)
85
+ , sentinel_(new boost::asio::io_service::work(io_service_))
86
+ , lifetime_thread_([=] () { io_service_.run (); }) {
91
87
92
88
}
93
89
@@ -96,30 +92,32 @@ namespace network {
96
92
lifetime_thread_.join ();
97
93
}
98
94
99
- void client::impl::connect (client::string_type host,
100
- const boost::system::error_code &ec,
95
+ void client::impl::connect (const boost::system::error_code &ec,
101
96
tcp::resolver::iterator endpoint_iterator) {
102
- if (ec) {
103
- return ;
104
- }
105
-
106
- if (endpoint_iterator == tcp::resolver::iterator ()) {
107
- return ;
108
- }
109
-
110
97
tcp::endpoint endpoint (*endpoint_iterator);
111
- connection_.async_connect (endpoint, host,
112
- [=] (const boost::system::error_code &ec) {
113
- write_request (ec);
114
- });
98
+ std::cout << " Resolved " << endpoint << std::endl;
99
+ // connection_.async_connect(endpoint,
100
+ // [=] (const boost::system::error_code &ec) {
101
+ // if (ec) {
102
+ // return;
103
+ // }
104
+ //
105
+ // std::cout << "Oh." << std::endl;
106
+ // //response_promise_.set_value(v2::response());
107
+ // //write_request(ec);
108
+ // });
115
109
}
116
110
117
111
void client::impl::write_request (const boost::system::error_code &ec) {
118
112
if (ec) {
119
113
return ;
120
114
}
121
115
122
- // request!
116
+ connection_.async_write (request_,
117
+ [=] (const boost::system::error_code &ec,
118
+ std::size_t bytes_written) {
119
+ read_response_status (ec, bytes_written);
120
+ });
123
121
}
124
122
125
123
void client::impl::read_response_status (const boost::system::error_code &ec,
@@ -128,7 +126,13 @@ namespace network {
128
126
return ;
129
127
}
130
128
131
- // response
129
+ connection_.async_read_until (response_,
130
+ " \r\n " ,
131
+ [=] (const boost::system::error_code &ec,
132
+ std::size_t bytes_read) {
133
+ // fill headers
134
+ read_response_headers (ec, bytes_read);
135
+ });
132
136
}
133
137
134
138
void client::impl::read_response_headers (const boost::system::error_code &ec,
@@ -137,8 +141,13 @@ namespace network {
137
141
return ;
138
142
}
139
143
140
- // response
141
- }
144
+ connection_.async_read_until (response_,
145
+ " \r\n\r\n " ,
146
+ [=] (const boost::system::error_code &ec,
147
+ std::size_t bytes_read) {
148
+ // um...
149
+ });
150
+ }
142
151
143
152
std::future<response> client::impl::do_request (method met,
144
153
request req,
@@ -154,13 +163,33 @@ namespace network {
154
163
return response;
155
164
}
156
165
157
- //
158
-
159
- auto endpoints = resolver_.async_resolve (req.host (), req.port (),
160
- [=](const boost::system::error_code &ec,
161
- tcp::resolver::iterator endpoint_iterator) {
162
- this ->connect (ec, endpoint_iterator);
163
- });
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
+ }
175
+ }
176
+ auto auth = builder.uri ();
177
+ auto host = auth.host ()?
178
+ uri::string_type (std::begin (*auth.host ()), std::end (*auth.host ())) : uri::string_type ();
179
+ auto port = auth.port <std::uint16_t >()? *auth.port <std::uint16_t >() : 80 ;
180
+
181
+ resolver_.async_resolve (host, port,
182
+ [=](const boost::system::error_code &ec,
183
+ tcp::resolver::iterator endpoint_iterator) {
184
+ if (ec) {
185
+ if (endpoint_iterator == tcp::resolver::iterator ()) {
186
+ return ;
187
+ }
188
+ return ;
189
+ }
190
+
191
+ connect (ec, endpoint_iterator);
192
+ });
164
193
165
194
return response;
166
195
}
0 commit comments