@@ -51,12 +51,12 @@ namespace boost { namespace network { namespace http { namespace impl {
51
51
52
52
http_async_connection (
53
53
resolver_type & resolver,
54
- resolve_function resolve,
55
- bool follow_redirect
56
- ) :
54
+ resolve_function resolve,
55
+ bool follow_redirect
56
+ ) :
57
57
follow_redirect_ (follow_redirect),
58
58
resolver_ (resolver),
59
- resolve_ (resolve),
59
+ resolve_ (resolve),
60
60
request_strand_ (resolver.get_io_service())
61
61
{}
62
62
@@ -68,7 +68,7 @@ namespace boost { namespace network { namespace http { namespace impl {
68
68
std::ostreambuf_iterator<typename char_<Tag>::type>(&command_streambuf));
69
69
this ->method = method;
70
70
boost::uint16_t port_ = port (request);
71
- resolve_ (resolver_, host (request),
71
+ resolve_ (resolver_, host (request),
72
72
port_,
73
73
request_strand_.wrap (
74
74
boost::bind (
@@ -83,6 +83,17 @@ namespace boost { namespace network { namespace http { namespace impl {
83
83
84
84
http_async_connection (http_async_connection const &); // = delete
85
85
86
+ void set_errors (boost::system::error_code const & ec) {
87
+ boost::system ::system_error error (ec);
88
+ this ->version_promise .set_exception (boost::copy_exception (error));
89
+ this ->status_promise .set_exception (boost::copy_exception (error));
90
+ this ->status_message_promise .set_exception (boost::copy_exception (error));
91
+ this ->headers_promise .set_exception (boost::copy_exception (error));
92
+ this ->source_promise .set_exception (boost::copy_exception (error));
93
+ this ->destination_promise .set_exception (boost::copy_exception (error));
94
+ this ->body_promise .set_exception (boost::copy_exception (error));
95
+ }
96
+
86
97
void handle_resolved (boost::uint16_t port, bool get_body, body_callback_function_type callback, boost::system::error_code const & ec, resolver_iterator_pair endpoint_range) {
87
98
resolver_iterator iter = boost::begin (endpoint_range);
88
99
if (!ec && !boost::empty (endpoint_range)) {
@@ -103,14 +114,7 @@ namespace boost { namespace network { namespace http { namespace impl {
103
114
boost::asio::placeholders::error
104
115
)));
105
116
} else {
106
- boost::system ::system_error error (ec ? ec : boost::asio::error::host_not_found);
107
- this ->version_promise .set_exception (boost::copy_exception (error));
108
- this ->status_promise .set_exception (boost::copy_exception (error));
109
- this ->status_message_promise .set_exception (boost::copy_exception (error));
110
- this ->headers_promise .set_exception (boost::copy_exception (error));
111
- this ->source_promise .set_exception (boost::copy_exception (error));
112
- this ->destination_promise .set_exception (boost::copy_exception (error));
113
- this ->body_promise .set_exception (boost::copy_exception (error));
117
+ set_errors (ec ? ec : boost::asio::error::host_not_found);
114
118
}
115
119
}
116
120
@@ -123,7 +127,7 @@ namespace boost { namespace network { namespace http { namespace impl {
123
127
boost::bind (
124
128
&http_async_connection<Tag,version_major,version_minor>::handle_sent_request,
125
129
http_async_connection<Tag,version_major,version_minor>::shared_from_this (),
126
- get_body, callback,
130
+ get_body, callback,
127
131
boost::asio::placeholders::error,
128
132
boost::asio::placeholders::bytes_transferred
129
133
)));
@@ -146,16 +150,7 @@ namespace boost { namespace network { namespace http { namespace impl {
146
150
boost::asio::placeholders::error
147
151
)));
148
152
} else {
149
- boost::system ::system_error error (
150
- ec ? ec : boost::asio::error::host_not_found
151
- );
152
- this ->version_promise .set_exception (boost::copy_exception (error));
153
- this ->status_promise .set_exception (boost::copy_exception (error));
154
- this ->status_message_promise .set_exception (boost::copy_exception (error));
155
- this ->headers_promise .set_exception (boost::copy_exception (error));
156
- this ->source_promise .set_exception (boost::copy_exception (error));
157
- this ->destination_promise .set_exception (boost::copy_exception (error));
158
- this ->body_promise .set_exception (boost::copy_exception (error));
153
+ set_errors (ec ? ec : boost::asio::error::host_not_found);
159
154
}
160
155
}
161
156
}
@@ -173,36 +168,34 @@ namespace boost { namespace network { namespace http { namespace impl {
173
168
&http_async_connection<Tag,version_major,version_minor>::handle_received_data,
174
169
http_async_connection<Tag,version_major,version_minor>::shared_from_this (),
175
170
version, get_body, callback,
176
- boost::asio::placeholders::error,
171
+ boost::asio::placeholders::error,
177
172
boost::asio::placeholders::bytes_transferred)));
178
173
} else {
179
- boost::system ::system_error error (ec);
180
- this ->version_promise .set_exception (boost::copy_exception (error));
181
- this ->status_promise .set_exception (boost::copy_exception (error));
182
- this ->status_message_promise .set_exception (boost::copy_exception (error));
183
- this ->headers_promise .set_exception (boost::copy_exception (error));
184
- this ->source_promise .set_exception (boost::copy_exception (error));
185
- this ->destination_promise .set_exception (boost::copy_exception (error));
186
- this ->body_promise .set_exception (boost::copy_exception (error));
174
+ set_errors (ec);
187
175
}
188
176
}
189
177
190
178
void handle_received_data (state_t state, bool get_body, body_callback_function_type callback, boost::system::error_code const & ec, std::size_t bytes_transferred) {
191
179
if (!ec || ec == boost::asio::error::eof) {
180
+ // Sanity check
181
+ if (ec == boost::asio::error::eof && state < headers) {
182
+ return ;
183
+ }
192
184
logic::tribool parsed_ok;
193
185
size_t remainder ;
194
186
switch (state) {
195
187
case version:
196
- parsed_ok =
188
+ parsed_ok =
197
189
this ->parse_version (*socket_,
198
190
request_strand_.wrap (
199
191
boost::bind (
200
192
&http_async_connection<Tag,version_major,version_minor>::handle_received_data,
201
193
http_async_connection<Tag,version_major,version_minor>::shared_from_this (),
202
- status , get_body, callback,
194
+ version , get_body, callback,
203
195
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred
204
196
)
205
- )
197
+ ),
198
+ bytes_transferred
206
199
);
207
200
if (parsed_ok != true ) return ;
208
201
case status:
@@ -212,10 +205,11 @@ namespace boost { namespace network { namespace http { namespace impl {
212
205
boost::bind (
213
206
&http_async_connection<Tag,version_major,version_minor>::handle_received_data,
214
207
http_async_connection<Tag,version_major,version_minor>::shared_from_this (),
215
- status_message , get_body, callback,
208
+ status , get_body, callback,
216
209
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred
217
210
)
218
- )
211
+ ),
212
+ bytes_transferred
219
213
);
220
214
if (parsed_ok != true ) return ;
221
215
case status_message:
@@ -225,10 +219,11 @@ namespace boost { namespace network { namespace http { namespace impl {
225
219
boost::bind (
226
220
&http_async_connection<Tag,version_major,version_minor>::handle_received_data,
227
221
http_async_connection<Tag,version_major,version_minor>::shared_from_this (),
228
- headers , get_body, callback,
222
+ status_message , get_body, callback,
229
223
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred
230
224
)
231
- )
225
+ ),
226
+ bytes_transferred
232
227
);
233
228
if (parsed_ok != true ) return ;
234
229
case headers:
@@ -241,7 +236,8 @@ namespace boost { namespace network { namespace http { namespace impl {
241
236
headers, get_body, callback,
242
237
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred
243
238
)
244
- )
239
+ ),
240
+ bytes_transferred
245
241
);
246
242
if (parsed_ok != true ) return ;
247
243
if (!get_body) {
@@ -253,8 +249,9 @@ namespace boost { namespace network { namespace http { namespace impl {
253
249
std::advance (begin, remainder );
254
250
typename protocol_base::buffer_type::const_iterator end = begin;
255
251
std::advance (end, this ->part .size () - remainder );
252
+ this ->body_promise .set_value (" " );
256
253
callback (make_iterator_range (begin, end), ec);
257
- socket_->async_read_some (
254
+ socket_->async_read_some (
258
255
boost::asio::mutable_buffers_1 (this ->part .c_array (), this ->part .size ()),
259
256
request_strand_.wrap (
260
257
boost::bind (
@@ -264,7 +261,6 @@ namespace boost { namespace network { namespace http { namespace impl {
264
261
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)
265
262
)
266
263
);
267
- this ->body_promise .set_value (" " );
268
264
} else {
269
265
this ->parse_body (
270
266
*socket_,
@@ -278,7 +274,7 @@ namespace boost { namespace network { namespace http { namespace impl {
278
274
),
279
275
remainder );
280
276
}
281
- break ;
277
+ return ;
282
278
case body:
283
279
if (ec == boost::asio::error::eof) {
284
280
if (callback) {
@@ -304,7 +300,7 @@ namespace boost { namespace network { namespace http { namespace impl {
304
300
typename protocol_base::buffer_type::const_iterator begin = this ->part .begin (), end = begin;
305
301
std::advance (end, bytes_transferred);
306
302
callback (make_iterator_range (begin, end), ec);
307
- socket_->async_read_some (
303
+ socket_->async_read_some (
308
304
boost::asio::mutable_buffers_1 (this ->part .c_array (), this ->part .size ()),
309
305
request_strand_.wrap (
310
306
boost::bind (
@@ -328,7 +324,7 @@ namespace boost { namespace network { namespace http { namespace impl {
328
324
bytes_transferred);
329
325
}
330
326
}
331
- break ;
327
+ return ;
332
328
default :
333
329
BOOST_ASSERT (false && " Bug, report this to the developers!" );
334
330
}
@@ -337,13 +333,13 @@ namespace boost { namespace network { namespace http { namespace impl {
337
333
this ->source_promise .set_exception (boost::copy_exception (error));
338
334
this ->destination_promise .set_exception (boost::copy_exception (error));
339
335
switch (state) {
340
- case version:
336
+ case version:
341
337
this ->version_promise .set_exception (boost::copy_exception (error));
342
- case status:
338
+ case status:
343
339
this ->status_promise .set_exception (boost::copy_exception (error));
344
340
case status_message:
345
341
this ->status_message_promise .set_exception (boost::copy_exception (error));
346
- case headers:
342
+ case headers:
347
343
this ->headers_promise .set_exception (boost::copy_exception (error));
348
344
case body:
349
345
this ->body_promise .set_exception (boost::copy_exception (error));
0 commit comments