ostrya-remote 0.17.0

The push and pull protocol of ostrya: the wire codec, the push client session, and the ssh and HTTP transports
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! The wire format of the push and the pull, and its codec.
//!
//! Each wire code is the [`Error`] variant of the same name, for example
//! [`Error::Protocol`] for the code `protocol`. [`ErrorCode`] names the
//! codes.
//!
//! `docs/format-reference.md` of the repository specifies the wire format,
//! and this module implements it. The sections below link to each part of the
//! specification.
//!
//! # Frames
//!
//! [Wire conventions] specifies the byte orders. [Frame layout] and
//! [Frame limits] specify the frame and its limits. [Messages] specifies the
//! message kinds with their bodies, and [Error codes] specifies the wire
//! codes.
//!
//! A client writes frames up to the limit that the server announces. It reads
//! frames up to a limit of its own, because the reader allocates each frame
//! body in full.
//!
//! The codec reads and writes every kind. A session refuses a kind of the
//! other protocol with [`Error::Protocol`].
//!
//! # Object stream
//!
//! [Chunks and the abandon marker] specifies the chunks of an object that
//! follow an `ObjectHeader`, and [Object streams] states the rules of the
//! server. [`ABANDON`] is the chunk length of the abandon marker.
//!
//! # One-way stream
//!
//! [The one-way stream] specifies the stream that carries a push in one
//! direction.
//!
//! # Pull
//!
//! [The pull session over a stream] specifies `PullHello`, `Get`, and
//! `ListRefs`. [`PULL_PROTOCOL_VERSION`] is the highest pull version that this
//! crate speaks. A [`GetReply`] answers each `Get` and each `ListRefs`.
//!
//! # Ref listing
//!
//! [The ref listing] specifies the body of a listing. A `ListRefs`
//! ([`ListRefsRequest`]) holds two lists of entries of a
//! [`RefSelector`](ostrya_core::RefSelector), which parses them: the entries
//! of the local refs, and the optional entries of the collection refs.
//! [`MAX_REF_PATTERNS`], [`MAX_REF_ENTRY_PARTS`], [`MAX_REF_ENTRY_LEN`], and
//! [`MAX_REF_LISTING`] are the limits of a request and of a listing.
//!
//! # Codec
//!
//! [`FrameReader`] and [`FrameWriter`] implement the frames, the object
//! stream, and the pull bodies over the `futures-io` traits, so the codec
//! needs no async runtime. Both enter the body state after an `ObjectHeader`
//! and after a `GetReply` whose `found` is `true`. One set of methods reads
//! and writes the bytes of both body kinds.
//!
//! The bytes pass through a buffer of the caller in bounded pieces, so no
//! call holds a whole object. [`ObjectBody`] presents the bytes of one body
//! as an `AsyncRead`.
//!
//! [`ErrorCode`]: crate::ErrorCode
//! [Wire conventions]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#wire-conventions
//! [Frame layout]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#frame-layout
//! [Frame limits]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#frame-limits
//! [Messages]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#messages
//! [Error codes]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#error-codes
//! [Chunks and the abandon marker]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#chunks-and-the-abandon-marker
//! [Object streams]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#object-streams
//! [The one-way stream]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#the-one-way-stream
//! [The pull session over a stream]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#the-pull-session-over-a-stream
//! [The ref listing]: https://github.com/growler/ostrya/blob/main/docs/format-reference.md#the-ref-listing

mod frame;
mod message;

pub use frame::{FrameReader, FrameWriter, ObjectBody, ObjectRead};
pub(crate) use frame::{Step, encode_frame};
pub use message::{
    CommitRequest, ErrorMessage, GetReply, HaveReply, Hello, HelloReply, ListRefsRequest, Message,
    ObjectHeader, ObjectsReply, PullHello, PullHelloReply, ServerCommit,
};
pub(crate) use message::{
    check_server_commit_metadata, server_commit_frame_len, server_commit_reply_frame_len,
};

use ostrya_core::{Checksum, ObjectName, ObjectType};
use ostrya_gvariant::Value;

use crate::error::{Error, Result};

/// The push protocol version that this crate speaks.
pub const PROTOCOL_VERSION: u32 = 1;

/// The highest pull protocol version that this crate speaks.
///
/// It is the version of `PullHello` and `PullHelloReply`. It is separate from
/// [`PROTOCOL_VERSION`], and each version changes on its own.
pub const PULL_PROTOCOL_VERSION: u32 = 1;

/// The lowest frame limit: 1 MiB.
///
/// This value is:
///
/// - the frame limit before the server announces one
/// - the lowest limit that a server can announce
/// - the limit of a stream that carries no announcement
pub const MIN_FRAME_LIMIT: u32 = 1 << 20;

/// The `max-frame` value that an ostrya server announces: 1 MiB.
///
/// Object bytes travel as chunks, so a frame carries only metadata messages.
/// The largest bounded message is a `Have` of [`MAX_HAVE`] entries, with a
/// frame length of 606,209 bytes. `Hello`, `HelloReply`, `Commit`, and the
/// `Error` message hold lists with no count limit. Only the frame limit
/// bounds them.
///
/// A chunk can be as long as the limit, so the framing costs 4 bytes for
/// each MiB of object data.
///
/// # Memory
///
/// The reader allocates a frame body in full. The decoder then keeps the body
/// and the decoded message together. On 1 MiB frames, the measured peak is:
///
/// - 2 times the frame for a `Have`
/// - 12 times the frame for a `HelloReply` of refs with empty names
/// - 13 times the frame for a `Commit` of ref updates with empty names
/// - 1 time the frame for a `Hello` whose one unknown key holds an `ab` of
///   1 MiB, or an array of one-byte values in 8 or 64 levels of one-member
///   tuples
/// - 2 times the frame for a `Commit` whose server commit holds one metadata
///   entry with an `ab` of 1 MiB, or with an array of one-byte values in 8 or
///   64 levels of one-member tuples
///
/// The decoder checks a dict value under an unknown key, and a value under a
/// repeated key, in place with no value tree, and then drops it. The decoder
/// checks each metadata entry of a server commit in place and keeps a copy
/// of its bytes, with no value tree. The peak grows in proportion to the
/// limit.
pub const MAX_FRAME: u32 = 1 << 20;

/// The largest ref listing: 128 MiB.
///
/// A ref listing is the summary of the refs that the ref patterns of one
/// request select. An ostrya server builds a listing for each request. The
/// build refuses a larger listing with [`Error::LimitExceeded`]. Over a
/// stream, the server sends this error as the code `limit-exceeded`. Over
/// HTTP, the server answers with the status 500.
///
/// [`PullSession::fetch_refs`](crate::session::PullSession::fetch_refs) reads
/// a listing of at most this many bytes.
pub const MAX_REF_LISTING: u64 = 128 * 1024 * 1024;

/// The most entries in one request of a ref listing: 256.
///
/// Positive and negative entries count together, and the entries of the
/// local refs and of the collection refs count together. A server refuses a
/// request with more entries. It counts an entry that the request holds two
/// times as two entries. Over HTTP, each `pattern=VALUE` pair and each
/// `collection=VALUE` pair of the query is one entry.
///
/// Before it counts, a client removes each exact duplicate of an entry of the
/// same kind in the same list. The
/// [ref patterns](crate::session::PullSession#ref-patterns) of a pull give
/// the client rules.
pub const MAX_REF_PATTERNS: usize = 256;

/// The most parts in one entry of a ref listing request: 256.
///
/// A part is one element of the entry split at each `/`, the first and the
/// last included. For example, `a` has 1 part, and `a/b` has 2 parts. The
/// leading `!` of a negative entry adds no part.
///
/// A server refuses an entry with more parts before it parses the entry. The
/// cost of a match is the product of the part counts of the pattern and the
/// name, so the limit bounds the work of one entry.
pub const MAX_REF_ENTRY_PARTS: usize = 256;

/// The most bytes in one entry of a ref listing request: 4,096.
///
/// The leading `!` of a negative entry counts as one byte. A server refuses a
/// longer entry before it parses the entry.
pub const MAX_REF_ENTRY_LEN: usize = 4096;

/// The `max-have` value that an ostrya server announces: 16,384 entries.
///
/// A `Have` entry is 33 bytes plus a 4-byte offset. A `Have` of 16,384
/// entries has a frame length of 606,209 bytes, which fits in [`MAX_FRAME`].
/// A `Have` of 32,768 entries does not fit. A full `HaveReply` of 16,384
/// entries is 2,048 bytes.
pub const MAX_HAVE: u32 = 16_384;

/// The chunk length that abandons an object or a pull body.
pub const ABANDON: u32 = 0xFFFF_FFFF;

/// The highest valid frame limit.
///
/// It is one less than [`ABANDON`], so a chunk length that the limit allows
/// never equals the abandon marker.
pub const MAX_FRAME_LIMIT: u32 = 0xFFFF_FFFE;

/// A message kind, the byte after the frame length.
///
/// A later protocol version can add kinds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
#[non_exhaustive]
pub enum Kind {
    /// The `Hello` message, from the client to the server.
    Hello = 1,
    /// The `HelloReply` message, from the server to the client.
    HelloReply = 2,
    /// The `Have` message, from the client to the server.
    Have = 3,
    /// The `HaveReply` message, from the server to the client.
    HaveReply = 4,
    /// The `ObjectHeader` message, from the client to the server.
    ///
    /// The chunks of the object follow it.
    ObjectHeader = 5,
    /// The `ObjectsEnd` message, from the client to the server.
    ///
    /// It closes the object stream.
    ObjectsEnd = 6,
    /// The `ObjectsReply` message, from the server to the client.
    ObjectsReply = 7,
    /// The `Commit` message, from the client to the server.
    Commit = 8,
    /// The `CommitReply` message, from the server to the client.
    CommitReply = 9,
    /// The `Error` message, from the server to the client.
    ///
    /// The push and the pull both use it.
    Error = 10,
    /// The `Abort` message, from the client to the server.
    Abort = 11,
    /// The `PullHello` message, from a pull client to the server.
    PullHello = 12,
    /// The `PullHelloReply` message, from the server to a pull client.
    PullHelloReply = 13,
    /// The `Get` message, from a pull client to the server.
    Get = 14,
    /// The `GetReply` message, from the server to a pull client.
    ///
    /// It answers a `Get` or a `ListRefs`. A body follows a reply whose
    /// `found` is `true`.
    GetReply = 15,
    /// The `ListRefs` message, from a pull client to the server.
    ///
    /// The server answers it with one `GetReply`.
    ListRefs = 16,
}

impl Kind {
    /// Returns the kind of a kind byte.
    ///
    /// # Errors
    ///
    /// - [`Error::Protocol`] if the byte is not a kind.
    pub fn from_u8(byte: u8) -> Result<Kind> {
        Ok(match byte {
            1 => Kind::Hello,
            2 => Kind::HelloReply,
            3 => Kind::Have,
            4 => Kind::HaveReply,
            5 => Kind::ObjectHeader,
            6 => Kind::ObjectsEnd,
            7 => Kind::ObjectsReply,
            8 => Kind::Commit,
            9 => Kind::CommitReply,
            10 => Kind::Error,
            11 => Kind::Abort,
            12 => Kind::PullHello,
            13 => Kind::PullHelloReply,
            14 => Kind::Get,
            15 => Kind::GetReply,
            16 => Kind::ListRefs,
            _ => return Err(protocol(format!("unknown message kind {byte}"))),
        })
    }

    /// Returns the kind byte.
    pub fn as_u8(self) -> u8 {
        self as u8
    }
}

/// The encoding of an object in the object stream.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
    /// The uncompressed object bytes.
    ///
    /// For a content object, the bytes are the framed file header and the
    /// payload.
    Raw,
    /// A content object in the compressed form of an archive repository.
    Deflate,
}

impl Encoding {
    /// Returns the wire byte: 0 for `raw`, 1 for `deflate`.
    pub fn as_u8(self) -> u8 {
        match self {
            Encoding::Raw => 0,
            Encoding::Deflate => 1,
        }
    }

    /// Returns the encoding of a wire byte.
    ///
    /// # Errors
    ///
    /// - [`Error::Protocol`] if the byte is not 0 or 1.
    pub fn from_u8(byte: u8) -> Result<Encoding> {
        match byte {
            0 => Ok(Encoding::Raw),
            1 => Ok(Encoding::Deflate),
            _ => Err(protocol(format!("unknown encoding {byte}"))),
        }
    }

    /// Returns the name in the `encodings` list of `HelloReply`.
    pub fn as_str(self) -> &'static str {
        match self {
            Encoding::Raw => "raw",
            Encoding::Deflate => "deflate",
        }
    }

    /// Returns the encoding of a name, or `None` if the name is not an
    /// encoding.
    pub fn from_name(name: &str) -> Option<Encoding> {
        match name {
            "raw" => Some(Encoding::Raw),
            "deflate" => Some(Encoding::Deflate),
            _ => None,
        }
    }
}

/// The state of a ref as the server reads it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RefState {
    /// The ref name, as the `Hello` holds it.
    pub name: String,
    /// The commit of the ref, or `None` if the ref is absent.
    pub commit: Option<Checksum>,
}

/// The state that a ref update expects the ref to be in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Expected {
    /// The ref is absent (wire state 0).
    Absent,
    /// The ref points at this commit (wire state 1).
    Commit(Checksum),
    /// The ref is in any state (wire state 2).
    Any,
}

/// One ref update of a `Commit` message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RefUpdate {
    /// The ref name, as the `Hello` holds it: `NAME` or `@ID:NAME`, and in a
    /// one-way stream also `REMOTE:NAME`.
    pub name: String,
    /// The state that the ref must be in before the write.
    pub expected: Expected,
    /// The new commit, or `None` to delete the ref.
    pub new: Option<Checksum>,
}

/// The result of one ref update in a `CommitReply` message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RefOutcome {
    /// The ref name, as the client sent it.
    pub name: String,
    /// The commit before the write, or `None` if the ref was absent.
    pub old: Option<Checksum>,
    /// The commit after the write, or `None` if the ref is now absent.
    pub new: Option<Checksum>,
}

pub(crate) fn protocol(msg: impl Into<String>) -> Error {
    Error::Protocol(msg.into())
}

/// Returns a checksum from an `ay` of 32 bytes.
fn checksum(bytes: &[u8]) -> Result<Checksum> {
    Checksum::from_ay(bytes).map_err(|_| protocol("checksum is not 32 bytes"))
}

fn checksum_value(c: &Checksum) -> Value {
    Value::Bytes(c.as_bytes().to_vec())
}

fn maybe_value(c: Option<Checksum>) -> Value {
    Value::Maybe(c.map(|c| Box::new(checksum_value(&c))))
}

/// Returns an object name from a `(y, ay)` pair whose type `allowed`
/// accepts.
fn object_name((byte, sum): (u8, &[u8]), allowed: fn(ObjectType) -> bool) -> Result<ObjectName> {
    let ty = ObjectType::from_u32(u32::from(byte))
        .ok()
        .filter(|t| allowed(*t))
        .ok_or_else(|| protocol(format!("object type {byte} is not allowed here")))?;
    Ok(ObjectName::new(checksum(sum)?, ty))
}

fn object_name_value(name: &ObjectName) -> Value {
    Value::Tuple(vec![
        Value::Byte(name.ty.as_u32() as u8),
        checksum_value(&name.checksum),
    ])
}

/// The length of one `Have` entry in a body of 65,536 bytes or more: the
/// type byte, the 32 bytes of the checksum, and an offset of 4 bytes. A
/// shorter body has offsets of 1 or 2 bytes.
const HAVE_ENTRY_LEN: u32 = 37;

/// Returns the most entries of a `Have` whose frame fits in `limit`.
///
/// The frame length counts the kind byte and the body. Fewer entries make a
/// shorter frame.
pub(crate) fn have_entries_within(limit: u32) -> u32 {
    (limit - 1) / HAVE_ENTRY_LEN
}

/// Returns `true` if a `Have` entry or a `missing` entry can name the type.
///
/// These are the types 1 to 4.
pub(crate) fn have_type(ty: ObjectType) -> bool {
    matches!(
        ty,
        ObjectType::File | ObjectType::DirTree | ObjectType::DirMeta | ObjectType::Commit
    )
}

/// Returns `true` if an `ObjectHeader` can name the type.
///
/// These are the types 1 to 4 and 6.
fn header_type(ty: ObjectType) -> bool {
    have_type(ty) || ty == ObjectType::CommitMeta
}