Download packages/tauri-plugins/plugins/http/src/lib.rs from echodict/readest: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/datasets/echodict/readest/resolve/main/packages/tauri-plugins/plugins/http/src/lib.rs
- Command line
-
hf download hf://datasets/echodict/readest/packages/tauri-plugins/plugins/http/src/lib.rs
-
curl -L -o lib.rs https://huggingface.co/datasets/echodict/readest/resolve/main/packages/tauri-plugins/plugins/http/src/lib.rs
2.74 kB
| // Copyright 2019-2023 Tauri Programme within The Commons Conservancy | |
| // SPDX-License-Identifier: Apache-2.0 | |
| // SPDX-License-Identifier: MIT | |
| //! Access the HTTP client written in Rust. | |
| pub use reqwest; | |
| use tauri::{ | |
| plugin::{Builder, TauriPlugin}, | |
| Manager, Runtime, | |
| }; | |
| pub use error::{Error, Result}; | |
| mod commands; | |
| mod error; | |
| mod reqwest_cookie_store; | |
| mod scope; | |
| const COOKIES_FILENAME: &str = ".cookies"; | |
| pub(crate) struct Http { | |
| cookies_jar: std::sync::Arc<crate::reqwest_cookie_store::CookieStoreMutex>, | |
| } | |
| pub fn init<R: Runtime>() -> TauriPlugin<R> { | |
| Builder::<R>::new("http") | |
| .setup(|app, _| { | |
| let cookies_jar = { | |
| use crate::reqwest_cookie_store::*; | |
| use std::fs::File; | |
| use std::io::BufReader; | |
| let cache_dir = app.path().app_cache_dir()?; | |
| std::fs::create_dir_all(&cache_dir)?; | |
| let path = cache_dir.join(COOKIES_FILENAME); | |
| let file = File::options() | |
| .create(true) | |
| .append(true) | |
| .read(true) | |
| .open(&path)?; | |
| let reader = BufReader::new(file); | |
| CookieStoreMutex::load(path.clone(), reader).unwrap_or_else(|_e| { | |
| tracing::warn!( | |
| "failed to load cookie store: {_e}, falling back to empty store" | |
| ); | |
| CookieStoreMutex::new(path, Default::default()) | |
| }) | |
| }; | |
| let state = Http { | |
| cookies_jar: std::sync::Arc::new(cookies_jar), | |
| }; | |
| app.manage(state); | |
| Ok(()) | |
| }) | |
| .on_event(|app, event| { | |
| if let tauri::RunEvent::Exit = event { | |
| let state = app.state::<Http>(); | |
| match state.cookies_jar.request_save() { | |
| Ok(rx) => { | |
| let _ = rx.recv(); | |
| } | |
| Err(_e) => { | |
| tracing::error!("failed to save cookie jar: {_e}"); | |
| } | |
| } | |
| } | |
| }) | |
| .invoke_handler(tauri::generate_handler![ | |
| commands::fetch, | |
| commands::fetch_cancel, | |
| commands::fetch_send, | |
| commands::fetch_read_body, | |
| commands::fetch_cancel_body, | |
| ]) | |
| .build() | |
| } | |