Make headers method on WASM client for compatibility with async_impl (#991)
The replace_headers method had to be moved since the async_impl module is not compiled while compiling to wasm. This caused the replace_headers method to be unavailable. fast_random had to be excluded from the wasm build to prevent dead code warnings in the wasm target.
This commit is contained in:
committed by
GitHub
parent
77d7e452e1
commit
83fa93ccaf
@@ -185,7 +185,7 @@ impl RequestBuilder {
|
|||||||
/// The headers will be merged in to any already set.
|
/// The headers will be merged in to any already set.
|
||||||
pub fn headers(mut self, headers: crate::header::HeaderMap) -> RequestBuilder {
|
pub fn headers(mut self, headers: crate::header::HeaderMap) -> RequestBuilder {
|
||||||
if let Ok(ref mut req) = self.request {
|
if let Ok(ref mut req) = self.request {
|
||||||
replace_headers(req.headers_mut(), headers);
|
crate::util::replace_headers(req.headers_mut(), headers);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@@ -467,33 +467,6 @@ fn fmt_request_fields<'a, 'b>(
|
|||||||
.field("headers", &req.headers)
|
.field("headers", &req.headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) {
|
|
||||||
// IntoIter of HeaderMap yields (Option<HeaderName>, HeaderValue).
|
|
||||||
// The first time a name is yielded, it will be Some(name), and if
|
|
||||||
// there are more values with the same name, the next yield will be
|
|
||||||
// None.
|
|
||||||
//
|
|
||||||
// TODO: a complex exercise would be to optimize this to only
|
|
||||||
// require 1 hash/lookup of the key, but doing something fancy
|
|
||||||
// with header::Entry...
|
|
||||||
|
|
||||||
let mut prev_name = None;
|
|
||||||
for (key, value) in src {
|
|
||||||
match key {
|
|
||||||
Some(key) => {
|
|
||||||
dst.insert(key.clone(), value);
|
|
||||||
prev_name = Some(key);
|
|
||||||
}
|
|
||||||
None => match prev_name {
|
|
||||||
Some(ref key) => {
|
|
||||||
dst.append(key.clone(), value);
|
|
||||||
}
|
|
||||||
None => unreachable!("HeaderMap::into_iter yielded None first"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Check the request URL for a "username:password" type authority, and if
|
/// Check the request URL for a "username:password" type authority, and if
|
||||||
/// found, remove it from the URL and return it.
|
/// found, remove it from the URL and return it.
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ impl RequestBuilder {
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn headers(mut self, headers: crate::header::HeaderMap) -> RequestBuilder {
|
pub fn headers(mut self, headers: crate::header::HeaderMap) -> RequestBuilder {
|
||||||
if let Ok(ref mut req) = self.request {
|
if let Ok(ref mut req) = self.request {
|
||||||
async_impl::request::replace_headers(req.headers_mut(), headers);
|
crate::util::replace_headers(req.headers_mut(), headers);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@@ -605,7 +605,7 @@ impl<T> TryFrom<HttpRequest<T>> for Request where T:Into<Body> {
|
|||||||
let url = Url::parse(&uri.to_string())
|
let url = Url::parse(&uri.to_string())
|
||||||
.map_err(crate::error::builder)?;
|
.map_err(crate::error::builder)?;
|
||||||
let mut inner = async_impl::Request::new(method, url);
|
let mut inner = async_impl::Request::new(method, url);
|
||||||
async_impl::request::replace_headers(inner.headers_mut(), headers);
|
crate::util::replace_headers(inner.headers_mut(), headers);
|
||||||
Ok(Request {
|
Ok(Request {
|
||||||
body: Some(body.into()),
|
body: Some(body.into()),
|
||||||
inner,
|
inner,
|
||||||
|
|||||||
@@ -306,6 +306,7 @@ if_hyper! {
|
|||||||
|
|
||||||
if_wasm! {
|
if_wasm! {
|
||||||
mod wasm;
|
mod wasm;
|
||||||
|
mod util;
|
||||||
|
|
||||||
pub use self::wasm::{multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response};
|
pub use self::wasm::{multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response};
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/util.rs
30
src/util.rs
@@ -1,4 +1,7 @@
|
|||||||
|
use crate::header::HeaderMap;
|
||||||
|
|
||||||
// xor-shift
|
// xor-shift
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub(crate) fn fast_random() -> u64 {
|
pub(crate) fn fast_random() -> u64 {
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::collections::hash_map::RandomState;
|
use std::collections::hash_map::RandomState;
|
||||||
@@ -33,3 +36,30 @@ pub(crate) fn fast_random() -> u64 {
|
|||||||
n.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
|
n.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) {
|
||||||
|
// IntoIter of HeaderMap yields (Option<HeaderName>, HeaderValue).
|
||||||
|
// The first time a name is yielded, it will be Some(name), and if
|
||||||
|
// there are more values with the same name, the next yield will be
|
||||||
|
// None.
|
||||||
|
//
|
||||||
|
// TODO: a complex exercise would be to optimize this to only
|
||||||
|
// require 1 hash/lookup of the key, but doing something fancy
|
||||||
|
// with header::Entry...
|
||||||
|
|
||||||
|
let mut prev_name = None;
|
||||||
|
for (key, value) in src {
|
||||||
|
match key {
|
||||||
|
Some(key) => {
|
||||||
|
dst.insert(key.clone(), value);
|
||||||
|
prev_name = Some(key);
|
||||||
|
}
|
||||||
|
None => match prev_name {
|
||||||
|
Some(ref key) => {
|
||||||
|
dst.append(key.clone(), value);
|
||||||
|
}
|
||||||
|
None => unreachable!("HeaderMap::into_iter yielded None first"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -224,6 +224,16 @@ impl RequestBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a set of Headers to the existing ones on this Request.
|
||||||
|
///
|
||||||
|
/// The headers will be merged in to any already set.
|
||||||
|
pub fn headers(mut self, headers: crate::header::HeaderMap) -> RequestBuilder {
|
||||||
|
if let Ok(ref mut req) = self.request {
|
||||||
|
crate::util::replace_headers(req.headers_mut(), headers);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Disable CORS on fetching the request.
|
/// Disable CORS on fetching the request.
|
||||||
///
|
///
|
||||||
/// # WASM
|
/// # WASM
|
||||||
|
|||||||
Reference in New Issue
Block a user