@@ -188,14 +188,12 @@ impl Read for Encoder {
188
188
None => buf. len ( ) - 2 ,
189
189
} ;
190
190
191
- // Read bytes, and update internal tracking stuff.
192
- body_bytes_read = ready ! (
193
- Pin :: new( & mut self . res) . poll_read( cx, & mut buf[ head_bytes_read..upper_bound] )
194
- ) ?;
195
- self . body_bytes_read += body_bytes_read; // total body bytes read on all polls
196
-
197
191
match self . res . len ( ) {
198
192
Some ( len) => {
193
+ // Read bytes, and update internal tracking stuff.
194
+ body_bytes_read = ready ! ( Pin :: new( & mut self . res)
195
+ . poll_read( cx, & mut buf[ head_bytes_read..upper_bound] ) ) ?;
196
+
199
197
debug_assert ! (
200
198
self . body_bytes_read <= len,
201
199
"Too many bytes read. Expected: {}, read: {}" ,
@@ -208,22 +206,46 @@ impl Read for Encoder {
208
206
}
209
207
}
210
208
None => {
209
+ let mut chunk_buf = vec ! [ 0 ; buf. len( ) ] ;
210
+ // Read bytes from body reader
211
+ let chunk_length = ready ! ( Pin :: new( & mut self . res)
212
+ . poll_read( cx, & mut chunk_buf[ 0 ..buf. len( ) - head_bytes_read] ) ) ?;
213
+
214
+ // serialize chunk length as hex
215
+ let chunk_length_string = format ! ( "{:X}" , chunk_length) ;
216
+ let chunk_length_bytes = chunk_length_string. as_bytes ( ) ;
217
+ let chunk_length_bytes_len = chunk_length_bytes. len ( ) ;
218
+ body_bytes_read += chunk_length_bytes_len;
219
+ buf[ head_bytes_read..head_bytes_read + body_bytes_read]
220
+ . copy_from_slice ( chunk_length_bytes) ;
221
+
222
+ // follow chunk length with CRLF
223
+ buf[ head_bytes_read + body_bytes_read] = b'\r' ;
224
+ buf[ head_bytes_read + body_bytes_read + 1 ] = b'\n' ;
225
+ body_bytes_read += 2 ;
226
+
227
+ // copy chunk into buf
228
+ buf[ head_bytes_read + body_bytes_read
229
+ ..head_bytes_read + body_bytes_read + chunk_length]
230
+ . copy_from_slice ( & chunk_buf[ ..chunk_length] ) ;
231
+ body_bytes_read += chunk_length;
232
+
211
233
// TODO: relax this constraint at some point by adding extra state
212
234
let bytes_read = head_bytes_read + body_bytes_read;
213
- assert ! ( buf. len( ) >= bytes_read + 4 , "Buffers should have room for the head, the body , and 4 extra bytes when using chunked encoding" ) ;
235
+ assert ! ( buf. len( ) >= bytes_read + 7 , "Buffers should have room for the head, the chunk length, 2 bytes, the chunk , and 4 extra bytes when using chunked encoding" ) ;
214
236
215
- buf[ bytes_read] = b'\r' ;
216
- buf[ bytes_read + 1 ] = b'\n' ;
237
+ buf[ bytes_read..bytes_read + 7 ] . copy_from_slice ( b"\r \n 0\r \n \r \n " ) ;
217
238
218
- if body_bytes_read == 0 {
219
- self . body_done = true ;
220
- buf[ bytes_read + 2 ] = b'\r' ;
221
- buf[ bytes_read + 3 ] = b'\n' ;
222
- }
239
+ // if body_bytes_read == 0 {
240
+ self . body_done = true ;
241
+ body_bytes_read += 7 ;
242
+ // }
223
243
}
224
244
}
225
245
}
226
246
247
+ self . body_bytes_read += body_bytes_read; // total body bytes read on all polls
248
+
227
249
// Return the total amount of bytes read.
228
250
let bytes_read = head_bytes_read + body_bytes_read;
229
251
Poll :: Ready ( Ok ( bytes_read as usize ) )
0 commit comments