Skip to content

Commit

Permalink
credentials_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Heidecker committed Aug 29, 2024
1 parent 4e63d73 commit eb7113c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions ehttp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ web-sys = { version = "0.3.52", features = [
"Request",
"RequestInit",
"RequestMode",
"RequestCredentials",
"Response",
"Window",
] }
2 changes: 2 additions & 0 deletions ehttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions ehttp/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,35 @@ impl From<Mode> 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 <https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials>
#[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<Credentials> 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 {
Expand All @@ -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 {
Expand All @@ -157,6 +189,7 @@ impl Request {
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
mode: Mode::default(),
credentials: Credentials::default(),
}
}

Expand All @@ -169,6 +202,7 @@ impl Request {
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
mode: Mode::default(),
credentials: Credentials::default(),
}
}

Expand All @@ -184,6 +218,7 @@ impl Request {
("Content-Type", "text/plain; charset=utf-8"),
]),
mode: Mode::default(),
credentials: Credentials::default(),
}
}

Expand Down
1 change: 1 addition & 0 deletions ehttp/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub(crate) async fn fetch_base(request: &Request) -> Result<web_sys::Response, J
let mut opts = web_sys::RequestInit::new();
opts.method(&request.method);
opts.mode(request.mode.into());
opts.credentials(request.credentials.into());

if !request.body.is_empty() {
let body_bytes: &[u8] = &request.body;
Expand Down

0 comments on commit eb7113c

Please sign in to comment.