gix_protocol/handshake/
function.rsuse gix_features::{progress, progress::Progress};
use gix_transport::{client, client::SetServiceResponse, Service};
use maybe_async::maybe_async;
use super::{Error, Outcome};
use crate::{credentials, handshake::refs};
#[allow(clippy::result_large_err)]
#[maybe_async]
pub async fn handshake<AuthFn, T>(
mut transport: T,
service: Service,
mut authenticate: AuthFn,
extra_parameters: Vec<(String, Option<String>)>,
progress: &mut impl Progress,
) -> Result<Outcome, Error>
where
AuthFn: FnMut(credentials::helper::Action) -> credentials::protocol::Result,
T: client::Transport,
{
let _span = gix_features::trace::detail!("gix_protocol::handshake()", service = ?service, extra_parameters = ?extra_parameters);
let (server_protocol_version, refs, capabilities) = {
progress.init(None, progress::steps());
progress.set_name("handshake".into());
progress.step();
let extra_parameters: Vec<_> = extra_parameters
.iter()
.map(|(k, v)| (k.as_str(), v.as_deref()))
.collect();
let supported_versions: Vec<_> = transport.supported_protocol_versions().into();
let result = transport.handshake(service, &extra_parameters).await;
let SetServiceResponse {
actual_protocol,
capabilities,
refs,
} = match result {
Ok(v) => Ok(v),
Err(client::Error::Io(ref err)) if err.kind() == std::io::ErrorKind::PermissionDenied => {
drop(result); let url = transport.to_url().into_owned();
progress.set_name("authentication".into());
let credentials::protocol::Outcome { identity, next } =
authenticate(credentials::helper::Action::get_for_url(url.clone()))?
.ok_or(Error::EmptyCredentials)?;
transport.set_identity(identity)?;
progress.step();
progress.set_name("handshake (authenticated)".into());
match transport.handshake(service, &extra_parameters).await {
Ok(v) => {
authenticate(next.store())?;
Ok(v)
}
Err(client::Error::Io(err)) if err.kind() == std::io::ErrorKind::PermissionDenied => {
authenticate(next.erase())?;
return Err(Error::InvalidCredentials { url, source: err });
}
Err(err) => Err(err),
}
}
Err(err) => Err(err),
}?;
if !supported_versions.is_empty() && !supported_versions.contains(&actual_protocol) {
return Err(Error::TransportProtocolPolicyViolation {
actual_version: actual_protocol,
});
}
let parsed_refs = match refs {
Some(mut refs) => {
assert!(
matches!(
actual_protocol,
gix_transport::Protocol::V0 | gix_transport::Protocol::V1
),
"Only V(0|1) auto-responds with refs"
);
Some(
refs::from_v1_refs_received_as_part_of_handshake_and_capabilities(&mut refs, capabilities.iter())
.await?,
)
}
None => None,
};
(actual_protocol, parsed_refs, capabilities)
}; let (refs, v1_shallow_updates) = refs
.map(|(refs, shallow)| (Some(refs), Some(shallow)))
.unwrap_or_default();
Ok(Outcome {
server_protocol_version,
refs,
v1_shallow_updates,
capabilities,
})
}