@@ -24,11 +24,18 @@ impl<R: Read + Unpin> ChunkedEncoder<R> {
24
24
25
25
fn max_bytes_to_read ( buf_len : usize ) -> usize {
26
26
if buf_len < 6 {
27
+ // the minimum read size is of 6 represents one byte of
28
+ // content from the body. the other five bytes are 1\r\n_\r\n
29
+ // where _ is the actual content in question
27
30
panic ! ( "buffers of length {} are too small for this implementation. if this is a problem for you, please open an issue" , buf_len) ;
28
31
}
32
+
33
+ let bytes_remaining_after_two_cr_lns = ( buf_len - 4 ) as f64 ;
34
+
29
35
// the maximum number of bytes that the hex representation of remaining bytes might take
30
- let max_bytes_of_hex_framing = ( ( ( buf_len - 5 ) as f64 ) . log2 ( ) / 4f64 ) . floor ( ) ;
31
- buf_len - 5 - ( max_bytes_of_hex_framing as usize )
36
+ let max_bytes_of_hex_framing = bytes_remaining_after_two_cr_lns. log2 ( ) / 4f64 ;
37
+
38
+ ( bytes_remaining_after_two_cr_lns - max_bytes_of_hex_framing. ceil ( ) ) as usize
32
39
}
33
40
34
41
#[ cfg( test) ]
@@ -39,10 +46,10 @@ mod test_bytes_to_read {
39
46
// and a nonobvious but intentional consequence of the
40
47
// implementation. in order to avoid overflowing, we must use
41
48
// one fewer than the available buffer bytes because
42
- // increasing the read size by one byte would increase the
43
- // number of framed bytes by two. This occurs when the hex
44
- // representation of the content bytes increases order of
45
- // magnitude (F->10, FF->100, FFF-> 1000, etc)
49
+ // increasing the read size increase the number of framed
50
+ // bytes by two. This occurs when the hex representation of
51
+ // the content bytes is near an increase in order of magnitude
52
+ // (F->10, FF->100, FFF-> 1000, etc)
46
53
let values = vec ! [
47
54
( 6 , 1 ) , // 1
48
55
( 7 , 2 ) , // 2
@@ -52,12 +59,13 @@ mod test_bytes_to_read {
52
59
( 23 , 17 ) , // 11
53
60
( 260 , 254 ) , // FE
54
61
( 261 , 254 ) , // FE <-
55
- ( 262 , 255 ) , // FF
62
+ ( 262 , 255 ) , // FF <-
56
63
( 263 , 256 ) , // 100
57
64
( 4100 , 4093 ) , // FFD
58
65
( 4101 , 4093 ) , // FFD <-
59
- ( 4102 , 4094 ) , // FFE
60
- ( 4103 , 4095 ) , // FFF
66
+ ( 4102 , 4094 ) , // FFE <-
67
+ ( 4103 , 4095 ) , // FFF <-
68
+ ( 4104 , 4096 ) , // 1000
61
69
] ;
62
70
63
71
for ( input, expected) in values {
0 commit comments