|
5 | 5 | //! The [`wry`] Tauri [`Runtime`]. |
6 | 6 |
|
7 | 7 | use tauri_runtime::{ |
| 8 | + http::{ |
| 9 | + Request as HttpRequest, RequestParts as HttpRequestParts, Response as HttpResponse, |
| 10 | + ResponseParts as HttpResponseParts, |
| 11 | + }, |
8 | 12 | menu::{CustomMenuItem, Menu, MenuEntry, MenuHash, MenuItem, MenuUpdate, Submenu}, |
9 | 13 | monitor::Monitor, |
10 | 14 | webview::{ |
@@ -54,6 +58,10 @@ use wry::{ |
54 | 58 | monitor::MonitorHandle, |
55 | 59 | window::{Fullscreen, Icon as WindowIcon, UserAttentionType as WryUserAttentionType}, |
56 | 60 | }, |
| 61 | + http::{ |
| 62 | + Request as WryHttpRequest, RequestParts as WryRequestParts, Response as WryHttpResponse, |
| 63 | + ResponseParts as WryResponseParts, |
| 64 | + }, |
57 | 65 | webview::{ |
58 | 66 | FileDropEvent as WryFileDropEvent, RpcRequest as WryRpcRequest, RpcResponse, WebContext, |
59 | 67 | WebView, WebViewBuilder, |
@@ -95,9 +103,6 @@ mod system_tray; |
95 | 103 | #[cfg(feature = "system-tray")] |
96 | 104 | use system_tray::*; |
97 | 105 |
|
98 | | -mod mime_type; |
99 | | -use mime_type::MimeType; |
100 | | - |
101 | 106 | type WebContextStore = Mutex<HashMap<Option<PathBuf>, WebContext>>; |
102 | 107 | // window |
103 | 108 | type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>; |
@@ -152,6 +157,72 @@ struct EventLoopContext { |
152 | 157 | proxy: EventLoopProxy<Message>, |
153 | 158 | } |
154 | 159 |
|
| 160 | +struct HttpRequestPartsWrapper(HttpRequestParts); |
| 161 | + |
| 162 | +impl From<HttpRequestPartsWrapper> for HttpRequestParts { |
| 163 | + fn from(parts: HttpRequestPartsWrapper) -> Self { |
| 164 | + Self { |
| 165 | + method: parts.0.method, |
| 166 | + uri: parts.0.uri, |
| 167 | + headers: parts.0.headers, |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +impl From<HttpRequestParts> for HttpRequestPartsWrapper { |
| 173 | + fn from(request: HttpRequestParts) -> Self { |
| 174 | + Self(HttpRequestParts { |
| 175 | + method: request.method, |
| 176 | + uri: request.uri, |
| 177 | + headers: request.headers, |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +impl From<WryRequestParts> for HttpRequestPartsWrapper { |
| 183 | + fn from(request: WryRequestParts) -> Self { |
| 184 | + Self(HttpRequestParts { |
| 185 | + method: request.method, |
| 186 | + uri: request.uri, |
| 187 | + headers: request.headers, |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +struct HttpRequestWrapper(HttpRequest); |
| 193 | + |
| 194 | +impl From<&WryHttpRequest> for HttpRequestWrapper { |
| 195 | + fn from(req: &WryHttpRequest) -> Self { |
| 196 | + Self(HttpRequest { |
| 197 | + body: req.body.clone(), |
| 198 | + head: HttpRequestPartsWrapper::from(req.head.clone()).0, |
| 199 | + }) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +// response |
| 204 | +struct HttpResponsePartsWrapper(WryResponseParts); |
| 205 | +impl From<HttpResponseParts> for HttpResponsePartsWrapper { |
| 206 | + fn from(response: HttpResponseParts) -> Self { |
| 207 | + Self(WryResponseParts { |
| 208 | + mimetype: response.mimetype, |
| 209 | + status: response.status, |
| 210 | + version: response.version, |
| 211 | + headers: response.headers, |
| 212 | + }) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +struct HttpResponseWrapper(WryHttpResponse); |
| 217 | +impl From<HttpResponse> for HttpResponseWrapper { |
| 218 | + fn from(response: HttpResponse) -> Self { |
| 219 | + Self(WryHttpResponse { |
| 220 | + body: response.body, |
| 221 | + head: HttpResponsePartsWrapper::from(response.head).0, |
| 222 | + }) |
| 223 | + } |
| 224 | +} |
| 225 | + |
155 | 226 | pub struct MenuItemAttributesWrapper<'a>(pub WryMenuItemAttributes<'a>); |
156 | 227 |
|
157 | 228 | impl<'a> From<&'a CustomMenuItem> for MenuItemAttributesWrapper<'a> { |
@@ -2327,6 +2398,7 @@ fn create_webview( |
2327 | 2398 | #[allow(unused_mut)] |
2328 | 2399 | let PendingWindow { |
2329 | 2400 | webview_attributes, |
| 2401 | + uri_scheme_protocols, |
2330 | 2402 | mut window_builder, |
2331 | 2403 | rpc_handler, |
2332 | 2404 | file_drop_handler, |
@@ -2375,13 +2447,10 @@ fn create_webview( |
2375 | 2447 | handler, |
2376 | 2448 | )); |
2377 | 2449 | } |
2378 | | - for (scheme, protocol) in webview_attributes.uri_scheme_protocols { |
2379 | | - webview_builder = webview_builder.with_custom_protocol(scheme, move |url| { |
2380 | | - protocol(url) |
2381 | | - .map(|data| { |
2382 | | - let mime_type = MimeType::parse(&data, url); |
2383 | | - (data, mime_type) |
2384 | | - }) |
| 2450 | + for (scheme, protocol) in uri_scheme_protocols { |
| 2451 | + webview_builder = webview_builder.with_custom_protocol(scheme, move |wry_request| { |
| 2452 | + protocol(&HttpRequestWrapper::from(wry_request).0) |
| 2453 | + .map(|tauri_response| HttpResponseWrapper::from(tauri_response).0) |
2385 | 2454 | .map_err(|_| wry::Error::InitScriptError) |
2386 | 2455 | }); |
2387 | 2456 | } |
@@ -2409,7 +2478,7 @@ fn create_webview( |
2409 | 2478 | .build() |
2410 | 2479 | .map_err(|e| Error::CreateWebview(Box::new(e)))? |
2411 | 2480 | } else { |
2412 | | - let mut context = WebContext::new(webview_attributes.data_directory.clone()); |
| 2481 | + let mut context = WebContext::new(webview_attributes.data_directory); |
2413 | 2482 | webview_builder |
2414 | 2483 | .with_web_context(&mut context) |
2415 | 2484 | .build() |
|
0 commit comments