From eb7113cce01f41510a316bc364eb6b8d9277cdba Mon Sep 17 00:00:00 2001 From: Jay Heidecker Date: Thu, 29 Aug 2024 12:35:39 -0600 Subject: [PATCH 1/4] credentials_patch --- ehttp/Cargo.toml | 1 + ehttp/src/lib.rs | 2 ++ ehttp/src/types.rs | 35 +++++++++++++++++++++++++++++++++++ ehttp/src/web.rs | 1 + 4 files changed, 39 insertions(+) diff --git a/ehttp/Cargo.toml b/ehttp/Cargo.toml index 38caf46..f150f72 100644 --- a/ehttp/Cargo.toml +++ b/ehttp/Cargo.toml @@ -74,6 +74,7 @@ web-sys = { version = "0.3.52", features = [ "Request", "RequestInit", "RequestMode", + "RequestCredentials", "Response", "Window", ] } diff --git a/ehttp/src/lib.rs b/ehttp/src/lib.rs index 550b96e..6e3d62d 100644 --- a/ehttp/src/lib.rs +++ b/ehttp/src/lib.rs @@ -77,6 +77,8 @@ pub use types::{Error, Headers, PartialResponse, Request, Response, Result}; #[cfg(target_arch = "wasm32")] pub use types::Mode; +#[cfg(target_arch = "wasm32")] +pub use types::Credentials; #[cfg(not(target_arch = "wasm32"))] mod native; diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index 86b4557..d8ab355 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -126,6 +126,35 @@ impl From for web_sys::RequestMode { } } +// ---------------------------------------------------------------------------- + +/// Determines whether or not the browser sends credentials with the request, as well as whether any Set-Cookie response headers are respected. +/// +/// Based on +#[derive(Default, Clone, Copy, Debug)] +pub enum Credentials { + /// Never send credentials in the request or include credentials in the response. + #[default] + Omit = 0, + + /// Only send and include credentials for same-origin requests. + SameOrigin = 1, + + /// Always include credentials, even for cross-origin requests. + Include = 2, +} + +#[cfg(target_arch = "wasm32")] +impl From for web_sys::RequestCredentials { + fn from(credentials: Credentials) -> Self { + match credentials { + Credentials::Omit => web_sys::RequestCredentials::Omit, + Credentials::SameOrigin => web_sys::RequestCredentials::SameOrigin, + Credentials::Include => web_sys::RequestCredentials::Include, + } + } +} + /// A simple HTTP request. #[derive(Clone, Debug)] pub struct Request { @@ -145,6 +174,9 @@ pub struct Request { /// /// Used on Web to control CORS. pub mode: Mode, + + /// Credential options for fetch. + pub credentials: Credentials, } impl Request { @@ -157,6 +189,7 @@ impl Request { body: vec![], headers: Headers::new(&[("Accept", "*/*")]), mode: Mode::default(), + credentials: Credentials::default(), } } @@ -169,6 +202,7 @@ impl Request { body: vec![], headers: Headers::new(&[("Accept", "*/*")]), mode: Mode::default(), + credentials: Credentials::default(), } } @@ -184,6 +218,7 @@ impl Request { ("Content-Type", "text/plain; charset=utf-8"), ]), mode: Mode::default(), + credentials: Credentials::default(), } } diff --git a/ehttp/src/web.rs b/ehttp/src/web.rs index cb7d4c3..00ea3cf 100644 --- a/ehttp/src/web.rs +++ b/ehttp/src/web.rs @@ -43,6 +43,7 @@ pub(crate) async fn fetch_base(request: &Request) -> Result Date: Fri, 30 Aug 2024 09:10:20 -0600 Subject: [PATCH 2/4] Update ehttp/src/types.rs Update doc to indicate credentials only apply to web backend. Co-authored-by: Emil Ernerfeldt --- ehttp/src/types.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index d8ab355..61a9cc7 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -176,6 +176,8 @@ pub struct Request { pub mode: Mode, /// Credential options for fetch. + /// + /// Only applies to the web backend. pub credentials: Credentials, } From 61cc01b9d277fffdbbae2aadcb1e4e9553fa9ae1 Mon Sep 17 00:00:00 2001 From: Jay Heidecker Date: Fri, 30 Aug 2024 09:55:48 -0600 Subject: [PATCH 3/4] missed multipart and json --- ehttp/src/types.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index 61a9cc7..fdefc32 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -255,6 +255,7 @@ impl Request { body: data, headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]), mode: Mode::default(), + credentials: Credentials::default(), } } @@ -271,6 +272,7 @@ impl Request { body: serde_json::to_string(body)?.into_bytes(), headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]), mode: Mode::default(), + credentials: Credentials::default(), }) } } From 3394080225991402e9f0273dac733036d0e1fc18 Mon Sep 17 00:00:00 2001 From: Jay Heidecker Date: Fri, 30 Aug 2024 11:00:12 -0600 Subject: [PATCH 4/4] fixed conflict when using rust-analyzer --- ehttp/src/types.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index fdefc32..e9020d3 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -173,11 +173,13 @@ pub struct Request { /// Request mode used on fetch. /// /// Used on Web to control CORS. + #[cfg(target_arch = "wasm32")] pub mode: Mode, /// Credential options for fetch. /// /// Only applies to the web backend. + #[cfg(target_arch = "wasm32")] pub credentials: Credentials, } @@ -190,7 +192,9 @@ impl Request { url: url.to_string(), body: vec![], headers: Headers::new(&[("Accept", "*/*")]), + #[cfg(target_arch = "wasm32")] mode: Mode::default(), + #[cfg(target_arch = "wasm32")] credentials: Credentials::default(), } } @@ -203,7 +207,9 @@ impl Request { url: url.to_string(), body: vec![], headers: Headers::new(&[("Accept", "*/*")]), + #[cfg(target_arch = "wasm32")] mode: Mode::default(), + #[cfg(target_arch = "wasm32")] credentials: Credentials::default(), } } @@ -219,7 +225,9 @@ impl Request { ("Accept", "*/*"), ("Content-Type", "text/plain; charset=utf-8"), ]), + #[cfg(target_arch = "wasm32")] mode: Mode::default(), + #[cfg(target_arch = "wasm32")] credentials: Credentials::default(), } } @@ -254,7 +262,9 @@ impl Request { url: url.to_string(), body: data, headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]), + #[cfg(target_arch = "wasm32")] mode: Mode::default(), + #[cfg(target_arch = "wasm32")] credentials: Credentials::default(), } } @@ -271,7 +281,9 @@ impl Request { url: url.to_string(), body: serde_json::to_string(body)?.into_bytes(), headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]), + #[cfg(target_arch = "wasm32")] mode: Mode::default(), + #[cfg(target_arch = "wasm32")] credentials: Credentials::default(), }) }