Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ reqwest = { version = "0.12", optional = true, default-features = false }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.10"
ureq = { version = "2", optional = true }
ureq = { version = "3", optional = true }
url = { version = "2.1", features = ["serde"] }
chrono = { version = "0.4.31", default-features = false, features = ["clock", "serde", "std", "wasmbind"] }
serde_path_to_error = "0.1.2"
Expand Down
57 changes: 7 additions & 50 deletions src/ureq_client.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
use crate::{HttpClientError, HttpRequest, HttpResponse};

use http::{
header::{HeaderValue, CONTENT_TYPE},
method::Method,
status::StatusCode,
};

use std::io::Read;

use crate::{HttpClientError, HttpRequest, HttpResponse};

impl crate::SyncHttpClient for ureq::Agent {
type Error = HttpClientError<ureq::Error>;

fn call(&self, request: HttpRequest) -> Result<HttpResponse, Self::Error> {
let mut req = if *request.method() == Method::POST {
self.post(&request.uri().to_string())
} else {
debug_assert_eq!(*request.method(), Method::GET);
self.get(&request.uri().to_string())
};

for (name, value) in request.headers() {
req = req.set(
name.as_ref(),
// TODO: In newer `ureq` it should be easier to convert arbitrary byte sequences
// without unnecessary UTF-8 fallibility here.
value.to_str().map_err(|_| {
HttpClientError::Other(format!(
"invalid `{name}` header value {:?}",
value.as_bytes()
))
})?,
);
}

let response = if let Method::POST = *request.method() {
req.send_bytes(request.body())
} else {
req.call()
}
.map_err(Box::new)?;

let mut builder = http::Response::builder()
.status(StatusCode::from_u16(response.status()).map_err(http::Error::from)?);

if let Some(content_type) = response
.header(CONTENT_TYPE.as_str())
.map(HeaderValue::from_str)
.transpose()
.map_err(http::Error::from)?
{
builder = builder.header(CONTENT_TYPE, content_type);
}
let response = self.run(request).map_err(Box::new)?;

let mut body = Vec::new();
response.into_reader().read_to_end(&mut body)?;
let (parts, body) = response.into_parts();
let mut body_vec = Vec::new();
body.into_reader().read_to_end(&mut body_vec)?;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be changed to

let body_vec = body.read_to_vec()?;

That function has a 10MB cap, which is a safety mechanism to avoid a misbehaving remote server causing problems.

I struggled to find a good Error variant to use though.


builder.body(body).map_err(HttpClientError::Http)
Ok(HttpResponse::from_parts(parts, body_vec))
}
}
Loading