sark-h2 0.5.0

HTTP/2 protocol primitives + client/server bootstrap for sark stack
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use std::collections::BTreeMap;
use std::future::Future;
use std::marker::PhantomData;
use std::task::{Context, Poll};

use dope::Driver;
use dope::fiber::{Fiber, Slab, TaskId};
use dope::manifold::Outcome;
use dope::manifold::listener::{self, Application};
use dope::transport::link::Slot;
use dope::transport::wire::{Identity, RecvChunk, Wire};
use o3::buffer::Shared;

use crate::conn::{self, Conn, ConnError, Settings};
use crate::frame::{ErrorCode, ParseError};
use crate::hpack::OwnedHeader;
use crate::role::ServerRole;
use crate::stream::StreamId;

pub const HANDLER_SLAB_CAP: usize = 256;
const OUTBOUND_SOFT_CAP: usize = 64 * 1024;

pub struct Request {
    pub headers: Vec<OwnedHeader>,
    pub body: Vec<u8>,
}

pub struct Response {
    pub headers: Vec<OwnedHeader>,
    pub body: Shared,
}

impl Response {
    pub fn new(headers: Vec<OwnedHeader>, body: Shared) -> Self {
        Self { headers, body }
    }
}

pub trait Handler: 'static {
    type Fut<'h>: Future<Output = Response> + 'h
    where
        Self: 'h;

    fn on_request<'h>(&'h self, req: Request) -> Fiber<'h, Self::Fut<'h>>;
}

/// A request being assembled across its HEADERS -> DATA* -> trailers event
/// sequence. Buffered until the terminating end_stream so the handler receives
/// the complete header list and body in one `Request`.
struct Incoming {
    headers: Vec<OwnedHeader>,
    body: Vec<u8>,
}

impl From<Incoming> for Request {
    fn from(inc: Incoming) -> Self {
        Request {
            headers: inc.headers,
            body: inc.body,
        }
    }
}

struct PendingBody {
    stream_id: StreamId,
    body: Shared,
    off: usize,
    stalled: bool,
}

impl PendingBody {
    fn pump(&mut self, conn: &mut Conn<ServerRole>) -> Result<bool, ConnError> {
        loop {
            if self.off >= self.body.len() {
                return Ok(true);
            }
            if conn.outbound().len() >= OUTBOUND_SOFT_CAP {
                self.stalled = false;
                return Ok(false);
            }
            let rest = &self.body.as_slice()[self.off..];
            let n = conn.send_data(self.stream_id, rest, true)?;
            if n == 0 {
                self.stalled = true;
                return Ok(false);
            }
            self.off += n;
        }
    }
}

pub struct ConnState {
    pub conn: Conn<ServerRole>,
    incoming: BTreeMap<StreamId, Incoming>,
    pending: Vec<PendingBody>,
    waiting: Vec<(StreamId, TaskId)>,
}

impl Default for ConnState {
    fn default() -> Self {
        Self {
            conn: Conn::<ServerRole>::with_local_settings(Settings {
                max_concurrent_streams: Some(256),
                ..Settings::DEFAULT
            }),
            incoming: BTreeMap::new(),
            pending: Vec::new(),
            waiting: Vec::new(),
        }
    }
}

pub struct App<'h, H: Handler, W: Wire = Identity> {
    user: &'h H,
    slab: Slab<'h, H::Fut<'h>, HANDLER_SLAB_CAP>,
    _wire: PhantomData<W>,
}

impl<'h, H: Handler, W: Wire> App<'h, H, W> {
    pub fn new(user: &'h H) -> Self {
        Self {
            user,
            slab: Slab::new(),
            _wire: PhantomData,
        }
    }

    pub fn handler(&self) -> &H {
        self.user
    }
}

impl<'h, H: Handler, W: Wire> Application for App<'h, H, W> {
    type Conn = ConnState;
    type Wire = W;

    fn on_chunk(
        &mut self,
        slot: &mut Slot<W, listener::State<ConnState>>,
        chunk: RecvChunk<'_>,
        aux: &mut listener::Aux,
        driver: &mut Driver,
    ) -> Outcome {
        let bytes = chunk.as_slice();
        let state = &mut slot.state.conn;
        if state.conn.goaway_sent() || state.conn.goaway_received().is_some() {
            return Outcome::Ok;
        }
        if let Err(e) = state.conn.ingest(bytes) {
            let code = Self::map_error(&e);
            state.conn.goaway(code, b"");
            Self::flush_into(slot, aux, driver, true);
            return Outcome::Ok;
        }
        self.drain_events(slot, driver);
        let close_after = slot.state.conn.conn.goaway_sent();
        Self::flush_into(slot, aux, driver, close_after);
        Outcome::Ok
    }

    fn on_send(
        &mut self,
        slot: &mut Slot<W, listener::State<ConnState>>,
        _sent: usize,
        aux: &mut listener::Aux,
        driver: &mut Driver,
    ) {
        Self::pump_pending(&mut slot.state.conn);
        let close_after = slot.state.conn.conn.goaway_sent();
        if !slot.state.conn.conn.outbound().is_empty() {
            Self::flush_into(slot, aux, driver, close_after);
        }
    }

    fn on_wake(
        &mut self,
        slot: &mut Slot<W, listener::State<ConnState>>,
        aux: &mut listener::Aux,
        driver: &mut Driver,
    ) {
        if slot.state.conn.waiting.is_empty() {
            return;
        }
        let waker = slot.make_waker(driver);
        let mut cx = Context::from_waker(&waker);
        let conn_ptr: *mut ConnState = &mut slot.state.conn;
        // SAFETY: conn_ptr aliases slot.state.conn; the poll loop below touches only state.conn / state.waiting / self.slab through conn_ptr and never re-borrows slot.state.conn via slot (slot is used only for make_waker, already dropped), so &mut ConnState and &mut Slot stay disjoint.
        let cstate: &mut ConnState = unsafe { &mut *conn_ptr };
        let mut i = 0;
        while i < cstate.waiting.len() {
            let (stream_id, ref task) = cstate.waiting[i];
            match self.slab.poll(task, &mut cx) {
                Poll::Ready(resp) => {
                    let (_, task) = cstate.waiting.swap_remove(i);
                    self.slab.release(task);
                    Self::begin_response(cstate, stream_id, resp);
                }
                Poll::Pending => i += 1,
            }
        }
        let close_after = cstate.conn.goaway_sent();
        if !cstate.conn.outbound().is_empty() {
            Self::flush_into(slot, aux, driver, close_after);
        }
    }

    fn on_close(
        &mut self,
        slot: &mut Slot<W, listener::State<ConnState>>,
        _aux: &mut listener::Aux,
    ) {
        for (_, task) in slot.state.conn.waiting.drain(..) {
            self.slab.release(task);
        }
        slot.state.conn.pending.clear();
        slot.state.conn.incoming.clear();
    }
}

impl<'h, H: Handler, W: Wire> App<'h, H, W> {
    fn drain_events(
        &mut self,
        slot: &mut Slot<W, listener::State<ConnState>>,
        driver: &mut Driver,
    ) {
        let conn_ptr: *mut ConnState = &mut slot.state.conn;
        // SAFETY: conn_ptr aliases slot.state.conn; this fn touches slot only via make_waker (which borrows the parker, not state) before re-deriving the waker, and otherwise drives state.conn / self.slab through conn_ptr, keeping &mut ConnState disjoint from any &mut Slot use.
        let cstate: &mut ConnState = unsafe { &mut *conn_ptr };
        let waker = slot.make_waker(driver);
        let mut cx = Context::from_waker(&waker);
        while let Some(ev) = cstate.conn.poll_event() {
            let release = ev.release_hint();
            match ev {
                conn::Event::Headers {
                    stream_id,
                    headers,
                    end_stream,
                    trailing,
                } => {
                    if trailing {
                        // Trailers terminate a bodied request: flush whatever was
                        // buffered, appending the trailer fields to the head list.
                        if let Some(mut inc) = cstate.incoming.remove(&stream_id) {
                            inc.headers.extend(headers);
                            self.dispatch(cstate, stream_id, inc.into(), &mut cx);
                        }
                    } else if end_stream {
                        let req = Request {
                            headers,
                            body: Vec::new(),
                        };
                        self.dispatch(cstate, stream_id, req, &mut cx);
                    } else {
                        cstate.incoming.insert(
                            stream_id,
                            Incoming {
                                headers,
                                body: Vec::new(),
                            },
                        );
                    }
                }
                conn::Event::Data {
                    stream_id,
                    data,
                    end_stream,
                } => {
                    if end_stream {
                        if let Some(mut inc) = cstate.incoming.remove(&stream_id) {
                            inc.body.extend_from_slice(&data);
                            self.dispatch(cstate, stream_id, inc.into(), &mut cx);
                        }
                    } else if let Some(inc) = cstate.incoming.get_mut(&stream_id) {
                        inc.body.extend_from_slice(&data);
                    }
                }
                conn::Event::StreamReset { stream_id, .. } => {
                    cstate.incoming.remove(&stream_id);
                    cstate.pending.retain(|p| p.stream_id != stream_id);
                    if let Some(pos) = cstate.waiting.iter().position(|(s, _)| *s == stream_id) {
                        let (_, task) = cstate.waiting.swap_remove(pos);
                        self.slab.release(task);
                    }
                }
                _ => {}
            }
            if let Some((stream_id, n)) = release {
                let _ = cstate.conn.release_capacity(stream_id, n);
            }
        }
        // Window updates in this batch are coalesced into one resume pass.
        Self::resume_pending(cstate);
    }

    fn dispatch(
        &mut self,
        cstate: &mut ConnState,
        stream_id: StreamId,
        req: Request,
        cx: &mut Context<'_>,
    ) {
        let fiber = self.user.on_request(req);
        match self.slab.alloc(fiber) {
            Some(task) => match self.slab.poll(&task, cx) {
                Poll::Ready(resp) => {
                    self.slab.release(task);
                    Self::begin_response(cstate, stream_id, resp);
                }
                Poll::Pending => cstate.waiting.push((stream_id, task)),
            },
            None => {
                let _ = cstate
                    .conn
                    .reset_stream(stream_id, ErrorCode::RefusedStream);
            }
        }
    }

    fn begin_response(cstate: &mut ConnState, stream_id: StreamId, resp: Response) {
        if !cstate.conn.has_stream(stream_id) {
            return;
        }
        let end_stream = resp.body.is_empty();
        if cstate
            .conn
            .send_response(
                stream_id,
                resp.headers.iter().map(|h| h.as_ref()),
                end_stream,
            )
            .is_err()
        {
            return;
        }
        if end_stream {
            return;
        }
        let mut body = PendingBody {
            stream_id,
            body: resp.body,
            off: 0,
            stalled: false,
        };
        match body.pump(&mut cstate.conn) {
            Ok(true) => {}
            Ok(false) | Err(_) => cstate.pending.push(body),
        }
    }

    fn resume_pending(cstate: &mut ConnState) {
        if !cstate.conn.take_window_opened() {
            return;
        }
        for body in cstate.pending.iter_mut() {
            body.stalled = false;
        }
        Self::pump_pending(cstate);
    }

    fn pump_pending(cstate: &mut ConnState) {
        let ConnState { conn, pending, .. } = cstate;
        let mut i = 0;
        while i < pending.len() {
            if pending[i].stalled || conn.outbound().len() >= OUTBOUND_SOFT_CAP {
                i += 1;
                continue;
            }
            match pending[i].pump(conn) {
                Ok(true) | Err(_) => {
                    pending.swap_remove(i);
                }
                Ok(false) => i += 1,
            }
        }
    }

    fn flush_into(
        slot: &mut Slot<W, listener::State<ConnState>>,
        aux: &mut listener::Aux,
        driver: &mut Driver,
        close_after: bool,
    ) {
        let send_ud = slot.token();
        let write_buf = aux.write_buf_for(slot);
        let state = &mut slot.state.conn;
        let out = state.conn.outbound();
        let n = out.len().min(write_buf.len());
        write_buf[..n].copy_from_slice(&out[..n]);
        state.conn.drain_outbound(n);
        if close_after {
            slot.core.set_close_after();
        }
        slot.submit_buffered(write_buf, n, send_ud, driver);
    }

    fn map_error(e: &ConnError) -> ErrorCode {
        match e {
            ConnError::BadPreface
            | ConnError::Protocol
            | ConnError::BadStream
            | ConnError::Continuation
            | ConnError::BadSettings
            | ConnError::StreamGoneAway => ErrorCode::ProtocolError,
            ConnError::StreamClosed => ErrorCode::StreamClosed,
            ConnError::ParseError(ParseError::FrameSize)
            | ConnError::ParseError(ParseError::BadLength) => ErrorCode::FrameSize,
            ConnError::ParseError(_) => ErrorCode::ProtocolError,
            ConnError::FlowControl => ErrorCode::FlowControl,
            ConnError::FrameSize => ErrorCode::FrameSize,
            ConnError::Hpack(_) => ErrorCode::Compression,
            ConnError::GoAwayReceived(c) => *c,
            ConnError::StreamLimit => ErrorCode::RefusedStream,
            ConnError::HeaderListTooLarge | ConnError::Overload => ErrorCode::EnhanceYourCalm,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::conn::CLIENT_PREFACE;
    use crate::frame::{self, FrameHeader, WindowUpdate};
    use crate::hpack::{Encoder, Header};

    type TestApp = App<'static, NoopHandler>;

    struct NoopHandler;

    impl Handler for NoopHandler {
        type Fut<'h> = std::future::Ready<Response>;

        fn on_request<'h>(&'h self, _req: Request) -> Fiber<'h, Self::Fut<'h>> {
            Fiber::new(std::future::ready(Response::new(
                Vec::new(),
                Shared::from(Vec::new()),
            )))
        }
    }

    fn open_stream(cstate: &mut ConnState, stream_id: StreamId) {
        cstate.conn.drain_outbound(cstate.conn.outbound().len());
        cstate.conn.ingest(CLIENT_PREFACE).unwrap();
        let mut enc = Encoder::new(4096);
        let mut block = Vec::new();
        enc.encode(
            [
                Header {
                    name: b":method",
                    value: b"GET",
                },
                Header {
                    name: b":scheme",
                    value: b"http",
                },
                Header {
                    name: b":path",
                    value: b"/",
                },
                Header {
                    name: b":authority",
                    value: b"x",
                },
            ],
            &mut block,
        );
        let mut frame = Vec::new();
        crate::frame::Headers {
            stream_id,
            end_stream: true,
            end_headers: true,
            priority: None,
            block_fragment: &block,
        }
        .encode(&mut frame);
        cstate.conn.ingest(&frame).unwrap();
        while cstate.conn.poll_event().is_some() {}
        cstate.conn.drain_outbound(cstate.conn.outbound().len());
    }

    fn ingest_window_update(cstate: &mut ConnState, stream_id: StreamId, increment: u32) {
        let mut bytes = Vec::new();
        WindowUpdate {
            stream_id,
            increment,
        }
        .encode(&mut bytes);
        cstate.conn.ingest(&bytes).unwrap();
        while cstate.conn.poll_event().is_some() {}
        TestApp::resume_pending(cstate);
    }

    fn collect_data(out: &[u8]) -> (Vec<u8>, bool) {
        let mut pos = 0;
        let mut body = Vec::new();
        let mut saw_end = false;
        while pos < out.len() {
            let h = FrameHeader::parse(&out[pos..]).unwrap();
            let total = 9 + h.length as usize;
            if h.kind == frame::Type::Data {
                assert!(!saw_end, "DATA after END_STREAM");
                let payload = &out[pos + 9..pos + total];
                body.extend_from_slice(payload);
                if h.flags.has(frame::Flags::END_STREAM) {
                    saw_end = true;
                }
            }
            pos += total;
        }
        (body, saw_end)
    }

    fn drain_data(cstate: &mut ConnState, acc: &mut Vec<u8>, end: &mut bool) {
        let out = cstate.conn.outbound().to_vec();
        let (body, saw_end) = collect_data(&out);
        acc.extend_from_slice(&body);
        *end |= saw_end;
        cstate.conn.drain_outbound(cstate.conn.outbound().len());
    }

    #[test]
    fn body_larger_than_window_delivered_across_stream_and_conn_updates() {
        let mut cstate = ConnState::default();
        let stream_id = StreamId(1);
        open_stream(&mut cstate, stream_id);

        let total = 200_000usize;
        let payload: Vec<u8> = (0..total).map(|i| (i % 251) as u8).collect();
        let resp = Response::new(
            vec![OwnedHeader::new(b":status", b"200")],
            Shared::from(payload.clone()),
        );
        TestApp::begin_response(&mut cstate, stream_id, resp);

        let mut acc = Vec::new();
        let mut end = false;
        drain_data(&mut cstate, &mut acc, &mut end);

        assert!(acc.len() <= 65_535, "stalled at the initial send window");
        assert!(!end, "END_STREAM must not be set before the true end");

        loop {
            if end {
                break;
            }
            ingest_window_update(&mut cstate, StreamId::CONNECTION, 50_000);
            ingest_window_update(&mut cstate, stream_id, 50_000);
            drain_data(&mut cstate, &mut acc, &mut end);
        }

        assert_eq!(acc, payload, "all body bytes delivered intact and in order");
        assert!(end, "END_STREAM seen at the true end");
    }

    #[test]
    fn awaiting_handler_suspends_then_pump_after_ready() {
        use std::cell::Cell;
        use std::pin::Pin;
        use std::task::Poll;

        struct OnceReady {
            polled: Cell<bool>,
            out: Cell<Option<Response>>,
        }

        impl Future for OnceReady {
            type Output = Response;
            fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Response> {
                if self.polled.get() {
                    Poll::Ready(self.out.take().unwrap())
                } else {
                    self.polled.set(true);
                    cx.waker().wake_by_ref();
                    Poll::Pending
                }
            }
        }

        let mut cstate = ConnState::default();
        let stream_id = StreamId(1);
        open_stream(&mut cstate, stream_id);

        let resp = Response::new(
            vec![OwnedHeader::new(b":status", b"200")],
            Shared::from(b"deferred".to_vec()),
        );
        let fut = OnceReady {
            polled: Cell::new(false),
            out: Cell::new(Some(resp)),
        };
        let mut slab: Slab<'static, OnceReady, HANDLER_SLAB_CAP> = Slab::new();
        let waker = std::task::Waker::noop();
        let mut cx = Context::from_waker(waker);

        let task = slab.alloc(Fiber::new(fut)).unwrap();
        assert!(matches!(slab.poll(&task, &mut cx), Poll::Pending));
        cstate.waiting.push((stream_id, task));

        let (_, task) = cstate.waiting.pop().unwrap();
        let resp = match slab.poll(&task, &mut cx) {
            Poll::Ready(r) => r,
            Poll::Pending => panic!("expected ready on second poll"),
        };
        slab.release(task);
        TestApp::begin_response(&mut cstate, stream_id, resp);

        let mut acc = Vec::new();
        let mut end = false;
        drain_data(&mut cstate, &mut acc, &mut end);
        assert_eq!(acc, b"deferred");
        assert!(end);
    }

    #[test]
    fn sync_handler_completes_inline_without_suspension() {
        let mut cstate = ConnState::default();
        let mut app: TestApp = App::new(&NoopHandler);
        let stream_id = StreamId(1);
        open_stream(&mut cstate, stream_id);

        let body = Shared::from(b"small body".to_vec());
        let resp = Response::new(vec![OwnedHeader::new(b":status", b"200")], body);
        TestApp::begin_response(&mut cstate, stream_id, resp);

        assert!(
            cstate.waiting.is_empty(),
            "a non-awaiting handler retains nothing"
        );
        assert!(
            cstate.pending.is_empty(),
            "a small body fits the window and leaves no pending pump"
        );
        let _ = &mut app;

        let mut acc = Vec::new();
        let mut end = false;
        drain_data(&mut cstate, &mut acc, &mut end);
        assert_eq!(acc, b"small body");
        assert!(end);
    }

    #[test]
    fn connection_level_update_alone_resumes() {
        let mut cstate = ConnState::default();
        let s1 = StreamId(1);
        open_stream(&mut cstate, s1);

        let total = 120_000usize;
        let payload = vec![7u8; total];
        let resp = Response::new(
            vec![OwnedHeader::new(b":status", b"200")],
            Shared::from(payload.clone()),
        );
        TestApp::begin_response(&mut cstate, s1, resp);

        let mut acc = Vec::new();
        let mut end = false;
        drain_data(&mut cstate, &mut acc, &mut end);
        let first = acc.len();
        assert!(first <= 65_535);
        assert!(!end);

        ingest_window_update(&mut cstate, s1, 200_000);
        drain_data(&mut cstate, &mut acc, &mut end);
        assert_eq!(
            acc.len(),
            first,
            "stream update alone cannot exceed the conn window"
        );
        assert!(!end);

        ingest_window_update(&mut cstate, StreamId::CONNECTION, 200_000);
        drain_data(&mut cstate, &mut acc, &mut end);
        assert_eq!(acc, payload);
        assert!(end);
    }
}