Skip to content

Commit

Permalink
reqwest: Migrate to shared Error type and use thiserror's From
Browse files Browse the repository at this point in the history
…impl (#238)

`reqwest` no longer has a separate error type for sync and async
implementations.  In addition `thiserror` can generate `From`
implementations for automatic conversions in the `Try` (`?`) operator.

Finally, delete some unnecessary `#[cfg]` wraps that are already
guarding the module as a whole, and that the code won't compile without
anyway.
  • Loading branch information
MarijnS95 authored Nov 28, 2023
1 parent ed63126 commit 8c31046
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 45 deletions.
64 changes: 20 additions & 44 deletions src/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,25 @@ use thiserror::Error;
///
/// Error type returned by failed reqwest HTTP requests.
///
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum Error<T>
where
T: std::error::Error + 'static,
{
pub enum Error {
/// Error returned by reqwest crate.
#[error("request failed")]
Reqwest(#[source] T),
Reqwest(#[from] reqwest::Error),
/// Non-reqwest HTTP error.
#[error("HTTP error")]
Http(#[source] http::Error),
Http(#[from] http::Error),
/// I/O error.
#[error("I/O error")]
Io(#[source] std::io::Error),
/// Other error.
#[error("Other error: {}", _0)]
Other(String),
Io(#[from] std::io::Error),
}

#[cfg(not(target_arch = "wasm32"))]
pub use blocking::http_client;
///
/// Error type returned by failed reqwest blocking HTTP requests.
///
#[cfg(not(target_arch = "wasm32"))]
pub type HttpClientError = Error<blocking::reqwest::Error>;

pub use async_client::async_http_client;

///
/// Error type returned by failed reqwest async HTTP requests.
///
pub type AsyncHttpClientError = Error<reqwest::Error>;

#[cfg(not(target_arch = "wasm32"))]
mod blocking {
use super::super::{HttpRequest, HttpResponse};
Expand All @@ -51,36 +36,29 @@ mod blocking {
///
/// Synchronous HTTP client.
///
pub fn http_client(request: HttpRequest) -> Result<HttpResponse, Error<reqwest::Error>> {
pub fn http_client(request: HttpRequest) -> Result<HttpResponse, Error> {
let client = blocking::Client::builder()
// Following redirects opens the client up to SSRF vulnerabilities.
.redirect(RedirectPolicy::none())
.build()
.map_err(Error::Reqwest)?;
.build()?;

#[cfg(feature = "reqwest")]
let mut request_builder = client
.request(request.method, request.url.as_str())
.body(request.body);

for (name, value) in &request.headers {
request_builder = request_builder.header(name.as_str(), value.as_bytes());
}
let mut response = client
.execute(request_builder.build().map_err(Error::Reqwest)?)
.map_err(Error::Reqwest)?;
let mut response = client.execute(request_builder.build()?)?;

let mut body = Vec::new();
response.read_to_end(&mut body).map_err(Error::Io)?;

#[cfg(feature = "reqwest")]
{
Ok(HttpResponse {
status_code: response.status(),
headers: response.headers().to_owned(),
body,
})
}
response.read_to_end(&mut body)?;

Ok(HttpResponse {
status_code: response.status(),
headers: response.headers().to_owned(),
body,
})
}
}

Expand All @@ -93,9 +71,7 @@ mod async_client {
///
/// Asynchronous HTTP client.
///
pub async fn async_http_client(
request: HttpRequest,
) -> Result<HttpResponse, Error<reqwest::Error>> {
pub async fn async_http_client(request: HttpRequest) -> Result<HttpResponse, Error> {
let client = {
let builder = reqwest::Client::builder();

Expand All @@ -104,7 +80,7 @@ mod async_client {
#[cfg(not(target_arch = "wasm32"))]
let builder = builder.redirect(reqwest::redirect::Policy::none());

builder.build().map_err(Error::Reqwest)?
builder.build()?
};

let mut request_builder = client
Expand All @@ -113,13 +89,13 @@ mod async_client {
for (name, value) in &request.headers {
request_builder = request_builder.header(name.as_str(), value.as_bytes());
}
let request = request_builder.build().map_err(Error::Reqwest)?;
let request = request_builder.build()?;

let response = client.execute(request).await.map_err(Error::Reqwest)?;
let response = client.execute(request).await?;

let status_code = response.status();
let headers = response.headers().to_owned();
let chunks = response.bytes().await.map_err(Error::Reqwest)?;
let chunks = response.bytes().await?;
Ok(HttpResponse {
status_code,
headers,
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2616,5 +2616,5 @@ fn test_send_sync_impl() {
#[cfg(feature = "curl")]
is_sync_and_send::<super::curl::Error>();
#[cfg(feature = "reqwest")]
is_sync_and_send::<super::reqwest::Error<TestError>>();
is_sync_and_send::<super::reqwest::Error>();
}

0 comments on commit 8c31046

Please sign in to comment.