@@ -55,6 +55,8 @@ enum ChunkedState {
55
55
Body ,
56
56
BodyCr ,
57
57
BodyLf ,
58
+ Trailer ,
59
+ TrailerLf ,
58
60
EndCr ,
59
61
EndLf ,
60
62
End ,
@@ -196,6 +198,8 @@ impl ChunkedState {
196
198
Body => ChunkedState :: read_body ( cx, body, size, buf) ,
197
199
BodyCr => ChunkedState :: read_body_cr ( cx, body) ,
198
200
BodyLf => ChunkedState :: read_body_lf ( cx, body) ,
201
+ Trailer => ChunkedState :: read_trailer ( cx, body) ,
202
+ TrailerLf => ChunkedState :: read_trailer_lf ( cx, body) ,
199
203
EndCr => ChunkedState :: read_end_cr ( cx, body) ,
200
204
EndLf => ChunkedState :: read_end_lf ( cx, body) ,
201
205
End => Poll :: Ready ( Ok ( ChunkedState :: End ) ) ,
@@ -340,18 +344,38 @@ impl ChunkedState {
340
344
}
341
345
}
342
346
343
- fn read_end_cr < R : MemRead > (
347
+ fn read_trailer < R : MemRead > (
344
348
cx : & mut task:: Context < ' _ > ,
345
349
rdr : & mut R ,
346
350
) -> Poll < Result < ChunkedState , io:: Error > > {
351
+ trace ! ( "read_trailer" ) ;
347
352
match byte ! ( rdr, cx) {
348
- b'\r' => Poll :: Ready ( Ok ( ChunkedState :: EndLf ) ) ,
353
+ b'\r' => Poll :: Ready ( Ok ( ChunkedState :: TrailerLf ) ) ,
354
+ _ => Poll :: Ready ( Ok ( ChunkedState :: Trailer ) ) ,
355
+ }
356
+ }
357
+ fn read_trailer_lf < R : MemRead > (
358
+ cx : & mut task:: Context < ' _ > ,
359
+ rdr : & mut R ,
360
+ ) -> Poll < Result < ChunkedState , io:: Error > > {
361
+ match byte ! ( rdr, cx) {
362
+ b'\n' => Poll :: Ready ( Ok ( ChunkedState :: EndCr ) ) ,
349
363
_ => Poll :: Ready ( Err ( io:: Error :: new (
350
364
io:: ErrorKind :: InvalidInput ,
351
- "Invalid chunk end CR " ,
365
+ "Invalid trailer end LF " ,
352
366
) ) ) ,
353
367
}
354
368
}
369
+
370
+ fn read_end_cr < R : MemRead > (
371
+ cx : & mut task:: Context < ' _ > ,
372
+ rdr : & mut R ,
373
+ ) -> Poll < Result < ChunkedState , io:: Error > > {
374
+ match byte ! ( rdr, cx) {
375
+ b'\r' => Poll :: Ready ( Ok ( ChunkedState :: EndLf ) ) ,
376
+ _ => Poll :: Ready ( Ok ( ChunkedState :: Trailer ) ) ,
377
+ }
378
+ }
355
379
fn read_end_lf < R : MemRead > (
356
380
cx : & mut task:: Context < ' _ > ,
357
381
rdr : & mut R ,
@@ -537,6 +561,15 @@ mod tests {
537
561
assert_eq ! ( "1234567890abcdef" , & result) ;
538
562
}
539
563
564
+ #[ tokio:: test]
565
+ async fn test_read_chunked_trailer_with_missing_lf ( ) {
566
+ let mut mock_buf = & b"10\r \n 1234567890abcdef\r \n 0\r \n bad\r \r \n " [ ..] ;
567
+ let mut decoder = Decoder :: chunked ( ) ;
568
+ decoder. decode_fut ( & mut mock_buf) . await . expect ( "decode" ) ;
569
+ let e = decoder. decode_fut ( & mut mock_buf) . await . unwrap_err ( ) ;
570
+ assert_eq ! ( e. kind( ) , io:: ErrorKind :: InvalidInput ) ;
571
+ }
572
+
540
573
#[ tokio:: test]
541
574
async fn test_read_chunked_after_eof ( ) {
542
575
let mut mock_buf = & b"10\r \n 1234567890abcdef\r \n 0\r \n \r \n " [ ..] ;
0 commit comments