#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(
test,
allow(
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic,
clippy::unwrap_used,
clippy::unreachable
)
)]
pub use reqwest;
#[derive(Clone, Debug)]
pub struct ReqwestClient(Built);
pub fn shared() -> DynHttpClient {
DynHttpClient::new(ReqwestClient::default())
}
#[derive(Clone, Debug)]
enum Built {
Shared(Arc<reqwest::Client>),
Client(Arc<reqwest::Client>),
Failed(Arc<reqwest::Error>),
}
impl Default for ReqwestClient {
fn default() -> Self {
fn build() -> ReqwestClient {
ReqwestClient(match build_shared(shared_builder) {
Ok(client) => Built::Shared(Arc::new(client)),
Err(error) => Built::Failed(Arc::new(error)),
})
}
#[cfg(not(target_family = "wasm"))]
{
static SHARED: std::sync::LazyLock<ReqwestClient> = std::sync::LazyLock::new(build);
SHARED.clone()
}
#[cfg(target_family = "wasm")]
{
thread_local! {
static SHARED: ReqwestClient = build();
}
SHARED.with(Clone::clone)
}
}
}
impl ReqwestClient {
pub fn inner(&self) -> Option<&reqwest::Client> {
match &self.0 {
#[cfg(not(target_family = "wasm"))]
Built::Shared(_) => {
static FOLLOWING: std::sync::LazyLock<Option<reqwest::Client>> =
std::sync::LazyLock::new(|| build_shared(reqwest::Client::builder).ok());
FOLLOWING.as_ref()
}
#[cfg(target_family = "wasm")]
Built::Shared(client) => Some(client),
Built::Client(client) => Some(client),
Built::Failed(_) => None,
}
}
#[cfg(test)]
fn same(&self, other: &Self) -> bool {
match (&self.0, &other.0) {
(Built::Shared(a), Built::Shared(b)) | (Built::Client(a), Built::Client(b)) => {
Arc::ptr_eq(a, b)
}
(Built::Failed(a), Built::Failed(b)) => Arc::ptr_eq(a, b),
_ => false,
}
}
}
fn shared_builder() -> reqwest::ClientBuilder {
let builder = reqwest::Client::builder();
#[cfg(not(target_family = "wasm"))]
let builder = builder.redirect(reqwest::redirect::Policy::none());
builder
}
fn build_shared(builder: fn() -> reqwest::ClientBuilder) -> reqwest::Result<reqwest::Client> {
let built = builder().build();
#[cfg(all(feature = "rustls", not(target_family = "wasm")))]
let built = built.or_else(|error| with_bundled_roots(builder(), error));
built
}
#[cfg(all(feature = "rustls", not(target_family = "wasm")))]
fn with_bundled_roots(
builder: reqwest::ClientBuilder,
error: reqwest::Error,
) -> reqwest::Result<reqwest::Client> {
let roots = webpki_root_certs::TLS_SERVER_ROOT_CERTS
.iter()
.filter_map(|der| reqwest::Certificate::from_der(der).ok());
let Ok(client) = builder.tls_certs_only(roots).build() else {
return Err(error);
};
tracing::warn!(
"the system's CA certificates could not be loaded ({}), so rig trusts its bundled \
Mozilla roots; install the system's CA certificates, or set SSL_CERT_FILE or \
SSL_CERT_DIR, to trust those instead",
Chain(&error)
);
Ok(client)
}
fn unbuilt(error: &Arc<reqwest::Error>) -> Error {
Error::permanent(TransportBuildError(Arc::clone(error)))
}
impl From<reqwest::Client> for ReqwestClient {
fn from(client: reqwest::Client) -> Self {
Self(Built::Client(Arc::new(client)))
}
}
use bytes::Bytes;
use rig_http::http_client::{
BoxedStream, DynHttpClient, Error, HttpClientExt, LazyBody, MultipartForm, Request, Response,
Result, StreamingResponse, multipart::PartContent, retryable_status,
};
use rig_http::wasm_compat::*;
use std::sync::Arc;
#[derive(Debug)]
struct TransportBuildError(Arc<reqwest::Error>);
impl std::fmt::Display for TransportBuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"could not build the bundled reqwest transport: {}",
Chain(&*self.0)
)
}
}
struct Chain<'a>(&'a (dyn std::error::Error + 'static));
impl std::fmt::Display for Chain<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)?;
std::iter::successors(self.0.source(), |cause| cause.source())
.try_for_each(|cause| write!(f, ": {cause}"))
}
}
impl std::error::Error for TransportBuildError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&*self.0)
}
}
pub fn from_reqwest(err: reqwest::Error) -> Error {
if is_transient(&err) {
Error::transient(err)
} else {
Error::permanent(err)
}
}
fn is_transient(err: &reqwest::Error) -> bool {
match err.status() {
Some(status) => retryable_status(Some(status.as_u16())),
None => !(err.is_builder() || err.is_redirect()),
}
}
const ERROR_BODY_LIMIT: usize = 64 * 1024;
async fn non_success_status_error(response: reqwest::Response) -> Error {
let status = response.status();
let headers = response.headers().clone();
non_success_body_error(status, headers, &mut Box::pin(response.bytes_stream())).await
}
pub async fn non_success_body_error<S, B, E>(
status: http::StatusCode,
headers: http::HeaderMap,
body: &mut S,
) -> Error
where
S: futures::Stream<Item = std::result::Result<B, E>> + Unpin,
B: AsRef<[u8]>,
E: std::fmt::Display,
{
use futures::StreamExt;
let mut bytes = Vec::new();
let mut failure = None;
while let Some(chunk) = body.next().await {
match chunk {
Ok(chunk) => {
let remaining = ERROR_BODY_LIMIT + 1 - bytes.len();
bytes.extend(chunk.as_ref().iter().take(remaining));
if bytes.len() > ERROR_BODY_LIMIT {
break;
}
}
Err(error) => {
failure = Some(error.to_string());
break;
}
}
}
let kept = bytes.get(..ERROR_BODY_LIMIT).unwrap_or(&bytes);
let mut text = if kept.is_empty() {
failure
.map(|failure| format!("failed to read error response body: {failure}"))
.unwrap_or_default()
} else {
decode_error_body(kept.to_vec(), &headers).await
};
if bytes.len() > ERROR_BODY_LIMIT {
text.push_str("\n[Response body truncated after 64 KiB]");
}
Error::non_success_with_details(status, headers, text)
}
#[cfg(not(target_family = "wasm"))]
async fn decode_error_body(bytes: Vec<u8>, headers: &http::HeaderMap) -> String {
let mut response = http::Response::new(bytes);
*response.headers_mut() = headers.clone();
reqwest::Response::from(response)
.text()
.await
.unwrap_or_else(|error| format!("failed to read error response body: {error}"))
}
#[cfg(target_family = "wasm")]
async fn decode_error_body(bytes: Vec<u8>, _headers: &http::HeaderMap) -> String {
let bytes = bytes.strip_prefix(&[0xef, 0xbb, 0xbf]).unwrap_or(&bytes);
String::from_utf8_lossy(bytes).into_owned()
}
async fn into_response(mut response: reqwest::Response) -> Result<Response<LazyBody>> {
if !response.status().is_success() {
return Err(non_success_status_error(response).await);
}
let res = response_head(&mut response);
let body = async { response.bytes().await.map_err(from_reqwest) };
#[cfg(not(target_family = "wasm"))]
let body = rig_http::runtime::bind(body)?;
let body: LazyBody = Box::pin(body);
res.body(body).map_err(Error::Protocol)
}
fn response_head(response: &mut reqwest::Response) -> http::response::Builder {
#[cfg(not(target_family = "wasm"))]
let mut res = Response::builder()
.status(response.status())
.version(response.version());
#[cfg(target_family = "wasm")]
let mut res = Response::builder().status(response.status());
if let Some(hs) = res.headers_mut() {
*hs = std::mem::take(response.headers_mut());
}
res
}
async fn into_streaming_response(mut response: reqwest::Response) -> Result<StreamingResponse> {
if !response.status().is_success() {
return Err(non_success_status_error(response).await);
}
let res = response_head(&mut response);
use futures::StreamExt;
let stream = response
.bytes_stream()
.map(|chunk| chunk.map_err(from_reqwest));
#[cfg(not(target_family = "wasm"))]
let stream = rig_http::runtime::bind_stream(stream)?;
let stream: BoxedStream = Box::pin(stream);
res.body(stream).map_err(Error::Protocol)
}
#[derive(Debug, thiserror::Error)]
#[error("multipart part {part:?} has an unusable content type {content_type:?}: {source}")]
struct InvalidPartContentType {
part: String,
content_type: String,
source: reqwest::Error,
}
pub fn multipart_form(value: MultipartForm) -> Result<reqwest::multipart::Form> {
let mut form = reqwest::multipart::Form::new();
for part in value.into_parts() {
let (name, content, filename, content_type) = part.into_pieces();
match content {
PartContent::Text(text) => {
form = form.text(name, text);
}
PartContent::Binary(bytes) => {
let mut req_part = reqwest::multipart::Part::stream(bytes);
if let Some(content_type) = content_type.as_ref() {
req_part = req_part.mime_str(content_type.as_ref()).map_err(|source| {
Error::permanent(InvalidPartContentType {
part: name.clone(),
content_type: content_type.as_ref().to_string(),
source,
})
})?;
}
if let Some(filename) = filename {
req_part = req_part.file_name(filename);
}
form = form.part(name, req_part);
}
}
}
Ok(form)
}
enum Payload {
Bytes(Bytes),
Form(reqwest::multipart::Form),
}
trait Execute: WasmCompatSend + WasmCompatSync {
fn execute(
&self,
parts: http::request::Parts,
payload: Result<Payload>,
) -> Result<impl Future<Output = Result<reqwest::Response>> + WasmCompatSend + 'static>;
}
impl Execute for ReqwestClient {
fn execute(
&self,
parts: http::request::Parts,
payload: Result<Payload>,
) -> Result<impl Future<Output = Result<reqwest::Response>> + WasmCompatSend + 'static> {
let client = match &self.0 {
Built::Shared(client) | Built::Client(client) => client,
Built::Failed(error) => return Err(unbuilt(error)),
};
#[cfg(not(target_family = "wasm"))]
let follow = matches!(self.0, Built::Shared(_)).then(|| match &payload {
Ok(Payload::Bytes(bytes)) => Some(bytes.clone()),
_ => None,
});
let request = client
.request(parts.method, parts.uri.to_string())
.headers(parts.headers);
let request = match payload? {
Payload::Bytes(bytes) => request.body(bytes),
Payload::Form(form) => request.multipart(form),
};
let request = request.build().map_err(from_reqwest)?;
let client = Arc::clone(client);
Ok(async move {
#[cfg(not(target_family = "wasm"))]
if let Some(body) = follow {
return follow_redirects(client, request, body).await;
}
client.execute(request).await.map_err(from_reqwest)
})
}
}
#[cfg(not(target_family = "wasm"))]
const MAX_REDIRECTS: usize = 10;
#[cfg(not(target_family = "wasm"))]
async fn follow_redirects(
client: Arc<reqwest::Client>,
request: reqwest::Request,
mut body: Option<Bytes>,
) -> Result<reqwest::Response> {
use http::header::{
ACCEPT, CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TYPE, LOCATION, RANGE, REFERER,
TRANSFER_ENCODING, USER_AGENT,
};
use http::{HeaderValue, Method, StatusCode};
let mut method = request.method().clone();
let mut headers = request.headers().clone();
let mut response = client.execute(request).await.map_err(from_reqwest)?;
for _ in 0..MAX_REDIRECTS {
let drop_body = match response.status() {
StatusCode::MOVED_PERMANENTLY | StatusCode::FOUND => method == Method::POST,
StatusCode::SEE_OTHER => true,
StatusCode::TEMPORARY_REDIRECT | StatusCode::PERMANENT_REDIRECT => false,
_ => return Ok(response),
};
if drop_body {
if method != Method::HEAD {
method = Method::GET;
}
body = Some(Bytes::new());
headers.remove(CONTENT_TYPE);
headers.remove(CONTENT_LENGTH);
headers.remove(CONTENT_ENCODING);
headers.remove(TRANSFER_ENCODING);
}
let location = response.headers().get(LOCATION);
let location = location.and_then(|location| location.to_str().ok());
let previous = response.url();
let (Some(mut url), Some(replay)) = (
location.and_then(|location| previous.join(location).ok()),
body.clone(),
) else {
return Ok(response);
};
if url.origin() == previous.origin() {
let mut referer = previous.clone();
let _ = referer.set_username("");
let _ = referer.set_password(None);
referer.set_fragment(None);
if let Ok(referer) = HeaderValue::from_str(referer.as_str()) {
headers.insert(REFERER, referer);
}
} else {
let kept = [ACCEPT, CONTENT_ENCODING, CONTENT_TYPE, RANGE, USER_AGENT];
headers = headers
.iter()
.filter(|(name, _)| kept.contains(*name))
.map(|(name, value)| (name.clone(), value.clone()))
.collect();
}
let _ = url.set_username("");
let _ = url.set_password(None);
let request = client.request(method.clone(), url).headers(headers.clone());
response = request.body(replay).send().await.map_err(from_reqwest)?;
}
Ok(response)
}
fn send_with<C, T, F>(
client: &C,
parts: http::request::Parts,
payload: Result<Payload>,
convert: impl FnOnce(reqwest::Response) -> F + WasmCompatSend + 'static,
) -> impl Future<Output = Result<T>> + WasmCompatSend + 'static
where
C: Execute,
F: Future<Output = Result<T>> + WasmCompatSend,
{
let sent = client.execute(parts, payload);
async move {
let sent = sent?;
let operation = async move { convert(sent.await?).await };
#[cfg(not(target_family = "wasm"))]
let operation = rig_http::runtime::bind(operation)?;
operation.await
}
}
macro_rules! http_client_ext {
($client:ty) => {
impl HttpClientExt for $client {
fn send(
&self,
req: Request<Bytes>,
) -> impl Future<Output = Result<Response<LazyBody>>> + WasmCompatSend + 'static {
let (parts, body) = req.into_parts();
send_with(self, parts, Ok(Payload::Bytes(body)), into_response)
}
fn send_multipart(
&self,
req: Request<MultipartForm>,
) -> impl Future<Output = Result<Response<LazyBody>>> + WasmCompatSend + 'static {
let (parts, form) = req.into_parts();
let payload = multipart_form(form).map(Payload::Form);
send_with(self, parts, payload, into_response)
}
fn send_streaming(
&self,
req: Request<Bytes>,
) -> impl Future<Output = Result<StreamingResponse>> + WasmCompatSend {
let (parts, body) = req.into_parts();
let payload = Ok(Payload::Bytes(body));
send_with(self, parts, payload, into_streaming_response)
}
}
};
}
http_client_ext!(ReqwestClient);
#[cfg(any(
feature = "reqwest-middleware-rustls",
feature = "reqwest-middleware-native-tls"
))]
mod middleware;
#[cfg(any(
feature = "reqwest-middleware-rustls",
feature = "reqwest-middleware-native-tls"
))]
pub use middleware::ReqwestMiddlewareClient;
#[cfg(not(target_family = "wasm"))]
const _: fn() = || {
fn assert_send_sync_static<T: Send + Sync + 'static>() {}
assert_send_sync_static::<ReqwestClient>();
};
#[cfg(test)]
mod tests;