Skip to content

Commit

Permalink
Add redirect::Policy::none to wasm builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ostenbom committed Apr 26, 2024
1 parent 81c0399 commit 16a7d97
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ features = [
"ServiceWorkerGlobalScope",
"RequestCredentials",
"File",
"ReadableStream"
"ReadableStream",
"RequestRedirect"
]

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ if_wasm! {
mod wasm;
mod util;

pub use self::wasm::{Body, Client, ClientBuilder, Request, RequestBuilder, Response};
pub use self::wasm::{Body, Client, ClientBuilder, Request, RequestBuilder, Response, redirect};
#[cfg(feature = "multipart")]
pub use self::wasm::multipart;
}
17 changes: 15 additions & 2 deletions src/wasm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use url::Url;
use wasm_bindgen::prelude::{wasm_bindgen, UnwrapThrowExt as _};

use super::{AbortGuard, Request, RequestBuilder, Response};
use crate::wasm::redirect;
use crate::IntoUrl;

#[wasm_bindgen]
Expand Down Expand Up @@ -156,7 +157,7 @@ impl Client {
mut req: Request,
) -> impl Future<Output = crate::Result<Response>> {
self.merge_headers(&mut req);
fetch(req)
fetch(req, self.config.redirect_policy.clone())
}
}

Expand All @@ -182,7 +183,7 @@ impl fmt::Debug for ClientBuilder {
}
}

async fn fetch(req: Request) -> crate::Result<Response> {
async fn fetch(req: Request, redirect_policy: redirect::Policy) -> crate::Result<Response> {
// Build the js Request
let mut init = web_sys::RequestInit::new();
init.method(req.method().as_str());
Expand Down Expand Up @@ -221,6 +222,10 @@ async fn fetch(req: Request) -> crate::Result<Response> {
let abort = AbortGuard::new()?;
init.signal(Some(&abort.signal()));

if redirect_policy == redirect::Policy::none() {
init.redirect(web_sys::RequestRedirect::Manual);
}

let js_req = web_sys::Request::new_with_str_and_init(req.url().as_str(), &init)
.map_err(crate::error::wasm)
.map_err(crate::error::builder)?;
Expand Down Expand Up @@ -295,6 +300,12 @@ impl ClientBuilder {
self
}

/// Set a redirect `Policy` for this client.
pub fn redirect(mut self, policy: redirect::Policy) -> ClientBuilder {
self.config.redirect_policy = policy;
self
}

/// Sets the default headers for every request
pub fn default_headers(mut self, headers: HeaderMap) -> ClientBuilder {
for (key, value) in headers.iter() {
Expand All @@ -314,13 +325,15 @@ impl Default for ClientBuilder {
struct Config {
headers: HeaderMap,
error: Option<crate::Error>,
redirect_policy: redirect::Policy,
}

impl Default for Config {
fn default() -> Config {
Config {
headers: HeaderMap::new(),
error: None,
redirect_policy: redirect::Policy::default(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod client;
/// TODO
#[cfg(feature = "multipart")]
pub mod multipart;
pub mod redirect;
mod request;
mod response;

Expand Down
41 changes: 41 additions & 0 deletions src/wasm/redirect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! Redirect Handling
//!
//! By default, a `Client` will automatically follow HTTP redirects. To customize this behavior, a
//! `redirect::Policy` can be used with a `ClientBuilder`.

/// A type that controls the policy on how to handle the following of redirects.
///
/// The default value follow redirects https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect
///
/// - `none` can be used to disable all redirect behavior. This sets the redirect value to "manual".
#[derive(Debug, PartialEq, Clone)]
pub struct Policy {
inner: PolicyKind,
}

impl Policy {
/// Create a `Policy` that does not follow any redirect.
pub fn none() -> Self {
Self {
inner: PolicyKind::None,
}
}

fn not_set() -> Self {
Self {
inner: PolicyKind::NotSet,
}
}
}

#[derive(Debug, PartialEq, Clone)]
enum PolicyKind {
None,
NotSet,
}

impl Default for Policy {
fn default() -> Policy {
Policy::not_set()
}
}

0 comments on commit 16a7d97

Please sign in to comment.