gix_transport/client/capabilities.rs
1use bstr::{BStr, BString, ByteSlice};
2
3#[cfg(any(feature = "blocking-client", feature = "async-client"))]
4use crate::client;
5use crate::Protocol;
6
7/// The error used in [`Capabilities::from_bytes()`] and [`Capabilities::from_lines()`].
8#[derive(Debug, thiserror::Error)]
9#[allow(missing_docs)]
10pub enum Error {
11 #[error("Capabilities were missing entirely as there was no 0 byte")]
12 MissingDelimitingNullByte,
13 #[error("there was not a single capability behind the delimiter")]
14 NoCapabilities,
15 #[error("a version line was expected, but none was retrieved")]
16 MissingVersionLine,
17 #[error("expected 'version X', got {0:?}")]
18 MalformattedVersionLine(BString),
19 #[error("Got unsupported version {actual:?}, expected {}", *desired as u8)]
20 UnsupportedVersion { desired: Protocol, actual: BString },
21 #[error("An IO error occurred while reading V2 lines")]
22 Io(#[from] std::io::Error),
23}
24
25/// A structure to represent multiple [capabilities][Capability] or features supported by the server.
26///
27/// ### Deviation
28///
29/// As a *shortcoming*, we are unable to parse `V1` as emitted from `git-upload-pack` without a `git-daemon` or server,
30/// as it will not emit any capabilities for some reason. Only `V2` and `V0` work in that context.
31#[derive(Debug, Clone)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub struct Capabilities {
34 data: BString,
35 value_sep: u8,
36}
37
38/// This implementation yields exactly those minimal capabilities that are required for `gix` to work, nothing more and nothing less.
39///
40/// This is a bit of a hack just get tests with Protocol V0 to work, which is a good way to enforce stateful transports.
41/// Of course, V1 would also do that but when calling `git-upload-pack` directly, it advertises so badly that this is easier to implement.
42impl Default for Capabilities {
43 fn default() -> Self {
44 Capabilities::from_lines("version 2\nmulti_ack_detailed\nside-band-64k\n".into())
45 .expect("valid format, known at compile time")
46 }
47}
48
49/// The name of a single capability.
50pub struct Capability<'a>(&'a BStr);
51
52impl<'a> Capability<'a> {
53 /// Returns the name of the capability.
54 ///
55 /// Most capabilities only consist of a name, making them appear like a feature toggle.
56 pub fn name(&self) -> &'a BStr {
57 self.0
58 .splitn(2, |b| *b == b'=')
59 .next()
60 .expect("there is always a single item")
61 .as_bstr()
62 }
63 /// Returns the value associated with the capability.
64 ///
65 /// Note that the caller must know whether a single or multiple values are expected, in which
66 /// case [`values()`][Capability::values()] should be called.
67 pub fn value(&self) -> Option<&'a BStr> {
68 self.0.splitn(2, |b| *b == b'=').nth(1).map(ByteSlice::as_bstr)
69 }
70 /// Returns the values of a capability if its [`value()`][Capability::value()] is space separated.
71 pub fn values(&self) -> Option<impl Iterator<Item = &'a BStr>> {
72 self.value().map(|v| v.split(|b| *b == b' ').map(ByteSlice::as_bstr))
73 }
74 /// Returns true if its space-separated [`value()`][Capability::value()] contains the given `want`ed capability.
75 pub fn supports(&self, want: impl Into<&'a BStr>) -> Option<bool> {
76 let want = want.into();
77 self.values().map(|mut iter| iter.any(|v| v == want))
78 }
79}
80
81impl Capabilities {
82 /// Parse capabilities from the given `bytes`.
83 ///
84 /// Useful in case they are encoded within a `ref` behind a null byte.
85 pub fn from_bytes(bytes: &[u8]) -> Result<(Capabilities, usize), Error> {
86 let delimiter_pos = bytes.find_byte(0).ok_or(Error::MissingDelimitingNullByte)?;
87 if delimiter_pos + 1 == bytes.len() {
88 return Err(Error::NoCapabilities);
89 }
90 let capabilities = &bytes[delimiter_pos + 1..];
91 Ok((
92 Capabilities {
93 data: capabilities.as_bstr().to_owned(),
94 value_sep: b' ',
95 },
96 delimiter_pos,
97 ))
98 }
99
100 /// Parse capabilities from the given a `lines_buf` which is expected to be all newline separated lines
101 /// from the server.
102 ///
103 /// Useful for parsing capabilities from a data sent from a server, and to avoid having to deal with
104 /// blocking and async traits for as long as possible. There is no value in parsing a few bytes
105 /// in a non-blocking fashion.
106 pub fn from_lines(lines_buf: BString) -> Result<Capabilities, Error> {
107 let mut lines = <_ as bstr::ByteSlice>::lines(lines_buf.as_slice().trim());
108 let version_line = lines.next().ok_or(Error::MissingVersionLine)?;
109 let (name, value) = version_line.split_at(
110 version_line
111 .find(b" ")
112 .ok_or_else(|| Error::MalformattedVersionLine(version_line.to_owned().into()))?,
113 );
114 if name != b"version" {
115 return Err(Error::MalformattedVersionLine(version_line.to_owned().into()));
116 }
117 if value != b" 2" {
118 return Err(Error::UnsupportedVersion {
119 desired: Protocol::V2,
120 actual: value.to_owned().into(),
121 });
122 }
123 Ok(Capabilities {
124 value_sep: b'\n',
125 data: lines.as_bytes().into(),
126 })
127 }
128
129 /// Returns true of the given `feature` is mentioned in this list of capabilities.
130 pub fn contains(&self, feature: &str) -> bool {
131 self.capability(feature).is_some()
132 }
133
134 /// Returns the capability with `name`.
135 pub fn capability(&self, name: &str) -> Option<Capability<'_>> {
136 self.iter().find(|c| c.name() == name.as_bytes().as_bstr())
137 }
138
139 /// Returns an iterator over all capabilities.
140 pub fn iter(&self) -> impl Iterator<Item = Capability<'_>> {
141 self.data
142 .split(move |b| *b == self.value_sep)
143 .map(|c| Capability(c.as_bstr()))
144 }
145}
146
147/// internal use
148#[cfg(any(feature = "blocking-client", feature = "async-client"))]
149impl Capabilities {
150 fn extract_protocol(capabilities_or_version: gix_packetline::TextRef<'_>) -> Result<Protocol, client::Error> {
151 let line = capabilities_or_version.as_bstr();
152 let version = if line.starts_with_str("version ") {
153 if line.len() != "version X".len() {
154 return Err(client::Error::UnsupportedProtocolVersion(line.as_bstr().into()));
155 }
156 match line {
157 line if line.ends_with_str("1") => Protocol::V1,
158 line if line.ends_with_str("2") => Protocol::V2,
159 _ => return Err(client::Error::UnsupportedProtocolVersion(line.as_bstr().into())),
160 }
161 } else {
162 Protocol::V1
163 };
164 Ok(version)
165 }
166}
167
168#[cfg(feature = "blocking-client")]
169///
170pub mod recv {
171 use std::io;
172
173 use bstr::ByteVec;
174
175 use crate::{client, client::Capabilities, Protocol};
176
177 /// Success outcome of [`Capabilities::from_lines_with_version_detection`].
178 pub struct Outcome<'a> {
179 /// The [`Capabilities`] the remote advertised.
180 pub capabilities: Capabilities,
181 /// The remote refs as a [`io::BufRead`].
182 ///
183 /// This is `Some` only when protocol v1 is used. The [`io::BufRead`] must be exhausted by
184 /// the caller.
185 pub refs: Option<Box<dyn crate::client::ReadlineBufRead + 'a>>,
186 /// The [`Protocol`] the remote advertised.
187 pub protocol: Protocol,
188 }
189
190 impl Capabilities {
191 /// Read the capabilities and version advertisement from the given packetline reader.
192 ///
193 /// If [`Protocol::V1`] was requested, or the remote decided to downgrade, the remote refs
194 /// advertisement will also be included in the [`Outcome`].
195 pub fn from_lines_with_version_detection<T: io::Read>(
196 rd: &mut gix_packetline::StreamingPeekableIter<T>,
197 ) -> Result<Outcome<'_>, client::Error> {
198 // NOTE that this is vitally important - it is turned on and stays on for all following requests so
199 // we automatically abort if the server sends an ERR line anywhere.
200 // We are sure this can't clash with binary data when sent due to the way the PACK
201 // format looks like, thus there is no binary blob that could ever look like an ERR line by accident.
202 rd.fail_on_err_lines(true);
203
204 Ok(match rd.peek_line() {
205 Some(line) => {
206 let line = line??.as_text().ok_or(client::Error::ExpectedLine("text"))?;
207 let version = Capabilities::extract_protocol(line)?;
208 match version {
209 Protocol::V0 => unreachable!("already handled in `None` case"),
210 Protocol::V1 => {
211 let (capabilities, delimiter_position) = Capabilities::from_bytes(line.0)?;
212 rd.peek_buffer_replace_and_truncate(delimiter_position, b'\n');
213 Outcome {
214 capabilities,
215 refs: Some(Box::new(rd.as_read())),
216 protocol: Protocol::V1,
217 }
218 }
219 Protocol::V2 => Outcome {
220 capabilities: {
221 let mut rd = rd.as_read();
222 let mut buf = Vec::new();
223 while let Some(line) = rd.read_data_line() {
224 let line = line??;
225 match line.as_bstr() {
226 Some(line) => {
227 buf.push_str(line);
228 if buf.last() != Some(&b'\n') {
229 buf.push(b'\n');
230 }
231 }
232 None => break,
233 }
234 }
235 Capabilities::from_lines(buf.into())?
236 },
237 refs: None,
238 protocol: Protocol::V2,
239 },
240 }
241 }
242 None => Outcome {
243 capabilities: Capabilities::default(),
244 refs: Some(Box::new(rd.as_read())),
245 protocol: Protocol::V0,
246 },
247 })
248 }
249 }
250}
251
252#[cfg(feature = "async-client")]
253#[allow(missing_docs)]
254///
255pub mod recv {
256 use bstr::ByteVec;
257 use futures_io::AsyncRead;
258
259 use crate::{client, client::Capabilities, Protocol};
260
261 /// Success outcome of [`Capabilities::from_lines_with_version_detection`].
262 pub struct Outcome<'a> {
263 /// The [`Capabilities`] the remote advertised.
264 pub capabilities: Capabilities,
265 /// The remote refs as an [`AsyncBufRead`].
266 ///
267 /// This is `Some` only when protocol v1 is used. The [`AsyncBufRead`] must be exhausted by
268 /// the caller.
269 pub refs: Option<Box<dyn crate::client::ReadlineBufRead + Unpin + 'a>>,
270 /// The [`Protocol`] the remote advertised.
271 pub protocol: Protocol,
272 }
273
274 impl Capabilities {
275 /// Read the capabilities and version advertisement from the given packetline reader.
276 ///
277 /// If [`Protocol::V1`] was requested, or the remote decided to downgrade, the remote refs
278 /// advertisement will also be included in the [`Outcome`].
279 pub async fn from_lines_with_version_detection<T: AsyncRead + Unpin>(
280 rd: &mut gix_packetline::StreamingPeekableIter<T>,
281 ) -> Result<Outcome<'_>, client::Error> {
282 // NOTE that this is vitally important - it is turned on and stays on for all following requests so
283 // we automatically abort if the server sends an ERR line anywhere.
284 // We are sure this can't clash with binary data when sent due to the way the PACK
285 // format looks like, thus there is no binary blob that could ever look like an ERR line by accident.
286 rd.fail_on_err_lines(true);
287
288 Ok(match rd.peek_line().await {
289 Some(line) => {
290 let line = line??.as_text().ok_or(client::Error::ExpectedLine("text"))?;
291 let version = Capabilities::extract_protocol(line)?;
292 match version {
293 Protocol::V0 => unreachable!("already handled in `None` case"),
294 Protocol::V1 => {
295 let (capabilities, delimiter_position) = Capabilities::from_bytes(line.0)?;
296 rd.peek_buffer_replace_and_truncate(delimiter_position, b'\n');
297 Outcome {
298 capabilities,
299 refs: Some(Box::new(rd.as_read())),
300 protocol: Protocol::V1,
301 }
302 }
303 Protocol::V2 => Outcome {
304 capabilities: {
305 let mut rd = rd.as_read();
306 let mut buf = Vec::new();
307 while let Some(line) = rd.read_data_line().await {
308 let line = line??;
309 match line.as_bstr() {
310 Some(line) => {
311 buf.push_str(line);
312 if buf.last() != Some(&b'\n') {
313 buf.push(b'\n');
314 }
315 }
316 None => break,
317 }
318 }
319 Capabilities::from_lines(buf.into())?
320 },
321 refs: None,
322 protocol: Protocol::V2,
323 },
324 }
325 }
326 None => Outcome {
327 capabilities: Capabilities::default(),
328 refs: Some(Box::new(rd.as_read())),
329 protocol: Protocol::V0,
330 },
331 })
332 }
333 }
334}