tauri_plugin_http/lib.rs
1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Access the HTTP client written in Rust.
6//!
7//! ## Cargo features
8//!
9//! ### Reqwest feature forwards
10//!
11//! These features forwards [`reqwest`](https://docs.rs/reqwest/0.12.28/reqwest/index.html) features:
12//!
13//! - **http2** *(enabled by default)*: Enables HTTP/2 support.
14//! - **native-tls**: Enables TLS functionality provided by native-tls.
15//! - **native-tls-vendored**: Enables the vendored feature of native-tls.
16//! - **native-tls-alpn**: Enables the alpn feature of native-tls.
17//! - **rustls-tls** *(enabled by default)*: Enables TLS functionality provided by rustls. Equivalent to
18//! rustls-tls-webpki-roots.
19//! - **rustls-tls-manual-roots**: Enables TLS functionality provided by rustls, without setting any root
20//! certificates. Roots have to be specified manually.
21//! - **rustls-tls-webpki-roots**: Enables TLS functionality provided by rustls, while using root certificates
22//! from the webpki-roots crate.
23//! - **rustls-tls-native-roots**: Enables TLS functionality provided by rustls, while using root certificates
24//! from the rustls-native-certs crate.
25//! - **blocking**: Provides the [blocking](https://docs.rs/reqwest/0.12.28/reqwest/blocking/index.html) client API.
26//! - **charset** *(enabled by default)*: Improved support for decoding text.
27//! - **cookies** *(enabled by default)*: Provides cookie session support.
28//! - **gzip**: Provides response body gzip decompression.
29//! - **brotli**: Provides response body brotli decompression.
30//! - **zstd**: Provides response body zstd decompression.
31//! - **deflate**: Provides response body deflate decompression.
32//! - **json**: Provides serialization and deserialization for JSON bodies.
33//! - **multipart**: Provides functionality for multipart forms.
34//! - **stream**: Adds support for futures::Stream.
35//! - **socks**: Provides SOCKS5 proxy support.
36//! - **trust-dns**: Enables a trust-dns/Hickory DNS async resolver instead of the default threadpool using
37//! getaddrinfo.
38//! - **macos-system-configuration** *(deprecated, use `system-proxy` instead)*: Use Windows and macOS system proxy settings automatically.
39//! - **system-proxy** *(enabled by default)*: Use Windows and macOS system proxy settings automatically.
40//!
41//! ### tauri-plugin-http features
42//!
43//! - **tracing**: Adds request, response, and cookie-store diagnostics through `tracing`.
44//! - **unsafe-headers**: Allows webview requests to send any headers.
45//! - **dangerous-settings**: Allows dangerous client settings such as accepting invalid certificates or hostnames.
46
47pub use reqwest;
48use tauri::{
49 plugin::{Builder, TauriPlugin},
50 Manager, Runtime,
51};
52
53pub use error::{Error, Result};
54
55mod commands;
56mod error;
57#[cfg(feature = "cookies")]
58mod reqwest_cookie_store;
59mod scope;
60
61#[cfg(feature = "cookies")]
62const COOKIES_FILENAME: &str = ".cookies";
63
64pub(crate) struct Http {
65 #[cfg(feature = "cookies")]
66 cookies_jar: std::sync::Arc<crate::reqwest_cookie_store::CookieStoreMutex>,
67}
68
69pub fn init<R: Runtime>() -> TauriPlugin<R> {
70 Builder::<R>::new("http")
71 .setup(|app, _| {
72 #[cfg(feature = "cookies")]
73 let cookies_jar = {
74 use crate::reqwest_cookie_store::*;
75 use std::fs::File;
76 use std::io::BufReader;
77
78 let cache_dir = app.path().app_cache_dir()?;
79 std::fs::create_dir_all(&cache_dir)?;
80
81 let path = cache_dir.join(COOKIES_FILENAME);
82 let file = File::options()
83 .create(true)
84 .append(true)
85 .read(true)
86 .open(&path)?;
87
88 let reader = BufReader::new(file);
89 CookieStoreMutex::load(path.clone(), reader).unwrap_or_else(|_e| {
90 #[cfg(feature = "tracing")]
91 tracing::warn!(
92 "failed to load cookie store: {_e}, falling back to empty store"
93 );
94 CookieStoreMutex::new(path, Default::default())
95 })
96 };
97
98 let state = Http {
99 #[cfg(feature = "cookies")]
100 cookies_jar: std::sync::Arc::new(cookies_jar),
101 };
102
103 app.manage(state);
104
105 Ok(())
106 })
107 .on_event(|app, event| {
108 #[cfg(feature = "cookies")]
109 if let tauri::RunEvent::Exit = event {
110 let state = app.state::<Http>();
111
112 match state.cookies_jar.request_save() {
113 Ok(rx) => {
114 let _ = rx.recv();
115 }
116 Err(_e) => {
117 #[cfg(feature = "tracing")]
118 tracing::error!("failed to save cookie jar: {_e}");
119 }
120 }
121 }
122 })
123 .invoke_handler(tauri::generate_handler![
124 commands::fetch,
125 commands::fetch_cancel,
126 commands::fetch_send,
127 commands::fetch_read_body,
128 commands::fetch_cancel_body,
129 ])
130 .build()
131}