sark-h2 0.28.1

Fiber-free HTTP/2 protocol primitives and manifold server for sark
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
use std::{iter, ops};

use active::{Active, Source};
use decode::mode::Discard;
use o3::buffer::{pool, write::consume::Consume as _};
use sark_core::{
    life::session,
    manifold::stream::{Boundary, Section},
};
use sark_http::{
    self,
    field::metadata::{
        block,
        section::{rules, state::Selected, storage},
    },
    payload::compress::dynamic::{mode, plan},
};
use sink::output;
use state::{Mode, State};

use crate::{
    conn,
    conn::{
        build, event,
        ingress::{
            Decode, events, header,
            header::sink,
            input::{self, Source as _},
        },
        registry,
    },
    frame::numbers,
    hpack::{decode, decode::phase::Phase},
    stream,
};

mod active;
mod fragment;
mod state;

pub(crate) use fragment::Fragment;

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Action {
    Deliver,
    Reset(numbers::ErrorCode),
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Kind {
    Headers {
        boundary: Boundary,
        section: Section,
        action: Action,
    },
    PushPromise {
        promised: stream::Nonzero,
        associated: registry::Association,
    },
}

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct Continuation(u32);

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct Pending {
    pub(crate) stream_id: stream::Nonzero,
    pub(crate) kind: Kind,
    pub(crate) continuations: Continuation,
}

pub(crate) struct Blocks<P: rules::Policy> {
    header_pool: pool::Growing,
    header_slots: usize,
    decoder: State<P>,
    header_cap: usize,
    connect: block::Connect,
}

impl Continuation {
    const LIMIT: u32 = 64;
    pub(crate) const ZERO: Self = Self(0);

    pub(crate) fn next(self) -> Result<Self, conn::Error> {
        let actual = self.0 + 1;
        if actual > Self::LIMIT {
            return Err(conn::Error::Continuation(conn::Continuation::Limit {
                actual,
                limit: Self::LIMIT,
            }));
        }
        Ok(Self(actual))
    }
}

impl<P: rules::Policy> Blocks<P> {
    pub(crate) fn acknowledge(&mut self, maximum: usize) -> Result<(), conn::Error> {
        self.decoder.map_idle(|mut decoder, scratch| {
            decoder.set_max_size(maximum);
            (Mode::Idle(decoder, scratch), Ok(()))
        })
    }

    pub(crate) fn try_new(
        plan: pool::Layout,
        table: plan::Plan<mode::Staged>,
        cap: usize,
        connect: block::Connect,
    ) -> Result<Self, build::Ingress> {
        let plan = storage::try_layout::<P::Block>(plan, cap)
            .map_err(|(_, error)| build::Ingress::HeaderLayout(error))?;
        let mut dec = decode::Decoder::try_from(table).map_err(build::Ingress::Decoder)?;
        dec.set_max_list(Some(cap));
        Ok(Self {
            header_pool: pool::Growing::try_new(plan).map_err(build::Ingress::Pool)?,
            header_slots: plan.slots() as usize,
            decoder: State::new(dec),
            header_cap: cap,
            connect,
        })
    }

    fn admit(used: usize, incoming: usize, limit: usize) -> Result<usize, conn::Error> {
        if used > limit || incoming > limit - used {
            return Err(conn::Error::HeaderList {
                used,
                incoming,
                limit,
            });
        }
        Ok(used + incoming)
    }

    pub(crate) fn begin<C: session::Storage, A: rules::Push>(
        &mut self,
        kind: Option<rules::Kind>,
        head: Pending,
        input: &mut header::Input<'_, '_, C>,
        part: Fragment,
    ) -> Result<(), conn::Error> {
        self.start_mode(kind, head)?;
        let bytes = input.get(part.range())?;
        self.feed_parts::<A>(bytes.len(), bytes.fragments())?;
        input.consume(part.total)
    }

    fn start_mode(&mut self, kind: Option<rules::Kind>, head: Pending) -> Result<(), conn::Error> {
        let pool = &mut self.header_pool;
        let slots = self.header_slots;
        let connect = self.connect;
        self.decoder.map_idle(|decoder, scratch| match kind {
            Some(kind) => {
                let lease = match pool.try_acquire() {
                    Ok(Some(lease)) => lease,
                    Ok(None) => {
                        return (
                            Mode::Idle(decoder, scratch),
                            Err(conn::Error::HeaderDecodeFull {
                                used: slots,
                                limit: slots,
                            }),
                        );
                    }
                    Err(cause) => {
                        return (Mode::Idle(decoder, scratch), Err(cause.into()));
                    }
                };
                let sink = match sink::Header::try_new(lease, kind, connect, scratch) {
                    Ok(sink) => sink,
                    Err((scratch, cause)) => {
                        return (
                            Mode::Idle(decoder, scratch),
                            Err(conn::Error::Header(cause)),
                        );
                    }
                };
                let budget = decoder.budget();
                let block = Phase::<Decode>::Representation(decoder);
                (
                    Mode::Active(Active {
                        pending: head,
                        source: Source::Store {
                            block,
                            budget,
                            sink,
                        },
                        wire_len: 0,
                    }),
                    Ok(()),
                )
            }
            None => {
                let budget = decoder.budget();
                (
                    Mode::Active(Active {
                        pending: head,
                        source: Source::Discard(
                            Phase::<Discard>::Representation(decoder),
                            budget,
                            scratch,
                        ),
                        wire_len: 0,
                    }),
                    Ok(()),
                )
            }
        })
    }

    pub(crate) fn discard<C: session::Storage>(
        &mut self,
        input: &mut header::Input<'_, '_, C>,
        part: Fragment,
    ) -> Result<(), conn::Error> {
        let total = part.total;
        let bytes = input.get(part.range())?;
        Self::admit(0, bytes.len(), self.header_cap)?;
        self.decoder.map_idle(|decoder, scratch| {
            let (first, rest) = bytes.split();
            let decoded = match rest {
                None => decoder.discard(first),
                Some(rest) => {
                    let mut budget = decoder.budget();
                    let block = Phase::<Discard>::Representation(decoder);
                    match block.feed_all(&mut budget, iter::once(first).chain(rest), ()) {
                        Ok((block, ())) => block.finish(),
                        Err(((), cause)) => Err(cause),
                    }
                }
            };
            match decoded {
                Ok(decoder) => (Mode::Idle(decoder, scratch), Ok(())),
                Err(cause) => (Mode::Failed, Err(conn::Error::Hpack(cause))),
            }
        })?;
        input.consume(total)
    }

    pub(crate) fn prepare<'a, C: session::Storage, A: rules::Push, E: event::Evidence>(
        &mut self,
        input: &'a mut header::Input<'_, '_, C>,
        events: &'a mut events::Events<Selected<P>, P::Block, C, E>,
        part: Fragment,
    ) -> Result<header::Prepared<'a, P, C, E>, conn::Error> {
        let total = part.total;
        let event = events.vacant()?;
        let out = self.decode_all::<C, A>(input, part)?;
        let frame = input.prepare(total)?;
        Ok(header::Prepared::new(
            frame,
            event,
            out.head,
            out.validation,
        ))
    }

    pub(crate) fn feed_range<C: session::Storage, A: rules::Push>(
        &mut self,
        input: &input::FrameInput<C>,
        range: ops::Range<usize>,
    ) -> Result<(), conn::Error> {
        let len = range.len();
        let offset = range.start;
        let bytes = input.bytes.get(range).ok_or(conn::Error::InputRange {
            offset,
            need: len,
            have: input.bytes.len(),
        })?;
        self.feed_parts::<A>(len, bytes.fragments())
    }

    fn feed_parts<'a, A: rules::Push>(
        &mut self,
        len: usize,
        parts: impl IntoIterator<Item = &'a [u8]>,
    ) -> Result<(), conn::Error> {
        let limit = self.header_cap;
        self.decoder.map_active(|active| {
            let wire_len = match Self::admit(active.wire_len, len, limit) {
                Ok(wire_len) => wire_len,
                Err(error) => return (Mode::Active(active), Err(error)),
            };
            match active.decode::<A, _>(parts, wire_len) {
                Ok(active) => (Mode::Active(active), Ok(())),
                Err(cause) => (Mode::Failed, Err(cause.into())),
            }
        })
    }

    /// Feeds one complete non-final CONTINUATION payload directly from its
    /// receive owner. Such a fragment cannot publish an event, so it needs no
    /// retained-input node once HPACK has consumed it synchronously.
    pub(crate) fn feed_direct<A: rules::Push>(&mut self, part: &[u8]) -> Result<(), conn::Error> {
        self.feed_parts::<A>(part.len(), [part])
    }

    pub(crate) fn resume<'a, C: session::Storage, A: rules::Push, E: event::Evidence>(
        &mut self,
        input: &'a mut input::FrameInput<C>,
        events: &'a mut events::Events<Selected<P>, P::Block, C, E>,
        range: ops::Range<usize>,
        total: usize,
    ) -> Result<header::Prepared<'a, P, C, E>, conn::Error> {
        let event = events.vacant()?;
        self.feed_range::<C, A>(input, range)?;
        let out = self.decoder.map_active(|active| match active.source {
            Source::Store {
                block,
                budget,
                sink,
            } => match sink.finish_block(block, budget) {
                Ok((decoder, scratch, decoded)) => (Mode::Idle(decoder, scratch), Ok(decoded)),
                Err(cause) => (Mode::Failed, Err(cause)),
            },
            Source::Discard(block, _, scratch) => match block.finish() {
                Ok(decoder) => (
                    Mode::Idle(decoder, scratch),
                    Err(conn::Error::HeaderState(conn::HeaderState {
                        expected: conn::HeaderPhase::Store,
                        actual: conn::HeaderPhase::Discard,
                    })),
                ),
                Err(cause) => (Mode::Failed, Err(conn::Error::Hpack(cause))),
            },
        })?;
        let frame = input.prepare(total)?;
        Ok(header::Prepared::new(
            frame,
            event,
            out.head,
            out.validation,
        ))
    }

    pub(crate) fn finish_drop<C: session::Storage, A: rules::Push>(
        &mut self,
        input: &mut input::FrameInput<C>,
        range: ops::Range<usize>,
        total: usize,
    ) -> Result<(), conn::Error> {
        self.feed_range::<C, A>(input, range)?;
        self.decoder.map_active(|active| match active.source {
            Source::Discard(block, _, scratch) => match block.finish() {
                Ok(decoder) => (Mode::Idle(decoder, scratch), Ok(())),
                Err(cause) => (Mode::Failed, Err(conn::Error::Hpack(cause))),
            },
            Source::Store {
                block,
                budget: _,
                sink,
            } => {
                let done = block.finish();
                let scratch = sink.recycle();
                match done {
                    Ok(decoder) => (
                        Mode::Idle(decoder, scratch),
                        Err(conn::Error::HeaderState(conn::HeaderState {
                            expected: conn::HeaderPhase::Discard,
                            actual: conn::HeaderPhase::Store,
                        })),
                    ),
                    Err(cause) => (Mode::Failed, Err(conn::Error::Hpack(cause))),
                }
            }
        })?;
        input
            .bytes
            .try_consume(total)
            .map_err(conn::Error::Consume)?;
        Ok(())
    }

    pub(crate) fn pending(&self) -> Option<Pending> {
        self.decoder.active().map(|active| active.pending)
    }

    pub(crate) fn try_pending(&self) -> Result<Pending, conn::Error> {
        Ok(self.decoder.try_active()?.pending)
    }

    pub(crate) fn try_head_mut(&mut self) -> Result<&mut Pending, conn::Error> {
        Ok(&mut self.decoder.try_mut()?.pending)
    }

    fn decode_all<C: session::Storage, A: rules::Push>(
        &mut self,
        input: &header::Input<'_, '_, C>,
        part: Fragment,
    ) -> Result<output::Decoded<P>, conn::Error> {
        Self::admit(0, part.len, self.header_cap)?;
        let bytes = input.get(part.range())?;
        let lease = self
            .header_pool
            .try_acquire()?
            .ok_or(conn::Error::HeaderDecodeFull {
                used: self.header_slots,
                limit: self.header_slots,
            })?;
        let connect = self.connect;
        self.decoder.map_idle(|decoder, scratch| {
            let sink = match sink::Header::try_new(lease, part.kind, connect, scratch) {
                Ok(sink) => sink,
                Err((scratch, cause)) => {
                    return (
                        Mode::Idle(decoder, scratch),
                        Err(conn::Error::Header(cause)),
                    );
                }
            };
            let (first, rest) = bytes.split();
            let Some(rest) = rest else {
                return match decoder.decode_to::<Decode, _>(first, sink.hpack::<A>()) {
                    Ok((decoder, admit, sink)) => match sink.into_header().finish(admit) {
                        Ok((scratch, decoded)) => (Mode::Idle(decoder, scratch), Ok(decoded)),
                        Err(cause) => (Mode::Failed, Err(cause)),
                    },
                    Err((sink, cause)) => {
                        drop(sink.into_header().recycle());
                        (Mode::Failed, Err(cause.into()))
                    }
                };
            };
            let mut budget = decoder.budget();
            let block = Phase::<Decode>::Representation(decoder);
            let (block, sink) = match block.feed_all(
                &mut budget,
                iter::once(first).chain(rest),
                sink.hpack::<A>(),
            ) {
                Ok((block, sink)) => (block, sink.into_header()),
                Err((sink, cause)) => {
                    drop(sink.into_header().recycle());
                    return (Mode::Failed, Err(cause.into()));
                }
            };
            match sink.finish_block(block, budget) {
                Ok((decoder, scratch, decoded)) => (Mode::Idle(decoder, scratch), Ok(decoded)),
                Err(cause) => (Mode::Failed, Err(cause)),
            }
        })
    }
}

const _: () = {
    assert!(core::mem::size_of::<Kind>() == 8);
    assert!(core::mem::size_of::<Pending>() == 16);
};