Skip to content
Merged
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
129 changes: 99 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ehttp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ serde_json = { version = "1.0", optional = true }
# For compiling natively:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# ureq = { version = "2.0", default-features = false, features = ["gzip", "tls_native_certs"] }
ureq = "2.0"
ureq = "3.1"
async-channel = { version = "2.0", optional = true }

# For compiling to web:
Expand Down
2 changes: 1 addition & 1 deletion ehttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub async fn fetch_async(request: Request) -> Result<Response> {
}

mod types;
pub use types::{Error, Headers, PartialResponse, Request, Response, Result};
pub use types::{Error, Headers, Method, PartialResponse, Request, Response, Result};

#[cfg(target_arch = "wasm32")]
pub use types::Mode;
Expand Down
62 changes: 30 additions & 32 deletions ehttp/src/native.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Request, Response};
use crate::{Method, Request, Response};

#[cfg(feature = "native-async")]
use async_channel::{Receiver, Sender};
Expand All @@ -24,45 +24,43 @@ use async_channel::{Receiver, Sender};
/// * A browser extension blocked the request (e.g. ad blocker)
/// * …
pub fn fetch_blocking(request: &Request) -> crate::Result<Response> {
let mut req = ureq::request(&request.method, &request.url);
let mut resp = request.fetch_raw_native(true)?;

if let Some(timeout) = request.timeout {
req = req.timeout(timeout);
}

for (k, v) in &request.headers {
req = req.set(k, v);
}

let resp = if request.body.is_empty() {
req.call()
} else {
req.send_bytes(&request.body)
};

let (ok, resp) = match resp {
Ok(resp) => (true, resp),
Err(ureq::Error::Status(_, resp)) => (false, resp), // Still read the body on e.g. 404
Err(ureq::Error::Transport(err)) => return Err(err.to_string()),
};

let url = resp.get_url().to_owned();
let status = resp.status();
let status_text = resp.status_text().to_owned();
let ok = resp.status().is_success();
use ureq::ResponseExt as _;
let url = resp.get_uri().to_string();
let status = resp.status().as_u16();
let status_text = resp
.status()
.canonical_reason()
.unwrap_or("ERROR")
.to_string();
let mut headers = crate::Headers::default();
for key in &resp.headers_names() {
if let Some(value) = resp.header(key) {
headers.insert(key, value.to_owned());
}
for (k, v) in resp.headers().iter() {
headers.insert(
k,
v.to_str()
.map_err(|e| format!("Failed to convert header value to string: {e}"))?,
);
}
headers.sort(); // It reads nicer, and matches web backend.

let mut reader = resp.into_reader();
let mut reader = resp.body_mut().as_reader();
let mut bytes = vec![];
use std::io::Read as _;
if let Err(err) = reader.read_to_end(&mut bytes) {
if request.method == "HEAD" && err.kind() == std::io::ErrorKind::UnexpectedEof {
// We don't really expect a body for HEAD requests, so this is fine.
if err.kind() == std::io::ErrorKind::Other && request.method == Method::HEAD {
match err.downcast::<ureq::Error>() {
Ok(ureq::Error::Decompress(_, io_err))
if io_err.kind() == std::io::ErrorKind::UnexpectedEof =>
{
// We don't really expect a body for HEAD requests, so this is fine.
}
Ok(err_inner) => return Err(format!("Failed to read response body: {err_inner}")),
Err(err) => {
return Err(format!("Failed to read response body: {err}"));
}
}
} else {
return Err(format!("Failed to read response body: {err}"));
}
Expand Down
Loading
Loading