@@ -142,11 +142,6 @@ fn set_csp<R: Runtime>(
142142 Csp :: DirectiveMap ( csp) . to_string ( )
143143}
144144
145- #[ cfg( target_os = "linux" ) ]
146- fn set_html_csp ( html : & str , csp : & str ) -> String {
147- html. replacen ( tauri_utils:: html:: CSP_TOKEN , csp, 1 )
148- }
149-
150145// inspired by https://github.com/rust-lang/rust/blob/1be5c8f90912c446ecbdc405cbc4a89f9acd20fd/library/alloc/src/str.rs#L260-L297
151146fn replace_with_callback < F : FnMut ( ) -> String > (
152147 original : & str ,
@@ -374,7 +369,13 @@ impl<R: Runtime> WindowManager<R> {
374369 /// Get the origin as it will be seen in the webview.
375370 fn get_browser_origin ( & self ) -> String {
376371 match self . base_path ( ) {
377- AppUrl :: Url ( WindowUrl :: External ( url) ) => url. origin ( ) . ascii_serialization ( ) ,
372+ AppUrl :: Url ( WindowUrl :: External ( url) ) => {
373+ if cfg ! ( dev) && !cfg ! ( target_os = "linux" ) {
374+ format_real_schema ( "tauri" )
375+ } else {
376+ url. origin ( ) . ascii_serialization ( )
377+ }
378+ }
378379 _ => format_real_schema ( "tauri" ) ,
379380 }
380381 }
@@ -820,8 +821,12 @@ impl<R: Runtime> WindowManager<R> {
820821 > ,
821822 ) -> Box < dyn Fn ( & HttpRequest ) -> Result < HttpResponse , Box < dyn std:: error:: Error > > + Send + Sync >
822823 {
824+ #[ cfg( dev) ]
825+ let url = self . get_url ( ) . into_owned ( ) ;
826+ #[ cfg( not( dev) ) ]
823827 let manager = self . clone ( ) ;
824828 let window_origin = window_origin. to_string ( ) ;
829+
825830 Box :: new ( move |request| {
826831 let path = request
827832 . uri ( )
@@ -834,32 +839,47 @@ impl<R: Runtime> WindowManager<R> {
834839 // the `strip_prefix` only returns None when a request is made to `https://tauri.$P` on Windows
835840 // where `$P` is not `localhost/*`
836841 . unwrap_or_else ( || "" . to_string ( ) ) ;
837- let asset = manager. get_asset ( path) ?;
838- let mut builder = HttpResponseBuilder :: new ( )
839- . header ( "Access-Control-Allow-Origin" , & window_origin)
840- . mimetype ( & asset. mime_type ) ;
841- if let Some ( csp) = & asset. csp_header {
842- builder = builder. header ( "Content-Security-Policy" , csp) ;
843- }
844- let mut response = builder. body ( asset. bytes ) ?;
845- if let Some ( handler) = & web_resource_request_handler {
846- handler ( request, & mut response) ;
847842
848- // if it's an HTML file, we need to set the CSP meta tag on Linux
849- #[ cfg( target_os = "linux" ) ]
850- if let Some ( response_csp) = response. headers ( ) . get ( "Content-Security-Policy" ) {
851- let response_csp = String :: from_utf8_lossy ( response_csp. as_bytes ( ) ) ;
852- let body = set_html_csp ( & String :: from_utf8_lossy ( response. body ( ) ) , & response_csp) ;
853- * response. body_mut ( ) = body. as_bytes ( ) . to_vec ( ) ;
854- }
855- } else {
856- #[ cfg( target_os = "linux" ) ]
857- {
858- if let Some ( csp) = & asset. csp_header {
859- let body = set_html_csp ( & String :: from_utf8_lossy ( response. body ( ) ) , csp) ;
860- * response. body_mut ( ) = body. as_bytes ( ) . to_vec ( ) ;
843+ let mut builder =
844+ HttpResponseBuilder :: new ( ) . header ( "Access-Control-Allow-Origin" , & window_origin) ;
845+
846+ #[ cfg( dev) ]
847+ let mut response = {
848+ let mut url = url. clone ( ) ;
849+ url. set_path ( & path) ;
850+ match attohttpc:: get ( url. as_str ( ) ) . send ( ) {
851+ Ok ( r) => {
852+ for ( name, value) in r. headers ( ) {
853+ builder = builder. header ( name, value) ;
854+ }
855+ builder. status ( r. status ( ) ) . body ( r. bytes ( ) ?) ?
856+ }
857+ Err ( e) => {
858+ debug_eprintln ! ( "Failed to request {}: {}" , url. path( ) , e) ;
859+ return Err ( Box :: new ( e) ) ;
861860 }
862861 }
862+ } ;
863+
864+ #[ cfg( not( dev) ) ]
865+ let mut response = {
866+ let asset = manager. get_asset ( path) ?;
867+ builder = builder. mimetype ( & asset. mime_type ) ;
868+ if let Some ( csp) = & asset. csp_header {
869+ builder = builder. header ( "Content-Security-Policy" , csp) ;
870+ }
871+ builder. body ( asset. bytes ) ?
872+ } ;
873+ if let Some ( handler) = & web_resource_request_handler {
874+ handler ( request, & mut response) ;
875+ }
876+ // if it's an HTML file, we need to set the CSP meta tag on Linux
877+ #[ cfg( all( not( dev) , target_os = "linux" ) ) ]
878+ if let Some ( response_csp) = response. headers ( ) . get ( "Content-Security-Policy" ) {
879+ let response_csp = String :: from_utf8_lossy ( response_csp. as_bytes ( ) ) ;
880+ let html = String :: from_utf8_lossy ( response. body ( ) ) ;
881+ let body = html. replacen ( tauri_utils:: html:: CSP_TOKEN , & response_csp, 1 ) ;
882+ * response. body_mut ( ) = body. as_bytes ( ) . to_vec ( ) ;
863883 }
864884 Ok ( response)
865885 } )
@@ -1061,7 +1081,10 @@ impl<R: Runtime> WindowManager<R> {
10611081 #[ allow( unused_mut) ] // mut url only for the data-url parsing
10621082 let ( is_local, mut url) = match & pending. webview_attributes . url {
10631083 WindowUrl :: App ( path) => {
1084+ #[ cfg( target_os = "linux" ) ]
10641085 let url = self . get_url ( ) ;
1086+ #[ cfg( not( target_os = "linux" ) ) ]
1087+ let url: Cow < ' _ , Url > = Cow :: Owned ( Url :: parse ( "tauri://localhost" ) . unwrap ( ) ) ;
10651088 (
10661089 true ,
10671090 // ignore "index.html" just to simplify the url
@@ -1078,7 +1101,13 @@ impl<R: Runtime> WindowManager<R> {
10781101 }
10791102 WindowUrl :: External ( url) => {
10801103 let config_url = self . get_url ( ) ;
1081- ( config_url. make_relative ( url) . is_some ( ) , url. clone ( ) )
1104+ let is_local = config_url. make_relative ( url) . is_some ( ) ;
1105+ let mut url = url. clone ( ) ;
1106+ if is_local && !cfg ! ( target_os = "linux" ) {
1107+ url. set_scheme ( "tauri" ) . unwrap ( ) ;
1108+ url. set_host ( Some ( "localhost" ) ) . unwrap ( ) ;
1109+ }
1110+ ( is_local, url)
10821111 }
10831112 _ => unimplemented ! ( ) ,
10841113 } ;
0 commit comments