@@ -22,7 +22,7 @@ namespace http {
22
22
namespace v2 {
23
23
using boost::asio::ip::tcp;
24
24
25
- struct request_helper {
25
+ struct request_context {
26
26
27
27
std::shared_ptr<client_connection::async_connection> connection_;
28
28
@@ -38,7 +38,7 @@ struct request_helper {
38
38
39
39
std::uint64_t total_bytes_written_, total_bytes_read_;
40
40
41
- request_helper (std::shared_ptr<client_connection::async_connection> connection,
41
+ request_context (std::shared_ptr<client_connection::async_connection> connection,
42
42
request request,
43
43
request_options options)
44
44
: connection_(connection)
@@ -59,32 +59,32 @@ struct client::impl {
59
59
60
60
~impl () noexcept ;
61
61
62
- std::future<response> execute (std::shared_ptr<request_helper> helper );
62
+ std::future<response> execute (std::shared_ptr<request_context> context );
63
63
64
64
void connect (const boost::system::error_code &ec,
65
65
tcp::resolver::iterator endpoint_iterator,
66
- std::shared_ptr<request_helper> helper );
66
+ std::shared_ptr<request_context> context );
67
67
68
68
void write_request (const boost::system::error_code &ec,
69
- std::shared_ptr<request_helper> helper );
69
+ std::shared_ptr<request_context> context );
70
70
71
71
void read_response (const boost::system::error_code &ec,
72
72
std::size_t bytes_written,
73
- std::shared_ptr<request_helper> helper );
73
+ std::shared_ptr<request_context> context );
74
74
75
75
void read_response_status (const boost::system::error_code &ec,
76
76
std::size_t bytes_written,
77
- std::shared_ptr<request_helper> helper ,
77
+ std::shared_ptr<request_context> context ,
78
78
std::shared_ptr<response> res);
79
79
80
80
void read_response_headers (const boost::system::error_code &ec,
81
81
std::size_t bytes_read,
82
- std::shared_ptr<request_helper> helper ,
82
+ std::shared_ptr<request_context> context ,
83
83
std::shared_ptr<response> res);
84
84
85
85
void read_response_body (const boost::system::error_code &ec,
86
86
std::size_t bytes_read,
87
- std::shared_ptr<request_helper> helper ,
87
+ std::shared_ptr<request_context> context ,
88
88
std::shared_ptr<response> res);
89
89
90
90
client_options options_;
@@ -122,112 +122,112 @@ client::impl::~impl() noexcept {
122
122
lifetime_thread_.join ();
123
123
}
124
124
125
- std::future<response> client::impl::execute (std::shared_ptr<request_helper> helper ) {
126
- std::future<response> res = helper ->response_promise_ .get_future ();
125
+ std::future<response> client::impl::execute (std::shared_ptr<request_context> context ) {
126
+ std::future<response> res = context ->response_promise_ .get_future ();
127
127
128
128
// TODO see linearize.hpp
129
129
130
130
// If there is no user-agent, provide one as a default.
131
- auto user_agent = helper ->request_ .header (" User-Agent" );
131
+ auto user_agent = context ->request_ .header (" User-Agent" );
132
132
if (!user_agent) {
133
- helper ->request_ .append_header (" User-Agent" , options_.user_agent ());
133
+ context ->request_ .append_header (" User-Agent" , options_.user_agent ());
134
134
}
135
135
136
- auto url = helper ->request_ .url ();
136
+ auto url = context ->request_ .url ();
137
137
auto host = url.host ()?
138
138
uri::string_type (std::begin (*url.host ()), std::end (*url.host ())) : uri::string_type ();
139
139
auto port = url.port <std::uint16_t >()? *url.port <std::uint16_t >() : 80 ;
140
140
141
141
resolver_->async_resolve (host, port,
142
142
strand_.wrap ([=](const boost::system::error_code &ec,
143
143
tcp::resolver::iterator endpoint_iterator) {
144
- connect (ec, endpoint_iterator, helper );
144
+ connect (ec, endpoint_iterator, context );
145
145
}));
146
146
147
147
return res;
148
148
}
149
149
150
150
void client::impl::connect (const boost::system::error_code &ec,
151
151
tcp::resolver::iterator endpoint_iterator,
152
- std::shared_ptr<request_helper> helper ) {
152
+ std::shared_ptr<request_context> context ) {
153
153
if (ec) {
154
- helper ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
154
+ context ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
155
155
return ;
156
156
}
157
157
158
- auto host = helper ->request_ .url ().host ();
158
+ auto host = context ->request_ .url ().host ();
159
159
tcp::endpoint endpoint (*endpoint_iterator);
160
- helper ->connection_ ->async_connect (endpoint,
160
+ context ->connection_ ->async_connect (endpoint,
161
161
std::string (std::begin (*host), std::end (*host)),
162
162
strand_.wrap ([=] (const boost::system::error_code &ec) {
163
163
if (ec && endpoint_iterator != tcp::resolver::iterator ()) {
164
164
// copy iterator because it is const after the lambda
165
165
// capture
166
166
auto it = endpoint_iterator;
167
167
boost::system::error_code ignore;
168
- connect (ignore, ++it, helper );
168
+ connect (ignore, ++it, context );
169
169
return ;
170
170
}
171
171
172
- write_request (ec, helper );
172
+ write_request (ec, context );
173
173
}));
174
174
}
175
175
176
176
void client::impl::write_request (const boost::system::error_code &ec,
177
- std::shared_ptr<request_helper> helper ) {
177
+ std::shared_ptr<request_context> context ) {
178
178
if (ec) {
179
- helper ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
179
+ context ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
180
180
return ;
181
181
}
182
182
183
- std::ostream request_stream (&helper ->request_buffer_ );
184
- request_stream << helper ->request_ ;
183
+ std::ostream request_stream (&context ->request_buffer_ );
184
+ request_stream << context ->request_ ;
185
185
if (!request_stream) {
186
- helper ->response_promise_ .set_exception (std::make_exception_ptr (client_exception (client_error::invalid_request)));
186
+ context ->response_promise_ .set_exception (std::make_exception_ptr (client_exception (client_error::invalid_request)));
187
187
}
188
188
189
189
// TODO write payload to request_buffer_
190
190
191
- helper ->connection_ ->async_write (helper ->request_buffer_ ,
191
+ context ->connection_ ->async_write (context ->request_buffer_ ,
192
192
strand_.wrap ([=] (const boost::system::error_code &ec,
193
193
std::size_t bytes_written) {
194
194
// TODO write chunked or write body
195
- read_response (ec, bytes_written, helper );
195
+ read_response (ec, bytes_written, context );
196
196
}));
197
197
}
198
198
199
199
void client::impl::read_response (const boost::system::error_code &ec,
200
200
std::size_t bytes_written,
201
- std::shared_ptr<request_helper> helper ) {
201
+ std::shared_ptr<request_context> context ) {
202
202
if (ec) {
203
- helper ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
203
+ context ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
204
204
return ;
205
205
}
206
206
207
- helper ->total_bytes_written_ += bytes_written;
208
- if (auto progress = helper ->options_ .progress ()) {
209
- progress (client_message::transfer_direction::bytes_written, helper ->total_bytes_written_ );
207
+ context ->total_bytes_written_ += bytes_written;
208
+ if (auto progress = context ->options_ .progress ()) {
209
+ progress (client_message::transfer_direction::bytes_written, context ->total_bytes_written_ );
210
210
}
211
211
212
212
std::shared_ptr<response> res (new response{});
213
- helper ->connection_ ->async_read_until (helper ->response_buffer_ ,
213
+ context ->connection_ ->async_read_until (context ->response_buffer_ ,
214
214
" \r\n " ,
215
215
strand_.wrap ([=] (const boost::system::error_code &ec,
216
216
std::size_t bytes_read) {
217
- read_response_status (ec, bytes_read, helper , res);
217
+ read_response_status (ec, bytes_read, context , res);
218
218
}));
219
219
}
220
220
221
221
void client::impl::read_response_status (const boost::system::error_code &ec,
222
222
std::size_t ,
223
- std::shared_ptr<request_helper> helper ,
223
+ std::shared_ptr<request_context> context ,
224
224
std::shared_ptr<response> res) {
225
225
if (ec) {
226
- helper ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
226
+ context ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
227
227
return ;
228
228
}
229
229
230
- std::istream is (&helper ->response_buffer_ );
230
+ std::istream is (&context ->response_buffer_ );
231
231
string_type version;
232
232
is >> version;
233
233
unsigned int status;
@@ -239,25 +239,25 @@ void client::impl::read_response_status(const boost::system::error_code &ec,
239
239
res->set_status (network::http::v2::status::code (status));
240
240
res->set_status_message (boost::trim_copy (message));
241
241
242
- helper ->connection_ ->async_read_until (helper ->response_buffer_ ,
242
+ context ->connection_ ->async_read_until (context ->response_buffer_ ,
243
243
" \r\n\r\n " ,
244
244
strand_.wrap ([=] (const boost::system::error_code &ec,
245
245
std::size_t bytes_read) {
246
- read_response_headers (ec, bytes_read, helper , res);
246
+ read_response_headers (ec, bytes_read, context , res);
247
247
}));
248
248
}
249
249
250
250
void client::impl::read_response_headers (const boost::system::error_code &ec,
251
251
std::size_t ,
252
- std::shared_ptr<request_helper> helper ,
252
+ std::shared_ptr<request_context> context ,
253
253
std::shared_ptr<response> res) {
254
254
if (ec) {
255
- helper ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
255
+ context ->response_promise_ .set_exception (std::make_exception_ptr (std::system_error (ec.value (), std::system_category ())));
256
256
return ;
257
257
}
258
258
259
259
// fill headers
260
- std::istream is (&helper ->response_buffer_ );
260
+ std::istream is (&context ->response_buffer_ );
261
261
string_type header;
262
262
while (std::getline (is, header) && (header != " \r " )) {
263
263
auto delim = boost::find_first_of (header, " :" );
@@ -267,10 +267,10 @@ void client::impl::read_response_headers(const boost::system::error_code &ec,
267
267
res->add_header (key, value);
268
268
}
269
269
270
- helper ->connection_ ->async_read (helper ->response_buffer_ ,
270
+ context ->connection_ ->async_read (context ->response_buffer_ ,
271
271
strand_.wrap ([=] (const boost::system::error_code &ec,
272
272
std::size_t bytes_read) {
273
- read_response_body (ec, bytes_read, helper , res);
273
+ read_response_body (ec, bytes_read, context , res);
274
274
}));
275
275
}
276
276
@@ -298,28 +298,28 @@ std::istream &getline_with_newline(std::istream &is, std::string &line) {
298
298
299
299
void client::impl::read_response_body (const boost::system::error_code &ec,
300
300
std::size_t bytes_read,
301
- std::shared_ptr<request_helper> helper ,
301
+ std::shared_ptr<request_context> context ,
302
302
std::shared_ptr<response> res) {
303
- helper ->total_bytes_read_ += bytes_read;
304
- if (auto progress = helper ->options_ .progress ()) {
305
- progress (client_message::transfer_direction::bytes_read, helper ->total_bytes_read_ );
303
+ context ->total_bytes_read_ += bytes_read;
304
+ if (auto progress = context ->options_ .progress ()) {
305
+ progress (client_message::transfer_direction::bytes_read, context ->total_bytes_read_ );
306
306
}
307
307
308
308
if (bytes_read == 0 ) {
309
- helper ->response_promise_ .set_value (*res);
309
+ context ->response_promise_ .set_value (*res);
310
310
return ;
311
311
}
312
312
313
- std::istream is (&helper ->response_buffer_ );
313
+ std::istream is (&context ->response_buffer_ );
314
314
string_type line;
315
315
while (!getline_with_newline (is, line).eof ()) {
316
316
res->append_body (line);
317
317
}
318
318
319
- helper ->connection_ ->async_read (helper ->response_buffer_ ,
319
+ context ->connection_ ->async_read (context ->response_buffer_ ,
320
320
strand_.wrap ([=] (const boost::system::error_code &ec,
321
321
std::size_t bytes_read) {
322
- read_response_body (ec, bytes_read, helper , res);
322
+ read_response_body (ec, bytes_read, context , res);
323
323
}));
324
324
}
325
325
@@ -348,7 +348,7 @@ std::future<response> client::execute(request req, request_options options) {
348
348
// TODO factory based on HTTP or HTTPS
349
349
connection = std::make_shared<client_connection::normal_connection>(pimpl_->io_service_ );
350
350
}
351
- return pimpl_->execute (std::make_shared<request_helper >(connection, req, options));
351
+ return pimpl_->execute (std::make_shared<request_context >(connection, req, options));
352
352
}
353
353
354
354
std::future<response> client::get (request req, request_options options) {
0 commit comments