Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): Enable cookie store #1006

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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 @@ -30,6 +30,7 @@ http = "0.2"
reqwest = { version = "0.11", default-features = false }
url = { workspace = true }
data-url = "0.3"
tauri-plugin-store = { path = "../store", version = "2.0.0-beta.1", optional = true }

[features]
multipart = [ "reqwest/multipart" ]
Expand All @@ -44,7 +45,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", "tauri-plugin-store" ]
gzip = [ "reqwest/gzip" ]
brotli = [ "reqwest/brotli" ]
deflate = [ "reqwest/deflate" ]
Expand Down
10 changes: 6 additions & 4 deletions plugins/http/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use tauri::{
AppHandle, Manager, ResourceId, Runtime,
};

use crate::{
scope::{Entry, Scope},
Error, Result,
};
use crate::{scope::{Entry, Scope}, Error, Result, HttpExt};

struct ReqwestResponse(reqwest::Response);

Expand Down Expand Up @@ -190,6 +187,11 @@ pub async fn fetch<R: Runtime>(
builder = attach_proxy(proxy_config, builder)?;
}

#[cfg(feature = "cookies")]
{
builder = builder.cookie_provider(app.http().jar.clone());
}

let mut request = builder.build()?.request(method.clone(), url);

for (name, value) in &headers {
Expand Down
16 changes: 15 additions & 1 deletion plugins/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
//!
//! Access the HTTP client written in Rust.

#[cfg(feature = "cookies")]
use std::sync::Arc;
#[cfg(feature = "cookies")]
use tauri_plugin_store::StoreBuilder;

pub use reqwest;
use tauri::{
plugin::{Builder, TauriPlugin},
Expand All @@ -21,6 +26,8 @@ mod scope;
struct Http<R: Runtime> {
#[allow(dead_code)]
app: AppHandle<R>,
#[cfg(feature = "cookies")]
jar: Arc<reqwest::cookie::Jar>
}

trait HttpExt<R: Runtime> {
Expand All @@ -43,7 +50,14 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
commands::fetch_read_body,
])
.setup(|app, _api| {
app.manage(Http { app: app.clone() });
#[cfg(feature = "cookies")]
let mut store = StoreBuilder::new("http/cookies.bin".parse()?).build(app);

app.manage(Http {
app: app.clone(),
#[cfg(feature = "cookies")]
jar: Arc::new(reqwest::cookie::Jar::default()),
});
Ok(())
})
.build()
Expand Down