@@ -64,6 +64,18 @@ pub const TEXT_MIME_TYPES: [&str; 24] = [
6464    "application/wasm" , 
6565] ; 
6666
67+ /// List of encodings that can be handled given enabled features. 
68+ const  AVAILABLE_ENCODINGS :  & [ ContentCoding ]  = & [ 
69+     #[ cfg( any( feature = "compression" ,  feature = "compression-deflate" ) ) ]  
70+     ContentCoding :: DEFLATE , 
71+     #[ cfg( any( feature = "compression" ,  feature = "compression-gzip" ) ) ]  
72+     ContentCoding :: GZIP , 
73+     #[ cfg( any( feature = "compression" ,  feature = "compression-brotli" ) ) ]  
74+     ContentCoding :: BROTLI , 
75+     #[ cfg( any( feature = "compression" ,  feature = "compression-zstd" ) ) ]  
76+     ContentCoding :: ZSTD , 
77+ ] ; 
78+ 
6779/// Create a wrapping handler that compresses the Body of a [`hyper::Response`] 
6880/// using gzip, `deflate`, `brotli` or `zstd` if is specified in the `Accept-Encoding` header, adding 
6981/// `content-encoding: <coding>` to the Response's [`HeaderMap`]. 
@@ -226,45 +238,12 @@ pub fn create_encoding_header(existing: Option<HeaderValue>, coding: ContentCodi
226238#[ inline( always) ]  
227239pub  fn  get_preferred_encoding ( headers :  & HeaderMap < HeaderValue > )  -> Option < ContentCoding >  { 
228240    if  let  Some ( ref  accept_encoding)  = headers. typed_get :: < AcceptEncoding > ( )  { 
229-         tracing:: trace!( "request with accept-ecoding header: {:?}" ,  accept_encoding) ; 
230- 
231-         let  encoding = accept_encoding. preferred_encoding ( ) ; 
232-         if  let  Some ( preferred_enc)  = encoding { 
233-             let  mut  feature_formats = Vec :: < ContentCoding > :: with_capacity ( 5 ) ; 
234-             if  cfg ! ( feature = "compression-deflate" )  { 
235-                 feature_formats. push ( ContentCoding :: DEFLATE ) ; 
236-             } 
237-             if  cfg ! ( feature = "compression-gzip" )  { 
238-                 feature_formats. push ( ContentCoding :: GZIP ) ; 
239-             } 
240-             if  cfg ! ( feature = "compression-brotli" )  { 
241-                 feature_formats. push ( ContentCoding :: BROTLI ) ; 
242-             } 
243-             if  cfg ! ( feature = "compression-zstd" )  { 
244-                 feature_formats. push ( ContentCoding :: ZSTD ) ; 
245-             } 
241+         tracing:: trace!( "request with accept-encoding header: {:?}" ,  accept_encoding) ; 
246242
247-             // If there is only one Cargo compression feature enabled 
248-             // then re-evaluate the preferred encoding and return 
249-             // that feature compression algorithm as `ContentCoding` only 
250-             // if was contained in the `AcceptEncoding` value. 
251-             if  feature_formats. len ( )  == 1  { 
252-                 let  feature_enc = * feature_formats. first ( ) . unwrap ( ) ; 
253-                 if  feature_enc != preferred_enc
254-                     && accept_encoding
255-                         . sorted_encodings ( ) 
256-                         . any ( |enc| enc == feature_enc) 
257-                 { 
258-                     tracing:: trace!( 
259-                         "preferred encoding {:?} is re-evalated to {:?} because is the only compression feature enabled that matches the `accept-encoding` header" , 
260-                         preferred_enc, 
261-                         feature_enc
262-                     ) ; 
263-                     return  Some ( feature_enc) ; 
264-                 } 
243+         for  encoding in  accept_encoding. sorted_encodings ( )  { 
244+             if  AVAILABLE_ENCODINGS . contains ( & encoding)  { 
245+                 return  Some ( encoding) ; 
265246            } 
266- 
267-             return  encoding; 
268247        } 
269248    } 
270249    None 
0 commit comments