@@ -7,7 +7,7 @@ use std::task::{Context, Poll};
77use async_std:: io:: { self , Read } ;
88use async_std:: sync:: Arc ;
99use byte_pool:: { Block , BytePool } ;
10- use http_types:: trailers:: { Trailers , TrailersSender } ;
10+ use http_types:: trailers:: { Sender , Trailers } ;
1111
1212const INITIAL_CAPACITY : usize = 1024 * 4 ;
1313const MAX_CAPACITY : usize = 512 * 1024 * 1024 ; // 512 MiB
@@ -32,11 +32,11 @@ pub(crate) struct ChunkedDecoder<R: Read> {
3232 /// Current state.
3333 state : State ,
3434 /// Trailer channel sender.
35- trailer_sender : Option < TrailersSender > ,
35+ trailer_sender : Option < Sender > ,
3636}
3737
3838impl < R : Read > ChunkedDecoder < R > {
39- pub ( crate ) fn new ( inner : R , trailer_sender : TrailersSender ) -> Self {
39+ pub ( crate ) fn new ( inner : R , trailer_sender : Sender ) -> Self {
4040 ChunkedDecoder {
4141 inner,
4242 buffer : POOL . alloc ( INITIAL_CAPACITY ) ,
@@ -156,7 +156,7 @@ impl<R: Read + Unpin> ChunkedDecoder<R> {
156156 let sender =
157157 sender. expect ( "invalid chunked state, tried sending multiple trailers" ) ;
158158
159- let fut = Box :: pin ( sender. send ( Ok ( headers) ) ) ;
159+ let fut = Box :: pin ( sender. send ( headers) ) ;
160160 Ok ( DecodeResult :: Some {
161161 read : 0 ,
162162 new_state : Some ( State :: TrailerSending ( fut) ) ,
@@ -483,10 +483,7 @@ fn decode_trailer(buffer: Block<'static>, pos: &Range<usize>) -> io::Result<Deco
483483 Ok ( Status :: Complete ( ( used, headers) ) ) => {
484484 let mut trailers = Trailers :: new ( ) ;
485485 for header in headers {
486- let value = std:: string:: String :: from_utf8_lossy ( header. value ) . to_string ( ) ;
487- if let Err ( err) = trailers. insert ( header. name , value) {
488- return Err ( io:: Error :: new ( io:: ErrorKind :: Other , err. to_string ( ) ) ) ;
489- }
486+ trailers. insert ( header. name , String :: from_utf8_lossy ( header. value ) . as_ref ( ) ) ;
490487 }
491488
492489 Ok ( DecodeResult :: Some {
@@ -527,7 +524,7 @@ mod tests {
527524 ) ;
528525
529526 let ( s, _r) = async_std:: sync:: channel ( 1 ) ;
530- let sender = TrailersSender :: new ( s) ;
527+ let sender = Sender :: new ( s) ;
531528 let mut decoder = ChunkedDecoder :: new ( input, sender) ;
532529
533530 let mut output = String :: new ( ) ;
@@ -553,7 +550,7 @@ mod tests {
553550 input. extend ( "\r \n 0\r \n \r \n " . as_bytes ( ) ) ;
554551
555552 let ( s, _r) = async_std:: sync:: channel ( 1 ) ;
556- let sender = TrailersSender :: new ( s) ;
553+ let sender = Sender :: new ( s) ;
557554 let mut decoder = ChunkedDecoder :: new ( async_std:: io:: Cursor :: new ( input) , sender) ;
558555
559556 let mut output = String :: new ( ) ;
@@ -583,20 +580,30 @@ mod tests {
583580 . as_bytes ( ) ,
584581 ) ;
585582 let ( s, r) = async_std:: sync:: channel ( 1 ) ;
586- let sender = TrailersSender :: new ( s) ;
583+ let sender = Sender :: new ( s) ;
587584 let mut decoder = ChunkedDecoder :: new ( input, sender) ;
588585
589586 let mut output = String :: new ( ) ;
590587 decoder. read_to_string ( & mut output) . await . unwrap ( ) ;
591588 assert_eq ! ( output, "MozillaDeveloperNetwork" ) ;
592589
593- let trailer = r. recv ( ) . await . unwrap ( ) . unwrap ( ) ;
590+ let trailers = r. recv ( ) . await . unwrap ( ) ;
591+ let as_vec = trailers. iter ( ) . collect :: < Vec < _ > > ( ) ;
592+
593+ assert_eq ! ( as_vec. len( ) , 1 ) ;
594+ let ( header_name, header_values) = as_vec[ 0 ] ;
595+ assert_eq ! (
596+ header_name,
597+ & "Expires"
598+ . parse:: <http_types:: headers:: HeaderName >( )
599+ . unwrap( )
600+ ) ;
601+ assert_eq ! ( header_values. iter( ) . collect:: <Vec <_>>( ) . len( ) , 1 ) ;
594602 assert_eq ! (
595- trailer. iter( ) . collect:: <Vec <_>>( ) ,
596- vec![ (
597- & "Expires" . parse( ) . unwrap( ) ,
598- & vec![ "Wed, 21 Oct 2015 07:28:00 GMT" . parse( ) . unwrap( ) ] ,
599- ) ]
603+ header_values. last( ) ,
604+ & "Wed, 21 Oct 2015 07:28:00 GMT"
605+ . parse:: <http_types:: headers:: HeaderValue >( )
606+ . unwrap( )
600607 ) ;
601608 } ) ;
602609 }
0 commit comments