use ostrya_fetch::{Fetcher, FetcherOptions};
use crate::error::{Error, Result};
use crate::session::StreamSessionOptions;
use crate::session::http::{Endpoint, MAX_PARALLEL};
const MAX_OUTSTANDING: usize = MAX_PARALLEL as usize + 1;
pub(super) async fn prepare(mut http: FetcherOptions) -> Result<Endpoint> {
let url = match http.mirrors.as_slice() {
[url] => url.clone(),
mirrors => {
return Err(Error::InvalidInput(format!(
"an HTTP push needs exactly one mirror, the push address, and the options \
name {}",
mirrors.len()
)));
}
};
ostrya_fetch::check_base_url(&url).map_err(|e| Error::InvalidInput(format!("address: {e}")))?;
http.max_outstanding = MAX_OUTSTANDING;
http.max_redirects = 0;
let fetcher = Box::pin(Fetcher::new(http)).await.map_err(Error::Fetch)?;
Ok(Endpoint { fetcher, url })
}
pub(super) async fn prepare_pull(
http: FetcherOptions,
opts: StreamSessionOptions,
) -> Result<Fetcher> {
let StreamSessionOptions {
agent,
max_outstanding,
} = &opts;
if agent.is_some() {
return Err(Error::InvalidInput(
"an HTTP pull takes no agent of a stream session: a User-Agent header of the \
fetcher options sets it"
.into(),
));
}
if max_outstanding.is_some() {
return Err(Error::InvalidInput(
"an HTTP pull takes no request limit of a stream session: max_outstanding of the \
fetcher options sets it"
.into(),
));
}
if http.mirrors.is_empty() {
return Err(Error::InvalidInput(
"the HTTP endpoint of a pull names no mirror".into(),
));
}
Fetcher::new(http).await.map_err(Error::Fetch)
}