use super::*;
pub(super) fn https_response_body_limit(remaining_policy_bytes: Option<u64>) -> u64 {
remaining_policy_bytes.map_or(DEFAULT_HTTPS_RESPONSE_BODY_LIMIT_BYTES, |remaining| {
remaining.min(DEFAULT_HTTPS_RESPONSE_BODY_LIMIT_BYTES)
})
}
#[cfg(any(test, rings_browser))]
pub(super) fn checked_status_code(status: f64) -> Result<u16> {
if !status.is_finite() || status.fract() != 0.0 || !(100.0..=999.0).contains(&status) {
return Err(Error::HttpRequestError(format!(
"fetch response status is not a valid HTTP status: {status:?}"
)));
}
status
.to_string()
.parse::<u16>()
.map_err(|_| Error::HttpRequestError(format!("invalid fetch response status {status:?}")))
}
pub(super) fn headers_bytes(headers: &[(String, String)]) -> Result<u64> {
usize_to_u64(
headers
.iter()
.map(|(name, value)| name.len().saturating_add(value.len()))
.fold(0_usize, usize::saturating_add),
)
}
fn usize_to_u64(value: usize) -> Result<u64> {
u64::try_from(value).map_err(|_| Error::InvalidData)
}
pub(super) fn reject_content_length_over_limit(
headers: &[(String, String)],
max_body_bytes: u64,
) -> Result<()> {
let Some(length) = headers
.iter()
.find(|(name, _)| name.eq_ignore_ascii_case("content-length"))
.and_then(|(_, value)| value.parse::<u64>().ok())
else {
return Ok(());
};
if length > max_body_bytes {
return Err(Error::NoPermission);
}
Ok(())
}
pub(super) fn admit_body_chunk(
body_len: usize,
chunk_len: usize,
max_body_bytes: u64,
record_bytes: &impl Fn(u64) -> Result<()>,
) -> Result<()> {
let chunk_len = usize_to_u64(chunk_len)?;
if usize_to_u64(body_len)?.saturating_add(chunk_len) > max_body_bytes {
return Err(Error::NoPermission);
}
record_bytes(chunk_len)
}
#[cfg(any(test, rings_browser))]
pub(super) trait FetchCancelTarget {
type Body;
type Reader;
fn cancel_body(&mut self, body: &Self::Body) -> bool;
fn cancel_reader(&mut self, reader: &Self::Reader) -> bool;
fn abort(&mut self);
}
#[cfg(any(test, rings_browser))]
pub(super) struct FetchCancellationGuard<T: FetchCancelTarget> {
target: T,
body: Option<T::Body>,
reader: Option<T::Reader>,
armed: bool,
}
#[cfg(any(test, rings_browser))]
impl<T: FetchCancelTarget> FetchCancellationGuard<T> {
pub(super) const fn new(target: T) -> Self {
Self {
target,
body: None,
reader: None,
armed: true,
}
}
pub(super) fn set_body(&mut self, body: T::Body) {
self.body = Some(body);
}
pub(super) fn set_reader(&mut self, reader: T::Reader) {
self.reader = Some(reader);
}
pub(super) fn disarm(&mut self) {
self.armed = false;
}
}
#[cfg(any(test, rings_browser))]
impl<T: FetchCancelTarget> Drop for FetchCancellationGuard<T> {
fn drop(&mut self) {
if !self.armed {
return;
}
let cancelled_reader = self
.reader
.take()
.is_some_and(|reader| self.target.cancel_reader(&reader));
if !cancelled_reader {
if let Some(body) = self.body.take() {
let _ = self.target.cancel_body(&body);
}
}
self.target.abort();
}
}
#[cfg(any(test, rings_browser))]
pub(super) trait CancellableHttpsFetch: Sized {
type Response;
type Body: Clone;
type Reader: Clone;
type Cancel: FetchCancelTarget<Body = Self::Body, Reader = Self::Reader>;
async fn await_response(&mut self) -> Result<Self::Response>;
fn response_status(&self, response: &Self::Response) -> Result<f64>;
fn response_body(&self, response: &Self::Response) -> Result<Option<Self::Body>>;
fn response_headers(&self, response: &Self::Response) -> Result<Vec<(String, String)>>;
fn body_reader(&self, body: &Self::Body) -> Result<Self::Reader>;
async fn read_chunk(&self, reader: &Self::Reader) -> Result<Option<Vec<u8>>>;
}
#[cfg(any(test, rings_browser))]
pub(super) async fn drive_cancellable_https_fetch<F>(
mut fetch: F,
cancel_target: F::Cancel,
max_body_bytes: u64,
timeout_duration: std::time::Duration,
record_bytes: impl Fn(u64) -> Result<()>,
) -> Result<OnionHttpsResponse>
where
F: CancellableHttpsFetch,
{
use futures::future::Either;
use futures::FutureExt;
let mut cancel_guard = FetchCancellationGuard::new(cancel_target);
let fetch_task = async {
let response = fetch.await_response().await?;
let body = fetch.response_body(&response)?;
if let Some(body) = &body {
cancel_guard.set_body(body.clone());
}
let status = checked_status_code(fetch.response_status(&response)?)?;
let headers = fetch.response_headers(&response)?;
reject_content_length_over_limit(&headers, max_body_bytes)?;
record_bytes(headers_bytes(&headers)?)?;
let collected = match body {
None => Vec::new(),
Some(body) => {
let reader = fetch.body_reader(&body)?;
cancel_guard.set_reader(reader.clone());
let mut collected = Vec::new();
while let Some(bytes) = fetch.read_chunk(&reader).await? {
admit_body_chunk(collected.len(), bytes.len(), max_body_bytes, &record_bytes)?;
collected.extend_from_slice(bytes.as_slice());
}
collected
}
};
cancel_guard.disarm();
Ok::<OnionHttpsResponse, Error>(OnionHttpsResponse {
status,
headers,
body: collected,
})
};
let timeout = rings_runtime::sleep(timeout_duration).fuse();
futures::pin_mut!(fetch_task, timeout);
match futures::future::select(fetch_task, timeout).await {
Either::Left((result, _)) => result,
Either::Right((_, _)) => Err(Error::HttpRequestError(
"browser HTTPS proxy request timed out".to_string(),
)),
}
}