diff --git a/.changes/http-cookies.md b/.changes/http-cookies.md new file mode 100644 index 000000000..57e493bf6 --- /dev/null +++ b/.changes/http-cookies.md @@ -0,0 +1,5 @@ +--- +"http": "patch" +--- + +Enable cookies store feature flag by default. diff --git a/.changes/http-origin.md b/.changes/http-origin.md new file mode 100644 index 000000000..8cc728986 --- /dev/null +++ b/.changes/http-origin.md @@ -0,0 +1,5 @@ +--- +"http": "patch" +--- + +Set the request origin to the current webview url. diff --git a/examples/api/src-tauri/capabilities/base.json b/examples/api/src-tauri/capabilities/base.json index e0d05d6f9..7ac9385ec 100644 --- a/examples/api/src-tauri/capabilities/base.json +++ b/examples/api/src-tauri/capabilities/base.json @@ -8,9 +8,9 @@ { "identifier": "http:default", "allow": [ - "https://tauri.app", + "https://*:*", { - "url": "http://localhost:3003" + "url": "http://*:*" } ] }, diff --git a/plugins/http/Cargo.toml b/plugins/http/Cargo.toml index 193ef4cae..9a97c9ae8 100644 --- a/plugins/http/Cargo.toml +++ b/plugins/http/Cargo.toml @@ -35,7 +35,7 @@ url = { workspace = true } data-url = "0.3" [features] -default = ["rustls-tls", "http2", "charset", "macos-system-configuration"] +default = ["rustls-tls", "http2", "charset", "macos-system-configuration", "cookies"] multipart = ["reqwest/multipart"] json = ["reqwest/json"] stream = ["reqwest/stream"] diff --git a/plugins/http/src/commands.rs b/plugins/http/src/commands.rs index e88608f60..a135cdab1 100644 --- a/plugins/http/src/commands.rs +++ b/plugins/http/src/commands.rs @@ -4,14 +4,14 @@ use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc, time::Duration}; -use http::{header, HeaderName, HeaderValue, Method, StatusCode}; +use http::{header, HeaderName, Method, StatusCode}; use reqwest::{redirect::Policy, NoProxy}; use serde::{Deserialize, Serialize}; use tauri::{ async_runtime::Mutex, command, ipc::{CommandScope, GlobalScope}, - AppHandle, Manager, ResourceId, Runtime, + AppHandle, Manager, ResourceId, Runtime, Webview, }; use crate::{ @@ -138,6 +138,7 @@ fn attach_proxy( #[command] pub async fn fetch( app: AppHandle, + webview: Webview, client_config: ClientConfig, command_scope: CommandScope, global_scope: GlobalScope, @@ -194,7 +195,6 @@ pub async fn fetch( for (name, value) in &headers { let name = HeaderName::from_bytes(name.as_bytes())?; - let value = HeaderValue::from_bytes(value.as_bytes())?; #[cfg(not(feature = "unsafe-headers"))] if matches!( name, @@ -228,22 +228,21 @@ pub async fn fetch( // POST and PUT requests should always have a 0 length content-length, // if there is no body. https://fetch.spec.whatwg.org/#http-network-or-cache-fetch if data.is_none() && matches!(method, Method::POST | Method::PUT) { - request = request.header(header::CONTENT_LENGTH, HeaderValue::from(0)); + request = request.header(header::CONTENT_LENGTH, 0); } if headers.contains_key(header::RANGE.as_str()) { // https://fetch.spec.whatwg.org/#http-network-or-cache-fetch step 18 // If httpRequest’s header list contains `Range`, then append (`Accept-Encoding`, `identity`) - request = request.header( - header::ACCEPT_ENCODING, - HeaderValue::from_static("identity"), - ); + request = request.header(header::ACCEPT_ENCODING, "identity"); } if !headers.contains_key(header::USER_AGENT.as_str()) { - request = request.header(header::USER_AGENT, HeaderValue::from_static("tauri")); + request = request.header(header::USER_AGENT, "tauri-plugin-http"); } + request = request.header(header::ORIGIN, webview.url().as_str()); + if let Some(data) = data { request = request.body(data); }