h3wire 0.1.0

Sans-I/O HTTP/3 (RFC 9114) and QPACK (RFC 9204) engine
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
//! Per-stream send path: uni stream binding, the core-owned byte queues, HEADERS,
//! scatter/gather DATA, and the send phases ending in exactly one FIN.

use super::shutdown::encode_goaway;
use super::{Connection, Role};
use crate::error::UsageError;
use crate::event::{Action, DataFrame};
use crate::frame::{HEADERS, data_prefix, encode_header, grease_frame_type};
use crate::headers::{FieldRef, HeadersKind, ValidateCtx, validate_outgoing};
use crate::qpack::encoder::encode_field_section;
use crate::settings::encode_local;
use crate::stream::{
    InFlight, RecvPhase, SendPhase, SendState, Stream, StreamId, TunnelState, UniKind,
};
use crate::varint;

impl Connection {
    /// Bind a stream opened for `Action::OpenUni(kind)`; queues its stream type
    /// (and, for the control stream, SETTINGS plus an optional GREASE frame).
    ///
    /// `Err(WrongStreamKind)` unless `stream` is a uni stream opened by this endpoint;
    /// `Err(WrongPhase)` if `kind` is already bound or `stream` is in use.
    pub fn bind_uni(&mut self, kind: UniKind, stream: StreamId) -> Result<(), UsageError> {
        self.check_open().map_err(UsageError::Closed)?;
        if !stream.is_uni() || !self.is_local(stream) {
            return Err(UsageError::WrongStreamKind);
        }
        if self.local_uni[kind as usize].is_some() || self.streams.contains_key(&stream) {
            return Err(UsageError::WrongPhase);
        }
        let ty = match kind {
            UniKind::Control => 0x00,
            UniKind::QpackEncoder => 0x02,
            UniKind::QpackDecoder => 0x03,
        };
        let mut queue = Vec::new();
        varint::encode(ty, &mut queue);
        if kind == UniKind::Control {
            encode_local(&self.config, self.grease_seed, &mut queue);
            self.settings_left = queue.len();
            if self.config.grease {
                encode_header(grease_frame_type(self.grease_seed), 0, &mut queue);
            }
            if let Some(id) = self.goaway_sent {
                encode_goaway(id, &mut queue);
            }
        }
        self.local_uni[kind as usize] = Some(stream);
        let send = SendState {
            queue,
            ..SendState::default()
        };
        self.streams.insert(
            stream,
            Stream {
                send,
                ..Stream::default()
            },
        );
        Ok(())
    }

    /// Streams with core-owned bytes pending (HEADERS, control and QPACK stream bytes) and
    /// no DATA frame in flight, ascending id.
    ///
    /// Collect the ids before writing (`sent` changes the set), then write each stream
    /// with [`poll_send`](Connection::poll_send) / [`sent`](Connection::sent) until the
    /// transport accepts less than offered: one stream blocked by flow control never
    /// stalls the others.
    pub fn sendable(&self) -> impl Iterator<Item = StreamId> + '_ {
        let open = self.check_open().is_ok();
        self.streams
            .iter()
            .filter(move |(_, st)| open && !st.send.pending().is_empty())
            .map(|(&id, _)| id)
    }

    /// The core-owned bytes stream `s` has to write now, if any. Report what the
    /// transport accepted with [`sent`](Connection::sent).
    pub fn poll_send(&self, s: StreamId) -> Option<&[u8]> {
        self.check_open().ok()?;
        let p = self.streams.get(&s)?.send.pending();
        (!p.is_empty()).then_some(p)
    }

    /// The transport accepted the first `n` bytes of `poll_send(s)`. May queue
    /// [`Action::FinishStream`].
    ///
    /// `Err(UnknownStream)` for a stream with no send state; `Err(WrongPhase)` if `n`
    /// exceeds what `poll_send(s)` returned.
    pub fn sent(&mut self, s: StreamId, n: usize) -> Result<(), UsageError> {
        self.check_open().map_err(UsageError::Closed)?;
        let st = self.streams.get_mut(&s).ok_or(UsageError::UnknownStream)?;
        if n > st.send.pending().len() {
            return Err(UsageError::WrongPhase);
        }
        st.send.advance(n);
        if Some(s) == self.local_uni[UniKind::Control as usize] && !self.local_settings_sent {
            self.settings_left = self.settings_left.saturating_sub(n);
            self.local_settings_sent = self.settings_left == 0;
        }
        self.finish_if_drained(s);
        Ok(())
    }

    /// Queue a HEADERS frame: a new request (client), a response (server), or trailers.
    ///
    /// - Client: on an unused client-initiated bidirectional id this starts a request;
    ///   after the body it sends trailers.
    /// - Server: a response only after the request's `Headers` was delivered; any number
    ///   of 1xx (`end = false`) may precede the final response; after the body, trailers.
    /// - Trailers require `end = true`. In a tunnel (after a 2xx to CONNECT) no HEADERS
    ///   may be sent.
    /// - `end = true`: nothing follows; FIN is requested by [`Action::FinishStream`] once
    ///   everything queued is written.
    ///
    /// The fields are validated like received ones: `Err(InvalidField)` if they do not
    /// form a valid request, response or trailer section. Also `Err(UnknownStream)`,
    /// `Err(WrongPhase)`, `Err(GoingAway)` (client, after the peer's GOAWAY) and
    /// `Err(NotNegotiated)` (`:protocol` without the peer's
    /// `SETTINGS_ENABLE_CONNECT_PROTOCOL`).
    pub fn send_headers(
        &mut self,
        s: StreamId,
        fields: &[FieldRef],
        end: bool,
    ) -> Result<(), UsageError> {
        self.check_open().map_err(UsageError::Closed)?;
        if !s.is_request() || s.0 > varint::MAX || self.is_reaped(s) {
            return Err(UsageError::UnknownStream);
        }
        let role = self.role;
        let st = if role == Role::Client && !self.streams.contains_key(&s) {
            let st = self.new_request(fields)?;
            self.streams.entry(s).or_insert(st)
        } else {
            let st = self.streams.get_mut(&s).ok_or(UsageError::UnknownStream)?;
            st.send.phase = next_phase(role, st, fields, end)?;
            st
        };
        let mut block = Vec::new();
        encode_field_section(fields, &mut block);
        encode_header(HEADERS, block.len() as u64, &mut st.send.queue);
        st.send.queue.extend_from_slice(&block);
        if end {
            st.send.phase = SendPhase::Ending;
        }
        self.finish_if_drained(s);
        Ok(())
    }

    /// Start a DATA frame of `payload_len` bytes; the caller writes `prefix()` then the payload.
    ///
    /// The payload never passes through the core: write
    /// `writev([frame.prefix(), payload])` and report every accepted byte (prefix
    /// included) with [`data_written`](Connection::data_written); keep the payload until
    /// it is fully written. `payload_len = 0` with `end = true` is FIN only: the prefix is
    /// empty and nothing is written.
    ///
    /// Allowed after the request (client) or a final response (server), and in a tunnel;
    /// a response to HEAD, 204 or 304 takes only `payload_len = 0`. `Err(Blocked)`
    /// (retryable) while HEADERS bytes are queued or a DATA frame is in flight;
    /// `Err(WrongPhase)` otherwise misplaced; `Err(OutOfRange)` beyond 2^62-1.
    pub fn send_data(
        &mut self,
        s: StreamId,
        payload_len: u64,
        end: bool,
    ) -> Result<DataFrame, UsageError> {
        self.check_open().map_err(UsageError::Closed)?;
        if payload_len > varint::MAX {
            return Err(UsageError::OutOfRange);
        }
        let st = &mut self
            .streams
            .get_mut(&s)
            .ok_or(UsageError::UnknownStream)?
            .send;
        let no_content = st.request_is_head || st.no_content_status;
        match st.phase {
            SendPhase::Tunnel => {}
            SendPhase::Body if payload_len == 0 || !no_content => {}
            _ => return Err(UsageError::WrongPhase),
        }
        if !st.drained() {
            return Err(UsageError::Blocked);
        }
        let frame = if payload_len == 0 && end {
            // FIN only: nothing to write.
            DataFrame {
                bytes: [0; 9],
                len: 0,
            }
        } else {
            let (bytes, len) = data_prefix(payload_len);
            st.in_flight = Some(InFlight {
                frame_len: u64::from(len) + payload_len,
                written: 0,
            });
            DataFrame { bytes, len }
        };
        if end {
            st.phase = SendPhase::Ending;
        }
        self.finish_if_drained(s);
        Ok(frame)
    }

    /// The transport accepted `n` more bytes of the in-flight DATA frame (prefix, then
    /// payload). May queue [`Action::FinishStream`]. `Err(WrongPhase)` with no frame in
    /// flight or past its end.
    pub fn data_written(&mut self, s: StreamId, n: usize) -> Result<(), UsageError> {
        self.check_open().map_err(UsageError::Closed)?;
        let st = &mut self
            .streams
            .get_mut(&s)
            .ok_or(UsageError::UnknownStream)?
            .send;
        let f = st.in_flight.as_mut().ok_or(UsageError::WrongPhase)?;
        let n = n as u64;
        if n > f.frame_len - f.written {
            return Err(UsageError::WrongPhase);
        }
        f.written += n;
        if f.written == f.frame_len {
            st.in_flight = None;
        }
        self.finish_if_drained(s);
        Ok(())
    }

    /// Validate a client's first HEADERS on a fresh request stream.
    fn new_request(&self, fields: &[FieldRef]) -> Result<Stream, UsageError> {
        if !self.may_start_request() {
            return Err(UsageError::GoingAway);
        }
        let enabled = self
            .peer_settings
            .as_ref()
            .is_some_and(|p| p.enable_connect_protocol);
        if !enabled && fields.iter().any(|f| f.name == b":protocol") {
            return Err(UsageError::NotNegotiated);
        }
        let m = validate_outgoing(
            fields,
            ValidateCtx::Request {
                connect_protocol_enabled: enabled,
            },
        )?;
        let mut st = Stream::default();
        st.send.phase = SendPhase::Body;
        st.recv.expects_no_content = m.method_is_head;
        if m.is_connect {
            st.recv.tunnel = TunnelState::ConnectPending;
        }
        Ok(st)
    }

    /// End latch: once an accepted `end` is fully written, request FIN exactly once.
    fn finish_if_drained(&mut self, s: StreamId) {
        let Some(st) = self.streams.get_mut(&s) else {
            return;
        };
        if st.send.phase == SendPhase::Ending && st.send.drained() {
            st.send.phase = SendPhase::Done;
            self.actions.push_back(Action::FinishStream(s));
            self.reap(s);
        }
    }
}

/// Phase after HEADERS on an existing request stream (response or trailers).
fn next_phase(
    role: Role,
    st: &mut Stream,
    fields: &[FieldRef],
    end: bool,
) -> Result<SendPhase, UsageError> {
    match st.send.phase {
        // A response only after the request HEADERS were delivered.
        SendPhase::Idle | SendPhase::Headers
            if role == Role::Server && st.recv.phase != RecvPhase::AwaitHeaders =>
        {
            let m = validate_outgoing(fields, ValidateCtx::Response)?;
            if m.kind == HeadersKind::Informational {
                return if end {
                    Err(UsageError::WrongPhase)
                } else {
                    Ok(SendPhase::Headers)
                };
            }
            st.send.no_content_status = matches!(m.status, Some(204 | 304));
            st.send.phase = SendPhase::Body;
            st.connect_final(m.status.is_some_and(|c| (200..300).contains(&c)));
            Ok(st.send.phase)
        }
        SendPhase::Body if end => {
            validate_outgoing(fields, ValidateCtx::Trailers)?;
            Ok(SendPhase::Trailers)
        }
        _ => Err(UsageError::WrongPhase),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;

    #[test]
    fn local_settings_sent_after_settings_fully_written() {
        let cfg = Config {
            grease: false,
            ..Config::default()
        };
        let mut c = Connection::new(Role::Client, cfg);
        c.bind_uni(UniKind::Control, StreamId(2)).unwrap(); // [0x00, 0x04, 0x00]
        c.sent(StreamId(2), 2).unwrap(); // stops inside SETTINGS
        assert!(!c.local_settings_sent);
        c.sent(StreamId(2), 1).unwrap();
        assert!(c.local_settings_sent);
    }

    fn status(code: &'static [u8]) -> [FieldRef<'static>; 1] {
        [FieldRef::new(b":status", code)]
    }

    /// A server with request stream 0 as Task 12 will create it on receive.
    fn server_with(recv_tunnel: TunnelState, request_is_head: bool) -> Connection {
        let mut c = Connection::new(Role::Server, Config::default());
        let mut st = Stream::default();
        st.recv.tunnel = recv_tunnel;
        st.send.request_is_head = request_is_head;
        st.recv.phase = RecvPhase::Body;
        c.streams.insert(StreamId(0), st);
        c
    }

    fn drain(c: &mut Connection) {
        let n = c.poll_send(StreamId(0)).map_or(0, <[u8]>::len);
        c.sent(StreamId(0), n).unwrap();
    }

    #[test]
    fn server_response_phases() {
        let s = StreamId(0);
        let mut c = server_with(TunnelState::Regular, false);
        assert_eq!(
            c.send_headers(s, &status(b"103"), true),
            Err(UsageError::WrongPhase)
        );
        c.send_headers(s, &status(b"103"), false).unwrap();
        assert_eq!(c.streams[&s].send.phase, SendPhase::Headers);
        assert_eq!(
            c.send_headers(s, &[FieldRef::new(b":method", b"GET")], false),
            Err(UsageError::InvalidField)
        );
        c.send_headers(s, &status(b"200"), false).unwrap();
        assert_eq!(c.streams[&s].send.phase, SendPhase::Body);
        drain(&mut c);
        c.send_data(s, 0, true).unwrap();
        assert!(c.actions.contains(&Action::FinishStream(s)));
    }

    #[test]
    fn server_no_content_responses() {
        let s = StreamId(0);
        for (code, head) in [(&b"204"[..], false), (b"304", false), (b"200", true)] {
            let mut c = server_with(TunnelState::Regular, head);
            c.send_headers(s, &status(code), false).unwrap();
            drain(&mut c);
            assert_eq!(c.send_data(s, 1, false), Err(UsageError::WrongPhase));
            c.send_headers(s, &[FieldRef::new(b"x-t", b"1")], true)
                .unwrap();
        }
    }

    #[test]
    fn server_connect_2xx_is_tunnel() {
        let s = StreamId(0);
        // 204 to CONNECT is a tunnel and carries DATA.
        for code in [&b"200"[..], b"204"] {
            let mut c = server_with(TunnelState::ConnectPending, false);
            c.send_headers(s, &status(b"100"), false).unwrap();
            c.send_headers(s, &status(code), false).unwrap();
            assert_eq!(c.streams[&s].send.phase, SendPhase::Tunnel);
            assert_eq!(c.streams[&s].recv.tunnel, TunnelState::Tunnel);
            drain(&mut c);
            c.send_data(s, 5, false).unwrap();
            c.data_written(s, 7).unwrap();
            assert_eq!(
                c.send_headers(s, &[FieldRef::new(b"x-t", b"1")], true),
                Err(UsageError::WrongPhase)
            );
        }
        let mut c = server_with(TunnelState::ConnectPending, false);
        c.send_headers(s, &status(b"404"), false).unwrap();
        assert_eq!(c.streams[&s].send.phase, SendPhase::Body);
        // The request body and trailers are a regular message again.
        assert_eq!(c.streams[&s].recv.tunnel, TunnelState::Regular);
    }

    #[test]
    fn client_head_and_connect_set_recv_state() {
        let mut c = Connection::new(Role::Client, Config::default());
        let head = [
            FieldRef::new(b":method", b"HEAD"),
            FieldRef::new(b":scheme", b"https"),
            FieldRef::new(b":authority", b"a"),
            FieldRef::new(b":path", b"/"),
        ];
        c.send_headers(StreamId(0), &head, true).unwrap();
        assert!(c.streams[&StreamId(0)].recv.expects_no_content);
        let connect = [
            FieldRef::new(b":method", b"CONNECT"),
            FieldRef::new(b":authority", b"a:443"),
        ];
        c.send_headers(StreamId(4), &connect, false).unwrap();
        let st = &c.streams[&StreamId(4)];
        assert_eq!(st.recv.tunnel, TunnelState::ConnectPending);
        assert!(!st.recv.expects_no_content);
    }
}