@@ -48,28 +48,32 @@ impl<S: State> Middleware<S> for Compress {
4848 let level = self . 0 ;
4949 let best_encoding = parse ( & ctx. req ( ) . headers )
5050 . map_err ( |err| Error :: new ( StatusCode :: BAD_REQUEST , err, true ) ) ?;
51+ let body = std:: mem:: take ( & mut ctx. resp_mut ( ) . body ) ;
5152 let content_encoding = match best_encoding {
5253 None | Some ( Encoding :: Gzip ) => {
5354 ctx. resp_mut ( )
54- . wrapped ( move |body| GzipEncoder :: with_quality ( body, level) ) ;
55+ . write_stream ( GzipEncoder :: with_quality ( body, level) ) ;
5556 Encoding :: Gzip . to_header_value ( )
5657 }
5758 Some ( Encoding :: Deflate ) => {
5859 ctx. resp_mut ( )
59- . wrapped ( move |body| ZlibEncoder :: with_quality ( body, level) ) ;
60+ . write_stream ( ZlibEncoder :: with_quality ( body, level) ) ;
6061 Encoding :: Deflate . to_header_value ( )
6162 }
6263 Some ( Encoding :: Brotli ) => {
6364 ctx. resp_mut ( )
64- . wrapped ( move |body| BrotliEncoder :: with_quality ( body, level) ) ;
65+ . write_stream ( BrotliEncoder :: with_quality ( body, level) ) ;
6566 Encoding :: Brotli . to_header_value ( )
6667 }
6768 Some ( Encoding :: Zstd ) => {
6869 ctx. resp_mut ( )
69- . wrapped ( move |body| ZstdEncoder :: with_quality ( body, level) ) ;
70+ . write_stream ( ZstdEncoder :: with_quality ( body, level) ) ;
7071 Encoding :: Zstd . to_header_value ( )
7172 }
72- Some ( Encoding :: Identity ) => Encoding :: Identity . to_header_value ( ) ,
73+ Some ( Encoding :: Identity ) => {
74+ ctx. resp_mut ( ) . body = body;
75+ Encoding :: Identity . to_header_value ( )
76+ }
7377 } ;
7478 ctx. resp_mut ( )
7579 . headers
@@ -122,9 +126,10 @@ mod tests {
122126 fn assert_consumed ( assert_counter : usize ) -> impl Middleware < ( ) > {
123127 move |mut ctx : Context < ( ) > , next : Next | async move {
124128 next. await ?;
125- ctx. resp_mut ( ) . wrapped ( move |stream| Consumer {
129+ let body = std:: mem:: take ( & mut ctx. resp_mut ( ) . body ) ;
130+ ctx. resp_mut ( ) . write_stream ( Consumer {
126131 counter : 0 ,
127- stream,
132+ stream : body ,
128133 assert_counter,
129134 } ) ;
130135 Ok ( ( ) )
0 commit comments