@@ -9,7 +9,6 @@ use async_std::io::{self, BufReader};
9
9
use async_std:: io:: { Read , Write } ;
10
10
use async_std:: prelude:: * ;
11
11
use async_std:: task:: { Context , Poll } ;
12
- use futures_core:: ready;
13
12
use http_types:: headers:: { HeaderName , HeaderValue , CONTENT_LENGTH , TRANSFER_ENCODING } ;
14
13
use http_types:: { ensure, ensure_eq, format_err} ;
15
14
use http_types:: { Body , Method , Request , Response } ;
@@ -198,9 +197,19 @@ impl Read for Encoder {
198
197
// Figure out how many bytes we can read.
199
198
let upper_bound = ( bytes_read + body_len - body_bytes_read) . min ( buf. len ( ) ) ;
200
199
// Read bytes from body
201
- let new_body_bytes_read =
202
- ready ! ( Pin :: new( & mut self . res)
203
- . poll_read( cx, & mut buf[ bytes_read..upper_bound] ) ) ?;
200
+ let inner_poll_result =
201
+ Pin :: new ( & mut self . res ) . poll_read ( cx, & mut buf[ bytes_read..upper_bound] ) ;
202
+ let new_body_bytes_read = match inner_poll_result {
203
+ Poll :: Ready ( Ok ( n) ) => n,
204
+ Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
205
+ Poll :: Pending => {
206
+ if bytes_read == 0 {
207
+ return Poll :: Pending ;
208
+ } else {
209
+ break ;
210
+ }
211
+ }
212
+ } ;
204
213
body_bytes_read += new_body_bytes_read;
205
214
bytes_read += new_body_bytes_read;
206
215
@@ -212,8 +221,13 @@ impl Read for Encoder {
212
221
body_len,
213
222
body_bytes_read
214
223
) ;
215
- // If we've read the `len` number of bytes, end
216
224
if body_len == body_bytes_read {
225
+ // If we've read the `len` number of bytes, end
226
+ self . state = EncoderState :: Done ;
227
+ break ;
228
+ } else if new_body_bytes_read == 0 {
229
+ // If we've reached unexpected EOF, end anyway
230
+ // TODO: do something?
217
231
self . state = EncoderState :: Done ;
218
232
break ;
219
233
} else {
@@ -237,8 +251,18 @@ impl Read for Encoder {
237
251
// it into the actual buffer
238
252
let mut chunk_buf = vec ! [ 0 ; buffer_remaining] ;
239
253
// Read bytes from body reader
240
- let chunk_length =
241
- ready ! ( Pin :: new( & mut self . res) . poll_read( cx, & mut chunk_buf) ) ?;
254
+ let inner_poll_result = Pin :: new ( & mut self . res ) . poll_read ( cx, & mut chunk_buf) ;
255
+ let chunk_length = match inner_poll_result {
256
+ Poll :: Ready ( Ok ( n) ) => n,
257
+ Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
258
+ Poll :: Pending => {
259
+ if bytes_read == 0 {
260
+ return Poll :: Pending ;
261
+ } else {
262
+ break ;
263
+ }
264
+ }
265
+ } ;
242
266
243
267
// serialize chunk length as hex
244
268
let chunk_length_string = format ! ( "{:X}" , chunk_length) ;
@@ -311,7 +335,18 @@ impl Read for Encoder {
311
335
ref mut chunk,
312
336
is_last,
313
337
} => {
314
- bytes_read += ready ! ( Pin :: new( chunk) . poll_read( cx, & mut buf) ) ?;
338
+ let inner_poll_result = Pin :: new ( chunk) . poll_read ( cx, & mut buf) ;
339
+ bytes_read += match inner_poll_result {
340
+ Poll :: Ready ( Ok ( n) ) => n,
341
+ Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
342
+ Poll :: Pending => {
343
+ if bytes_read == 0 {
344
+ return Poll :: Pending ;
345
+ } else {
346
+ break ;
347
+ }
348
+ }
349
+ } ;
315
350
if bytes_read == 0 {
316
351
self . state = match is_last {
317
352
true => EncoderState :: Done ,
0 commit comments