11use std:: collections:: HashMap ;
22use std:: net:: SocketAddr ;
3+ use std:: str:: from_utf8;
34
45use axum:: headers:: HeaderName ;
6+ use axum:: http:: header:: ACCEPT_ENCODING ;
57use axum:: http:: { HeaderMap , HeaderValue , StatusCode , Uri } ;
68use axum:: response:: { Html , IntoResponse , Response } ;
79use axum:: routing:: { get, get_service} ;
@@ -59,7 +61,8 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
5961}
6062
6163fn get_router ( options : & Options , output : WasmBindgenOutput ) -> Router {
62- let WasmBindgenOutput { js, compressed_wasm, snippets, local_modules } = output;
64+ let WasmBindgenOutput { js, compressed_wasm, gzip_compressed_wasm, snippets, local_modules } =
65+ output;
6366
6467 let middleware_stack = ServiceBuilder :: new ( )
6568 . layer ( CompressionLayer :: new ( ) )
@@ -87,7 +90,37 @@ fn get_router(options: &Options, output: WasmBindgenOutput) -> Router {
8790
8891 let serve_wasm = |headers : HeaderMap | async move {
8992 println ! ( " request headers: {:?}" , headers) ;
90- ( [ ( "content-encoding" , "br" ) ] , WithContentType ( "application/wasm" , compressed_wasm) )
93+ if let Some ( accept_encoding) = headers. get ( ACCEPT_ENCODING ) {
94+ match from_utf8 ( accept_encoding. as_bytes ( ) ) {
95+ Ok ( encodings) => {
96+ let split_encodings: Vec < & str > = encodings. split ( "," ) . map ( str:: trim) . collect ( ) ;
97+ println ! ( "split_encodings: {:?}" , split_encodings) ;
98+ if split_encodings. contains ( & "br" ) {
99+ println ! ( "serving br compressed wasm" ) ;
100+ Ok ( (
101+ [ ( "content-encoding" , "br" ) ] ,
102+ WithContentType ( "application/wasm" , compressed_wasm) ,
103+ ) )
104+ } else if split_encodings. contains ( & "gzip" ) {
105+ println ! ( "serving gzip compressed wasm" ) ;
106+ Ok ( (
107+ [ ( "content-encoding" , "gzip" ) ] ,
108+ WithContentType ( "application/wasm" , gzip_compressed_wasm) ,
109+ ) )
110+ } else {
111+ println ! ( "No support for requested encoding!" ) ;
112+ Err ( (
113+ StatusCode :: BAD_REQUEST ,
114+ format ! ( "Unsupported encoding(s): {:?}" , split_encodings) ,
115+ ) )
116+ }
117+ }
118+ Err ( err) => Err ( ( StatusCode :: BAD_REQUEST , err. to_string ( ) ) ) ,
119+ }
120+ } else {
121+ tracing:: error!( "Received request missing the accept-encoding header" ) ;
122+ Err ( ( StatusCode :: BAD_REQUEST , "Missing `accept-encoding` header" . to_string ( ) ) )
123+ }
91124 } ;
92125
93126 Router :: new ( )
@@ -217,7 +250,13 @@ mod tests {
217250 println ! ( "{:?}" , & router) ;
218251 let client = TestClient :: new ( router) ;
219252
220- // Test with br compression requested
253+ // Test without any supported compression
254+ let mut res =
255+ client. get ( "/api/wasm.wasm" ) . header ( "accept-encoding" , "deflate" ) . send ( ) . await ;
256+ assert_eq ! ( res. status( ) , StatusCode :: BAD_REQUEST ) ;
257+ let result = res. chunk ( ) . await . unwrap ( ) ;
258+ println ! ( "Bad Request result: {:?}" , result) ;
259+
221260 let mut res = client
222261 . get ( "/api/wasm.wasm" )
223262 . header ( "accept-encoding" , "gzip, deflate, br" )
@@ -227,17 +266,12 @@ mod tests {
227266 let result = res. chunk ( ) . await . unwrap ( ) ;
228267 assert_ne ! ( result[ 0 ..3 ] , Bytes :: from( vec![ 0x1f , 0x8b , 0x08 ] ) ) ;
229268
230- // Test without br compression
231- // let mut res = client
232- // .get("/api/wasm.wasm")
233- // .header("accept-encoding", "gzip, deflate")
234- // .send()
235- // .await;
236- // assert_eq!(res.status(), StatusCode::OK);
237- // let result = res.chunk().await.unwrap();
238- // // This is the gzip 3-byte file header
239- // assert_eq!(result[0..3], Bytes::from(vec![0x1f, 0x8b, 0x08]));
240-
241- //tokio_test::block_on(server).unwrap();
269+ // Test without br compression, defaulting to gzip
270+ let mut res =
271+ client. get ( "/api/wasm.wasm" ) . header ( "accept-encoding" , "gzip, deflate" ) . send ( ) . await ;
272+ assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
273+ let result = res. chunk ( ) . await . unwrap ( ) ;
274+ // This is the gzip 3-byte file header
275+ assert_eq ! ( result[ 0 ..3 ] , Bytes :: from( vec![ 0x1f , 0x8b , 0x08 ] ) ) ;
242276 }
243277}
0 commit comments