use std::num::NonZeroU32;
use std::sync::Arc;
use super::live_progress::LiveProgress;
use super::{Advertisement, DownloadedFetch, FetchError, FetchLimits, KnownHistory, protocol};
use crate::ObjectId;
use crate::packet::Wire;
use crate::transport::TransportControl;
use crate::transport::http::{HttpRemote, RequestBody};
pub async fn receive_http(
remote: &HttpRemote,
select: impl FnOnce(&Advertisement) -> Vec<ObjectId>,
known: Option<Arc<KnownHistory>>,
limits: FetchLimits,
control: TransportControl<'_>,
) -> Result<DownloadedFetch, FetchError> {
receive_http_with_depth(remote, select, known, None, limits, control).await
}
pub async fn receive_http_with_depth(
remote: &HttpRemote,
select: impl FnOnce(&Advertisement) -> Vec<ObjectId>,
known: Option<Arc<KnownHistory>>,
depth: Option<NonZeroU32>,
limits: FetchLimits,
control: TransportControl<'_>,
) -> Result<DownloadedFetch, FetchError> {
receive_http_with_depth_and_progress(remote, select, known, depth, limits, control, |_| {})
.await
}
pub async fn receive_http_with_progress(
remote: &HttpRemote,
select: impl FnOnce(&Advertisement) -> Vec<ObjectId>,
known: Option<Arc<KnownHistory>>,
limits: FetchLimits,
control: TransportControl<'_>,
progress: impl FnMut(&[u8]),
) -> Result<DownloadedFetch, FetchError> {
receive_http_with_depth_and_progress(remote, select, known, None, limits, control, progress)
.await
}
pub async fn receive_http_with_depth_and_progress(
remote: &HttpRemote,
select: impl FnOnce(&Advertisement) -> Vec<ObjectId>,
mut known: Option<Arc<KnownHistory>>,
depth: Option<NonZeroU32>,
limits: FetchLimits,
control: TransportControl<'_>,
mut progress: impl FnMut(&[u8]),
) -> Result<DownloadedFetch, FetchError> {
#[cfg(feature = "tracing")]
let span = tracing::debug_span!(
target: "girt",
"fetch.http",
outcome = "incomplete",
failure_class = tracing::field::Empty,
effects = tracing::field::Empty,
wire_bytes = tracing::field::Empty,
);
let operation = async {
control.check()?;
let empty = KnownHistory::default();
let history = known.as_deref().unwrap_or(&empty);
protocol::validate_known(history, limits, control.cancel)?;
let (bytes, advertisement_bytes) = remote
.discover(
"git-upload-pack",
limits.max_advertisement_bytes.min(limits.max_wire_bytes),
control,
)
.await?;
let mut reader = bytes.as_slice();
let mut wire = Wire {
reader: &mut reader,
remaining: limits.max_wire_bytes,
cancel: control.cancel,
};
let advertisement = protocol::advertise(&mut wire, limits)?;
wire.end()?;
let wants = select(&advertisement);
if depth.is_none()
&& known
.as_ref()
.is_some_and(|history| !history.applies_to(&wants))
{
known = known
.as_ref()
.map(|history| Arc::new(history.shallow_only()));
}
let history = known.as_deref().unwrap_or(&empty);
let remaining = limits.max_wire_bytes - advertisement_bytes;
control.check()?;
let mut request = Vec::new();
let negotiation = protocol::request(
&mut request,
&advertisement,
wants,
history,
depth,
limits,
control.cancel,
)?;
control.check()?;
if !negotiation.needs_pack {
return Ok(DownloadedFetch {
#[cfg(feature = "tracing")]
trace: crate::trace::DownloadContext::capture(),
advertisement,
negotiation,
known,
limits,
remaining,
body: Vec::new().into(),
});
}
let mut live = LiveProgress::new(depth.is_some(), limits.max_shallow_roots);
let mut validate_prefix = |bytes: &[u8]| {
let mut reader = bytes;
let mut wire = Wire {
reader: &mut reader,
remaining,
cancel: control.cancel,
};
protocol::read_response_prefix(&mut wire, &negotiation, limits).is_ok()
&& wire.end().is_ok()
};
let mut body = super::bytes::Bytes::temporary()?;
let mut io_error = None;
let result = remote
.transfer(
"git-upload-pack",
Some(RequestBody::new(request, Vec::new())?),
control,
|chunk| {
if chunk.len() > remaining.saturating_sub(body.len()) {
return Err(crate::transport::http::HttpError::Limit("response body"));
}
if let Err(error) = body.append(chunk) {
io_error = Some(error);
return Err(crate::transport::http::HttpError::Network);
}
live.feed(chunk, &mut validate_prefix, &mut progress);
Ok(())
},
)
.await;
if let Some(error) = io_error {
return Err(error);
}
result?;
Ok(DownloadedFetch {
#[cfg(feature = "tracing")]
trace: crate::trace::DownloadContext::capture(),
advertisement,
negotiation,
known,
limits,
remaining,
body,
})
};
#[cfg(feature = "tracing")]
let result = tracing::Instrument::instrument(operation, span.clone()).await;
#[cfg(not(feature = "tracing"))]
let result = operation.await;
#[cfg(feature = "tracing")]
crate::trace::finish(&span, &result, crate::trace::fetch);
#[cfg(feature = "tracing")]
if let Ok(value) = &result {
span.record("wire_bytes", value.body.len());
}
result
}