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

Added CORS Mode property to Request on web #52

Merged
merged 9 commits into from
Feb 13, 2024
Merged
3 changes: 3 additions & 0 deletions ehttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub async fn fetch_async(request: Request) -> Result<Response> {
mod types;
pub use types::{Error, Headers, PartialResponse, Request, Response, Result};

#[cfg(target_arch = "wasm32")]
pub use types::Mode;

#[cfg(not(target_arch = "wasm32"))]
mod native;
#[cfg(not(target_arch = "wasm32"))]
Expand Down
47 changes: 47 additions & 0 deletions ehttp/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ impl<'h> IntoIterator for &'h Headers {

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

/// Determine if cross-origin requests lead to valid responses.
/// Based on <https://developer.mozilla.org/en-US/docs/Web/API/Request/mode>
#[cfg(target_arch = "wasm32")]
#[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,

/// 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
NoCors = 1,

/// Includes an Origin header in the request and expects the server to respond with an
/// "Access-Control-Allow-Origin" header that indicates whether the request is allowed.
#[default]
Cors = 2,

/// A mode for supporting navigation
Navigate = 3,
}

#[cfg(target_arch = "wasm32")]
impl From<Mode> for web_sys::RequestMode {
fn from(mode: Mode) -> Self {
match mode {
Mode::SameOrigin => web_sys::RequestMode::SameOrigin,
Mode::NoCors => web_sys::RequestMode::NoCors,
Mode::Cors => web_sys::RequestMode::Cors,
Mode::Navigate => web_sys::RequestMode::Navigate,
}
}
}

/// A simple HTTP request.
#[derive(Clone, Debug)]
pub struct Request {
Expand All @@ -107,6 +140,10 @@ pub struct Request {

/// ("Accept", "*/*"), …
pub headers: Headers,

/// Request mode used on fetch. Only available on wasm builds
#[cfg(target_arch = "wasm32")]
pub mode: Mode,
}

impl Request {
Expand All @@ -118,6 +155,8 @@ impl Request {
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
}
}

Expand All @@ -129,6 +168,8 @@ impl Request {
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
}
}

Expand All @@ -143,6 +184,8 @@ impl Request {
("Accept", "*/*"),
("Content-Type", "text/plain; charset=utf-8"),
]),
#[cfg(target_arch = "wasm32")]
mode: Mode::default(),
}
}

Expand Down Expand Up @@ -176,6 +219,8 @@ 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(),
}
}

Expand All @@ -191,6 +236,8 @@ 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(),
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion ehttp/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn string_from_fetch_error(value: JsValue) -> String {
pub(crate) async fn fetch_base(request: &Request) -> Result<web_sys::Response, JsValue> {
let mut opts = web_sys::RequestInit::new();
opts.method(&request.method);
opts.mode(web_sys::RequestMode::Cors);
opts.mode(request.mode.into());

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