Skip to content

Commit

Permalink
enhance(http): persist cookies on disk
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Oct 28, 2024
1 parent 4341d7f commit 7ef9a00
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changes/persist-cookies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"http": "patch"
---

Persist cookies to disk and load it on next app start.

13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugins/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ http = "1"
reqwest = { version = "0.12", default-features = false }
url = { workspace = true }
data-url = "0.3"
reqwest_cookie_store = { version = "0.8", optional = true }

[features]
default = [
Expand All @@ -61,7 +62,7 @@ rustls-tls-manual-roots = ["reqwest/rustls-tls-manual-roots"]
rustls-tls-webpki-roots = ["reqwest/rustls-tls-webpki-roots"]
rustls-tls-native-roots = ["reqwest/rustls-tls-native-roots"]
blocking = ["reqwest/blocking"]
cookies = ["reqwest/cookies"]
cookies = ["reqwest/cookies", "dep:reqwest_cookie_store"]
gzip = ["reqwest/gzip"]
brotli = ["reqwest/brotli"]
deflate = ["reqwest/deflate"]
Expand Down
46 changes: 44 additions & 2 deletions plugins/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,63 @@ mod scope;

pub(crate) struct Http {
#[cfg(feature = "cookies")]
cookies_jar: std::sync::Arc<reqwest::cookie::Jar>,
cookies_jar_path: std::path::PathBuf,
#[cfg(feature = "cookies")]
cookies_jar: std::sync::Arc<reqwest_cookie_store::CookieStoreMutex>,
}

pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::<R>::new("http")
.setup(|app, _| {
#[cfg(feature = "cookies")]
let (cookies_jar_path, cookies_jar) = {
use reqwest_cookie_store::*;
use std::fs::File;
use std::io::BufReader;
use std::sync::Arc;

let cache_dir = app.path().app_cache_dir()?;
std::fs::create_dir_all(&cache_dir)?;

let path = cache_dir.join("Cookies");
let file = File::options()
.create(true)
.write(true)
.read(true)
.open(&path)?;

let reader = BufReader::new(file);
let store = CookieStore::load_json(reader).map_err(|e| e.to_string())?;

(path, Arc::new(CookieStoreMutex::new(store)))
};

let state = Http {
#[cfg(feature = "cookies")]
cookies_jar: std::sync::Arc::new(reqwest::cookie::Jar::default()),
cookies_jar_path,
#[cfg(feature = "cookies")]
cookies_jar,
};

app.manage(state);

Ok(())
})
.on_event(|app, event| {
#[cfg(feature = "cookies")]
if let tauri::RunEvent::Exit = event {
use std::fs::File;
use std::io::BufWriter;

let state = app.state::<Http>();

if let Ok(file) = File::create(&state.cookies_jar_path) {
let store = state.cookies_jar.lock().expect("poisoned cookie jar mutex");
let mut writer = BufWriter::new(file);
let _ = store.save_json(&mut writer);
}
}
})
.invoke_handler(tauri::generate_handler![
commands::fetch,
commands::fetch_cancel,
Expand Down

0 comments on commit 7ef9a00

Please sign in to comment.