@@ -4,7 +4,7 @@ use std::pin::Pin;
4
4
5
5
use async_std:: io;
6
6
use async_std:: io:: prelude:: * ;
7
- use async_std:: task:: { ready , Context , Poll } ;
7
+ use async_std:: task:: { Context , Poll } ;
8
8
use http_types:: Response ;
9
9
10
10
use crate :: date:: fmt_http_date;
@@ -201,15 +201,26 @@ impl Encoder {
201
201
cx : & mut Context < ' _ > ,
202
202
buf : & mut [ u8 ] ,
203
203
) -> Poll < io:: Result < usize > > {
204
- // Get bytes from the underlying stream. If a zero-length buffer is
205
- // returned we're done.
206
- let src = ready ! ( Pin :: new( & mut self . res) . poll_fill_buf( cx) ) ?;
204
+ // Get bytes from the underlying stream. If the stream is not ready yet,
205
+ // return the header bytes if we have any.
206
+ let src = match Pin :: new ( & mut self . res ) . poll_fill_buf ( cx) {
207
+ Poll :: Ready ( Ok ( n) ) => n,
208
+ Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
209
+ Poll :: Pending => match self . bytes_read {
210
+ 0 => return Poll :: Pending ,
211
+ n => return Poll :: Ready ( Ok ( n) ) ,
212
+ } ,
213
+ } ;
214
+
215
+ // If the stream doesn't have any more bytes left to read we're done.
207
216
if src. len ( ) == 0 {
208
- // Finalize the chunk with a final CRLF.
217
+ // Write out the final empty chunk
209
218
let idx = self . bytes_read ;
210
219
buf[ idx] = b'0' ;
211
220
buf[ idx + 1 ] = CR ;
212
221
buf[ idx + 2 ] = LF ;
222
+
223
+ // Write the final CRLF
213
224
buf[ idx + 3 ] = CR ;
214
225
buf[ idx + 4 ] = LF ;
215
226
self . bytes_read += 5 ;
@@ -224,9 +235,10 @@ impl Encoder {
224
235
// buffer to read all that.
225
236
let buf_len = buf. len ( ) . checked_sub ( self . bytes_read ) . unwrap_or ( 0 ) ;
226
237
let amt = src. len ( ) . min ( buf_len) ;
227
- let len_prefix = format ! ( "{}" , amt) . into_bytes ( ) ;
238
+ let len_prefix = format ! ( "{:X }" , amt) . into_bytes ( ) ;
228
239
let buf_upper = buf_len. checked_sub ( len_prefix. len ( ) + 4 ) . unwrap_or ( 0 ) ;
229
240
let amt = amt. min ( buf_upper) ;
241
+ let len_prefix = format ! ( "{:X}" , amt) . into_bytes ( ) ;
230
242
231
243
// Write our frame header to the buffer.
232
244
let lower = self . bytes_read ;
0 commit comments