@@ -9,7 +9,6 @@ use async_std::io::{self, BufReader};
99use async_std:: io:: { Read , Write } ;
1010use async_std:: prelude:: * ;
1111use async_std:: task:: { Context , Poll } ;
12- use futures_core:: ready;
1312use http_types:: headers:: { HeaderName , HeaderValue , CONTENT_LENGTH , TRANSFER_ENCODING } ;
1413use http_types:: { ensure, ensure_eq, format_err} ;
1514use http_types:: { Body , Method , Request , Response } ;
@@ -198,9 +197,19 @@ impl Read for Encoder {
198197 // Figure out how many bytes we can read.
199198 let upper_bound = ( bytes_read + body_len - body_bytes_read) . min ( buf. len ( ) ) ;
200199 // 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+ } ;
204213 body_bytes_read += new_body_bytes_read;
205214 bytes_read += new_body_bytes_read;
206215
@@ -212,8 +221,13 @@ impl Read for Encoder {
212221 body_len,
213222 body_bytes_read
214223 ) ;
215- // If we've read the `len` number of bytes, end
216224 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?
217231 self . state = EncoderState :: Done ;
218232 break ;
219233 } else {
@@ -237,8 +251,18 @@ impl Read for Encoder {
237251 // it into the actual buffer
238252 let mut chunk_buf = vec ! [ 0 ; buffer_remaining] ;
239253 // 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+ } ;
242266
243267 // serialize chunk length as hex
244268 let chunk_length_string = format ! ( "{:X}" , chunk_length) ;
@@ -311,7 +335,18 @@ impl Read for Encoder {
311335 ref mut chunk,
312336 is_last,
313337 } => {
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+ } ;
315350 if bytes_read == 0 {
316351 self . state = match is_last {
317352 true => EncoderState :: Done ,
0 commit comments