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

Add Request::credentials for web #62

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
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
51 changes: 51 additions & 0 deletions ehttp/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
#[derive(Default, Clone, Copy, Debug)]
pub enum Mode {
/// If a request is made to another origin with this mode set, the result is an error.
SameOrigin = 0,

Check failure on line 102 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo test

variants `SameOrigin`, `NoCors`, and `Navigate` are never constructed

Check failure on line 102 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo check --all-features

variants `SameOrigin`, `NoCors`, and `Navigate` are never constructed

Check failure on line 102 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

variants `SameOrigin`, `NoCors`, and `Navigate` are never constructed

/// The request will not include the Origin header in a request.
/// The server's response will be opaque, meaning that JavaScript code cannot access its contents
Expand All @@ -126,6 +126,35 @@
}
}

// ----------------------------------------------------------------------------

/// 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,

Check failure on line 141 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo test

variants `SameOrigin` and `Include` are never constructed

Check failure on line 141 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo check --all-features

variants `SameOrigin` and `Include` are never constructed

Check failure on line 141 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

variants `SameOrigin` and `Include` are never constructed

/// 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 @@ -144,7 +173,14 @@
/// Request mode used on fetch.
///
/// Used on Web to control CORS.
#[cfg(target_arch = "wasm32")]
pub mode: Mode,

/// Credential options for fetch.
jheidecker marked this conversation as resolved.
Show resolved Hide resolved
///
/// Only applies to the web backend.
#[cfg(target_arch = "wasm32")]
pub credentials: Credentials,
}

impl Request {
Expand All @@ -156,7 +192,10 @@
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
#[cfg(target_arch = "wasm32")]
credentials: Credentials::default(),
}
}

Expand All @@ -168,7 +207,10 @@
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
#[cfg(target_arch = "wasm32")]
credentials: Credentials::default(),
}
}

Expand All @@ -183,7 +225,10 @@
("Accept", "*/*"),
("Content-Type", "text/plain; charset=utf-8"),
]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
#[cfg(target_arch = "wasm32")]
credentials: Credentials::default(),
}
}

Expand Down Expand Up @@ -217,7 +262,10 @@
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(),
}
}

Expand All @@ -233,7 +281,10 @@
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(),
})
}
}
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
Loading