Skip to main content

quiche/h3/
mod.rs

1// Copyright (C) 2019, Cloudflare, Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright notice,
9//       this list of conditions and the following disclaimer.
10//
11//     * Redistributions in binary form must reproduce the above copyright
12//       notice, this list of conditions and the following disclaimer in the
13//       documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27//! HTTP/3 wire protocol and QPACK implementation.
28//!
29//! This module provides a high level API for sending and receiving HTTP/3
30//! requests and responses on top of the QUIC transport protocol.
31//!
32//! ## Connection setup
33//!
34//! HTTP/3 connections require a QUIC transport-layer connection, see
35//! [Connection setup] for a full description of the setup process.
36//!
37//! To use HTTP/3, the QUIC connection must be configured with a suitable
38//! Application Layer Protocol Negotiation (ALPN) Protocol ID:
39//!
40//! ```
41//! let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
42//! config.set_application_protos(quiche::h3::APPLICATION_PROTOCOL)?;
43//! # Ok::<(), quiche::Error>(())
44//! ```
45//!
46//! The QUIC handshake is driven by [sending] and [receiving] QUIC packets.
47//!
48//! Once the handshake has completed, the first step in establishing an HTTP/3
49//! connection is creating its configuration object:
50//!
51//! ```
52//! let h3_config = quiche::h3::Config::new()?;
53//! # Ok::<(), quiche::h3::Error>(())
54//! ```
55//!
56//! HTTP/3 client and server connections are both created using the
57//! [`with_transport()`] function, the role is inferred from the type of QUIC
58//! connection:
59//!
60//! ```no_run
61//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
62//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
63//! # let peer = "127.0.0.1:1234".parse().unwrap();
64//! # let local = "127.0.0.1:4321".parse().unwrap();
65//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config).unwrap();
66//! # let h3_config = quiche::h3::Config::new()?;
67//! let h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
68//! # Ok::<(), quiche::h3::Error>(())
69//! ```
70//!
71//! ## Sending a request
72//!
73//! An HTTP/3 client can send a request by using the connection's
74//! [`send_request()`] method to queue request headers; [sending] QUIC packets
75//! causes the requests to get sent to the peer:
76//!
77//! ```no_run
78//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
79//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
80//! # let peer = "127.0.0.1:1234".parse().unwrap();
81//! # let local = "127.0.0.1:4321".parse().unwrap();
82//! # let mut conn = quiche::connect(None, &scid, local, peer, &mut config).unwrap();
83//! # let h3_config = quiche::h3::Config::new()?;
84//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
85//! let req = vec![
86//!     quiche::h3::Header::new(b":method", b"GET"),
87//!     quiche::h3::Header::new(b":scheme", b"https"),
88//!     quiche::h3::Header::new(b":authority", b"quic.tech"),
89//!     quiche::h3::Header::new(b":path", b"/"),
90//!     quiche::h3::Header::new(b"user-agent", b"quiche"),
91//! ];
92//!
93//! h3_conn.send_request(&mut conn, &req, true)?;
94//! # Ok::<(), quiche::h3::Error>(())
95//! ```
96//!
97//! An HTTP/3 client can send a request with additional body data by using
98//! the connection's [`send_body()`] method:
99//!
100//! ```no_run
101//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
102//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
103//! # let peer = "127.0.0.1:1234".parse().unwrap();
104//! # let local = "127.0.0.1:4321".parse().unwrap();
105//! # let mut conn = quiche::connect(None, &scid, local, peer, &mut config).unwrap();
106//! # let h3_config = quiche::h3::Config::new()?;
107//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
108//! let req = vec![
109//!     quiche::h3::Header::new(b":method", b"GET"),
110//!     quiche::h3::Header::new(b":scheme", b"https"),
111//!     quiche::h3::Header::new(b":authority", b"quic.tech"),
112//!     quiche::h3::Header::new(b":path", b"/"),
113//!     quiche::h3::Header::new(b"user-agent", b"quiche"),
114//! ];
115//!
116//! let stream_id = h3_conn.send_request(&mut conn, &req, false)?;
117//! h3_conn.send_body(&mut conn, stream_id, b"Hello World!", true)?;
118//! # Ok::<(), quiche::h3::Error>(())
119//! ```
120//!
121//! ## Handling requests and responses
122//!
123//! After [receiving] QUIC packets, HTTP/3 data is processed using the
124//! connection's [`poll()`] method. On success, this returns an [`Event`] object
125//! and an ID corresponding to the stream where the `Event` originated.
126//!
127//! An HTTP/3 server uses [`poll()`] to read requests and responds to them using
128//! [`send_response()`] and [`send_body()`]:
129//!
130//! ```no_run
131//! use quiche::h3::NameValue;
132//!
133//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
134//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
135//! # let peer = "127.0.0.1:1234".parse().unwrap();
136//! # let local = "127.0.0.1:1234".parse().unwrap();
137//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config).unwrap();
138//! # let h3_config = quiche::h3::Config::new()?;
139//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
140//! loop {
141//!     match h3_conn.poll(&mut conn) {
142//!         Ok((stream_id, quiche::h3::Event::Headers{list, more_frames})) => {
143//!             let mut headers = list.into_iter();
144//!
145//!             // Look for the request's method.
146//!             let method = headers.find(|h| h.name() == b":method").unwrap();
147//!
148//!             // Look for the request's path.
149//!             let path = headers.find(|h| h.name() == b":path").unwrap();
150//!
151//!             if method.value() == b"GET" && path.value() == b"/" {
152//!                 let resp = vec![
153//!                     quiche::h3::Header::new(b":status", 200.to_string().as_bytes()),
154//!                     quiche::h3::Header::new(b"server", b"quiche"),
155//!                 ];
156//!
157//!                 h3_conn.send_response(&mut conn, stream_id, &resp, false)?;
158//!                 h3_conn.send_body(&mut conn, stream_id, b"Hello World!", true)?;
159//!             }
160//!         },
161//!
162//!         Ok((stream_id, quiche::h3::Event::Data)) => {
163//!             // Request body data, handle it.
164//!             # return Ok(());
165//!         },
166//!
167//!         Ok((stream_id, quiche::h3::Event::Finished)) => {
168//!             // Peer terminated stream, handle it.
169//!         },
170//!
171//!         Ok((stream_id, quiche::h3::Event::Reset(err))) => {
172//!             // Peer reset the stream, handle it.
173//!         },
174//!
175//!         Ok((_flow_id, quiche::h3::Event::PriorityUpdate)) => (),
176//!
177//!         Ok((goaway_id, quiche::h3::Event::GoAway)) => {
178//!              // Peer signalled it is going away, handle it.
179//!         },
180//!
181//!         Err(quiche::h3::Error::Done) => {
182//!             // Done reading.
183//!             break;
184//!         },
185//!
186//!         Err(e) => {
187//!             // An error occurred, handle it.
188//!             break;
189//!         },
190//!     }
191//! }
192//! # Ok::<(), quiche::h3::Error>(())
193//! ```
194//!
195//! An HTTP/3 client uses [`poll()`] to read responses:
196//!
197//! ```no_run
198//! use quiche::h3::NameValue;
199//!
200//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
201//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
202//! # let peer = "127.0.0.1:1234".parse().unwrap();
203//! # let local = "127.0.0.1:1234".parse().unwrap();
204//! # let mut conn = quiche::connect(None, &scid, local, peer, &mut config).unwrap();
205//! # let h3_config = quiche::h3::Config::new()?;
206//! # let mut h3_conn = quiche::h3::Connection::with_transport(&mut conn, &h3_config)?;
207//! loop {
208//!     match h3_conn.poll(&mut conn) {
209//!         Ok((stream_id, quiche::h3::Event::Headers{list, more_frames})) => {
210//!             let status = list.iter().find(|h| h.name() == b":status").unwrap();
211//!             println!("Received {} response on stream {}",
212//!                      std::str::from_utf8(status.value()).unwrap(),
213//!                      stream_id);
214//!         },
215//!
216//!         Ok((stream_id, quiche::h3::Event::Data)) => {
217//!             let mut body = vec![0; 4096];
218//!
219//!             // Consume all body data received on the stream.
220//!             while let Ok(read) =
221//!                 h3_conn.recv_body(&mut conn, stream_id, &mut body)
222//!             {
223//!                 println!("Received {} bytes of payload on stream {}",
224//!                          read, stream_id);
225//!             }
226//!         },
227//!
228//!         Ok((stream_id, quiche::h3::Event::Finished)) => {
229//!             // Peer terminated stream, handle it.
230//!         },
231//!
232//!         Ok((stream_id, quiche::h3::Event::Reset(err))) => {
233//!             // Peer reset the stream, handle it.
234//!         },
235//!
236//!         Ok((_prioritized_element_id, quiche::h3::Event::PriorityUpdate)) => (),
237//!
238//!         Ok((goaway_id, quiche::h3::Event::GoAway)) => {
239//!              // Peer signalled it is going away, handle it.
240//!         },
241//!
242//!         Err(quiche::h3::Error::Done) => {
243//!             // Done reading.
244//!             break;
245//!         },
246//!
247//!         Err(e) => {
248//!             // An error occurred, handle it.
249//!             break;
250//!         },
251//!     }
252//! }
253//! # Ok::<(), quiche::h3::Error>(())
254//! ```
255//!
256//! ## Detecting end of request or response
257//!
258//! A single HTTP/3 request or response may consist of several HEADERS and DATA
259//! frames; it is finished when the QUIC stream is closed. Calling [`poll()`]
260//! repeatedly will generate an [`Event`] for each of these. The application may
261//! use these event to do additional HTTP semantic validation.
262//!
263//! ## HTTP/3 protocol errors
264//!
265//! Quiche is responsible for managing the HTTP/3 connection, ensuring it is in
266//! a correct state and validating all messages received by a peer. This mainly
267//! takes place in the [`poll()`] method. If an HTTP/3 error occurs, quiche will
268//! close the connection and send an appropriate CONNECTION_CLOSE frame to the
269//! peer. An [`Error`] is returned to the application so that it can perform any
270//! required tidy up such as closing sockets.
271//!
272//! [`application_proto()`]: ../struct.Connection.html#method.application_proto
273//! [`stream_finished()`]: ../struct.Connection.html#method.stream_finished
274//! [Connection setup]: ../index.html#connection-setup
275//! [sending]: ../index.html#generating-outgoing-packets
276//! [receiving]: ../index.html#handling-incoming-packets
277//! [`with_transport()`]: struct.Connection.html#method.with_transport
278//! [`poll()`]: struct.Connection.html#method.poll
279//! [`Event`]: enum.Event.html
280//! [`Error`]: enum.Error.html
281//! [`send_request()`]: struct.Connection.html#method.send_response
282//! [`send_response()`]: struct.Connection.html#method.send_response
283//! [`send_body()`]: struct.Connection.html#method.send_body
284
285use std::collections::hash_map;
286use std::collections::HashSet;
287use std::collections::VecDeque;
288
289#[cfg(feature = "sfv")]
290use std::convert::TryFrom;
291use std::fmt;
292use std::fmt::Write;
293
294#[cfg(feature = "qlog")]
295use qlog::events::http3::FrameCreated;
296#[cfg(feature = "qlog")]
297use qlog::events::http3::FrameParsed;
298#[cfg(feature = "qlog")]
299use qlog::events::http3::Http3EventType;
300#[cfg(feature = "qlog")]
301use qlog::events::http3::Http3Frame;
302#[cfg(feature = "qlog")]
303use qlog::events::http3::Initiator;
304#[cfg(feature = "qlog")]
305use qlog::events::http3::StreamType;
306#[cfg(feature = "qlog")]
307use qlog::events::http3::StreamTypeSet;
308#[cfg(feature = "qlog")]
309use qlog::events::EventData;
310#[cfg(feature = "qlog")]
311use qlog::events::EventImportance;
312#[cfg(feature = "qlog")]
313use qlog::events::EventType;
314
315use crate::buffers::BufFactory;
316use crate::BufSplit;
317
318/// List of ALPN tokens of supported HTTP/3 versions.
319///
320/// This can be passed directly to the [`Config::set_application_protos()`]
321/// method when implementing HTTP/3 applications.
322///
323/// [`Config::set_application_protos()`]:
324/// ../struct.Config.html#method.set_application_protos
325pub const APPLICATION_PROTOCOL: &[&[u8]] = &[b"h3"];
326
327// The offset used when converting HTTP/3 urgency to quiche urgency.
328const PRIORITY_URGENCY_OFFSET: u8 = 124;
329
330// Parameter values as specified in [Extensible Priorities].
331//
332// [Extensible Priorities]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
333const PRIORITY_URGENCY_LOWER_BOUND: u8 = 0;
334const PRIORITY_URGENCY_UPPER_BOUND: u8 = 7;
335const PRIORITY_URGENCY_DEFAULT: u8 = 3;
336const PRIORITY_INCREMENTAL_DEFAULT: bool = false;
337
338/// The default value for the maximum size of PRIORITY_UPDATE
339/// frame payload.
340///
341/// See <https://datatracker.ietf.org/doc/html/rfc9218#section-7.2>
342pub const PRIORITY_UPDATE_FRAME_PAYLOAD_MAX_SIZE_DEFAULT: u64 = 256;
343
344/// The default value for SETTINGS_MAX_FIELD_SECTION_SIZE
345///
346/// See <https://datatracker.ietf.org/doc/html/rfc9114#section-4.2.2>.
347pub const SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT: u64 = 32_768;
348
349#[cfg(feature = "qlog")]
350const QLOG_FRAME_CREATED: EventType =
351    EventType::Http3EventType(Http3EventType::FrameCreated);
352#[cfg(feature = "qlog")]
353const QLOG_FRAME_PARSED: EventType =
354    EventType::Http3EventType(Http3EventType::FrameParsed);
355#[cfg(feature = "qlog")]
356const QLOG_STREAM_TYPE_SET: EventType =
357    EventType::Http3EventType(Http3EventType::StreamTypeSet);
358
359/// A specialized [`Result`] type for quiche HTTP/3 operations.
360///
361/// This type is used throughout quiche's HTTP/3 public API for any operation
362/// that can produce an error.
363///
364/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
365pub type Result<T> = std::result::Result<T, Error>;
366
367/// An HTTP/3 error.
368#[derive(Clone, Copy, Debug, PartialEq, Eq)]
369pub enum Error {
370    /// There is no error or no work to do
371    Done,
372
373    /// The provided buffer is too short.
374    BufferTooShort,
375
376    /// Internal error in the HTTP/3 stack.
377    InternalError,
378
379    /// Endpoint detected that the peer is exhibiting behavior that causes.
380    /// excessive load.
381    ExcessiveLoad,
382
383    /// Stream ID or Push ID greater that current maximum was
384    /// used incorrectly, such as exceeding a limit, reducing a limit,
385    /// or being reused.
386    IdError,
387
388    /// The endpoint detected that its peer created a stream that it will not
389    /// accept.
390    StreamCreationError,
391
392    /// A required critical stream was closed.
393    ClosedCriticalStream,
394
395    /// No SETTINGS frame at beginning of control stream.
396    MissingSettings,
397
398    /// A frame was received which is not permitted in the current state.
399    FrameUnexpected,
400
401    /// Frame violated layout or size rules.
402    FrameError,
403
404    /// QPACK Header block decompression failure.
405    QpackDecompressionFailed,
406
407    /// Error originated from the transport layer.
408    TransportError(crate::Error),
409
410    /// The underlying QUIC stream (or connection) doesn't have enough capacity
411    /// for the operation to complete. The application should retry later on.
412    StreamBlocked,
413
414    /// Error in the payload of a SETTINGS frame.
415    SettingsError,
416
417    /// Server rejected request.
418    RequestRejected,
419
420    /// Request or its response cancelled.
421    RequestCancelled,
422
423    /// Client's request stream terminated without containing a full-formed
424    /// request.
425    RequestIncomplete,
426
427    /// An HTTP message was malformed and cannot be processed.
428    MessageError,
429
430    /// The TCP connection established in response to a CONNECT request was
431    /// reset or abnormally closed.
432    ConnectError,
433
434    /// The requested operation cannot be served over HTTP/3. Peer should retry
435    /// over HTTP/1.1.
436    VersionFallback,
437}
438
439/// HTTP/3 error codes sent on the wire.
440///
441/// As defined in [RFC9114](https://www.rfc-editor.org/rfc/rfc9114.html#http-error-codes).
442#[derive(Copy, Clone, Debug, Eq, PartialEq)]
443pub enum WireErrorCode {
444    /// No error. This is used when the connection or stream needs to be closed,
445    /// but there is no error to signal.
446    NoError              = 0x100,
447    /// Peer violated protocol requirements in a way that does not match a more
448    /// specific error code or endpoint declines to use the more specific
449    /// error code.
450    GeneralProtocolError = 0x101,
451    /// An internal error has occurred in the HTTP stack.
452    InternalError        = 0x102,
453    /// The endpoint detected that its peer created a stream that it will not
454    /// accept.
455    StreamCreationError  = 0x103,
456    /// A stream required by the HTTP/3 connection was closed or reset.
457    ClosedCriticalStream = 0x104,
458    /// A frame was received that was not permitted in the current state or on
459    /// the current stream.
460    FrameUnexpected      = 0x105,
461    /// A frame that fails to satisfy layout requirements or with an invalid
462    /// size was received.
463    FrameError           = 0x106,
464    /// The endpoint detected that its peer is exhibiting a behavior that might
465    /// be generating excessive load.
466    ExcessiveLoad        = 0x107,
467    /// A stream ID or push ID was used incorrectly, such as exceeding a limit,
468    /// reducing a limit, or being reused.
469    IdError              = 0x108,
470    /// An endpoint detected an error in the payload of a SETTINGS frame.
471    SettingsError        = 0x109,
472    /// No SETTINGS frame was received at the beginning of the control stream.
473    MissingSettings      = 0x10a,
474    /// A server rejected a request without performing any application
475    /// processing.
476    RequestRejected      = 0x10b,
477    /// The request or its response (including pushed response) is cancelled.
478    RequestCancelled     = 0x10c,
479    /// The client's stream terminated without containing a fully formed
480    /// request.
481    RequestIncomplete    = 0x10d,
482    /// An HTTP message was malformed and cannot be processed.
483    MessageError         = 0x10e,
484    /// The TCP connection established in response to a CONNECT request was
485    /// reset or abnormally closed.
486    ConnectError         = 0x10f,
487    /// The requested operation cannot be served over HTTP/3. The peer should
488    /// retry over HTTP/1.1.
489    VersionFallback      = 0x110,
490}
491
492impl Error {
493    fn to_wire(self) -> u64 {
494        match self {
495            Error::Done => WireErrorCode::NoError as u64,
496            Error::InternalError => WireErrorCode::InternalError as u64,
497            Error::StreamCreationError =>
498                WireErrorCode::StreamCreationError as u64,
499            Error::ClosedCriticalStream =>
500                WireErrorCode::ClosedCriticalStream as u64,
501            Error::FrameUnexpected => WireErrorCode::FrameUnexpected as u64,
502            Error::FrameError => WireErrorCode::FrameError as u64,
503            Error::ExcessiveLoad => WireErrorCode::ExcessiveLoad as u64,
504            Error::IdError => WireErrorCode::IdError as u64,
505            Error::MissingSettings => WireErrorCode::MissingSettings as u64,
506            Error::QpackDecompressionFailed => 0x200,
507            Error::BufferTooShort => 0x999,
508            Error::TransportError { .. } | Error::StreamBlocked => 0xFF,
509            Error::SettingsError => WireErrorCode::SettingsError as u64,
510            Error::RequestRejected => WireErrorCode::RequestRejected as u64,
511            Error::RequestCancelled => WireErrorCode::RequestCancelled as u64,
512            Error::RequestIncomplete => WireErrorCode::RequestIncomplete as u64,
513            Error::MessageError => WireErrorCode::MessageError as u64,
514            Error::ConnectError => WireErrorCode::ConnectError as u64,
515            Error::VersionFallback => WireErrorCode::VersionFallback as u64,
516        }
517    }
518
519    #[cfg(feature = "ffi")]
520    fn to_c(self) -> libc::ssize_t {
521        match self {
522            Error::Done => -1,
523            Error::BufferTooShort => -2,
524            Error::InternalError => -3,
525            Error::ExcessiveLoad => -4,
526            Error::IdError => -5,
527            Error::StreamCreationError => -6,
528            Error::ClosedCriticalStream => -7,
529            Error::MissingSettings => -8,
530            Error::FrameUnexpected => -9,
531            Error::FrameError => -10,
532            Error::QpackDecompressionFailed => -11,
533            // -12 was previously used for TransportError, skip it
534            Error::StreamBlocked => -13,
535            Error::SettingsError => -14,
536            Error::RequestRejected => -15,
537            Error::RequestCancelled => -16,
538            Error::RequestIncomplete => -17,
539            Error::MessageError => -18,
540            Error::ConnectError => -19,
541            Error::VersionFallback => -20,
542
543            Error::TransportError(quic_error) => quic_error.to_c() - 1000,
544        }
545    }
546}
547
548impl fmt::Display for Error {
549    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
550        write!(f, "{self:?}")
551    }
552}
553
554impl std::error::Error for Error {
555    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
556        None
557    }
558}
559
560impl From<super::Error> for Error {
561    fn from(err: super::Error) -> Self {
562        match err {
563            super::Error::Done => Error::Done,
564
565            _ => Error::TransportError(err),
566        }
567    }
568}
569
570impl From<octets::BufferTooShortError> for Error {
571    fn from(_err: octets::BufferTooShortError) -> Self {
572        Error::BufferTooShort
573    }
574}
575
576/// An HTTP/3 configuration.
577pub struct Config {
578    max_field_section_size: Option<u64>,
579    qpack_max_table_capacity: Option<u64>,
580    qpack_blocked_streams: Option<u64>,
581    connect_protocol_enabled: Option<u64>,
582    /// additional settings are settings that are not part of the H3
583    /// settings explicitly handled above
584    additional_settings: Option<Vec<(u64, u64)>>,
585
586    max_priority_update_size: u64,
587}
588
589impl Config {
590    /// Creates a new configuration object with default settings.
591    pub const fn new() -> Result<Config> {
592        Ok(Config {
593            max_field_section_size: Some(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
594            qpack_max_table_capacity: None,
595            qpack_blocked_streams: None,
596            connect_protocol_enabled: None,
597            additional_settings: None,
598            max_priority_update_size:
599                PRIORITY_UPDATE_FRAME_PAYLOAD_MAX_SIZE_DEFAULT,
600        })
601    }
602
603    /// Sets the `SETTINGS_MAX_FIELD_SECTION_SIZE` setting.
604    ///
605    /// The default is [`SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT`]. The value is
606    /// measured in units of bytes, it is used as a limit when parsing frames
607    /// that contain encoded HTTP headers when [`poll()`] is called. A first
608    /// check is applied when handling HEADERS and PUSH_PROMISE frames
609    /// themselves, with some margin allowed on top of the provided size. A
610    /// second check is applied when decoding QPACK, implementing the rules
611    /// described in <https://datatracker.ietf.org/doc/html/rfc9114#section-4.2.2>.
612    ///
613    /// When headers exceed the limit set by the application, the call to the
614    /// [`poll()`] method will return the [`Error::ExcessiveLoad`] error, and
615    /// the connection will be closed.
616    ///
617    /// [`poll()`]: struct.Connection.html#method.poll
618    /// [`Error::ExcessiveLoad`]: enum.Error.html#variant.ExcessiveLoad
619    pub fn set_max_field_section_size(&mut self, v: u64) {
620        self.max_field_section_size = Some(v);
621    }
622
623    /// Sets the `SETTINGS_QPACK_MAX_TABLE_CAPACITY` setting.
624    ///
625    /// The default value is `0`.
626    pub fn set_qpack_max_table_capacity(&mut self, v: u64) {
627        self.qpack_max_table_capacity = Some(v);
628    }
629
630    /// Sets the `SETTINGS_QPACK_BLOCKED_STREAMS` setting.
631    ///
632    /// The default value is `0`.
633    pub fn set_qpack_blocked_streams(&mut self, v: u64) {
634        self.qpack_blocked_streams = Some(v);
635    }
636
637    /// Sets or omits the `SETTINGS_ENABLE_CONNECT_PROTOCOL` setting.
638    ///
639    /// The default value is `false`.
640    pub fn enable_extended_connect(&mut self, enabled: bool) {
641        if enabled {
642            self.connect_protocol_enabled = Some(1);
643        } else {
644            self.connect_protocol_enabled = None;
645        }
646    }
647
648    /// Sets additional HTTP/3 settings.
649    ///
650    /// The default value is no additional settings.
651    /// The `additional_settings` parameter must not the following
652    /// settings as they are already handled by this library:
653    ///
654    /// - SETTINGS_QPACK_MAX_TABLE_CAPACITY
655    /// - SETTINGS_MAX_FIELD_SECTION_SIZE
656    /// - SETTINGS_QPACK_BLOCKED_STREAMS
657    /// - SETTINGS_ENABLE_CONNECT_PROTOCOL
658    /// - SETTINGS_H3_DATAGRAM
659    ///
660    /// If such a setting is present in the `additional_settings`,
661    /// the method will return the [`Error::SettingsError`] error.
662    ///
663    /// If a setting identifier is present twice in `additional_settings`,
664    /// the method will return the [`Error::SettingsError`] error.
665    ///
666    /// [`Error::SettingsError`]: enum.Error.html#variant.SettingsError
667    pub fn set_additional_settings(
668        &mut self, additional_settings: Vec<(u64, u64)>,
669    ) -> Result<()> {
670        let explicit_quiche_settings = HashSet::from([
671            frame::SETTINGS_QPACK_MAX_TABLE_CAPACITY,
672            frame::SETTINGS_MAX_FIELD_SECTION_SIZE,
673            frame::SETTINGS_QPACK_BLOCKED_STREAMS,
674            frame::SETTINGS_ENABLE_CONNECT_PROTOCOL,
675            frame::SETTINGS_H3_DATAGRAM,
676            frame::SETTINGS_H3_DATAGRAM_00,
677        ]);
678
679        let dedup_settings: HashSet<u64> =
680            additional_settings.iter().map(|(key, _)| *key).collect();
681
682        if dedup_settings.len() != additional_settings.len() ||
683            !explicit_quiche_settings.is_disjoint(&dedup_settings)
684        {
685            return Err(Error::SettingsError);
686        }
687        self.additional_settings = Some(additional_settings);
688        Ok(())
689    }
690
691    /// Sets the maximum size for the payload of PRIORITY_UPDATE frames.
692    ///
693    /// The default is [`PRIORITY_UPDATE_FRAME_PAYLOAD_MAX_SIZE_DEFAULT`]. The
694    /// value uses units of bytes.
695    ///
696    /// When a PRIORITY_UPDATE frame exceeds the limit set by the application,
697    /// the call to the [`poll()`] method will return the
698    /// [`Error::ExcessiveLoad`] error, and the connection will be closed.
699    ///
700    /// [`poll()`]: struct.Connection.html#method.poll
701    /// [`Error::ExcessiveLoad`]: enum.Error.html#variant.ExcessiveLoad
702    pub fn set_max_priority_update_size(&mut self, v: u64) {
703        self.max_priority_update_size = v;
704    }
705}
706
707/// A trait for types with associated string name and value.
708pub trait NameValue {
709    /// Returns the object's name.
710    fn name(&self) -> &[u8];
711
712    /// Returns the object's value.
713    fn value(&self) -> &[u8];
714}
715
716impl<N, V> NameValue for (N, V)
717where
718    N: AsRef<[u8]>,
719    V: AsRef<[u8]>,
720{
721    fn name(&self) -> &[u8] {
722        self.0.as_ref()
723    }
724
725    fn value(&self) -> &[u8] {
726        self.1.as_ref()
727    }
728}
729
730/// An owned name-value pair representing a raw HTTP header.
731#[derive(Clone, PartialEq, Eq)]
732pub struct Header(Vec<u8>, Vec<u8>);
733
734fn try_print_as_readable(hdr: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
735    match std::str::from_utf8(hdr) {
736        Ok(s) => f.write_str(&s.escape_default().to_string()),
737        Err(_) => write!(f, "{hdr:?}"),
738    }
739}
740
741impl fmt::Debug for Header {
742    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
743        f.write_char('"')?;
744        try_print_as_readable(&self.0, f)?;
745        f.write_str(": ")?;
746        try_print_as_readable(&self.1, f)?;
747        f.write_char('"')
748    }
749}
750
751impl Header {
752    /// Creates a new header.
753    ///
754    /// Both `name` and `value` will be cloned.
755    pub fn new(name: &[u8], value: &[u8]) -> Self {
756        Self(name.to_vec(), value.to_vec())
757    }
758}
759
760impl NameValue for Header {
761    fn name(&self) -> &[u8] {
762        &self.0
763    }
764
765    fn value(&self) -> &[u8] {
766        &self.1
767    }
768}
769
770/// A non-owned name-value pair representing a raw HTTP header.
771#[derive(Clone, Debug, PartialEq, Eq)]
772pub struct HeaderRef<'a>(&'a [u8], &'a [u8]);
773
774impl<'a> HeaderRef<'a> {
775    /// Creates a new header.
776    pub const fn new(name: &'a [u8], value: &'a [u8]) -> Self {
777        Self(name, value)
778    }
779}
780
781impl NameValue for HeaderRef<'_> {
782    fn name(&self) -> &[u8] {
783        self.0
784    }
785
786    fn value(&self) -> &[u8] {
787        self.1
788    }
789}
790
791/// An HTTP/3 connection event.
792#[derive(Clone, Debug, PartialEq, Eq)]
793pub enum Event {
794    /// Request/response headers were received.
795    Headers {
796        /// The list of received header fields. The application should validate
797        /// pseudo-headers and headers.
798        list: Vec<Header>,
799
800        /// Whether more frames will follow the headers on the stream.
801        more_frames: bool,
802    },
803
804    /// Data was received.
805    ///
806    /// This indicates that the application can use the [`recv_body()`] method
807    /// to retrieve the data from the stream.
808    ///
809    /// Note that [`recv_body()`] will need to be called repeatedly until the
810    /// [`Done`] value is returned, as the event will not be re-armed until all
811    /// buffered data is read.
812    ///
813    /// [`recv_body()`]: struct.Connection.html#method.recv_body
814    /// [`Done`]: enum.Error.html#variant.Done
815    Data,
816
817    /// Stream was closed,
818    Finished,
819
820    /// Stream was reset.
821    ///
822    /// The associated data represents the error code sent by the peer.
823    Reset(u64),
824
825    /// PRIORITY_UPDATE was received.
826    ///
827    /// This indicates that the application can use the
828    /// [`take_last_priority_update()`] method to take the last received
829    /// PRIORITY_UPDATE for a specified stream.
830    ///
831    /// This event is triggered once per stream until the last PRIORITY_UPDATE
832    /// is taken. It is recommended that applications defer taking the
833    /// PRIORITY_UPDATE until after [`poll()`] returns [`Done`].
834    ///
835    /// [`take_last_priority_update()`]: struct.Connection.html#method.take_last_priority_update
836    /// [`poll()`]: struct.Connection.html#method.poll
837    /// [`Done`]: enum.Error.html#variant.Done
838    PriorityUpdate,
839
840    /// GOAWAY was received.
841    GoAway,
842}
843
844/// Extensible Priorities parameters.
845///
846/// The `TryFrom` trait supports constructing this object from the serialized
847/// Structured Fields Dictionary field value. I.e, use `TryFrom` to parse the
848/// value of a Priority header field or a PRIORITY_UPDATE frame. Using this
849/// trait requires the `sfv` feature to be enabled.
850#[derive(Clone, Copy, Debug, PartialEq, Eq)]
851#[repr(C)]
852pub struct Priority {
853    urgency: u8,
854    incremental: bool,
855}
856
857impl Default for Priority {
858    fn default() -> Self {
859        Priority {
860            urgency: PRIORITY_URGENCY_DEFAULT,
861            incremental: PRIORITY_INCREMENTAL_DEFAULT,
862        }
863    }
864}
865
866impl Priority {
867    /// Creates a new Priority.
868    pub const fn new(urgency: u8, incremental: bool) -> Self {
869        Priority {
870            urgency,
871            incremental,
872        }
873    }
874}
875
876#[cfg(feature = "sfv")]
877#[cfg_attr(docsrs, doc(cfg(feature = "sfv")))]
878impl TryFrom<&[u8]> for Priority {
879    type Error = Error;
880
881    /// Try to parse an Extensible Priority field value.
882    ///
883    /// The field value is expected to be a Structured Fields Dictionary; see
884    /// [Extensible Priorities].
885    ///
886    /// If the `u` or `i` fields are contained with correct types, a constructed
887    /// Priority object is returned. Note that urgency values outside of valid
888    /// range (0 through 7) are clamped to 7.
889    ///
890    /// If the `u` or `i` fields are contained with the wrong types,
891    /// Error::Done is returned.
892    ///
893    /// Omitted parameters will yield default values.
894    ///
895    /// [Extensible Priorities]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
896    fn try_from(value: &[u8]) -> std::result::Result<Self, Self::Error> {
897        let dict = match sfv::Parser::parse_dictionary(value) {
898            Ok(v) => v,
899
900            Err(_) => return Err(Error::Done),
901        };
902
903        let urgency = match dict.get("u") {
904            // If there is a u parameter, try to read it as an Item of type
905            // Integer. If the value out of the spec's allowed range
906            // (0 through 7), that's an error so set it to the upper
907            // bound (lowest priority) to avoid interference with
908            // other streams.
909            Some(sfv::ListEntry::Item(item)) => match item.bare_item.as_int() {
910                Some(v) => {
911                    if !(PRIORITY_URGENCY_LOWER_BOUND as i64..=
912                        PRIORITY_URGENCY_UPPER_BOUND as i64)
913                        .contains(&v)
914                    {
915                        PRIORITY_URGENCY_UPPER_BOUND
916                    } else {
917                        v as u8
918                    }
919                },
920
921                None => return Err(Error::Done),
922            },
923
924            Some(sfv::ListEntry::InnerList(_)) => return Err(Error::Done),
925
926            // Omitted so use default value.
927            None => PRIORITY_URGENCY_DEFAULT,
928        };
929
930        let incremental = match dict.get("i") {
931            Some(sfv::ListEntry::Item(item)) =>
932                item.bare_item.as_bool().ok_or(Error::Done)?,
933
934            // Omitted so use default value.
935            _ => false,
936        };
937
938        Ok(Priority::new(urgency, incremental))
939    }
940}
941
942struct ConnectionSettings {
943    pub max_field_section_size: Option<u64>,
944    pub qpack_max_table_capacity: Option<u64>,
945    pub qpack_blocked_streams: Option<u64>,
946    pub connect_protocol_enabled: Option<u64>,
947    pub h3_datagram: Option<u64>,
948    pub additional_settings: Option<Vec<(u64, u64)>>,
949    pub raw: Option<Vec<(u64, u64)>>,
950}
951
952#[derive(Default)]
953struct QpackStreams {
954    pub encoder_stream_id: Option<u64>,
955    pub encoder_stream_bytes: u64,
956    pub decoder_stream_id: Option<u64>,
957    pub decoder_stream_bytes: u64,
958}
959
960/// Statistics about the connection.
961///
962/// A connection's statistics can be collected using the [`stats()`] method.
963///
964/// [`stats()`]: struct.Connection.html#method.stats
965#[derive(Clone, Default)]
966#[non_exhaustive]
967pub struct Stats {
968    /// The number of bytes received on the QPACK encoder stream.
969    pub qpack_encoder_stream_recv_bytes: u64,
970    /// The number of bytes received on the QPACK decoder stream.
971    pub qpack_decoder_stream_recv_bytes: u64,
972}
973
974fn close_conn_critical_stream<F: BufFactory>(
975    conn: &mut super::Connection<F>,
976) -> Result<()> {
977    conn.close(
978        true,
979        Error::ClosedCriticalStream.to_wire(),
980        b"Critical stream closed.",
981    )?;
982
983    Err(Error::ClosedCriticalStream)
984}
985
986fn close_conn_if_critical_stream_finished<F: BufFactory>(
987    conn: &mut super::Connection<F>, stream_id: u64,
988) -> Result<()> {
989    if conn.stream_finished(stream_id) {
990        close_conn_critical_stream(conn)?;
991    }
992
993    Ok(())
994}
995
996/// An HTTP/3 connection.
997pub struct Connection {
998    is_server: bool,
999
1000    next_request_stream_id: u64,
1001    next_uni_stream_id: u64,
1002
1003    streams: crate::stream::StreamIdHashMap<stream::Stream>,
1004
1005    local_settings: ConnectionSettings,
1006    peer_settings: ConnectionSettings,
1007
1008    control_stream_id: Option<u64>,
1009    peer_control_stream_id: Option<u64>,
1010
1011    qpack_encoder: qpack::Encoder,
1012    qpack_decoder: qpack::Decoder,
1013
1014    local_qpack_streams: QpackStreams,
1015    peer_qpack_streams: QpackStreams,
1016
1017    max_push_id: u64,
1018
1019    // Streams whose peer send side has finished and still need a Finished
1020    // event. If the local send side is already done, poll() removes the H3
1021    // stream state when returning the event. Otherwise the send path removes it
1022    // when the local side finishes later.
1023    finished_streams: VecDeque<u64>,
1024
1025    frames_greased: bool,
1026
1027    local_goaway_id: Option<u64>,
1028    peer_goaway_id: Option<u64>,
1029
1030    max_priority_update_size: u64,
1031}
1032
1033impl Connection {
1034    fn new(
1035        config: &Config, is_server: bool, enable_dgram: bool,
1036    ) -> Result<Connection> {
1037        let initial_uni_stream_id = if is_server { 0x3 } else { 0x2 };
1038        let h3_datagram = if enable_dgram { Some(1) } else { None };
1039
1040        Ok(Connection {
1041            is_server,
1042
1043            next_request_stream_id: 0,
1044
1045            next_uni_stream_id: initial_uni_stream_id,
1046
1047            streams: Default::default(),
1048
1049            local_settings: ConnectionSettings {
1050                max_field_section_size: config.max_field_section_size,
1051                qpack_max_table_capacity: config.qpack_max_table_capacity,
1052                qpack_blocked_streams: config.qpack_blocked_streams,
1053                connect_protocol_enabled: config.connect_protocol_enabled,
1054                h3_datagram,
1055                additional_settings: config.additional_settings.clone(),
1056                raw: Default::default(),
1057            },
1058
1059            peer_settings: ConnectionSettings {
1060                max_field_section_size: None,
1061                qpack_max_table_capacity: None,
1062                qpack_blocked_streams: None,
1063                h3_datagram: None,
1064                connect_protocol_enabled: None,
1065                additional_settings: Default::default(),
1066                raw: Default::default(),
1067            },
1068
1069            control_stream_id: None,
1070            peer_control_stream_id: None,
1071
1072            qpack_encoder: qpack::Encoder::new(),
1073            qpack_decoder: qpack::Decoder::new(),
1074
1075            local_qpack_streams: Default::default(),
1076            peer_qpack_streams: Default::default(),
1077
1078            max_push_id: 0,
1079
1080            finished_streams: VecDeque::new(),
1081
1082            frames_greased: false,
1083
1084            local_goaway_id: None,
1085            peer_goaway_id: None,
1086
1087            max_priority_update_size: config.max_priority_update_size,
1088        })
1089    }
1090
1091    /// Creates a new HTTP/3 connection using the provided QUIC connection.
1092    ///
1093    /// This will also initiate the HTTP/3 handshake with the peer by opening
1094    /// all control streams (including QPACK) and sending the local settings.
1095    ///
1096    /// On success the new connection is returned.
1097    ///
1098    /// The [`StreamLimit`] error is returned when the HTTP/3 control stream
1099    /// cannot be created due to stream limits.
1100    ///
1101    /// The [`InternalError`] error is returned when either the underlying QUIC
1102    /// connection is not in a suitable state, or the HTTP/3 control stream
1103    /// cannot be created due to flow control limits.
1104    ///
1105    /// [`StreamLimit`]: ../enum.Error.html#variant.StreamLimit
1106    /// [`InternalError`]: ../enum.Error.html#variant.InternalError
1107    pub fn with_transport<F: BufFactory>(
1108        conn: &mut super::Connection<F>, config: &Config,
1109    ) -> Result<Connection> {
1110        let is_client = !conn.is_server;
1111        if is_client && !(conn.is_established() || conn.is_in_early_data()) {
1112            trace!("{} QUIC connection must be established or in early data before creating an HTTP/3 connection", conn.trace_id());
1113            return Err(Error::InternalError);
1114        }
1115
1116        let mut http3_conn =
1117            Connection::new(config, conn.is_server, conn.dgram_enabled())?;
1118
1119        match http3_conn.send_settings(conn) {
1120            Ok(_) => (),
1121
1122            Err(e) => {
1123                conn.close(true, e.to_wire(), b"Error opening control stream")?;
1124                return Err(e);
1125            },
1126        };
1127
1128        // Try opening QPACK streams, but ignore errors if it fails since we
1129        // don't need them right now.
1130        http3_conn.open_qpack_encoder_stream(conn).ok();
1131        http3_conn.open_qpack_decoder_stream(conn).ok();
1132
1133        if conn.grease {
1134            // Try opening a GREASE stream, but ignore errors since it's not
1135            // critical.
1136            http3_conn.open_grease_stream(conn).ok();
1137        }
1138
1139        Ok(http3_conn)
1140    }
1141
1142    /// Sends an HTTP/3 request.
1143    ///
1144    /// The request is encoded from the provided list of headers without a
1145    /// body, and sent on a newly allocated stream. To include a body,
1146    /// set `fin` as `false` and subsequently call [`send_body()`] with the
1147    /// same `conn` and the `stream_id` returned from this method.
1148    ///
1149    /// On success the newly allocated stream ID is returned.
1150    ///
1151    /// The [`StreamBlocked`] error is returned when the underlying QUIC stream
1152    /// doesn't have enough capacity for the operation to complete. When this
1153    /// happens the application should retry the **entire** `send_request` call
1154    /// once the stream is reported as writable again. Any partial state created
1155    /// by the failed call is rolled back, so repeating the call with the same
1156    /// arguments is safe.
1157    ///
1158    /// [`send_body()`]: struct.Connection.html#method.send_body
1159    /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1160    pub fn send_request<T: NameValue, F: BufFactory>(
1161        &mut self, conn: &mut super::Connection<F>, headers: &[T], fin: bool,
1162    ) -> Result<u64> {
1163        // If we received a GOAWAY from the peer, MUST NOT initiate new
1164        // requests.
1165        if self.peer_goaway_id.is_some() {
1166            return Err(Error::FrameUnexpected);
1167        }
1168
1169        let stream_id = self.next_request_stream_id;
1170
1171        self.streams.insert(
1172            stream_id,
1173            <stream::Stream>::new(
1174                stream_id,
1175                true,
1176                self.local_settings
1177                    .max_field_section_size
1178                    .unwrap_or(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
1179                self.max_priority_update_size,
1180            ),
1181        );
1182
1183        // The underlying QUIC stream does not exist yet, so calls to e.g.
1184        // stream_capacity() will fail. By writing a 0-length buffer, we force
1185        // the creation of the QUIC stream state, without actually writing
1186        // anything.
1187        if let Err(e) = conn.stream_send(stream_id, b"", false) {
1188            self.streams.remove(&stream_id);
1189
1190            if e == super::Error::Done {
1191                return Err(Error::StreamBlocked);
1192            }
1193
1194            return Err(e.into());
1195        };
1196
1197        if let Err(e) = self.send_headers(conn, stream_id, headers, fin) {
1198            // If the stream was blocked before any header bytes were written,
1199            // the QUIC stream exists but carries no H3 data yet. Roll back the
1200            // H3-layer stream entry so the stream ID is not consumed and a
1201            // subsequent retry of `send_request` starts from a clean state,
1202            // consistent with the `StreamBlocked` path above.
1203            if e == Error::StreamBlocked {
1204                self.streams.remove(&stream_id);
1205            }
1206
1207            return Err(e);
1208        }
1209
1210        // To avoid skipping stream IDs, we only calculate the next available
1211        // stream ID when a request has been successfully buffered.
1212        self.next_request_stream_id = self
1213            .next_request_stream_id
1214            .checked_add(4)
1215            .ok_or(Error::IdError)?;
1216
1217        Ok(stream_id)
1218    }
1219
1220    /// Sends an HTTP/3 response on the specified stream with default priority.
1221    ///
1222    /// This method sends the provided `headers` as a single initial response
1223    /// without a body.
1224    ///
1225    /// To send a non-final 1xx, then a final 200+ without body:
1226    ///   * send_response() with `fin` set to `false`.
1227    ///   * [`send_additional_headers()`] with fin set to `true` using the same
1228    ///     `stream_id` value.
1229    ///
1230    /// To send a non-final 1xx, then a final 200+ with body:
1231    ///   * send_response() with `fin` set to `false`.
1232    ///   * [`send_additional_headers()`] with fin set to `false` and same
1233    ///     `stream_id` value.
1234    ///   * [`send_body()`] with same `stream_id`.
1235    ///
1236    /// To send a final 200+ with body:
1237    ///   * send_response() with `fin` set to `false`.
1238    ///   * [`send_body()`] with same `stream_id`.
1239    ///
1240    /// Additional headers can only be sent during certain phases of an HTTP/3
1241    /// message exchange, see [Section 4.1 of RFC 9114]. The [`FrameUnexpected`]
1242    /// error is returned if this method, or [`send_response_with_priority()`],
1243    /// are called multiple times with the same `stream_id` value.
1244    ///
1245    /// The [`StreamBlocked`] error is returned when the underlying QUIC stream
1246    /// doesn't have enough capacity for the operation to complete. When this
1247    /// happens the application should retry the operation once the stream is
1248    /// reported as writable again.
1249    ///
1250    /// [`send_body()`]: struct.Connection.html#method.send_body
1251    /// [`send_additional_headers()`]:
1252    ///     struct.Connection.html#method.send_additional_headers
1253    /// [`send_response_with_priority()`]:
1254    ///     struct.Connection.html#method.send_response_with_priority
1255    /// [`FrameUnexpected`]: enum.Error.html#variant.FrameUnexpected
1256    /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1257    pub fn send_response<T: NameValue, F: BufFactory>(
1258        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1259        headers: &[T], fin: bool,
1260    ) -> Result<()> {
1261        let priority = Default::default();
1262
1263        self.send_response_with_priority(
1264            conn, stream_id, headers, &priority, fin,
1265        )?;
1266
1267        Ok(())
1268    }
1269
1270    /// Sends an HTTP/3 response on the specified stream with specified
1271    /// priority.
1272    ///
1273    /// This method sends the provided `headers` as a single initial response
1274    /// without a body.
1275    ///
1276    /// To send a non-final 1xx, then a final 200+ without body:
1277    ///   * send_response_with_priority() with `fin` set to `false`.
1278    ///   * [`send_additional_headers()`] with fin set to `true` using the same
1279    ///     `stream_id` value.
1280    ///
1281    /// To send a non-final 1xx, then a final 200+ with body:
1282    ///   * send_response_with_priority() with `fin` set to `false`.
1283    ///   * [`send_additional_headers()`] with fin set to `false` and same
1284    ///     `stream_id` value.
1285    ///   * [`send_body()`] with same `stream_id`.
1286    ///
1287    /// To send a final 200+ with body:
1288    ///   * send_response_with_priority() with `fin` set to `false`.
1289    ///   * [`send_body()`] with same `stream_id`.
1290    ///
1291    /// The `priority` parameter represents [Extensible Priority]
1292    /// parameters. If the urgency is outside the range 0-7, it will be clamped
1293    /// to 7.
1294    ///
1295    /// Additional headers can only be sent during certain phases of an HTTP/3
1296    /// message exchange, see [Section 4.1 of RFC 9114]. The [`FrameUnexpected`]
1297    /// error is returned if this method, or [`send_response()`],
1298    /// are called multiple times with the same `stream_id` value.
1299    ///
1300    /// The [`StreamBlocked`] error is returned when the underlying QUIC stream
1301    /// doesn't have enough capacity for the operation to complete. When this
1302    /// happens the application should retry the operation once the stream is
1303    /// reported as writable again.
1304    ///
1305    /// [`send_body()`]: struct.Connection.html#method.send_body
1306    /// [`send_additional_headers()`]:
1307    ///     struct.Connection.html#method.send_additional_headers
1308    /// [`send_response()`]:
1309    ///     struct.Connection.html#method.send_response
1310    /// [`FrameUnexpected`]: enum.Error.html#variant.FrameUnexpected
1311    /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1312    /// [Extensible Priority]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
1313    pub fn send_response_with_priority<T: NameValue, F: BufFactory>(
1314        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1315        headers: &[T], priority: &Priority, fin: bool,
1316    ) -> Result<()> {
1317        match self.streams.get(&stream_id) {
1318            Some(s) => {
1319                // Only one initial HEADERS allowed.
1320                if s.local_initialized() {
1321                    return Err(Error::FrameUnexpected);
1322                }
1323
1324                s
1325            },
1326
1327            None => return Err(Error::FrameUnexpected),
1328        };
1329
1330        self.send_headers(conn, stream_id, headers, fin)?;
1331
1332        // Clamp and shift urgency into quiche-priority space
1333        let urgency = priority
1334            .urgency
1335            .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND) +
1336            PRIORITY_URGENCY_OFFSET;
1337
1338        conn.stream_priority(stream_id, urgency, priority.incremental)?;
1339
1340        Ok(())
1341    }
1342
1343    /// Sends additional HTTP/3 headers.
1344    ///
1345    /// After the initial request or response headers have been sent, using
1346    /// [`send_request()`] or [`send_response()`] respectively, this method can
1347    /// be used send an additional HEADERS frame. For example, to send a single
1348    /// instance of trailers after a request with a body, or to issue another
1349    /// non-final 1xx after a preceding 1xx, or to issue a final response after
1350    /// a preceding 1xx.
1351    ///
1352    /// Additional headers can only be sent during certain phases of an HTTP/3
1353    /// message exchange, see [Section 4.1 of RFC 9114]. The [`FrameUnexpected`]
1354    /// error is returned when this method is called during the wrong phase,
1355    /// such as before initial headers have been sent, or if trailers have
1356    /// already been sent.
1357    ///
1358    /// The [`StreamBlocked`] error is returned when the underlying QUIC stream
1359    /// doesn't have enough capacity for the operation to complete. When this
1360    /// happens the application should retry the operation once the stream is
1361    /// reported as writable again.
1362    ///
1363    /// [`send_request()`]: struct.Connection.html#method.send_request
1364    /// [`send_response()`]: struct.Connection.html#method.send_response
1365    /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1366    /// [`FrameUnexpected`]: enum.Error.html#variant.FrameUnexpected
1367    /// [Section 4.1 of RFC 9114]:
1368    ///     https://www.rfc-editor.org/rfc/rfc9114.html#section-4.1.
1369    pub fn send_additional_headers<T: NameValue, F: BufFactory>(
1370        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1371        headers: &[T], is_trailer_section: bool, fin: bool,
1372    ) -> Result<()> {
1373        // Clients can only send trailer headers.
1374        if !self.is_server && !is_trailer_section {
1375            return Err(Error::FrameUnexpected);
1376        }
1377
1378        match self.streams.get(&stream_id) {
1379            Some(s) => {
1380                // Initial HEADERS must have been sent.
1381                if !s.local_initialized() {
1382                    return Err(Error::FrameUnexpected);
1383                }
1384
1385                // Only one trailing HEADERS allowed.
1386                if s.trailers_sent() {
1387                    return Err(Error::FrameUnexpected);
1388                }
1389
1390                s
1391            },
1392
1393            None => return Err(Error::FrameUnexpected),
1394        };
1395
1396        self.send_headers(conn, stream_id, headers, fin)?;
1397
1398        if is_trailer_section {
1399            // send_headers() might have tidied the stream away, so we need to
1400            // check again.
1401            if let Some(s) = self.streams.get_mut(&stream_id) {
1402                s.mark_trailers_sent();
1403            }
1404        }
1405
1406        Ok(())
1407    }
1408
1409    /// Sends additional HTTP/3 headers with specified priority.
1410    ///
1411    /// After the initial request or response headers have been sent, using
1412    /// [`send_request()`] or [`send_response()`] respectively, this method can
1413    /// be used send an additional HEADERS frame. For example, to send a single
1414    /// instance of trailers after a request with a body, or to issue another
1415    /// non-final 1xx after a preceding 1xx, or to issue a final response after
1416    /// a preceding 1xx.
1417    ///
1418    /// The `priority` parameter represents [Extensible Priority]
1419    /// parameters. If the urgency is outside the range 0-7, it will be clamped
1420    /// to 7.
1421    ///
1422    /// Additional headers can only be sent during certain phases of an HTTP/3
1423    /// message exchange, see [Section 4.1 of RFC 9114]. The [`FrameUnexpected`]
1424    /// error is returned when this method is called during the wrong phase,
1425    /// such as before initial headers have been sent, or if trailers have
1426    /// already been sent.
1427    ///
1428    /// The [`StreamBlocked`] error is returned when the underlying QUIC stream
1429    /// doesn't have enough capacity for the operation to complete. When this
1430    /// happens the application should retry the operation once the stream is
1431    /// reported as writable again.
1432    ///
1433    /// [`send_request()`]: struct.Connection.html#method.send_request
1434    /// [`send_response()`]: struct.Connection.html#method.send_response
1435    /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1436    /// [`FrameUnexpected`]: enum.Error.html#variant.FrameUnexpected
1437    /// [Section 4.1 of RFC 9114]:
1438    ///     https://www.rfc-editor.org/rfc/rfc9114.html#section-4.1.
1439    /// [Extensible Priority]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
1440    pub fn send_additional_headers_with_priority<T: NameValue, F: BufFactory>(
1441        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1442        headers: &[T], priority: &Priority, is_trailer_section: bool, fin: bool,
1443    ) -> Result<()> {
1444        self.send_additional_headers(
1445            conn,
1446            stream_id,
1447            headers,
1448            is_trailer_section,
1449            fin,
1450        )?;
1451
1452        // Clamp and shift urgency into quiche-priority space
1453        let urgency = priority
1454            .urgency
1455            .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND) +
1456            PRIORITY_URGENCY_OFFSET;
1457
1458        conn.stream_priority(stream_id, urgency, priority.incremental)?;
1459
1460        Ok(())
1461    }
1462
1463    fn encode_header_block<T: NameValue>(
1464        &mut self, headers: &[T],
1465    ) -> Result<Vec<u8>> {
1466        let headers_len = headers
1467            .iter()
1468            .fold(0, |acc, h| acc + h.value().len() + h.name().len() + 32);
1469
1470        let mut header_block = vec![0; headers_len];
1471        let len = self
1472            .qpack_encoder
1473            .encode(headers, &mut header_block)
1474            .map_err(|_| Error::InternalError)?;
1475
1476        header_block.truncate(len);
1477
1478        Ok(header_block)
1479    }
1480
1481    fn send_headers<T: NameValue, F: BufFactory>(
1482        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1483        headers: &[T], fin: bool,
1484    ) -> Result<()> {
1485        let mut d = [42; 10];
1486        let mut b = octets::OctetsMut::with_slice(&mut d);
1487
1488        if !self.frames_greased && conn.grease {
1489            self.send_grease_frames(conn, stream_id)?;
1490            self.frames_greased = true;
1491        }
1492
1493        let header_block = self.encode_header_block(headers)?;
1494
1495        let overhead = octets::varint_len(frame::HEADERS_FRAME_TYPE_ID) +
1496            octets::varint_len(header_block.len() as u64);
1497
1498        // Headers need to be sent atomically, so make sure the stream has
1499        // enough capacity.
1500        match conn.stream_writable(stream_id, overhead + header_block.len()) {
1501            Ok(true) => (),
1502
1503            Ok(false) => return Err(Error::StreamBlocked),
1504
1505            Err(e) => {
1506                if conn.stream_finished(stream_id) {
1507                    self.streams.remove(&stream_id);
1508                }
1509
1510                return Err(e.into());
1511            },
1512        };
1513
1514        b.put_varint(frame::HEADERS_FRAME_TYPE_ID)?;
1515        b.put_varint(header_block.len() as u64)?;
1516        let off = b.off();
1517        conn.stream_send(stream_id, &d[..off], false)?;
1518
1519        // Sending header block separately avoids unnecessary copy.
1520        conn.stream_send(stream_id, &header_block, fin)?;
1521
1522        trace!(
1523            "{} tx frm HEADERS stream={} len={} fin={}",
1524            conn.trace_id(),
1525            stream_id,
1526            header_block.len(),
1527            fin
1528        );
1529
1530        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
1531            let qlog_headers = headers
1532                .iter()
1533                .map(|h| qlog::events::http3::HttpHeader {
1534                    name: Some(String::from_utf8_lossy(h.name()).into_owned()),
1535                    name_bytes: None,
1536                    value: Some(String::from_utf8_lossy(h.value()).into_owned()),
1537                    value_bytes: None,
1538                })
1539                .collect();
1540
1541            let frame = Http3Frame::Headers {
1542                headers: qlog_headers,
1543                raw: None,
1544            };
1545            let ev_data = EventData::Http3FrameCreated(FrameCreated {
1546                stream_id,
1547                length: Some(header_block.len() as u64),
1548                frame,
1549                ..Default::default()
1550            });
1551
1552            q.add_event_data_now(ev_data).ok();
1553        });
1554
1555        if fin {
1556            self.finish_local_stream(conn, stream_id, true);
1557        } else if let Some(s) = self.streams.get_mut(&stream_id) {
1558            s.initialize_local();
1559        }
1560
1561        Ok(())
1562    }
1563
1564    /// Sends an HTTP/3 body chunk on the given stream.
1565    ///
1566    /// On success the number of bytes written is returned, or [`Done`] if no
1567    /// bytes could be written (e.g. because the stream is blocked).
1568    ///
1569    /// Note that the number of written bytes returned can be lower than the
1570    /// length of the input buffer when the underlying QUIC stream doesn't have
1571    /// enough capacity for the operation to complete.
1572    ///
1573    /// When a partial write happens (including when [`Done`] is returned) the
1574    /// application should retry the operation once the stream is reported as
1575    /// writable again.
1576    ///
1577    /// [`Done`]: enum.Error.html#variant.Done
1578    pub fn send_body<F: BufFactory>(
1579        &mut self, conn: &mut super::Connection<F>, stream_id: u64, body: &[u8],
1580        fin: bool,
1581    ) -> Result<usize> {
1582        self.do_send_body(
1583            conn,
1584            stream_id,
1585            body,
1586            fin,
1587            |conn: &mut super::Connection<F>,
1588             header: &[u8],
1589             stream_id: u64,
1590             body: &[u8],
1591             body_len: usize,
1592             fin: bool| {
1593                conn.stream_send(stream_id, header, false)?;
1594                Ok(conn
1595                    .stream_send(stream_id, &body[..body_len], fin)
1596                    .map(|v| (v, v))?)
1597            },
1598        )
1599    }
1600
1601    /// Sends an HTTP/3 body chunk provided as a raw buffer on the given stream.
1602    ///
1603    /// If the capacity allows it the buffer will be appended to the stream's
1604    /// send queue with zero copying.
1605    ///
1606    /// On success the number of bytes written is returned, or [`Done`] if no
1607    /// bytes could be written (e.g. because the stream is blocked).
1608    ///
1609    /// Note that the number of written bytes returned can be lower than the
1610    /// length of the input buffer when the underlying QUIC stream doesn't have
1611    /// enough capacity for the operation to complete.
1612    ///
1613    /// When a partial write happens (including when [`Done`] is returned) the
1614    /// remaining (unwrittent) buffer will also be returned. The application
1615    /// should retry the operation once the stream is reported as writable
1616    /// again.
1617    ///
1618    /// [`Done`]: enum.Error.html#variant.Done
1619    pub fn send_body_zc<F>(
1620        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1621        body: &mut F::Buf, fin: bool,
1622    ) -> Result<usize>
1623    where
1624        F: BufFactory,
1625        F::Buf: BufSplit,
1626    {
1627        self.do_send_body(
1628            conn,
1629            stream_id,
1630            body,
1631            fin,
1632            |conn: &mut super::Connection<F>,
1633             header: &[u8],
1634             stream_id: u64,
1635             body: &mut F::Buf,
1636             mut body_len: usize,
1637             fin: bool| {
1638                let with_prefix = body.try_add_prefix(header);
1639                if !with_prefix {
1640                    conn.stream_send(stream_id, header, false)?;
1641                } else {
1642                    body_len += header.len();
1643                }
1644
1645                let remainder = body.split_at(body_len);
1646                // body now contains the first `body_len` bytes of the original
1647                // buffer
1648                debug_assert_eq!(body.as_ref().len(), body_len);
1649
1650                let (mut n, rem) =
1651                    conn.stream_send_zc(stream_id, body.clone(), fin)?;
1652                if rem.as_ref().is_some_and(|v| !v.as_ref().is_empty()) {
1653                    // `do_send_body()` checked capacity before this write, so
1654                    // `rem` should always be `None` or empty.
1655                    debug_assert!(false);
1656                    return Err(Error::InternalError);
1657                }
1658
1659                if with_prefix {
1660                    n -= header.len();
1661                }
1662
1663                if !remainder.as_ref().is_empty() {
1664                    let _ = std::mem::replace(body, remainder);
1665                }
1666
1667                Ok((n, n))
1668            },
1669        )
1670    }
1671
1672    fn do_send_body<F, B, R, SND>(
1673        &mut self, conn: &mut super::Connection<F>, stream_id: u64, body: B,
1674        fin: bool, write_fn: SND,
1675    ) -> Result<R>
1676    where
1677        F: BufFactory,
1678        B: AsRef<[u8]>,
1679        SND: FnOnce(
1680            &mut super::Connection<F>,
1681            &[u8],
1682            u64,
1683            B,
1684            usize,
1685            bool,
1686        ) -> Result<(usize, R)>,
1687    {
1688        let mut d = [42; 10];
1689        let mut b = octets::OctetsMut::with_slice(&mut d);
1690
1691        let len = body.as_ref().len();
1692
1693        // Validate that it is sane to send data on the stream.
1694        if !stream_id.is_multiple_of(4) {
1695            return Err(Error::FrameUnexpected);
1696        }
1697
1698        match self.streams.get_mut(&stream_id) {
1699            Some(s) => {
1700                if !s.local_initialized() {
1701                    return Err(Error::FrameUnexpected);
1702                }
1703
1704                if s.trailers_sent() {
1705                    return Err(Error::FrameUnexpected);
1706                }
1707            },
1708
1709            None => {
1710                return Err(Error::FrameUnexpected);
1711            },
1712        };
1713
1714        // Avoid sending 0-length DATA frames when the fin flag is false.
1715        if len == 0 && !fin {
1716            return Err(Error::Done);
1717        }
1718
1719        let overhead = octets::varint_len(frame::DATA_FRAME_TYPE_ID) +
1720            octets::varint_len(len as u64);
1721
1722        let stream_cap = match conn.stream_capacity(stream_id) {
1723            Ok(v) => v,
1724
1725            Err(e) => {
1726                if conn.stream_finished(stream_id) {
1727                    self.streams.remove(&stream_id);
1728                }
1729
1730                return Err(e.into());
1731            },
1732        };
1733
1734        // Make sure there is enough capacity to send the DATA frame header.
1735        if stream_cap < overhead {
1736            let _ = conn.stream_writable(stream_id, overhead + 1);
1737            return Err(Error::Done);
1738        }
1739
1740        // Cap the frame payload length to the stream's capacity.
1741        let body_len = std::cmp::min(len, stream_cap - overhead);
1742
1743        // If we can't send the entire body, set the fin flag to false so the
1744        // application can try again later.
1745        let fin = if body_len != len { false } else { fin };
1746
1747        // Again, avoid sending 0-length DATA frames when the fin flag is false.
1748        if body_len == 0 && !fin {
1749            let _ = conn.stream_writable(stream_id, overhead + 1);
1750            return Err(Error::Done);
1751        }
1752
1753        b.put_varint(frame::DATA_FRAME_TYPE_ID)?;
1754        b.put_varint(body_len as u64)?;
1755        let off = b.off();
1756
1757        // Return how many bytes were written, excluding the frame header.
1758        // Sending body separately avoids unnecessary copy.
1759        let (written, ret) =
1760            write_fn(conn, &d[..off], stream_id, body, body_len, fin)?;
1761        if written != body_len {
1762            // This should never happen. If it does, it means we wrote an
1763            // incorrect frame length and thus we can't really continue.
1764            debug_assert!(false);
1765            return Err(Error::InternalError);
1766        }
1767
1768        trace!(
1769            "{} tx frm DATA stream={} len={} fin={}",
1770            conn.trace_id(),
1771            stream_id,
1772            written,
1773            fin
1774        );
1775
1776        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
1777            let frame = Http3Frame::Data { raw: None };
1778            let ev_data = EventData::Http3FrameCreated(FrameCreated {
1779                stream_id,
1780                length: Some(written as u64),
1781                frame,
1782                ..Default::default()
1783            });
1784
1785            q.add_event_data_now(ev_data).ok();
1786        });
1787
1788        if written < len {
1789            // Ensure the peer is notified that the connection or stream is
1790            // blocked when the stream's capacity is limited by flow control.
1791            //
1792            // We only need enough capacity to send a few bytes, to make sure
1793            // the stream doesn't hang due to congestion window not growing
1794            // enough.
1795            let _ = conn.stream_writable(stream_id, overhead + 1);
1796        }
1797
1798        if fin && written == len {
1799            self.finish_local_stream(conn, stream_id, false);
1800        }
1801
1802        Ok(ret)
1803    }
1804
1805    /// Returns whether the peer enabled HTTP/3 DATAGRAM frame support.
1806    ///
1807    /// Support is signalled by the peer's SETTINGS, so this method always
1808    /// returns false until they have been processed using the [`poll()`]
1809    /// method.
1810    ///
1811    /// [`poll()`]: struct.Connection.html#method.poll
1812    pub fn dgram_enabled_by_peer<F: BufFactory>(
1813        &self, conn: &super::Connection<F>,
1814    ) -> bool {
1815        self.peer_settings.h3_datagram == Some(1) &&
1816            conn.dgram_max_writable_len().is_some()
1817    }
1818
1819    /// Returns whether the peer enabled extended CONNECT support.
1820    ///
1821    /// Support is signalled by the peer's SETTINGS, so this method always
1822    /// returns false until they have been processed using the [`poll()`]
1823    /// method.
1824    ///
1825    /// [`poll()`]: struct.Connection.html#method.poll
1826    pub fn extended_connect_enabled_by_peer(&self) -> bool {
1827        self.peer_settings.connect_protocol_enabled == Some(1)
1828    }
1829
1830    /// Reads request or response body data into the provided buffer.
1831    ///
1832    /// Applications should call this method whenever the [`poll()`] method
1833    /// returns a [`Data`] event.
1834    ///
1835    /// On success the amount of bytes read is returned, or [`Done`] if there
1836    /// is no data to read.
1837    ///
1838    /// [`poll()`]: struct.Connection.html#method.poll
1839    /// [`Data`]: enum.Event.html#variant.Data
1840    /// [`Done`]: enum.Error.html#variant.Done
1841    pub fn recv_body<F: BufFactory>(
1842        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1843        out: &mut [u8],
1844    ) -> Result<usize> {
1845        self.recv_body_buf(conn, stream_id, out)
1846    }
1847
1848    /// Reads request or response body data into the provided BufMut buffer.
1849    ///
1850    /// **NOTE**:
1851    /// The BufMut will be populated with all available data up to its capacity.
1852    /// Since some BufMut implementations, e.g., [`Vec<u8>`], dynamically
1853    /// allocate additional memory, the caller may use
1854    /// [`BufMut::limit()`] to limit the maximum amount of data that
1855    /// can be written.
1856    ///
1857    /// Applications should call this method (or [`recv_body()`]) whenever
1858    /// the [`poll()`] method returns a [`Data`] event.
1859    ///
1860    /// On success the amount of bytes read is returned, or [`Done`] if there
1861    /// is no data to read.
1862    ///
1863    /// [`BufMut::limit()`]: bytes::BufMut::limit()
1864    /// [`recv_body()`]: Self::recv_body()
1865    /// [`poll()`]: struct.Connection.html#method.poll
1866    /// [`Data`]: enum.Event.html#variant.Data
1867    /// [`Done`]: enum.Error.html#variant.Done
1868    ///
1869    /// ## Example:
1870    /// ```no_run
1871    /// # use quiche::h3;
1872    /// fn receive(
1873    ///     qconn: &mut quiche::Connection, h3conn: &mut h3::Connection,
1874    /// ) -> Result<Vec<u8>, h3::Error> {
1875    ///     use bytes::BufMut as _;
1876    ///     let mut buffer = Vec::with_capacity(2048).limit(2048);
1877    ///     let bytes = h3conn.recv_body_buf(qconn, 0, &mut buffer)?;
1878    ///     let buffer = buffer.into_inner();
1879    ///     // The vec has been filled with exactly `bytes` number of bytes
1880    ///     assert_eq!(buffer.len(), bytes);
1881    ///     Ok(buffer)
1882    /// }
1883    /// ```
1884    pub fn recv_body_buf<F: BufFactory, OUT: bytes::BufMut>(
1885        &mut self, conn: &mut super::Connection<F>, stream_id: u64, mut out: OUT,
1886    ) -> Result<usize> {
1887        let mut total = 0;
1888
1889        // Try to consume all buffered data for the stream, even across multiple
1890        // DATA frames.
1891        // Note, that even if the BufMut does not have a limit defined, we are
1892        // inherently limited by how much data is in quiche's receive buffer for
1893        // that stream, so the BufMut cannot grow unbounded.
1894        while out.has_remaining_mut() {
1895            let stream = self.streams.get_mut(&stream_id).ok_or(Error::Done)?;
1896
1897            if stream.state() != stream::State::Data {
1898                break;
1899            }
1900
1901            let (read, fin) = match stream.try_consume_data(conn, &mut out) {
1902                Ok(v) => v,
1903
1904                Err(Error::Done) => break,
1905
1906                Err(e) => return Err(e),
1907            };
1908
1909            total += read;
1910
1911            // No more data to read, we are done.
1912            if read == 0 || fin {
1913                break;
1914            }
1915
1916            // Process incoming data from the stream. For example, if a whole
1917            // DATA frame was consumed, and another one is queued behind it,
1918            // this will ensure the additional data will also be returned to
1919            // the application.
1920            match self.process_readable_stream(conn, stream_id, false) {
1921                Ok(_) => unreachable!(),
1922
1923                Err(Error::Done) => (),
1924
1925                Err(e) => return Err(e),
1926            };
1927
1928            if conn.stream_finished(stream_id) {
1929                break;
1930            }
1931        }
1932
1933        // While body is being received, the stream is marked as finished only
1934        // when all data is read by the application.
1935        if conn.stream_finished(stream_id) {
1936            self.process_finished_stream(stream_id);
1937        }
1938
1939        if total == 0 {
1940            return Err(Error::Done);
1941        }
1942
1943        Ok(total)
1944    }
1945
1946    /// Sends a PRIORITY_UPDATE frame on the control stream with specified
1947    /// request stream ID and priority.
1948    ///
1949    /// The `priority` parameter represents [Extensible Priority]
1950    /// parameters. If the urgency is outside the range 0-7, it will be clamped
1951    /// to 7.
1952    ///
1953    /// The [`StreamBlocked`] error is returned when the underlying QUIC stream
1954    /// doesn't have enough capacity for the operation to complete. When this
1955    /// happens the application should retry the operation once the stream is
1956    /// reported as writable again.
1957    ///
1958    /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1959    /// [Extensible Priority]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
1960    pub fn send_priority_update_for_request<F: BufFactory>(
1961        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1962        priority: &Priority,
1963    ) -> Result<()> {
1964        let mut d = [42; 20];
1965        let mut b = octets::OctetsMut::with_slice(&mut d);
1966
1967        // Validate that it is sane to send PRIORITY_UPDATE.
1968        if self.is_server {
1969            return Err(Error::FrameUnexpected);
1970        }
1971
1972        if !stream_id.is_multiple_of(4) {
1973            return Err(Error::FrameUnexpected);
1974        }
1975
1976        let control_stream_id =
1977            self.control_stream_id.ok_or(Error::FrameUnexpected)?;
1978
1979        let urgency = priority
1980            .urgency
1981            .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND);
1982
1983        let mut field_value = format!("u={urgency}");
1984
1985        if priority.incremental {
1986            field_value.push_str(",i");
1987        }
1988
1989        let priority_field_value = field_value.as_bytes();
1990        let frame_payload_len =
1991            octets::varint_len(stream_id) + priority_field_value.len();
1992
1993        let overhead =
1994            octets::varint_len(frame::PRIORITY_UPDATE_FRAME_REQUEST_TYPE_ID) +
1995                octets::varint_len(stream_id) +
1996                octets::varint_len(frame_payload_len as u64);
1997
1998        // Make sure the control stream has enough capacity.
1999        match conn.stream_writable(
2000            control_stream_id,
2001            overhead + priority_field_value.len(),
2002        ) {
2003            Ok(true) => (),
2004
2005            Ok(false) => return Err(Error::StreamBlocked),
2006
2007            Err(e) => {
2008                return Err(e.into());
2009            },
2010        }
2011
2012        b.put_varint(frame::PRIORITY_UPDATE_FRAME_REQUEST_TYPE_ID)?;
2013        b.put_varint(frame_payload_len as u64)?;
2014        b.put_varint(stream_id)?;
2015        let off = b.off();
2016        conn.stream_send(control_stream_id, &d[..off], false)?;
2017
2018        // Sending field value separately avoids unnecessary copy.
2019        conn.stream_send(control_stream_id, priority_field_value, false)?;
2020
2021        trace!(
2022            "{} tx frm PRIORITY_UPDATE request_stream={} priority_field_value={}",
2023            conn.trace_id(),
2024            stream_id,
2025            field_value,
2026        );
2027
2028        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2029            let frame = Http3Frame::PriorityUpdate {
2030                stream_id: Some(stream_id),
2031                push_id: None,
2032                priority_field_value: field_value.clone(),
2033                raw: None,
2034            };
2035
2036            let ev_data = EventData::Http3FrameCreated(FrameCreated {
2037                stream_id,
2038                length: Some(priority_field_value.len() as u64),
2039                frame,
2040                ..Default::default()
2041            });
2042
2043            q.add_event_data_now(ev_data).ok();
2044        });
2045
2046        Ok(())
2047    }
2048
2049    /// Take the last PRIORITY_UPDATE for a prioritized element ID.
2050    ///
2051    /// When the [`poll()`] method returns a [`PriorityUpdate`] event for a
2052    /// prioritized element, the event has triggered and will not rearm until
2053    /// applications call this method. It is recommended that applications defer
2054    /// taking the PRIORITY_UPDATE until after [`poll()`] returns [`Done`].
2055    ///
2056    /// On success the Priority Field Value is returned, or [`Done`] if there is
2057    /// no PRIORITY_UPDATE to read (either because there is no value to take, or
2058    /// because the prioritized element does not exist).
2059    ///
2060    /// [`poll()`]: struct.Connection.html#method.poll
2061    /// [`PriorityUpdate`]: enum.Event.html#variant.PriorityUpdate
2062    /// [`Done`]: enum.Error.html#variant.Done
2063    pub fn take_last_priority_update(
2064        &mut self, prioritized_element_id: u64,
2065    ) -> Result<Vec<u8>> {
2066        if let Some(stream) = self.streams.get_mut(&prioritized_element_id) {
2067            return stream.take_last_priority_update().ok_or(Error::Done);
2068        }
2069
2070        Err(Error::Done)
2071    }
2072
2073    /// Processes HTTP/3 data received from the peer.
2074    ///
2075    /// On success it returns an [`Event`] and an ID, or [`Done`] when there are
2076    /// no events to report.
2077    ///
2078    /// Note that all events are edge-triggered, meaning that once reported they
2079    /// will not be reported again by calling this method again, until the event
2080    /// is re-armed.
2081    ///
2082    /// The events [`Headers`], [`Data`] and [`Finished`] return a stream ID,
2083    /// which is used in methods [`recv_body()`], [`send_response()`] or
2084    /// [`send_body()`].
2085    ///
2086    /// The event [`GoAway`] returns an ID that depends on the connection role.
2087    /// A client receives the largest processed stream ID. A server receives the
2088    /// the largest permitted push ID.
2089    ///
2090    /// The event [`PriorityUpdate`] only occurs at servers. It returns a
2091    /// prioritized element ID that is used in the method
2092    /// [`take_last_priority_update()`], which rearms the event for that ID.
2093    ///
2094    /// If an error occurs while processing data, the connection is closed with
2095    /// the appropriate error code, using the transport's [`close()`] method.
2096    ///
2097    /// [`Event`]: enum.Event.html
2098    /// [`Done`]: enum.Error.html#variant.Done
2099    /// [`Headers`]: enum.Event.html#variant.Headers
2100    /// [`Data`]: enum.Event.html#variant.Data
2101    /// [`Finished`]: enum.Event.html#variant.Finished
2102    /// [`GoAway`]: enum.Event.html#variant.GoAWay
2103    /// [`PriorityUpdate`]: enum.Event.html#variant.PriorityUpdate
2104    /// [`recv_body()`]: struct.Connection.html#method.recv_body
2105    /// [`send_response()`]: struct.Connection.html#method.send_response
2106    /// [`send_body()`]: struct.Connection.html#method.send_body
2107    /// [`recv_dgram()`]: struct.Connection.html#method.recv_dgram
2108    /// [`take_last_priority_update()`]: struct.Connection.html#method.take_last_priority_update
2109    /// [`close()`]: ../struct.Connection.html#method.close
2110    pub fn poll<F: BufFactory>(
2111        &mut self, conn: &mut super::Connection<F>,
2112    ) -> Result<(u64, Event)> {
2113        // When connection close is initiated by the local application (e.g. due
2114        // to a protocol error), the connection itself might be in a broken
2115        // state, so return early.
2116        if conn.local_error.is_some() {
2117            return Err(Error::Done);
2118        }
2119
2120        // Process control streams first.
2121        if let Some(stream_id) = self.peer_control_stream_id {
2122            match self.process_control_stream(conn, stream_id) {
2123                Ok(ev) => return Ok(ev),
2124
2125                Err(Error::Done) => (),
2126
2127                Err(e) => return Err(e),
2128            };
2129        }
2130
2131        if let Some(stream_id) = self.peer_qpack_streams.encoder_stream_id {
2132            match self.process_control_stream(conn, stream_id) {
2133                Ok(ev) => return Ok(ev),
2134
2135                Err(Error::Done) => (),
2136
2137                Err(e) => return Err(e),
2138            };
2139        }
2140
2141        if let Some(stream_id) = self.peer_qpack_streams.decoder_stream_id {
2142            match self.process_control_stream(conn, stream_id) {
2143                Ok(ev) => return Ok(ev),
2144
2145                Err(Error::Done) => (),
2146
2147                Err(e) => return Err(e),
2148            };
2149        }
2150
2151        // Process finished streams list.
2152        if let Some(ev) = self.pop_finished_stream(conn) {
2153            return Ok(ev);
2154        }
2155
2156        // Process HTTP/3 data from readable streams.
2157        for s in conn.readable() {
2158            trace!("{} stream id {} is readable", conn.trace_id(), s);
2159
2160            let ev = match self.process_readable_stream(conn, s, true) {
2161                Ok(v) => Some(v),
2162
2163                Err(Error::Done) => None,
2164
2165                // Return early if the stream was reset, to avoid returning
2166                // a Finished event later as well.
2167                Err(Error::TransportError(crate::Error::StreamReset(e))) => {
2168                    self.remove_local_finished_stream(s);
2169
2170                    return Ok((s, Event::Reset(e)));
2171                },
2172
2173                Err(e) => return Err(e),
2174            };
2175
2176            if conn.stream_finished(s) {
2177                self.process_finished_stream(s);
2178            }
2179
2180            // TODO: check if stream is completed so it can be freed
2181            if let Some(ev) = ev {
2182                return Ok(ev);
2183            }
2184        }
2185
2186        // Process finished streams list once again, to make sure `Finished`
2187        // events are returned when receiving empty stream frames with the fin
2188        // flag set.
2189        if let Some(ev) = self.pop_finished_stream(conn) {
2190            return Ok(ev);
2191        }
2192
2193        Err(Error::Done)
2194    }
2195
2196    /// Sends a GOAWAY frame to initiate graceful connection closure.
2197    ///
2198    /// When quiche is used in the server role, the `id` parameter is the stream
2199    /// ID of the highest processed request. This can be any valid ID between 0
2200    /// and 2^62-4. However, the ID cannot be increased. Failure to satisfy
2201    /// these conditions will return an error.
2202    ///
2203    /// This method does not close the QUIC connection. Applications are
2204    /// required to call [`close()`] themselves.
2205    ///
2206    /// [`close()`]: ../struct.Connection.html#method.close
2207    pub fn send_goaway<F: BufFactory>(
2208        &mut self, conn: &mut super::Connection<F>, id: u64,
2209    ) -> Result<()> {
2210        let mut id = id;
2211
2212        // TODO: server push
2213        //
2214        // In the meantime always send 0 from client.
2215        if !self.is_server {
2216            id = 0;
2217        }
2218
2219        if self.is_server && !id.is_multiple_of(4) {
2220            return Err(Error::IdError);
2221        }
2222
2223        if let Some(sent_id) = self.local_goaway_id {
2224            if id > sent_id {
2225                return Err(Error::IdError);
2226            }
2227        }
2228
2229        if let Some(stream_id) = self.control_stream_id {
2230            let mut d = [42; 10];
2231            let mut b = octets::OctetsMut::with_slice(&mut d);
2232
2233            let frame = frame::Frame::GoAway { id };
2234
2235            let wire_len = frame.to_bytes(&mut b)?;
2236            let stream_cap = conn.stream_capacity(stream_id)?;
2237
2238            if stream_cap < wire_len {
2239                return Err(Error::StreamBlocked);
2240            }
2241
2242            trace!("{} tx frm {:?}", conn.trace_id(), frame);
2243
2244            qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2245                let ev_data = EventData::Http3FrameCreated(FrameCreated {
2246                    stream_id,
2247                    length: Some(octets::varint_len(id) as u64),
2248                    frame: frame.to_qlog(),
2249                    ..Default::default()
2250                });
2251
2252                q.add_event_data_now(ev_data).ok();
2253            });
2254
2255            let off = b.off();
2256            conn.stream_send(stream_id, &d[..off], false)?;
2257
2258            self.local_goaway_id = Some(id);
2259        }
2260
2261        Ok(())
2262    }
2263
2264    /// Gets the raw settings from peer including unknown and reserved types.
2265    ///
2266    /// The order of settings is the same as received in the SETTINGS frame.
2267    pub fn peer_settings_raw(&self) -> Option<&[(u64, u64)]> {
2268        self.peer_settings.raw.as_deref()
2269    }
2270
2271    fn open_uni_stream<F: BufFactory>(
2272        &mut self, conn: &mut super::Connection<F>, ty: u64,
2273    ) -> Result<u64> {
2274        let stream_id = self.next_uni_stream_id;
2275
2276        let mut d = [0; 8];
2277        let mut b = octets::OctetsMut::with_slice(&mut d);
2278
2279        match ty {
2280            // Control and QPACK streams are the most important to schedule.
2281            stream::HTTP3_CONTROL_STREAM_TYPE_ID |
2282            stream::QPACK_ENCODER_STREAM_TYPE_ID |
2283            stream::QPACK_DECODER_STREAM_TYPE_ID => {
2284                conn.stream_priority(stream_id, 0, false)?;
2285            },
2286
2287            // TODO: Server push
2288            stream::HTTP3_PUSH_STREAM_TYPE_ID => (),
2289
2290            // Anything else is a GREASE stream, so make it the least important.
2291            _ => {
2292                conn.stream_priority(stream_id, 255, false)?;
2293            },
2294        }
2295
2296        conn.stream_send(stream_id, b.put_varint(ty)?, false)?;
2297
2298        // To avoid skipping stream IDs, we only calculate the next available
2299        // stream ID when data has been successfully buffered.
2300        self.next_uni_stream_id = self
2301            .next_uni_stream_id
2302            .checked_add(4)
2303            .ok_or(Error::IdError)?;
2304
2305        Ok(stream_id)
2306    }
2307
2308    fn open_qpack_encoder_stream<F: BufFactory>(
2309        &mut self, conn: &mut super::Connection<F>,
2310    ) -> Result<()> {
2311        let stream_id =
2312            self.open_uni_stream(conn, stream::QPACK_ENCODER_STREAM_TYPE_ID)?;
2313
2314        self.local_qpack_streams.encoder_stream_id = Some(stream_id);
2315
2316        qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2317            let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2318                stream_id,
2319                initiator: Some(Initiator::Local),
2320                stream_type: StreamType::QpackEncode,
2321                ..Default::default()
2322            });
2323
2324            q.add_event_data_now(ev_data).ok();
2325        });
2326
2327        Ok(())
2328    }
2329
2330    fn open_qpack_decoder_stream<F: BufFactory>(
2331        &mut self, conn: &mut super::Connection<F>,
2332    ) -> Result<()> {
2333        let stream_id =
2334            self.open_uni_stream(conn, stream::QPACK_DECODER_STREAM_TYPE_ID)?;
2335
2336        self.local_qpack_streams.decoder_stream_id = Some(stream_id);
2337
2338        qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2339            let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2340                stream_id,
2341                initiator: Some(Initiator::Local),
2342                stream_type: StreamType::QpackDecode,
2343                ..Default::default()
2344            });
2345
2346            q.add_event_data_now(ev_data).ok();
2347        });
2348
2349        Ok(())
2350    }
2351
2352    /// Send GREASE frames on the provided stream ID.
2353    fn send_grease_frames<F: BufFactory>(
2354        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
2355    ) -> Result<()> {
2356        let mut d = [0; 8];
2357
2358        let stream_cap = match conn.stream_capacity(stream_id) {
2359            Ok(v) => v,
2360
2361            Err(e) => {
2362                if conn.stream_finished(stream_id) {
2363                    self.streams.remove(&stream_id);
2364                }
2365
2366                return Err(e.into());
2367            },
2368        };
2369
2370        let grease_frame1 = grease_value();
2371        let grease_frame2 = grease_value();
2372        let grease_payload = b"GREASE is the word";
2373
2374        let overhead = octets::varint_len(grease_frame1) + // frame type
2375            1 + // payload len
2376            octets::varint_len(grease_frame2) + // frame type
2377            1 + // payload len
2378            grease_payload.len(); // payload
2379
2380        // Don't send GREASE if there is not enough capacity for it. Greasing
2381        // will _not_ be attempted again later on.
2382        if stream_cap < overhead {
2383            return Ok(());
2384        }
2385
2386        // Empty GREASE frame.
2387        let mut b = octets::OctetsMut::with_slice(&mut d);
2388        conn.stream_send(stream_id, b.put_varint(grease_frame1)?, false)?;
2389
2390        let mut b = octets::OctetsMut::with_slice(&mut d);
2391        conn.stream_send(stream_id, b.put_varint(0)?, false)?;
2392
2393        trace!(
2394            "{} tx frm GREASE stream={} len=0",
2395            conn.trace_id(),
2396            stream_id
2397        );
2398
2399        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2400            let frame = Http3Frame::Reserved {
2401                frame_type_bytes: grease_frame1,
2402                raw: None,
2403            };
2404            let ev_data = EventData::Http3FrameCreated(FrameCreated {
2405                stream_id,
2406                length: Some(0),
2407                frame,
2408                ..Default::default()
2409            });
2410
2411            q.add_event_data_now(ev_data).ok();
2412        });
2413
2414        // GREASE frame with payload.
2415        let mut b = octets::OctetsMut::with_slice(&mut d);
2416        conn.stream_send(stream_id, b.put_varint(grease_frame2)?, false)?;
2417
2418        let mut b = octets::OctetsMut::with_slice(&mut d);
2419        conn.stream_send(stream_id, b.put_varint(18)?, false)?;
2420
2421        conn.stream_send(stream_id, grease_payload, false)?;
2422
2423        trace!(
2424            "{} tx frm GREASE stream={} len={}",
2425            conn.trace_id(),
2426            stream_id,
2427            grease_payload.len()
2428        );
2429
2430        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2431            let frame = Http3Frame::Reserved {
2432                frame_type_bytes: grease_frame2,
2433                raw: None,
2434            };
2435            let ev_data = EventData::Http3FrameCreated(FrameCreated {
2436                stream_id,
2437                length: Some(grease_payload.len() as u64),
2438                frame,
2439                ..Default::default()
2440            });
2441
2442            q.add_event_data_now(ev_data).ok();
2443        });
2444
2445        Ok(())
2446    }
2447
2448    /// Opens a new unidirectional stream with a GREASE type and sends some
2449    /// unframed payload.
2450    fn open_grease_stream<F: BufFactory>(
2451        &mut self, conn: &mut super::Connection<F>,
2452    ) -> Result<()> {
2453        let ty = grease_value();
2454        match self.open_uni_stream(conn, ty) {
2455            Ok(stream_id) => {
2456                conn.stream_send(stream_id, b"GREASE is the word", true)?;
2457
2458                trace!("{} open GREASE stream {}", conn.trace_id(), stream_id);
2459
2460                qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2461                    let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2462                        stream_id,
2463                        initiator: Some(Initiator::Local),
2464                        stream_type: StreamType::Unknown,
2465                        stream_type_bytes: Some(ty),
2466                        ..Default::default()
2467                    });
2468
2469                    q.add_event_data_now(ev_data).ok();
2470                });
2471            },
2472
2473            Err(Error::IdError) => {
2474                trace!("{} GREASE stream blocked", conn.trace_id(),);
2475
2476                return Ok(());
2477            },
2478
2479            Err(e) => return Err(e),
2480        };
2481
2482        Ok(())
2483    }
2484
2485    /// Sends SETTINGS frame based on HTTP/3 configuration.
2486    fn send_settings<F: BufFactory>(
2487        &mut self, conn: &mut super::Connection<F>,
2488    ) -> Result<()> {
2489        let stream_id = match self
2490            .open_uni_stream(conn, stream::HTTP3_CONTROL_STREAM_TYPE_ID)
2491        {
2492            Ok(v) => v,
2493
2494            Err(e) => {
2495                trace!("{} Control stream blocked", conn.trace_id(),);
2496
2497                if e == Error::Done {
2498                    return Err(Error::InternalError);
2499                }
2500
2501                return Err(e);
2502            },
2503        };
2504
2505        self.control_stream_id = Some(stream_id);
2506
2507        qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2508            let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2509                stream_id,
2510                initiator: Some(Initiator::Local),
2511                stream_type: StreamType::Control,
2512                ..Default::default()
2513            });
2514
2515            q.add_event_data_now(ev_data).ok();
2516        });
2517
2518        let grease = if conn.grease {
2519            Some((grease_value(), grease_value()))
2520        } else {
2521            None
2522        };
2523
2524        let frame = frame::Frame::Settings {
2525            max_field_section_size: self.local_settings.max_field_section_size,
2526            qpack_max_table_capacity: self
2527                .local_settings
2528                .qpack_max_table_capacity,
2529            qpack_blocked_streams: self.local_settings.qpack_blocked_streams,
2530            connect_protocol_enabled: self
2531                .local_settings
2532                .connect_protocol_enabled,
2533            h3_datagram: self.local_settings.h3_datagram,
2534            grease,
2535            additional_settings: self.local_settings.additional_settings.clone(),
2536            raw: Default::default(),
2537        };
2538
2539        let mut d = [42; 128];
2540        let mut b = octets::OctetsMut::with_slice(&mut d);
2541
2542        frame.to_bytes(&mut b)?;
2543
2544        let off = b.off();
2545
2546        if let Some(id) = self.control_stream_id {
2547            conn.stream_send(id, &d[..off], false)?;
2548
2549            trace!(
2550                "{} tx frm SETTINGS stream={} len={}",
2551                conn.trace_id(),
2552                id,
2553                off
2554            );
2555
2556            qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2557                let frame = frame.to_qlog();
2558                let ev_data = EventData::Http3FrameCreated(FrameCreated {
2559                    stream_id: id,
2560                    length: Some(off as u64),
2561                    frame,
2562                    ..Default::default()
2563                });
2564
2565                q.add_event_data_now(ev_data).ok();
2566            });
2567        }
2568
2569        Ok(())
2570    }
2571
2572    fn process_control_stream<F: BufFactory>(
2573        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
2574    ) -> Result<(u64, Event)> {
2575        close_conn_if_critical_stream_finished(conn, stream_id)?;
2576
2577        if !conn.stream_readable(stream_id) {
2578            return Err(Error::Done);
2579        }
2580
2581        match self.process_readable_stream(conn, stream_id, true) {
2582            Ok(ev) => return Ok(ev),
2583
2584            Err(Error::Done) => (),
2585
2586            Err(e) => return Err(e),
2587        };
2588
2589        close_conn_if_critical_stream_finished(conn, stream_id)?;
2590
2591        Err(Error::Done)
2592    }
2593
2594    fn process_readable_stream<F: BufFactory>(
2595        &mut self, conn: &mut super::Connection<F>, stream_id: u64, polling: bool,
2596    ) -> Result<(u64, Event)> {
2597        self.streams.entry(stream_id).or_insert_with(|| {
2598            <stream::Stream>::new(
2599                stream_id,
2600                false,
2601                self.local_settings
2602                    .max_field_section_size
2603                    .unwrap_or(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
2604                self.max_priority_update_size,
2605            )
2606        });
2607
2608        // We need to get a fresh reference to the stream for each
2609        // iteration, to avoid borrowing `self` for the entire duration
2610        // of the loop, because we'll need to borrow it again in the
2611        // `State::FramePayload` case below.
2612        while let Some(stream) = self.streams.get_mut(&stream_id) {
2613            match stream.state() {
2614                stream::State::StreamType => {
2615                    stream.try_fill_buffer(conn)?;
2616
2617                    let varint = match stream.try_consume_varint() {
2618                        Ok(v) => v,
2619
2620                        Err(_) => continue,
2621                    };
2622
2623                    let ty = stream::Type::deserialize(varint)?;
2624
2625                    if let Err(e) = stream.set_ty(ty) {
2626                        conn.close(true, e.to_wire(), b"")?;
2627                        return Err(e);
2628                    }
2629
2630                    qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2631                        let ty_val = if matches!(ty, stream::Type::Unknown) {
2632                            Some(varint)
2633                        } else {
2634                            None
2635                        };
2636
2637                        let ev_data =
2638                            EventData::Http3StreamTypeSet(StreamTypeSet {
2639                                stream_id,
2640                                initiator: Some(Initiator::Remote),
2641                                stream_type: ty.to_qlog(),
2642                                stream_type_bytes: ty_val,
2643                                ..Default::default()
2644                            });
2645
2646                        q.add_event_data_now(ev_data).ok();
2647                    });
2648
2649                    match &ty {
2650                        stream::Type::Control => {
2651                            // Only one control stream allowed.
2652                            if self.peer_control_stream_id.is_some() {
2653                                conn.close(
2654                                    true,
2655                                    Error::StreamCreationError.to_wire(),
2656                                    b"Received multiple control streams",
2657                                )?;
2658
2659                                return Err(Error::StreamCreationError);
2660                            }
2661
2662                            trace!(
2663                                "{} open peer's control stream {}",
2664                                conn.trace_id(),
2665                                stream_id
2666                            );
2667
2668                            close_conn_if_critical_stream_finished(
2669                                conn, stream_id,
2670                            )?;
2671
2672                            self.peer_control_stream_id = Some(stream_id);
2673                        },
2674
2675                        stream::Type::Push => {
2676                            // Server push is not supported, so push streams at
2677                            // either client or server is a critical protocol
2678                            // error.
2679                            conn.close(
2680                                true,
2681                                Error::StreamCreationError.to_wire(),
2682                                b"Received push stream.",
2683                            )?;
2684
2685                            return Err(Error::StreamCreationError);
2686                        },
2687
2688                        stream::Type::QpackEncoder => {
2689                            // Only one qpack encoder stream allowed.
2690                            if self.peer_qpack_streams.encoder_stream_id.is_some()
2691                            {
2692                                conn.close(
2693                                    true,
2694                                    Error::StreamCreationError.to_wire(),
2695                                    b"Received multiple QPACK encoder streams",
2696                                )?;
2697
2698                                return Err(Error::StreamCreationError);
2699                            }
2700
2701                            close_conn_if_critical_stream_finished(
2702                                conn, stream_id,
2703                            )?;
2704
2705                            self.peer_qpack_streams.encoder_stream_id =
2706                                Some(stream_id);
2707                        },
2708
2709                        stream::Type::QpackDecoder => {
2710                            // Only one qpack decoder allowed.
2711                            if self.peer_qpack_streams.decoder_stream_id.is_some()
2712                            {
2713                                conn.close(
2714                                    true,
2715                                    Error::StreamCreationError.to_wire(),
2716                                    b"Received multiple QPACK decoder streams",
2717                                )?;
2718
2719                                return Err(Error::StreamCreationError);
2720                            }
2721
2722                            close_conn_if_critical_stream_finished(
2723                                conn, stream_id,
2724                            )?;
2725
2726                            self.peer_qpack_streams.decoder_stream_id =
2727                                Some(stream_id);
2728                        },
2729
2730                        stream::Type::Unknown => {
2731                            // Unknown stream types are ignored.
2732                            // TODO: we MAY send STOP_SENDING
2733                        },
2734
2735                        stream::Type::Request => unreachable!(),
2736                    }
2737                },
2738
2739                stream::State::PushId => {
2740                    stream.try_fill_buffer(conn)?;
2741
2742                    let varint = match stream.try_consume_varint() {
2743                        Ok(v) => v,
2744
2745                        Err(_) => continue,
2746                    };
2747
2748                    if let Err(e) = stream.set_push_id(varint) {
2749                        conn.close(true, e.to_wire(), b"")?;
2750                        return Err(e);
2751                    }
2752                },
2753
2754                stream::State::FrameType => {
2755                    stream.try_fill_buffer(conn)?;
2756
2757                    let varint = match stream.try_consume_varint() {
2758                        Ok(v) => v,
2759
2760                        Err(_) => continue,
2761                    };
2762
2763                    match stream.set_frame_type(varint) {
2764                        Err(Error::FrameUnexpected) => {
2765                            let msg = format!("Unexpected frame type {varint}");
2766
2767                            conn.close(
2768                                true,
2769                                Error::FrameUnexpected.to_wire(),
2770                                msg.as_bytes(),
2771                            )?;
2772
2773                            return Err(Error::FrameUnexpected);
2774                        },
2775
2776                        Err(e) => {
2777                            conn.close(
2778                                true,
2779                                e.to_wire(),
2780                                b"Error handling frame.",
2781                            )?;
2782
2783                            return Err(e);
2784                        },
2785
2786                        _ => (),
2787                    }
2788                },
2789
2790                stream::State::FramePayloadLen => {
2791                    stream.try_fill_buffer(conn)?;
2792
2793                    let payload_len = match stream.try_consume_varint() {
2794                        Ok(v) => v,
2795
2796                        Err(_) => continue,
2797                    };
2798
2799                    // DATA frames are handled uniquely. Log them here because
2800                    // DATA framing is no longer visible after this point.
2801                    if Some(frame::DATA_FRAME_TYPE_ID) == stream.frame_type() {
2802                        trace!(
2803                            "{} rx frm DATA stream={} wire_payload_len={}",
2804                            conn.trace_id(),
2805                            stream_id,
2806                            payload_len
2807                        );
2808
2809                        qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
2810                            let frame = Http3Frame::Data { raw: None };
2811
2812                            let ev_data =
2813                                EventData::Http3FrameParsed(FrameParsed {
2814                                    stream_id,
2815                                    length: Some(payload_len),
2816                                    frame,
2817                                    ..Default::default()
2818                                });
2819
2820                            q.add_event_data_now(ev_data).ok();
2821                        });
2822                    }
2823
2824                    let res = stream.set_frame_payload_len(payload_len);
2825
2826                    if let Err(e) = res {
2827                        conn.close(true, e.to_wire(), b"")?;
2828                        return Err(e);
2829                    }
2830                },
2831
2832                stream::State::FramePayload => {
2833                    // Do not emit events when not polling.
2834                    if !polling {
2835                        break;
2836                    }
2837
2838                    stream.try_fill_buffer(conn)?;
2839
2840                    let (frame, payload_len) = match stream.try_consume_frame() {
2841                        Ok(frame) => frame,
2842
2843                        Err(Error::Done) => return Err(Error::Done),
2844
2845                        Err(e) => {
2846                            conn.close(
2847                                true,
2848                                e.to_wire(),
2849                                b"Error handling frame.",
2850                            )?;
2851
2852                            return Err(e);
2853                        },
2854                    };
2855
2856                    match self.process_frame(conn, stream_id, frame, payload_len)
2857                    {
2858                        Ok(ev) => return Ok(ev),
2859
2860                        Err(Error::Done) => {
2861                            // Internal frames do not produce events. Avoid
2862                            // reading again if QUIC marked the stream finished.
2863                            if conn.stream_finished(stream_id) {
2864                                break;
2865                            }
2866                        },
2867
2868                        Err(e) => return Err(e),
2869                    };
2870                },
2871
2872                stream::State::Data => {
2873                    // Do not emit events when not polling.
2874                    if !polling {
2875                        break;
2876                    }
2877
2878                    if !stream.try_trigger_data_event() {
2879                        break;
2880                    }
2881
2882                    return Ok((stream_id, Event::Data));
2883                },
2884
2885                stream::State::QpackInstruction => {
2886                    let mut d = [0; 4096];
2887
2888                    // Read data from the stream and discard immediately.
2889                    loop {
2890                        let (recv, fin) = conn.stream_recv(stream_id, &mut d)?;
2891
2892                        match stream.ty() {
2893                            Some(stream::Type::QpackEncoder) =>
2894                                self.peer_qpack_streams.encoder_stream_bytes +=
2895                                    recv as u64,
2896                            Some(stream::Type::QpackDecoder) =>
2897                                self.peer_qpack_streams.decoder_stream_bytes +=
2898                                    recv as u64,
2899                            _ => unreachable!(),
2900                        };
2901
2902                        if fin {
2903                            close_conn_critical_stream(conn)?;
2904                        }
2905                    }
2906                },
2907
2908                stream::State::SkipFramePayload => {
2909                    stream.try_skip_frame(conn)?;
2910
2911                    // Check whether the frame has FIN'd by QUIC to prevent
2912                    // trying to read again on a closed stream.
2913                    if conn.stream_finished(stream_id) {
2914                        break;
2915                    }
2916                },
2917
2918                stream::State::Drain => {
2919                    // Discard incoming data on the stream.
2920                    conn.stream_shutdown(
2921                        stream_id,
2922                        crate::Shutdown::Read,
2923                        0x100,
2924                    )?;
2925
2926                    break;
2927                },
2928
2929                stream::State::Finished => break,
2930            }
2931        }
2932
2933        Err(Error::Done)
2934    }
2935
2936    fn process_finished_stream(&mut self, stream_id: u64) {
2937        let stream = match self.streams.get_mut(&stream_id) {
2938            Some(v) => v,
2939
2940            None => return,
2941        };
2942
2943        if stream.state() == stream::State::Finished {
2944            return;
2945        }
2946
2947        match stream.ty() {
2948            Some(stream::Type::Request) | Some(stream::Type::Push) => {
2949                stream.finished();
2950
2951                self.finished_streams.push_back(stream_id);
2952            },
2953            Some(stream::Type::Unknown) | None => {
2954                self.streams.remove(&stream_id);
2955            },
2956            // Closing any of the critical streams leads to connection close,
2957            // so there is no need for any cleanup actions here.
2958            Some(stream::Type::Control) |
2959            Some(stream::Type::QpackEncoder) |
2960            Some(stream::Type::QpackDecoder) => (),
2961        };
2962    }
2963
2964    fn finish_local_stream<F: BufFactory>(
2965        &mut self, conn: &super::Connection<F>, stream_id: u64,
2966        initialize_local: bool,
2967    ) {
2968        let hash_map::Entry::Occupied(mut stream) = self.streams.entry(stream_id)
2969        else {
2970            return;
2971        };
2972
2973        {
2974            let stream = stream.get_mut();
2975
2976            if initialize_local {
2977                stream.initialize_local();
2978            }
2979
2980            stream.finish_local();
2981        }
2982
2983        if conn.stream_finished(stream_id) {
2984            stream.remove();
2985        }
2986    }
2987
2988    fn remove_local_finished_stream(&mut self, stream_id: u64) {
2989        if let hash_map::Entry::Occupied(stream) = self.streams.entry(stream_id) {
2990            if stream.get().local_finished() {
2991                stream.remove();
2992            }
2993        }
2994    }
2995
2996    fn pop_finished_stream<F: BufFactory>(
2997        &mut self, conn: &mut super::Connection<F>,
2998    ) -> Option<(u64, Event)> {
2999        let finished = self.finished_streams.pop_front()?;
3000
3001        self.remove_local_finished_stream(finished);
3002
3003        if conn.stream_readable(finished) {
3004            // The stream is finished, but is still readable, it may indicate
3005            // that there is a pending error, such as reset.
3006            if let Err(crate::Error::StreamReset(e)) =
3007                conn.stream_recv(finished, &mut [])
3008            {
3009                return Some((finished, Event::Reset(e)));
3010            }
3011        }
3012
3013        Some((finished, Event::Finished))
3014    }
3015
3016    fn process_frame<F: BufFactory>(
3017        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
3018        frame: frame::Frame, payload_len: u64,
3019    ) -> Result<(u64, Event)> {
3020        trace!(
3021            "{} rx frm {:?} stream={} payload_len={}",
3022            conn.trace_id(),
3023            frame,
3024            stream_id,
3025            payload_len
3026        );
3027
3028        qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
3029            // HEADERS frames are special case and will be logged below.
3030            if !matches!(frame, frame::Frame::Headers { .. }) {
3031                let frame = frame.to_qlog();
3032                let ev_data = EventData::Http3FrameParsed(FrameParsed {
3033                    stream_id,
3034                    length: Some(payload_len),
3035                    frame,
3036                    ..Default::default()
3037                });
3038
3039                q.add_event_data_now(ev_data).ok();
3040            }
3041        });
3042
3043        match frame {
3044            frame::Frame::Settings {
3045                max_field_section_size,
3046                qpack_max_table_capacity,
3047                qpack_blocked_streams,
3048                connect_protocol_enabled,
3049                h3_datagram,
3050                additional_settings,
3051                raw,
3052                ..
3053            } => {
3054                self.peer_settings = ConnectionSettings {
3055                    max_field_section_size,
3056                    qpack_max_table_capacity,
3057                    qpack_blocked_streams,
3058                    connect_protocol_enabled,
3059                    h3_datagram,
3060                    additional_settings,
3061                    raw,
3062                };
3063
3064                if let Some(1) = h3_datagram {
3065                    // The peer MUST have also enabled DATAGRAM with a TP
3066                    if conn.dgram_max_writable_len().is_none() {
3067                        conn.close(
3068                            true,
3069                            Error::SettingsError.to_wire(),
3070                            b"H3_DATAGRAM sent with value 1 but max_datagram_frame_size TP not set.",
3071                        )?;
3072
3073                        return Err(Error::SettingsError);
3074                    }
3075                }
3076            },
3077
3078            frame::Frame::Headers { header_block } => {
3079                // Servers reject too many HEADERS frames.
3080                if let Some(s) = self.streams.get_mut(&stream_id) {
3081                    if self.is_server && s.headers_received_count() == 2 {
3082                        conn.close(
3083                            true,
3084                            Error::FrameUnexpected.to_wire(),
3085                            b"Too many HEADERS frames",
3086                        )?;
3087                        return Err(Error::FrameUnexpected);
3088                    }
3089
3090                    s.increment_headers_received();
3091                }
3092
3093                // Use "infinite" as default value for max_field_section_size if
3094                // it is not configured by the application.
3095                let max_size = self
3096                    .local_settings
3097                    .max_field_section_size
3098                    .unwrap_or(u64::MAX);
3099
3100                let headers = match self
3101                    .qpack_decoder
3102                    .decode(&header_block[..], max_size)
3103                {
3104                    Ok(v) => v,
3105
3106                    Err(e) => {
3107                        let e = match e {
3108                            qpack::Error::HeaderListTooLarge =>
3109                                Error::ExcessiveLoad,
3110
3111                            _ => Error::QpackDecompressionFailed,
3112                        };
3113
3114                        conn.close(true, e.to_wire(), b"Error parsing headers.")?;
3115
3116                        return Err(e);
3117                    },
3118                };
3119
3120                qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
3121                    let qlog_headers = headers
3122                        .iter()
3123                        .map(|h| qlog::events::http3::HttpHeader {
3124                            name: Some(
3125                                String::from_utf8_lossy(h.name()).into_owned(),
3126                            ),
3127                            name_bytes: None,
3128                            value: Some(
3129                                String::from_utf8_lossy(h.value()).into_owned(),
3130                            ),
3131                            value_bytes: None,
3132                        })
3133                        .collect();
3134
3135                    let frame = Http3Frame::Headers {
3136                        headers: qlog_headers,
3137                        raw: None,
3138                    };
3139
3140                    let ev_data = EventData::Http3FrameParsed(FrameParsed {
3141                        stream_id,
3142                        length: Some(payload_len),
3143                        frame,
3144                        ..Default::default()
3145                    });
3146
3147                    q.add_event_data_now(ev_data).ok();
3148                });
3149
3150                let more_frames = !conn.stream_finished(stream_id);
3151
3152                return Ok((stream_id, Event::Headers {
3153                    list: headers,
3154                    more_frames,
3155                }));
3156            },
3157
3158            frame::Frame::Data { .. } => {
3159                // Do nothing. The Data event is returned separately.
3160            },
3161
3162            frame::Frame::GoAway { id } => {
3163                if !self.is_server && id % 4 != 0 {
3164                    conn.close(
3165                        true,
3166                        Error::FrameUnexpected.to_wire(),
3167                        b"GOAWAY received with ID of non-request stream",
3168                    )?;
3169
3170                    return Err(Error::IdError);
3171                }
3172
3173                if let Some(received_id) = self.peer_goaway_id {
3174                    if id > received_id {
3175                        conn.close(
3176                            true,
3177                            Error::IdError.to_wire(),
3178                            b"GOAWAY received with ID larger than previously received",
3179                        )?;
3180
3181                        return Err(Error::IdError);
3182                    }
3183                }
3184
3185                self.peer_goaway_id = Some(id);
3186
3187                return Ok((id, Event::GoAway));
3188            },
3189
3190            frame::Frame::MaxPushId { push_id } => {
3191                if !self.is_server {
3192                    conn.close(
3193                        true,
3194                        Error::FrameUnexpected.to_wire(),
3195                        b"MAX_PUSH_ID received by client",
3196                    )?;
3197
3198                    return Err(Error::FrameUnexpected);
3199                }
3200
3201                if push_id < self.max_push_id {
3202                    conn.close(
3203                        true,
3204                        Error::IdError.to_wire(),
3205                        b"MAX_PUSH_ID reduced limit",
3206                    )?;
3207
3208                    return Err(Error::IdError);
3209                }
3210
3211                self.max_push_id = push_id;
3212            },
3213
3214            frame::Frame::PushPromise { .. } => {
3215                if self.is_server {
3216                    conn.close(
3217                        true,
3218                        Error::FrameUnexpected.to_wire(),
3219                        b"PUSH_PROMISE received by server",
3220                    )?;
3221
3222                    return Err(Error::FrameUnexpected);
3223                }
3224
3225                if !stream_id.is_multiple_of(4) {
3226                    conn.close(
3227                        true,
3228                        Error::FrameUnexpected.to_wire(),
3229                        b"PUSH_PROMISE received on non-request stream",
3230                    )?;
3231
3232                    return Err(Error::FrameUnexpected);
3233                }
3234
3235                // TODO: implement more checks and PUSH_PROMISE event
3236            },
3237
3238            frame::Frame::CancelPush { .. } => {
3239                // TODO: implement CANCEL_PUSH frame
3240            },
3241
3242            frame::Frame::PriorityUpdateRequest {
3243                prioritized_element_id,
3244                priority_field_value,
3245            } => {
3246                if !self.is_server {
3247                    conn.close(
3248                        true,
3249                        Error::FrameUnexpected.to_wire(),
3250                        b"PRIORITY_UPDATE received by client",
3251                    )?;
3252
3253                    return Err(Error::FrameUnexpected);
3254                }
3255
3256                if prioritized_element_id % 4 != 0 {
3257                    conn.close(
3258                        true,
3259                        Error::FrameUnexpected.to_wire(),
3260                        b"PRIORITY_UPDATE for request stream type with wrong ID",
3261                    )?;
3262
3263                    return Err(Error::FrameUnexpected);
3264                }
3265
3266                if prioritized_element_id > conn.streams.max_streams_bidi() * 4 {
3267                    conn.close(
3268                        true,
3269                        Error::IdError.to_wire(),
3270                        b"PRIORITY_UPDATE for request stream beyond max streams limit",
3271                    )?;
3272
3273                    return Err(Error::IdError);
3274                }
3275
3276                // PRIORITY_UPDATE can arrive before the request stream exists,
3277                // so a missing transport stream is allowed. Ignore updates only
3278                // once the transport stream was collected or both transport
3279                // directions are finished.
3280                if conn.stream_closed(prioritized_element_id) {
3281                    return Err(Error::Done);
3282                }
3283
3284                // If the stream did not yet exist, create it and store.
3285                let stream = self
3286                    .streams
3287                    .entry(prioritized_element_id)
3288                    .or_insert_with(|| {
3289                        <stream::Stream>::new(
3290                            prioritized_element_id,
3291                            false,
3292                            self.local_settings.max_field_section_size.unwrap_or(
3293                                SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT,
3294                            ),
3295                            self.max_priority_update_size,
3296                        )
3297                    });
3298
3299                let had_priority_update = stream.has_last_priority_update();
3300                stream.set_last_priority_update(Some(priority_field_value));
3301
3302                // Only trigger the event when there wasn't already a stored
3303                // PRIORITY_UPDATE.
3304                if !had_priority_update {
3305                    return Ok((prioritized_element_id, Event::PriorityUpdate));
3306                } else {
3307                    return Err(Error::Done);
3308                }
3309            },
3310
3311            frame::Frame::PriorityUpdatePush {
3312                prioritized_element_id,
3313                ..
3314            } => {
3315                if !self.is_server {
3316                    conn.close(
3317                        true,
3318                        Error::FrameUnexpected.to_wire(),
3319                        b"PRIORITY_UPDATE received by client",
3320                    )?;
3321
3322                    return Err(Error::FrameUnexpected);
3323                }
3324
3325                if prioritized_element_id % 3 != 0 {
3326                    conn.close(
3327                        true,
3328                        Error::FrameUnexpected.to_wire(),
3329                        b"PRIORITY_UPDATE for push stream type with wrong ID",
3330                    )?;
3331
3332                    return Err(Error::FrameUnexpected);
3333                }
3334
3335                // TODO: we only implement this if we implement server push
3336            },
3337
3338            frame::Frame::Unknown { .. } => (),
3339        }
3340
3341        Err(Error::Done)
3342    }
3343
3344    /// Collects and returns statistics about the connection.
3345    #[inline]
3346    pub fn stats(&self) -> Stats {
3347        Stats {
3348            qpack_encoder_stream_recv_bytes: self
3349                .peer_qpack_streams
3350                .encoder_stream_bytes,
3351            qpack_decoder_stream_recv_bytes: self
3352                .peer_qpack_streams
3353                .decoder_stream_bytes,
3354        }
3355    }
3356}
3357
3358/// Generates an HTTP/3 GREASE variable length integer.
3359pub fn grease_value() -> u64 {
3360    let n = super::rand::rand_u64_uniform(148_764_065_110_560_899);
3361    31 * n + 33
3362}
3363
3364#[doc(hidden)]
3365#[cfg(any(test, feature = "internal"))]
3366pub mod testing {
3367    use super::*;
3368
3369    use crate::test_utils;
3370    use crate::DefaultBufFactory;
3371
3372    /// Session is an HTTP/3 test helper structure. It holds a client, server
3373    /// and pipe that allows them to communicate.
3374    ///
3375    /// `default()` creates a session with some sensible default
3376    /// configuration. `with_configs()` allows for providing a specific
3377    /// configuration.
3378    ///
3379    /// `handshake()` performs all the steps needed to establish an HTTP/3
3380    /// connection.
3381    ///
3382    /// Some utility functions are provided that make it less verbose to send
3383    /// request, responses and individual headers. The full quiche API remains
3384    /// available for any test that need to do unconventional things (such as
3385    /// bad behaviour that triggers errors).
3386    pub struct Session<F = DefaultBufFactory>
3387    where
3388        F: BufFactory,
3389    {
3390        pub pipe: test_utils::Pipe<F>,
3391        pub client: Connection,
3392        pub server: Connection,
3393    }
3394
3395    impl Session {
3396        pub fn new() -> Result<Session> {
3397            Session::<DefaultBufFactory>::new_with_buf()
3398        }
3399
3400        pub fn with_configs(
3401            config: &mut crate::Config, h3_config: &Config,
3402        ) -> Result<Session> {
3403            Session::<DefaultBufFactory>::with_configs_and_buf(config, h3_config)
3404        }
3405
3406        pub fn default_configs() -> Result<(crate::Config, Config)> {
3407            fn path_relative_to_manifest_dir(path: &str) -> String {
3408                std::fs::canonicalize(
3409                    std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(path),
3410                )
3411                .unwrap()
3412                .to_string_lossy()
3413                .into_owned()
3414            }
3415
3416            let mut config = crate::Config::new(crate::PROTOCOL_VERSION)?;
3417            config.load_cert_chain_from_pem_file(
3418                &path_relative_to_manifest_dir("examples/cert.crt"),
3419            )?;
3420            config.load_priv_key_from_pem_file(
3421                &path_relative_to_manifest_dir("examples/cert.key"),
3422            )?;
3423            config.set_application_protos(&[b"h3"])?;
3424            config.set_initial_max_data(1500);
3425            config.set_initial_max_stream_data_bidi_local(150);
3426            config.set_initial_max_stream_data_bidi_remote(150);
3427            config.set_initial_max_stream_data_uni(150);
3428            config.set_initial_max_streams_bidi(5);
3429            config.set_initial_max_streams_uni(5);
3430            config.verify_peer(false);
3431            config.enable_dgram(true, 3, 3);
3432            config.set_ack_delay_exponent(8);
3433
3434            let h3_config = Config::new()?;
3435            Ok((config, h3_config))
3436        }
3437    }
3438
3439    impl<F: BufFactory> Session<F> {
3440        pub fn new_with_buf() -> Result<Session<F>> {
3441            let (mut config, h3_config) = Session::default_configs()?;
3442            Session::with_configs_and_buf(&mut config, &h3_config)
3443        }
3444
3445        pub fn with_configs_and_buf(
3446            config: &mut crate::Config, h3_config: &Config,
3447        ) -> Result<Session<F>> {
3448            let pipe = test_utils::Pipe::with_config_and_buf(config)?;
3449            let client_dgram = pipe.client.dgram_enabled();
3450            let server_dgram = pipe.server.dgram_enabled();
3451            Ok(Session {
3452                pipe,
3453                client: Connection::new(h3_config, false, client_dgram)?,
3454                server: Connection::new(h3_config, true, server_dgram)?,
3455            })
3456        }
3457
3458        /// Do the HTTP/3 handshake so both ends are in sane initial state.
3459        pub fn handshake(&mut self) -> Result<()> {
3460            self.pipe.handshake()?;
3461
3462            // Client streams.
3463            self.client.send_settings(&mut self.pipe.client)?;
3464            self.pipe.advance().ok();
3465
3466            self.client
3467                .open_qpack_encoder_stream(&mut self.pipe.client)?;
3468            self.pipe.advance().ok();
3469
3470            self.client
3471                .open_qpack_decoder_stream(&mut self.pipe.client)?;
3472            self.pipe.advance().ok();
3473
3474            if self.pipe.client.grease {
3475                self.client.open_grease_stream(&mut self.pipe.client)?;
3476            }
3477
3478            self.pipe.advance().ok();
3479
3480            // Server streams.
3481            self.server.send_settings(&mut self.pipe.server)?;
3482            self.pipe.advance().ok();
3483
3484            self.server
3485                .open_qpack_encoder_stream(&mut self.pipe.server)?;
3486            self.pipe.advance().ok();
3487
3488            self.server
3489                .open_qpack_decoder_stream(&mut self.pipe.server)?;
3490            self.pipe.advance().ok();
3491
3492            if self.pipe.server.grease {
3493                self.server.open_grease_stream(&mut self.pipe.server)?;
3494            }
3495
3496            self.advance().ok();
3497
3498            while self.client.poll(&mut self.pipe.client).is_ok() {
3499                // Do nothing.
3500            }
3501
3502            while self.server.poll(&mut self.pipe.server).is_ok() {
3503                // Do nothing.
3504            }
3505
3506            Ok(())
3507        }
3508
3509        /// Advances the session pipe over the buffer.
3510        pub fn advance(&mut self) -> crate::Result<()> {
3511            self.pipe.advance()
3512        }
3513
3514        /// Polls the client for events.
3515        pub fn poll_client(&mut self) -> Result<(u64, Event)> {
3516            self.client.poll(&mut self.pipe.client)
3517        }
3518
3519        /// Polls the server for events.
3520        pub fn poll_server(&mut self) -> Result<(u64, Event)> {
3521            self.server.poll(&mut self.pipe.server)
3522        }
3523
3524        /// Sends a request from client with default headers.
3525        ///
3526        /// On success it returns the newly allocated stream and the headers.
3527        pub fn send_request(&mut self, fin: bool) -> Result<(u64, Vec<Header>)> {
3528            let req = vec![
3529                Header::new(b":method", b"GET"),
3530                Header::new(b":scheme", b"https"),
3531                Header::new(b":authority", b"quic.tech"),
3532                Header::new(b":path", b"/test"),
3533                Header::new(b"user-agent", b"quiche-test"),
3534            ];
3535
3536            let stream =
3537                self.client.send_request(&mut self.pipe.client, &req, fin)?;
3538
3539            self.advance().ok();
3540
3541            Ok((stream, req))
3542        }
3543
3544        /// Sends a response from server with default headers.
3545        ///
3546        /// On success it returns the headers.
3547        pub fn send_response(
3548            &mut self, stream: u64, fin: bool,
3549        ) -> Result<Vec<Header>> {
3550            let resp = vec![
3551                Header::new(b":status", b"200"),
3552                Header::new(b"server", b"quiche-test"),
3553            ];
3554
3555            self.server.send_response(
3556                &mut self.pipe.server,
3557                stream,
3558                &resp,
3559                fin,
3560            )?;
3561
3562            self.advance().ok();
3563
3564            Ok(resp)
3565        }
3566
3567        /// Sends some default payload from client.
3568        ///
3569        /// On success it returns the payload.
3570        pub fn send_body_client(
3571            &mut self, stream: u64, fin: bool,
3572        ) -> Result<Vec<u8>> {
3573            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3574
3575            self.client
3576                .send_body(&mut self.pipe.client, stream, &bytes, fin)?;
3577
3578            self.advance().ok();
3579
3580            Ok(bytes)
3581        }
3582
3583        /// Fetches DATA payload from the server.
3584        ///
3585        /// On success it returns the number of bytes received.
3586        pub fn recv_body_client(
3587            &mut self, stream: u64, buf: &mut [u8],
3588        ) -> Result<usize> {
3589            self.client.recv_body(&mut self.pipe.client, stream, buf)
3590        }
3591
3592        /// Fetches DATA payload from the server.
3593        ///
3594        /// On success it returns the number of bytes received.
3595        pub fn recv_body_buf_client<B: bytes::BufMut>(
3596            &mut self, stream: u64, buf: B,
3597        ) -> Result<usize> {
3598            self.client
3599                .recv_body_buf(&mut self.pipe.client, stream, buf)
3600        }
3601
3602        /// Sends some default payload from server.
3603        ///
3604        /// On success it returns the payload.
3605        pub fn send_body_server(
3606            &mut self, stream: u64, fin: bool,
3607        ) -> Result<Vec<u8>> {
3608            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3609
3610            self.server
3611                .send_body(&mut self.pipe.server, stream, &bytes, fin)?;
3612
3613            self.advance().ok();
3614
3615            Ok(bytes)
3616        }
3617
3618        /// Fetches DATA payload from the client.
3619        ///
3620        /// On success it returns the number of bytes received.
3621        pub fn recv_body_server(
3622            &mut self, stream: u64, buf: &mut [u8],
3623        ) -> Result<usize> {
3624            self.server.recv_body(&mut self.pipe.server, stream, buf)
3625        }
3626
3627        /// Fetches DATA payload from the client.
3628        ///
3629        /// On success it returns the number of bytes received.
3630        pub fn recv_body_buf_server<B: bytes::BufMut>(
3631            &mut self, stream: u64, buf: B,
3632        ) -> Result<usize> {
3633            self.server
3634                .recv_body_buf(&mut self.pipe.server, stream, buf)
3635        }
3636
3637        /// Sends a single HTTP/3 frame from the client.
3638        pub fn send_frame_client(
3639            &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3640        ) -> Result<()> {
3641            let mut d = [42; 65535];
3642
3643            let mut b = octets::OctetsMut::with_slice(&mut d);
3644
3645            frame.to_bytes(&mut b)?;
3646
3647            let off = b.off();
3648            self.pipe.client.stream_send(stream_id, &d[..off], fin)?;
3649
3650            self.advance().ok();
3651
3652            Ok(())
3653        }
3654
3655        /// Send an HTTP/3 DATAGRAM with default data from the client.
3656        ///
3657        /// On success it returns the data.
3658        pub fn send_dgram_client(&mut self, flow_id: u64) -> Result<Vec<u8>> {
3659            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3660            let len = octets::varint_len(flow_id) + bytes.len();
3661            let mut d = vec![0; len];
3662            let mut b = octets::OctetsMut::with_slice(&mut d);
3663
3664            b.put_varint(flow_id)?;
3665            b.put_bytes(&bytes)?;
3666
3667            self.pipe.client.dgram_send(&d)?;
3668
3669            self.advance().ok();
3670
3671            Ok(bytes)
3672        }
3673
3674        /// Receives an HTTP/3 DATAGRAM from the server.
3675        ///
3676        /// On success it returns the DATAGRAM length, flow ID and flow ID
3677        /// length.
3678        pub fn recv_dgram_client(
3679            &mut self, buf: &mut [u8],
3680        ) -> Result<(usize, u64, usize)> {
3681            let len = self.pipe.client.dgram_recv(buf)?;
3682            let mut b = octets::Octets::with_slice(buf);
3683            let flow_id = b.get_varint()?;
3684
3685            Ok((len, flow_id, b.off()))
3686        }
3687
3688        /// Send an HTTP/3 DATAGRAM with default data from the server
3689        ///
3690        /// On success it returns the data.
3691        pub fn send_dgram_server(&mut self, flow_id: u64) -> Result<Vec<u8>> {
3692            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3693            let len = octets::varint_len(flow_id) + bytes.len();
3694            let mut d = vec![0; len];
3695            let mut b = octets::OctetsMut::with_slice(&mut d);
3696
3697            b.put_varint(flow_id)?;
3698            b.put_bytes(&bytes)?;
3699
3700            self.pipe.server.dgram_send(&d)?;
3701
3702            self.advance().ok();
3703
3704            Ok(bytes)
3705        }
3706
3707        /// Receives an HTTP/3 DATAGRAM from the client.
3708        ///
3709        /// On success it returns the DATAGRAM length, flow ID and flow ID
3710        /// length.
3711        pub fn recv_dgram_server(
3712            &mut self, buf: &mut [u8],
3713        ) -> Result<(usize, u64, usize)> {
3714            let len = self.pipe.server.dgram_recv(buf)?;
3715            let mut b = octets::Octets::with_slice(buf);
3716            let flow_id = b.get_varint()?;
3717
3718            Ok((len, flow_id, b.off()))
3719        }
3720
3721        /// Sends a single HTTP/3 frame from the server.
3722        pub fn send_frame_server(
3723            &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3724        ) -> Result<()> {
3725            let mut d = [42; 65535];
3726
3727            let mut b = octets::OctetsMut::with_slice(&mut d);
3728
3729            frame.to_bytes(&mut b)?;
3730
3731            let off = b.off();
3732            self.pipe.server.stream_send(stream_id, &d[..off], fin)?;
3733
3734            self.advance().ok();
3735
3736            Ok(())
3737        }
3738
3739        /// Sends an arbitrary buffer of HTTP/3 stream data from the client.
3740        pub fn send_arbitrary_stream_data_client(
3741            &mut self, data: &[u8], stream_id: u64, fin: bool,
3742        ) -> Result<()> {
3743            self.pipe.client.stream_send(stream_id, data, fin)?;
3744
3745            self.advance().ok();
3746
3747            Ok(())
3748        }
3749
3750        /// Sends an arbitrary buffer of HTTP/3 stream data from the server.
3751        pub fn send_arbitrary_stream_data_server(
3752            &mut self, data: &[u8], stream_id: u64, fin: bool,
3753        ) -> Result<()> {
3754            self.pipe.server.stream_send(stream_id, data, fin)?;
3755
3756            self.advance().ok();
3757
3758            Ok(())
3759        }
3760    }
3761}
3762
3763#[cfg(test)]
3764mod tests {
3765    use bytes::BufMut as _;
3766
3767    use super::*;
3768
3769    use super::testing::*;
3770
3771    #[test]
3772    /// Make sure that random GREASE values is within the specified limit.
3773    fn grease_value_in_varint_limit() {
3774        assert!(grease_value() < 2u64.pow(62) - 1);
3775    }
3776
3777    #[test]
3778    fn h3_handshake_0rtt() {
3779        let mut buf = [0; 65535];
3780
3781        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
3782        config
3783            .load_cert_chain_from_pem_file("examples/cert.crt")
3784            .unwrap();
3785        config
3786            .load_priv_key_from_pem_file("examples/cert.key")
3787            .unwrap();
3788        config
3789            .set_application_protos(&[b"proto1", b"proto2"])
3790            .unwrap();
3791        config.set_initial_max_data(30);
3792        config.set_initial_max_stream_data_bidi_local(15);
3793        config.set_initial_max_stream_data_bidi_remote(15);
3794        config.set_initial_max_stream_data_uni(15);
3795        config.set_initial_max_streams_bidi(3);
3796        config.set_initial_max_streams_uni(3);
3797        config.enable_early_data();
3798        config.verify_peer(false);
3799
3800        let h3_config = Config::new().unwrap();
3801
3802        // Perform initial handshake.
3803        let mut pipe = crate::test_utils::Pipe::with_config(&mut config).unwrap();
3804        assert_eq!(pipe.handshake(), Ok(()));
3805
3806        // Extract session,
3807        let session = pipe.client.session().unwrap();
3808
3809        // Configure session on new connection.
3810        let mut pipe = crate::test_utils::Pipe::with_config(&mut config).unwrap();
3811        assert_eq!(pipe.client.set_session(session), Ok(()));
3812
3813        // Can't create an H3 connection until the QUIC connection is determined
3814        // to have made sufficient early data progress.
3815        assert!(matches!(
3816            Connection::with_transport(&mut pipe.client, &h3_config),
3817            Err(Error::InternalError)
3818        ));
3819
3820        // Client sends initial flight.
3821        let (len, _) = pipe.client.send(&mut buf).unwrap();
3822
3823        // Now an H3 connection can be created.
3824        assert!(Connection::with_transport(&mut pipe.client, &h3_config).is_ok());
3825        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
3826
3827        // Client sends 0-RTT packet.
3828        let pkt_type = crate::packet::Type::ZeroRTT;
3829
3830        let frames = [crate::frame::Frame::Stream {
3831            stream_id: 6,
3832            data: <crate::range_buf::RangeBuf>::from(b"aaaaa", 0, true),
3833        }];
3834
3835        assert_eq!(
3836            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
3837            Ok(1200)
3838        );
3839
3840        assert_eq!(pipe.server.undecryptable_pkts.len(), 0);
3841
3842        // 0-RTT stream data is readable.
3843        let mut r = pipe.server.readable();
3844        assert_eq!(r.next(), Some(6));
3845        assert_eq!(r.next(), None);
3846
3847        let mut b = [0; 15];
3848        assert_eq!(pipe.server.stream_recv(6, &mut b), Ok((5, true)));
3849        assert_eq!(&b[..5], b"aaaaa");
3850    }
3851
3852    #[test]
3853    /// Send a request with no body, get a response with no body.
3854    fn request_no_body_response_no_body() {
3855        let mut s = Session::new().unwrap();
3856        s.handshake().unwrap();
3857
3858        let (stream, req) = s.send_request(true).unwrap();
3859
3860        assert_eq!(stream, 0);
3861
3862        let ev_headers = Event::Headers {
3863            list: req,
3864            more_frames: false,
3865        };
3866
3867        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3868        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3869
3870        let resp = s.send_response(stream, true).unwrap();
3871
3872        let ev_headers = Event::Headers {
3873            list: resp,
3874            more_frames: false,
3875        };
3876
3877        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3878        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3879        assert_eq!(s.poll_client(), Err(Error::Done));
3880    }
3881
3882    #[test]
3883    /// Send a request with no body, get a response with one DATA frame.
3884    fn request_no_body_response_one_chunk() {
3885        let mut s = Session::new().unwrap();
3886        s.handshake().unwrap();
3887
3888        let (stream, req) = s.send_request(true).unwrap();
3889        assert_eq!(stream, 0);
3890
3891        let ev_headers = Event::Headers {
3892            list: req,
3893            more_frames: false,
3894        };
3895
3896        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3897
3898        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3899
3900        let resp = s.send_response(stream, false).unwrap();
3901
3902        let body = s.send_body_server(stream, true).unwrap();
3903
3904        let mut recv_buf = vec![0; body.len()];
3905
3906        let ev_headers = Event::Headers {
3907            list: resp,
3908            more_frames: true,
3909        };
3910
3911        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3912
3913        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
3914        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
3915
3916        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3917        assert_eq!(s.poll_client(), Err(Error::Done));
3918    }
3919
3920    #[test]
3921    /// Send a request with no body, get a response with multiple DATA frames.
3922    fn request_no_body_response_many_chunks() {
3923        let mut s = Session::new().unwrap();
3924        s.handshake().unwrap();
3925
3926        let (stream, req) = s.send_request(true).unwrap();
3927
3928        let ev_headers = Event::Headers {
3929            list: req,
3930            more_frames: false,
3931        };
3932
3933        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3934        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3935
3936        let total_data_frames = 4;
3937
3938        let resp = s.send_response(stream, false).unwrap();
3939
3940        for _ in 0..total_data_frames - 1 {
3941            s.send_body_server(stream, false).unwrap();
3942        }
3943
3944        let body = s.send_body_server(stream, true).unwrap();
3945
3946        let mut recv_buf = vec![0; body.len()];
3947
3948        let ev_headers = Event::Headers {
3949            list: resp,
3950            more_frames: true,
3951        };
3952
3953        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3954        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
3955        assert_eq!(s.poll_client(), Err(Error::Done));
3956
3957        for _ in 0..total_data_frames {
3958            assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
3959        }
3960
3961        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3962        assert_eq!(s.poll_client(), Err(Error::Done));
3963    }
3964
3965    #[test]
3966    /// Send a request with no body, get a response with multiple DATA frames.
3967    fn request_no_body_response_many_chunks_with_buf() {
3968        let (mut config, h3_config) = Session::default_configs().unwrap();
3969        // we don't want to be limited by flow or cong. control
3970        config.set_initial_congestion_window_packets(100);
3971        config.set_initial_max_data(200_000);
3972        config.set_initial_max_stream_data_bidi_local(200_000);
3973        config.set_initial_max_stream_data_bidi_remote(200_000);
3974        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
3975        s.handshake().unwrap();
3976
3977        let (stream, req) = s.send_request(true).unwrap();
3978
3979        let ev_headers = Event::Headers {
3980            list: req,
3981            more_frames: false,
3982        };
3983
3984        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3985        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3986
3987        let total_data_frames = 4;
3988
3989        // Use a large body
3990        let data = vec![0xab_u8; 16 * 1024];
3991
3992        let resp = s.send_response(stream, false).unwrap();
3993
3994        for _ in 0..total_data_frames - 1 {
3995            assert_eq!(
3996                s.server.send_body(&mut s.pipe.server, stream, &data, false),
3997                Ok(data.len())
3998            );
3999            s.advance().ok();
4000        }
4001
4002        s.server
4003            .send_body(&mut s.pipe.server, stream, &data, true)
4004            .unwrap();
4005        s.advance().ok();
4006
4007        let ev_headers = Event::Headers {
4008            list: resp,
4009            more_frames: true,
4010        };
4011
4012        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4013        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
4014        assert_eq!(s.poll_client(), Err(Error::Done));
4015
4016        // Reads may span multiple DATA frames and need not end at frame
4017        // boundaries. Read one and a half times the payload of one frame.
4018        let how_much_to_read_per_call = data.len() * 2 / 3;
4019        let mut remaining_to_read = total_data_frames * data.len();
4020        let mut recv_buf = Vec::new().limit(how_much_to_read_per_call);
4021        assert_eq!(
4022            s.recv_body_buf_client(stream, &mut recv_buf),
4023            Ok(how_much_to_read_per_call)
4024        );
4025        remaining_to_read -= how_much_to_read_per_call;
4026        assert_eq!(recv_buf.get_ref().len(), how_much_to_read_per_call);
4027
4028        while remaining_to_read > 0 {
4029            // Set a different limit for the following reads.
4030            recv_buf.set_limit(data.len());
4031            // We should either read up to the limit we set above, or to
4032            // the end of buffered data.
4033            let expected = std::cmp::min(data.len(), remaining_to_read);
4034            assert_eq!(
4035                s.recv_body_buf_client(stream, &mut recv_buf),
4036                Ok(expected)
4037            );
4038            remaining_to_read -= expected;
4039        }
4040        // We've read everything now. Ensure the Vec reflects that
4041        assert_eq!(recv_buf.get_ref().len(), total_data_frames * data.len());
4042
4043        // No more data to read.
4044        assert_eq!(
4045            s.recv_body_buf_client(stream, &mut recv_buf),
4046            Err(Error::Done)
4047        );
4048
4049        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4050        assert_eq!(s.poll_client(), Err(Error::Done));
4051    }
4052
4053    #[test]
4054    /// Send a request with one DATA frame, get a response with no body.
4055    fn request_one_chunk_response_no_body() {
4056        let mut s = Session::new().unwrap();
4057        s.handshake().unwrap();
4058
4059        let (stream, req) = s.send_request(false).unwrap();
4060
4061        let body = s.send_body_client(stream, true).unwrap();
4062
4063        let mut recv_buf = vec![0; body.len()];
4064
4065        let ev_headers = Event::Headers {
4066            list: req,
4067            more_frames: true,
4068        };
4069
4070        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4071
4072        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4073        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4074
4075        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4076
4077        let resp = s.send_response(stream, true).unwrap();
4078
4079        let ev_headers = Event::Headers {
4080            list: resp,
4081            more_frames: false,
4082        };
4083
4084        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4085        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4086    }
4087
4088    #[test]
4089    /// Send a request with multiple DATA frames, get a response with no body.
4090    fn request_many_chunks_response_no_body() {
4091        let mut s = Session::new().unwrap();
4092        s.handshake().unwrap();
4093
4094        let (stream, req) = s.send_request(false).unwrap();
4095
4096        let total_data_frames = 4;
4097
4098        for _ in 0..total_data_frames - 1 {
4099            s.send_body_client(stream, false).unwrap();
4100        }
4101
4102        let body = s.send_body_client(stream, true).unwrap();
4103
4104        let mut recv_buf = vec![0; body.len()];
4105
4106        let ev_headers = Event::Headers {
4107            list: req,
4108            more_frames: true,
4109        };
4110
4111        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4112        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4113        assert_eq!(s.poll_server(), Err(Error::Done));
4114
4115        for _ in 0..total_data_frames {
4116            assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4117        }
4118
4119        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4120
4121        let resp = s.send_response(stream, true).unwrap();
4122
4123        let ev_headers = Event::Headers {
4124            list: resp,
4125            more_frames: false,
4126        };
4127
4128        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4129        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4130    }
4131
4132    #[test]
4133    /// Send a request with multiple DATA frames, get a response with one DATA
4134    /// frame.
4135    fn many_requests_many_chunks_response_one_chunk() {
4136        let mut s = Session::new().unwrap();
4137        s.handshake().unwrap();
4138
4139        let mut reqs = Vec::new();
4140
4141        let (stream1, req1) = s.send_request(false).unwrap();
4142        assert_eq!(stream1, 0);
4143        reqs.push(req1);
4144
4145        let (stream2, req2) = s.send_request(false).unwrap();
4146        assert_eq!(stream2, 4);
4147        reqs.push(req2);
4148
4149        let (stream3, req3) = s.send_request(false).unwrap();
4150        assert_eq!(stream3, 8);
4151        reqs.push(req3);
4152
4153        let body = s.send_body_client(stream1, false).unwrap();
4154        s.send_body_client(stream2, false).unwrap();
4155        s.send_body_client(stream3, false).unwrap();
4156
4157        let mut recv_buf = vec![0; body.len()];
4158
4159        // Reverse order of writes.
4160
4161        s.send_body_client(stream3, true).unwrap();
4162        s.send_body_client(stream2, true).unwrap();
4163        s.send_body_client(stream1, true).unwrap();
4164
4165        let (_, ev) = s.poll_server().unwrap();
4166        let ev_headers = Event::Headers {
4167            list: reqs[0].clone(),
4168            more_frames: true,
4169        };
4170        assert_eq!(ev, ev_headers);
4171
4172        let (_, ev) = s.poll_server().unwrap();
4173        let ev_headers = Event::Headers {
4174            list: reqs[1].clone(),
4175            more_frames: true,
4176        };
4177        assert_eq!(ev, ev_headers);
4178
4179        let (_, ev) = s.poll_server().unwrap();
4180        let ev_headers = Event::Headers {
4181            list: reqs[2].clone(),
4182            more_frames: true,
4183        };
4184        assert_eq!(ev, ev_headers);
4185
4186        assert_eq!(s.poll_server(), Ok((0, Event::Data)));
4187        assert_eq!(s.recv_body_server(0, &mut recv_buf), Ok(body.len()));
4188        assert_eq!(s.poll_client(), Err(Error::Done));
4189        assert_eq!(s.recv_body_server(0, &mut recv_buf), Ok(body.len()));
4190        assert_eq!(s.poll_server(), Ok((0, Event::Finished)));
4191
4192        assert_eq!(s.poll_server(), Ok((4, Event::Data)));
4193        assert_eq!(s.recv_body_server(4, &mut recv_buf), Ok(body.len()));
4194        assert_eq!(s.poll_client(), Err(Error::Done));
4195        assert_eq!(s.recv_body_server(4, &mut recv_buf), Ok(body.len()));
4196        assert_eq!(s.poll_server(), Ok((4, Event::Finished)));
4197
4198        assert_eq!(s.poll_server(), Ok((8, Event::Data)));
4199        assert_eq!(s.recv_body_server(8, &mut recv_buf), Ok(body.len()));
4200        assert_eq!(s.poll_client(), Err(Error::Done));
4201        assert_eq!(s.recv_body_server(8, &mut recv_buf), Ok(body.len()));
4202        assert_eq!(s.poll_server(), Ok((8, Event::Finished)));
4203
4204        assert_eq!(s.poll_server(), Err(Error::Done));
4205
4206        let mut resps = Vec::new();
4207
4208        let resp1 = s.send_response(stream1, true).unwrap();
4209        resps.push(resp1);
4210
4211        let resp2 = s.send_response(stream2, true).unwrap();
4212        resps.push(resp2);
4213
4214        let resp3 = s.send_response(stream3, true).unwrap();
4215        resps.push(resp3);
4216
4217        for _ in 0..resps.len() {
4218            let (stream, ev) = s.poll_client().unwrap();
4219            let ev_headers = Event::Headers {
4220                list: resps[(stream / 4) as usize].clone(),
4221                more_frames: false,
4222            };
4223            assert_eq!(ev, ev_headers);
4224            assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4225        }
4226
4227        assert_eq!(s.poll_client(), Err(Error::Done));
4228    }
4229
4230    #[test]
4231    /// Send a request with no body, get a response with one DATA frame and an
4232    /// empty FIN after reception from the client.
4233    fn request_no_body_response_one_chunk_empty_fin() {
4234        let mut s = Session::new().unwrap();
4235        s.handshake().unwrap();
4236
4237        let (stream, req) = s.send_request(true).unwrap();
4238
4239        let ev_headers = Event::Headers {
4240            list: req,
4241            more_frames: false,
4242        };
4243
4244        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4245        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4246
4247        let resp = s.send_response(stream, false).unwrap();
4248
4249        let body = s.send_body_server(stream, false).unwrap();
4250
4251        let mut recv_buf = vec![0; body.len()];
4252
4253        let ev_headers = Event::Headers {
4254            list: resp,
4255            more_frames: true,
4256        };
4257
4258        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4259
4260        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
4261        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
4262
4263        assert_eq!(s.pipe.server.stream_send(stream, &[], true), Ok(0));
4264        s.advance().ok();
4265
4266        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4267        assert_eq!(s.poll_client(), Err(Error::Done));
4268    }
4269
4270    #[test]
4271    /// Send a request with no body, get a response with no body followed by
4272    /// GREASE that is STREAM frame with a FIN.
4273    fn request_no_body_response_no_body_with_grease() {
4274        let mut s = Session::new().unwrap();
4275        s.handshake().unwrap();
4276
4277        let (stream, req) = s.send_request(true).unwrap();
4278
4279        assert_eq!(stream, 0);
4280
4281        let ev_headers = Event::Headers {
4282            list: req,
4283            more_frames: false,
4284        };
4285
4286        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4287        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4288
4289        let resp = s.send_response(stream, false).unwrap();
4290
4291        let ev_headers = Event::Headers {
4292            list: resp,
4293            more_frames: true,
4294        };
4295
4296        // Inject a GREASE frame.
4297        let mut d = [42; 10];
4298        let mut b = octets::OctetsMut::with_slice(&mut d);
4299
4300        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
4301        s.pipe.server.stream_send(0, frame_type, false).unwrap();
4302
4303        let frame_len = b.put_varint(10).unwrap();
4304        s.pipe.server.stream_send(0, frame_len, false).unwrap();
4305
4306        s.pipe.server.stream_send(0, &d, true).unwrap();
4307
4308        s.advance().ok();
4309
4310        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4311        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4312        assert_eq!(s.poll_client(), Err(Error::Done));
4313    }
4314
4315    #[test]
4316    /// Try to send DATA frames before HEADERS.
4317    fn body_response_before_headers() {
4318        let mut s = Session::new().unwrap();
4319        s.handshake().unwrap();
4320
4321        let (stream, req) = s.send_request(true).unwrap();
4322        assert_eq!(stream, 0);
4323
4324        let ev_headers = Event::Headers {
4325            list: req,
4326            more_frames: false,
4327        };
4328
4329        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4330
4331        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4332
4333        assert_eq!(
4334            s.send_body_server(stream, true),
4335            Err(Error::FrameUnexpected)
4336        );
4337
4338        assert_eq!(s.poll_client(), Err(Error::Done));
4339    }
4340
4341    #[test]
4342    /// Try to send DATA frames on wrong streams, ensure the API returns an
4343    /// error before anything hits the transport layer.
4344    fn send_body_invalid_client_stream() {
4345        let mut s = Session::new().unwrap();
4346        s.handshake().unwrap();
4347
4348        assert_eq!(s.send_body_client(0, true), Err(Error::FrameUnexpected));
4349
4350        assert_eq!(
4351            s.send_body_client(s.client.control_stream_id.unwrap(), true),
4352            Err(Error::FrameUnexpected)
4353        );
4354
4355        assert_eq!(
4356            s.send_body_client(
4357                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
4358                true
4359            ),
4360            Err(Error::FrameUnexpected)
4361        );
4362
4363        assert_eq!(
4364            s.send_body_client(
4365                s.client.local_qpack_streams.decoder_stream_id.unwrap(),
4366                true
4367            ),
4368            Err(Error::FrameUnexpected)
4369        );
4370
4371        assert_eq!(
4372            s.send_body_client(s.client.peer_control_stream_id.unwrap(), true),
4373            Err(Error::FrameUnexpected)
4374        );
4375
4376        assert_eq!(
4377            s.send_body_client(
4378                s.client.peer_qpack_streams.encoder_stream_id.unwrap(),
4379                true
4380            ),
4381            Err(Error::FrameUnexpected)
4382        );
4383
4384        assert_eq!(
4385            s.send_body_client(
4386                s.client.peer_qpack_streams.decoder_stream_id.unwrap(),
4387                true
4388            ),
4389            Err(Error::FrameUnexpected)
4390        );
4391    }
4392
4393    #[test]
4394    /// Try to send DATA frames on wrong streams, ensure the API returns an
4395    /// error before anything hits the transport layer.
4396    fn send_body_invalid_server_stream() {
4397        let mut s = Session::new().unwrap();
4398        s.handshake().unwrap();
4399
4400        assert_eq!(s.send_body_server(0, true), Err(Error::FrameUnexpected));
4401
4402        assert_eq!(
4403            s.send_body_server(s.server.control_stream_id.unwrap(), true),
4404            Err(Error::FrameUnexpected)
4405        );
4406
4407        assert_eq!(
4408            s.send_body_server(
4409                s.server.local_qpack_streams.encoder_stream_id.unwrap(),
4410                true
4411            ),
4412            Err(Error::FrameUnexpected)
4413        );
4414
4415        assert_eq!(
4416            s.send_body_server(
4417                s.server.local_qpack_streams.decoder_stream_id.unwrap(),
4418                true
4419            ),
4420            Err(Error::FrameUnexpected)
4421        );
4422
4423        assert_eq!(
4424            s.send_body_server(s.server.peer_control_stream_id.unwrap(), true),
4425            Err(Error::FrameUnexpected)
4426        );
4427
4428        assert_eq!(
4429            s.send_body_server(
4430                s.server.peer_qpack_streams.encoder_stream_id.unwrap(),
4431                true
4432            ),
4433            Err(Error::FrameUnexpected)
4434        );
4435
4436        assert_eq!(
4437            s.send_body_server(
4438                s.server.peer_qpack_streams.decoder_stream_id.unwrap(),
4439                true
4440            ),
4441            Err(Error::FrameUnexpected)
4442        );
4443    }
4444
4445    #[test]
4446    /// Client sends request with body and trailers.
4447    fn trailers() {
4448        let mut s = Session::new().unwrap();
4449        s.handshake().unwrap();
4450
4451        let (stream, req) = s.send_request(false).unwrap();
4452
4453        let body = s.send_body_client(stream, false).unwrap();
4454
4455        let mut recv_buf = vec![0; body.len()];
4456
4457        let req_trailers = vec![Header::new(b"foo", b"bar")];
4458
4459        s.client
4460            .send_additional_headers(
4461                &mut s.pipe.client,
4462                stream,
4463                &req_trailers,
4464                true,
4465                true,
4466            )
4467            .unwrap();
4468
4469        s.advance().ok();
4470
4471        let ev_headers = Event::Headers {
4472            list: req,
4473            more_frames: true,
4474        };
4475
4476        let ev_trailers = Event::Headers {
4477            list: req_trailers,
4478            more_frames: false,
4479        };
4480
4481        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4482
4483        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4484        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4485
4486        assert_eq!(s.poll_server(), Ok((stream, ev_trailers)));
4487        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4488    }
4489
4490    #[test]
4491    /// Server responds with a 103, then a 200 with no body.
4492    fn informational_response() {
4493        let mut s = Session::new().unwrap();
4494        s.handshake().unwrap();
4495
4496        let (stream, req) = s.send_request(true).unwrap();
4497
4498        assert_eq!(stream, 0);
4499
4500        let ev_headers = Event::Headers {
4501            list: req,
4502            more_frames: false,
4503        };
4504
4505        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4506        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4507
4508        let info_resp = vec![
4509            Header::new(b":status", b"103"),
4510            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4511        ];
4512
4513        let resp = vec![
4514            Header::new(b":status", b"200"),
4515            Header::new(b"server", b"quiche-test"),
4516        ];
4517
4518        s.server
4519            .send_response(&mut s.pipe.server, stream, &info_resp, false)
4520            .unwrap();
4521
4522        s.server
4523            .send_additional_headers(
4524                &mut s.pipe.server,
4525                stream,
4526                &resp,
4527                false,
4528                true,
4529            )
4530            .unwrap();
4531
4532        s.advance().ok();
4533
4534        let ev_info_headers = Event::Headers {
4535            list: info_resp,
4536            more_frames: true,
4537        };
4538
4539        let ev_headers = Event::Headers {
4540            list: resp,
4541            more_frames: false,
4542        };
4543
4544        assert_eq!(s.poll_client(), Ok((stream, ev_info_headers)));
4545        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4546        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4547        assert_eq!(s.poll_client(), Err(Error::Done));
4548    }
4549
4550    #[test]
4551    /// Server responds with a 103, then attempts to send a 200 using
4552    /// send_response again, which should fail.
4553    fn no_multiple_response() {
4554        let mut s = Session::new().unwrap();
4555        s.handshake().unwrap();
4556
4557        let (stream, req) = s.send_request(true).unwrap();
4558
4559        assert_eq!(stream, 0);
4560
4561        let ev_headers = Event::Headers {
4562            list: req,
4563            more_frames: false,
4564        };
4565
4566        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4567        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4568
4569        let info_resp = vec![
4570            Header::new(b":status", b"103"),
4571            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4572        ];
4573
4574        let resp = vec![
4575            Header::new(b":status", b"200"),
4576            Header::new(b"server", b"quiche-test"),
4577        ];
4578
4579        s.server
4580            .send_response(&mut s.pipe.server, stream, &info_resp, false)
4581            .unwrap();
4582
4583        assert_eq!(
4584            Err(Error::FrameUnexpected),
4585            s.server
4586                .send_response(&mut s.pipe.server, stream, &resp, true)
4587        );
4588
4589        s.advance().ok();
4590
4591        let ev_info_headers = Event::Headers {
4592            list: info_resp,
4593            more_frames: true,
4594        };
4595
4596        assert_eq!(s.poll_client(), Ok((stream, ev_info_headers)));
4597        assert_eq!(s.poll_client(), Err(Error::Done));
4598    }
4599
4600    #[test]
4601    /// Server attempts to use send_additional_headers before initial response.
4602    fn no_send_additional_before_initial_response() {
4603        let mut s = Session::new().unwrap();
4604        s.handshake().unwrap();
4605
4606        let (stream, req) = s.send_request(true).unwrap();
4607
4608        assert_eq!(stream, 0);
4609
4610        let ev_headers = Event::Headers {
4611            list: req,
4612            more_frames: false,
4613        };
4614
4615        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4616        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4617
4618        let info_resp = vec![
4619            Header::new(b":status", b"103"),
4620            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4621        ];
4622
4623        assert_eq!(
4624            Err(Error::FrameUnexpected),
4625            s.server.send_additional_headers(
4626                &mut s.pipe.server,
4627                stream,
4628                &info_resp,
4629                false,
4630                false
4631            )
4632        );
4633
4634        s.advance().ok();
4635
4636        assert_eq!(s.poll_client(), Err(Error::Done));
4637    }
4638
4639    #[test]
4640    /// Client sends multiple HEADERS before data.
4641    fn additional_headers_before_data_client() {
4642        let mut s = Session::new().unwrap();
4643        s.handshake().unwrap();
4644
4645        let (stream, req) = s.send_request(false).unwrap();
4646
4647        let req_trailer = vec![Header::new(b"goodbye", b"world")];
4648
4649        assert_eq!(
4650            s.client.send_additional_headers(
4651                &mut s.pipe.client,
4652                stream,
4653                &req_trailer,
4654                true,
4655                false
4656            ),
4657            Ok(())
4658        );
4659
4660        s.advance().ok();
4661
4662        let ev_initial_headers = Event::Headers {
4663            list: req,
4664            more_frames: true,
4665        };
4666
4667        let ev_trailing_headers = Event::Headers {
4668            list: req_trailer,
4669            more_frames: true,
4670        };
4671
4672        assert_eq!(s.poll_server(), Ok((stream, ev_initial_headers)));
4673        assert_eq!(s.poll_server(), Ok((stream, ev_trailing_headers)));
4674        assert_eq!(s.poll_server(), Err(Error::Done));
4675    }
4676
4677    #[test]
4678    /// Client sends multiple HEADERS before data.
4679    fn data_after_trailers_client() {
4680        let mut s = Session::new().unwrap();
4681        s.handshake().unwrap();
4682
4683        let (stream, req) = s.send_request(false).unwrap();
4684
4685        let body = s.send_body_client(stream, false).unwrap();
4686
4687        let mut recv_buf = vec![0; body.len()];
4688
4689        let req_trailers = vec![Header::new(b"foo", b"bar")];
4690
4691        s.client
4692            .send_additional_headers(
4693                &mut s.pipe.client,
4694                stream,
4695                &req_trailers,
4696                true,
4697                false,
4698            )
4699            .unwrap();
4700
4701        s.advance().ok();
4702
4703        s.send_frame_client(
4704            frame::Frame::Data {
4705                payload: vec![1, 2, 3, 4],
4706            },
4707            stream,
4708            true,
4709        )
4710        .unwrap();
4711
4712        let ev_headers = Event::Headers {
4713            list: req,
4714            more_frames: true,
4715        };
4716
4717        let ev_trailers = Event::Headers {
4718            list: req_trailers,
4719            more_frames: true,
4720        };
4721
4722        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4723        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4724        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4725        assert_eq!(s.poll_server(), Ok((stream, ev_trailers)));
4726        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4727    }
4728
4729    #[test]
4730    /// Send a MAX_PUSH_ID frame from the client on a valid stream.
4731    fn max_push_id_from_client_good() {
4732        let mut s = Session::new().unwrap();
4733        s.handshake().unwrap();
4734
4735        s.send_frame_client(
4736            frame::Frame::MaxPushId { push_id: 1 },
4737            s.client.control_stream_id.unwrap(),
4738            false,
4739        )
4740        .unwrap();
4741
4742        assert_eq!(s.poll_server(), Err(Error::Done));
4743    }
4744
4745    #[test]
4746    /// Send a MAX_PUSH_ID frame from the client on an invalid stream.
4747    fn max_push_id_from_client_bad_stream() {
4748        let mut s = Session::new().unwrap();
4749        s.handshake().unwrap();
4750
4751        let (stream, req) = s.send_request(false).unwrap();
4752
4753        s.send_frame_client(
4754            frame::Frame::MaxPushId { push_id: 2 },
4755            stream,
4756            false,
4757        )
4758        .unwrap();
4759
4760        let ev_headers = Event::Headers {
4761            list: req,
4762            more_frames: true,
4763        };
4764
4765        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4766        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4767    }
4768
4769    #[test]
4770    /// Send a sequence of MAX_PUSH_ID frames from the client that attempt to
4771    /// reduce the limit.
4772    fn max_push_id_from_client_limit_reduction() {
4773        let mut s = Session::new().unwrap();
4774        s.handshake().unwrap();
4775
4776        s.send_frame_client(
4777            frame::Frame::MaxPushId { push_id: 2 },
4778            s.client.control_stream_id.unwrap(),
4779            false,
4780        )
4781        .unwrap();
4782
4783        s.send_frame_client(
4784            frame::Frame::MaxPushId { push_id: 1 },
4785            s.client.control_stream_id.unwrap(),
4786            false,
4787        )
4788        .unwrap();
4789
4790        assert_eq!(s.poll_server(), Err(Error::IdError));
4791    }
4792
4793    #[test]
4794    /// Send a MAX_PUSH_ID frame from the server, which is forbidden.
4795    fn max_push_id_from_server() {
4796        let mut s = Session::new().unwrap();
4797        s.handshake().unwrap();
4798
4799        s.send_frame_server(
4800            frame::Frame::MaxPushId { push_id: 1 },
4801            s.server.control_stream_id.unwrap(),
4802            false,
4803        )
4804        .unwrap();
4805
4806        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
4807    }
4808
4809    #[test]
4810    /// Send a PUSH_PROMISE frame from the client, which is forbidden.
4811    fn push_promise_from_client() {
4812        let mut s = Session::new().unwrap();
4813        s.handshake().unwrap();
4814
4815        let (stream, req) = s.send_request(false).unwrap();
4816
4817        let header_block = s.client.encode_header_block(&req).unwrap();
4818
4819        s.send_frame_client(
4820            frame::Frame::PushPromise {
4821                push_id: 1,
4822                header_block,
4823            },
4824            stream,
4825            false,
4826        )
4827        .unwrap();
4828
4829        let ev_headers = Event::Headers {
4830            list: req,
4831            more_frames: true,
4832        };
4833
4834        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4835        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4836    }
4837
4838    #[test]
4839    /// Server push streams from client are not allowed by the protocol.
4840    fn push_stream_from_client() {
4841        let mut s = Session::new().unwrap();
4842        s.handshake().unwrap();
4843
4844        s.client
4845            .open_uni_stream(
4846                &mut s.pipe.client,
4847                stream::HTTP3_PUSH_STREAM_TYPE_ID,
4848            )
4849            .unwrap();
4850
4851        s.advance().ok();
4852
4853        assert_eq!(s.poll_server(), Err(Error::StreamCreationError));
4854    }
4855
4856    #[test]
4857    /// Server push streams from server are not allowed since the client does
4858    /// not advertise server push support by default.
4859    fn push_stream_from_server() {
4860        let mut s = Session::new().unwrap();
4861        s.handshake().unwrap();
4862
4863        s.server
4864            .open_uni_stream(
4865                &mut s.pipe.server,
4866                stream::HTTP3_PUSH_STREAM_TYPE_ID,
4867            )
4868            .unwrap();
4869
4870        s.advance().ok();
4871
4872        assert_eq!(s.poll_client(), Err(Error::StreamCreationError));
4873    }
4874
4875    #[test]
4876    /// Send a CANCEL_PUSH frame from the client.
4877    fn cancel_push_from_client() {
4878        let mut s = Session::new().unwrap();
4879        s.handshake().unwrap();
4880
4881        s.send_frame_client(
4882            frame::Frame::CancelPush { push_id: 1 },
4883            s.client.control_stream_id.unwrap(),
4884            false,
4885        )
4886        .unwrap();
4887
4888        assert_eq!(s.poll_server(), Err(Error::Done));
4889    }
4890
4891    #[test]
4892    /// Send a CANCEL_PUSH frame from the client on an invalid stream.
4893    fn cancel_push_from_client_bad_stream() {
4894        let mut s = Session::new().unwrap();
4895        s.handshake().unwrap();
4896
4897        let (stream, req) = s.send_request(false).unwrap();
4898
4899        s.send_frame_client(
4900            frame::Frame::CancelPush { push_id: 2 },
4901            stream,
4902            false,
4903        )
4904        .unwrap();
4905
4906        let ev_headers = Event::Headers {
4907            list: req,
4908            more_frames: true,
4909        };
4910
4911        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4912        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4913    }
4914
4915    #[test]
4916    /// Send a CANCEL_PUSH frame from the client.
4917    fn cancel_push_from_server() {
4918        let mut s = Session::new().unwrap();
4919        s.handshake().unwrap();
4920
4921        s.send_frame_server(
4922            frame::Frame::CancelPush { push_id: 1 },
4923            s.server.control_stream_id.unwrap(),
4924            false,
4925        )
4926        .unwrap();
4927
4928        assert_eq!(s.poll_client(), Err(Error::Done));
4929    }
4930
4931    #[test]
4932    /// Send a GOAWAY frame from the client.
4933    fn goaway_from_client_good() {
4934        let mut s = Session::new().unwrap();
4935        s.handshake().unwrap();
4936
4937        s.client.send_goaway(&mut s.pipe.client, 100).unwrap();
4938
4939        s.advance().ok();
4940
4941        // TODO: server push
4942        assert_eq!(s.poll_server(), Ok((0, Event::GoAway)));
4943    }
4944
4945    #[test]
4946    /// Send a GOAWAY frame from the server.
4947    fn goaway_from_server_good() {
4948        let mut s = Session::new().unwrap();
4949        s.handshake().unwrap();
4950
4951        s.server.send_goaway(&mut s.pipe.server, 4000).unwrap();
4952
4953        s.advance().ok();
4954
4955        assert_eq!(s.poll_client(), Ok((4000, Event::GoAway)));
4956    }
4957
4958    #[test]
4959    /// A client MUST NOT send a request after it receives GOAWAY.
4960    fn client_request_after_goaway() {
4961        let mut s = Session::new().unwrap();
4962        s.handshake().unwrap();
4963
4964        s.server.send_goaway(&mut s.pipe.server, 4000).unwrap();
4965
4966        s.advance().ok();
4967
4968        assert_eq!(s.poll_client(), Ok((4000, Event::GoAway)));
4969
4970        assert_eq!(s.send_request(true), Err(Error::FrameUnexpected));
4971    }
4972
4973    #[test]
4974    /// Send a GOAWAY frame from the server, using an invalid goaway ID.
4975    fn goaway_from_server_invalid_id() {
4976        let mut s = Session::new().unwrap();
4977        s.handshake().unwrap();
4978
4979        s.send_frame_server(
4980            frame::Frame::GoAway { id: 1 },
4981            s.server.control_stream_id.unwrap(),
4982            false,
4983        )
4984        .unwrap();
4985
4986        assert_eq!(s.poll_client(), Err(Error::IdError));
4987    }
4988
4989    #[test]
4990    /// Send multiple GOAWAY frames from the server, that increase the goaway
4991    /// ID.
4992    fn goaway_from_server_increase_id() {
4993        let mut s = Session::new().unwrap();
4994        s.handshake().unwrap();
4995
4996        s.send_frame_server(
4997            frame::Frame::GoAway { id: 0 },
4998            s.server.control_stream_id.unwrap(),
4999            false,
5000        )
5001        .unwrap();
5002
5003        s.send_frame_server(
5004            frame::Frame::GoAway { id: 4 },
5005            s.server.control_stream_id.unwrap(),
5006            false,
5007        )
5008        .unwrap();
5009
5010        assert_eq!(s.poll_client(), Ok((0, Event::GoAway)));
5011
5012        assert_eq!(s.poll_client(), Err(Error::IdError));
5013    }
5014
5015    #[test]
5016    #[cfg(feature = "sfv")]
5017    fn parse_priority_field_value() {
5018        // Legal dicts
5019        assert_eq!(
5020            Ok(Priority::new(0, false)),
5021            Priority::try_from(b"u=0".as_slice())
5022        );
5023        assert_eq!(
5024            Ok(Priority::new(3, false)),
5025            Priority::try_from(b"u=3".as_slice())
5026        );
5027        assert_eq!(
5028            Ok(Priority::new(7, false)),
5029            Priority::try_from(b"u=7".as_slice())
5030        );
5031
5032        assert_eq!(
5033            Ok(Priority::new(0, true)),
5034            Priority::try_from(b"u=0, i".as_slice())
5035        );
5036        assert_eq!(
5037            Ok(Priority::new(3, true)),
5038            Priority::try_from(b"u=3, i".as_slice())
5039        );
5040        assert_eq!(
5041            Ok(Priority::new(7, true)),
5042            Priority::try_from(b"u=7, i".as_slice())
5043        );
5044
5045        assert_eq!(
5046            Ok(Priority::new(0, true)),
5047            Priority::try_from(b"u=0, i=?1".as_slice())
5048        );
5049        assert_eq!(
5050            Ok(Priority::new(3, true)),
5051            Priority::try_from(b"u=3, i=?1".as_slice())
5052        );
5053        assert_eq!(
5054            Ok(Priority::new(7, true)),
5055            Priority::try_from(b"u=7, i=?1".as_slice())
5056        );
5057
5058        assert_eq!(
5059            Ok(Priority::new(3, false)),
5060            Priority::try_from(b"".as_slice())
5061        );
5062
5063        assert_eq!(
5064            Ok(Priority::new(0, true)),
5065            Priority::try_from(b"u=0;foo, i;bar".as_slice())
5066        );
5067        assert_eq!(
5068            Ok(Priority::new(3, true)),
5069            Priority::try_from(b"u=3;hello, i;world".as_slice())
5070        );
5071        assert_eq!(
5072            Ok(Priority::new(7, true)),
5073            Priority::try_from(b"u=7;croeso, i;gymru".as_slice())
5074        );
5075
5076        assert_eq!(
5077            Ok(Priority::new(0, true)),
5078            Priority::try_from(b"u=0, i, spinaltap=11".as_slice())
5079        );
5080
5081        // Illegal formats
5082        assert_eq!(Err(Error::Done), Priority::try_from(b"0".as_slice()));
5083        assert_eq!(
5084            Ok(Priority::new(7, false)),
5085            Priority::try_from(b"u=-1".as_slice())
5086        );
5087        assert_eq!(Err(Error::Done), Priority::try_from(b"u=0.2".as_slice()));
5088        assert_eq!(
5089            Ok(Priority::new(7, false)),
5090            Priority::try_from(b"u=100".as_slice())
5091        );
5092        assert_eq!(
5093            Err(Error::Done),
5094            Priority::try_from(b"u=3, i=true".as_slice())
5095        );
5096
5097        // Trailing comma in dict is malformed
5098        assert_eq!(Err(Error::Done), Priority::try_from(b"u=7, ".as_slice()));
5099    }
5100
5101    #[test]
5102    /// Send a PRIORITY_UPDATE for request stream from the client.
5103    fn priority_update_request() {
5104        let mut s = Session::new().unwrap();
5105        s.handshake().unwrap();
5106
5107        s.client
5108            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5109                urgency: 3,
5110                incremental: false,
5111            })
5112            .unwrap();
5113        s.advance().ok();
5114
5115        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5116        assert_eq!(s.poll_server(), Err(Error::Done));
5117    }
5118
5119    #[test]
5120    /// Send a PRIORITY_UPDATE for request stream from the client that is too
5121    /// large.
5122    fn priority_update_request_max_size_limit_default() {
5123        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5124        config
5125            .load_cert_chain_from_pem_file("examples/cert.crt")
5126            .unwrap();
5127        config
5128            .load_priv_key_from_pem_file("examples/cert.key")
5129            .unwrap();
5130        config.set_application_protos(&[b"h3"]).unwrap();
5131        config.set_initial_max_data(1500);
5132        config.set_initial_max_stream_data_bidi_local(1500);
5133        config.set_initial_max_stream_data_bidi_remote(1500);
5134        config.set_initial_max_stream_data_uni(1500);
5135        config.set_initial_max_streams_bidi(5);
5136        config.set_initial_max_streams_uni(5);
5137        config.verify_peer(false);
5138
5139        let h3_config = Config::new().unwrap();
5140
5141        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
5142
5143        s.handshake().unwrap();
5144
5145        let mut d = vec![42; 600];
5146        let mut b = octets::OctetsMut::with_slice(&mut d);
5147
5148        let pu = frame::Frame::PriorityUpdateRequest {
5149            prioritized_element_id: 0,
5150            priority_field_value: vec![0; 512],
5151        };
5152
5153        pu.to_bytes(&mut b).unwrap();
5154
5155        s.pipe.client.stream_send(2, &d, true).unwrap();
5156
5157        s.advance().ok();
5158
5159        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
5160
5161        assert_eq!(
5162            s.pipe.server.local_error.as_ref().unwrap().error_code,
5163            Error::to_wire(Error::ExcessiveLoad)
5164        );
5165    }
5166
5167    #[test]
5168    /// Send a PRIORITY_UPDATE for request stream from the client.
5169    fn priority_update_single_stream_rearm() {
5170        let mut s = Session::new().unwrap();
5171        s.handshake().unwrap();
5172
5173        s.client
5174            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5175                urgency: 3,
5176                incremental: false,
5177            })
5178            .unwrap();
5179        s.advance().ok();
5180
5181        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5182        assert_eq!(s.poll_server(), Err(Error::Done));
5183
5184        s.client
5185            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5186                urgency: 5,
5187                incremental: false,
5188            })
5189            .unwrap();
5190        s.advance().ok();
5191
5192        assert_eq!(s.poll_server(), Err(Error::Done));
5193
5194        // There is only one PRIORITY_UPDATE frame to read. Once read, the event
5195        // will rearm ready for more.
5196        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=5".to_vec()));
5197        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5198
5199        s.client
5200            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5201                urgency: 7,
5202                incremental: false,
5203            })
5204            .unwrap();
5205        s.advance().ok();
5206
5207        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5208        assert_eq!(s.poll_server(), Err(Error::Done));
5209
5210        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=7".to_vec()));
5211        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5212    }
5213
5214    #[test]
5215    /// Send multiple PRIORITY_UPDATE frames for different streams from the
5216    /// client across multiple flights of exchange.
5217    fn priority_update_request_multiple_stream_arm_multiple_flights() {
5218        let mut s = Session::new().unwrap();
5219        s.handshake().unwrap();
5220
5221        s.client
5222            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5223                urgency: 3,
5224                incremental: false,
5225            })
5226            .unwrap();
5227        s.advance().ok();
5228
5229        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5230        assert_eq!(s.poll_server(), Err(Error::Done));
5231
5232        s.client
5233            .send_priority_update_for_request(&mut s.pipe.client, 4, &Priority {
5234                urgency: 1,
5235                incremental: false,
5236            })
5237            .unwrap();
5238        s.advance().ok();
5239
5240        assert_eq!(s.poll_server(), Ok((4, Event::PriorityUpdate)));
5241        assert_eq!(s.poll_server(), Err(Error::Done));
5242
5243        s.client
5244            .send_priority_update_for_request(&mut s.pipe.client, 8, &Priority {
5245                urgency: 2,
5246                incremental: false,
5247            })
5248            .unwrap();
5249        s.advance().ok();
5250
5251        assert_eq!(s.poll_server(), Ok((8, Event::PriorityUpdate)));
5252        assert_eq!(s.poll_server(), Err(Error::Done));
5253
5254        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5255        assert_eq!(s.server.take_last_priority_update(4), Ok(b"u=1".to_vec()));
5256        assert_eq!(s.server.take_last_priority_update(8), Ok(b"u=2".to_vec()));
5257        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5258    }
5259
5260    #[test]
5261    /// Send multiple PRIORITY_UPDATE frames for different streams from the
5262    /// client across a single flight.
5263    fn priority_update_request_multiple_stream_arm_single_flight() {
5264        let mut s = Session::new().unwrap();
5265        s.handshake().unwrap();
5266
5267        let mut d = [42; 65535];
5268
5269        let mut b = octets::OctetsMut::with_slice(&mut d);
5270
5271        let p1 = frame::Frame::PriorityUpdateRequest {
5272            prioritized_element_id: 0,
5273            priority_field_value: b"u=3".to_vec(),
5274        };
5275
5276        let p2 = frame::Frame::PriorityUpdateRequest {
5277            prioritized_element_id: 4,
5278            priority_field_value: b"u=3".to_vec(),
5279        };
5280
5281        let p3 = frame::Frame::PriorityUpdateRequest {
5282            prioritized_element_id: 8,
5283            priority_field_value: b"u=3".to_vec(),
5284        };
5285
5286        p1.to_bytes(&mut b).unwrap();
5287        p2.to_bytes(&mut b).unwrap();
5288        p3.to_bytes(&mut b).unwrap();
5289
5290        let off = b.off();
5291        s.pipe
5292            .client
5293            .stream_send(s.client.control_stream_id.unwrap(), &d[..off], false)
5294            .unwrap();
5295
5296        s.advance().ok();
5297
5298        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5299        assert_eq!(s.poll_server(), Ok((4, Event::PriorityUpdate)));
5300        assert_eq!(s.poll_server(), Ok((8, Event::PriorityUpdate)));
5301        assert_eq!(s.poll_server(), Err(Error::Done));
5302
5303        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5304        assert_eq!(s.server.take_last_priority_update(4), Ok(b"u=3".to_vec()));
5305        assert_eq!(s.server.take_last_priority_update(8), Ok(b"u=3".to_vec()));
5306
5307        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5308    }
5309
5310    #[test]
5311    /// Send a PRIORITY_UPDATE for a request stream, before and after the stream
5312    /// has been completed.
5313    fn priority_update_request_collected_completed() {
5314        let mut s = Session::new().unwrap();
5315        s.handshake().unwrap();
5316
5317        s.client
5318            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5319                urgency: 3,
5320                incremental: false,
5321            })
5322            .unwrap();
5323        s.advance().ok();
5324
5325        let (stream, req) = s.send_request(true).unwrap();
5326        let ev_headers = Event::Headers {
5327            list: req,
5328            more_frames: false,
5329        };
5330
5331        // Priority event is generated before request headers.
5332        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5333        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5334        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
5335        assert_eq!(s.poll_server(), Err(Error::Done));
5336
5337        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5338        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5339
5340        let resp = s.send_response(stream, true).unwrap();
5341
5342        let ev_headers = Event::Headers {
5343            list: resp,
5344            more_frames: false,
5345        };
5346
5347        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
5348        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
5349        assert_eq!(s.poll_client(), Err(Error::Done));
5350
5351        // Now send a PRIORITY_UPDATE for the completed request stream.
5352        s.client
5353            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5354                urgency: 3,
5355                incremental: false,
5356            })
5357            .unwrap();
5358        s.advance().ok();
5359
5360        // No event generated at server
5361        assert_eq!(s.poll_server(), Err(Error::Done));
5362    }
5363
5364    #[test]
5365    /// Send a PRIORITY_UPDATE for a request stream after H3 has collected it,
5366    /// but before the transport stream has been collected.
5367    fn priority_update_request_after_h3_collection() {
5368        let mut s = Session::new().unwrap();
5369        s.handshake().unwrap();
5370
5371        let init_streams_server = s.server.streams.len();
5372
5373        let (stream, req) = s.send_request(true).unwrap();
5374        let ev_headers = Event::Headers {
5375            list: req,
5376            more_frames: false,
5377        };
5378
5379        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5380        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
5381        assert_eq!(s.poll_server(), Err(Error::Done));
5382
5383        let resp = vec![
5384            Header::new(b":status", b"200"),
5385            Header::new(b"server", b"quiche-test"),
5386        ];
5387
5388        s.server
5389            .send_response(&mut s.pipe.server, stream, &resp, true)
5390            .unwrap();
5391
5392        // H3 no longer needs its stream state once it has sent the response
5393        // FIN and consumed the request FIN. The QUIC stream remains until the
5394        // response FIN is acknowledged by the peer.
5395        assert_eq!(s.server.streams.len(), init_streams_server);
5396        assert!(s.pipe.server.stream_finished(stream));
5397        assert!(s.pipe.server.stream_closed(stream));
5398
5399        let stream_state = s.pipe.server.streams.get(stream).unwrap();
5400        assert!(stream_state.recv.is_fin());
5401        assert!(stream_state.send.is_fin());
5402        assert!(!s.pipe.server.streams.is_collected(stream));
5403
5404        s.client
5405            .send_priority_update_for_request(
5406                &mut s.pipe.client,
5407                stream,
5408                &Priority {
5409                    urgency: 3,
5410                    incremental: false,
5411                },
5412            )
5413            .unwrap();
5414
5415        let flight = crate::test_utils::emit_flight(&mut s.pipe.client).unwrap();
5416        crate::test_utils::process_flight(&mut s.pipe.server, flight).unwrap();
5417
5418        assert_eq!(s.poll_server(), Err(Error::Done));
5419        assert_eq!(s.server.streams.len(), init_streams_server);
5420    }
5421
5422    #[test]
5423    /// Send a PRIORITY_UPDATE for a request stream, before and after the stream
5424    /// has been stopped.
5425    fn priority_update_request_collected_stopped() {
5426        let mut s = Session::new().unwrap();
5427        s.handshake().unwrap();
5428
5429        s.client
5430            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5431                urgency: 3,
5432                incremental: false,
5433            })
5434            .unwrap();
5435        s.advance().ok();
5436
5437        let (stream, req) = s.send_request(false).unwrap();
5438        let ev_headers = Event::Headers {
5439            list: req,
5440            more_frames: true,
5441        };
5442
5443        // Priority event is generated before request headers.
5444        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5445        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5446        assert_eq!(s.poll_server(), Err(Error::Done));
5447
5448        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5449        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5450
5451        s.pipe
5452            .client
5453            .stream_shutdown(stream, crate::Shutdown::Write, 0x100)
5454            .unwrap();
5455        s.pipe
5456            .client
5457            .stream_shutdown(stream, crate::Shutdown::Read, 0x100)
5458            .unwrap();
5459
5460        s.advance().ok();
5461
5462        assert_eq!(s.poll_server(), Ok((0, Event::Reset(0x100))));
5463        assert_eq!(s.poll_server(), Err(Error::Done));
5464
5465        // Now send a PRIORITY_UPDATE for the closed request stream.
5466        s.client
5467            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5468                urgency: 3,
5469                incremental: false,
5470            })
5471            .unwrap();
5472        s.advance().ok();
5473
5474        // No event generated at server
5475        assert_eq!(s.poll_server(), Err(Error::Done));
5476
5477        assert!(s.pipe.server.streams.is_collected(0));
5478        assert!(s.pipe.client.streams.is_collected(0));
5479    }
5480
5481    #[test]
5482    /// Send a PRIORITY_UPDATE for push stream from the client.
5483    fn priority_update_push() {
5484        let mut s = Session::new().unwrap();
5485        s.handshake().unwrap();
5486
5487        s.send_frame_client(
5488            frame::Frame::PriorityUpdatePush {
5489                prioritized_element_id: 3,
5490                priority_field_value: b"u=3".to_vec(),
5491            },
5492            s.client.control_stream_id.unwrap(),
5493            false,
5494        )
5495        .unwrap();
5496
5497        assert_eq!(s.poll_server(), Err(Error::Done));
5498    }
5499
5500    #[test]
5501    /// Send a PRIORITY_UPDATE for request stream from the client but for an
5502    /// incorrect stream type.
5503    fn priority_update_request_bad_stream() {
5504        let mut s = Session::new().unwrap();
5505        s.handshake().unwrap();
5506
5507        s.send_frame_client(
5508            frame::Frame::PriorityUpdateRequest {
5509                prioritized_element_id: 5,
5510                priority_field_value: b"u=3".to_vec(),
5511            },
5512            s.client.control_stream_id.unwrap(),
5513            false,
5514        )
5515        .unwrap();
5516
5517        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
5518    }
5519
5520    #[test]
5521    /// Send a PRIORITY_UPDATE for push stream from the client but for an
5522    /// incorrect stream type.
5523    fn priority_update_push_bad_stream() {
5524        let mut s = Session::new().unwrap();
5525        s.handshake().unwrap();
5526
5527        s.send_frame_client(
5528            frame::Frame::PriorityUpdatePush {
5529                prioritized_element_id: 5,
5530                priority_field_value: b"u=3".to_vec(),
5531            },
5532            s.client.control_stream_id.unwrap(),
5533            false,
5534        )
5535        .unwrap();
5536
5537        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
5538    }
5539
5540    #[test]
5541    /// Send a PRIORITY_UPDATE for request stream from the server.
5542    fn priority_update_request_from_server() {
5543        let mut s = Session::new().unwrap();
5544        s.handshake().unwrap();
5545
5546        s.send_frame_server(
5547            frame::Frame::PriorityUpdateRequest {
5548                prioritized_element_id: 0,
5549                priority_field_value: b"u=3".to_vec(),
5550            },
5551            s.server.control_stream_id.unwrap(),
5552            false,
5553        )
5554        .unwrap();
5555
5556        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
5557    }
5558
5559    #[test]
5560    /// Send a PRIORITY_UPDATE for request stream from the server.
5561    fn priority_update_push_from_server() {
5562        let mut s = Session::new().unwrap();
5563        s.handshake().unwrap();
5564
5565        s.send_frame_server(
5566            frame::Frame::PriorityUpdatePush {
5567                prioritized_element_id: 0,
5568                priority_field_value: b"u=3".to_vec(),
5569            },
5570            s.server.control_stream_id.unwrap(),
5571            false,
5572        )
5573        .unwrap();
5574
5575        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
5576    }
5577
5578    #[test]
5579    /// Ensure quiche allocates streams for client and server roles as expected.
5580    fn uni_stream_local_counting() {
5581        let config = Config::new().unwrap();
5582
5583        let h3_cln = Connection::new(&config, false, false).unwrap();
5584        assert_eq!(h3_cln.next_uni_stream_id, 2);
5585
5586        let h3_srv = Connection::new(&config, true, false).unwrap();
5587        assert_eq!(h3_srv.next_uni_stream_id, 3);
5588    }
5589
5590    #[test]
5591    /// Client opens multiple control streams, which is forbidden.
5592    fn open_multiple_control_streams() {
5593        let mut s = Session::new().unwrap();
5594        s.handshake().unwrap();
5595
5596        let stream_id = s.client.next_uni_stream_id;
5597
5598        let mut d = [42; 8];
5599        let mut b = octets::OctetsMut::with_slice(&mut d);
5600
5601        s.pipe
5602            .client
5603            .stream_send(
5604                stream_id,
5605                b.put_varint(stream::HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(),
5606                false,
5607            )
5608            .unwrap();
5609
5610        s.advance().ok();
5611
5612        assert_eq!(s.poll_server(), Err(Error::StreamCreationError));
5613    }
5614
5615    #[test]
5616    /// Client closes the control stream, which is forbidden.
5617    fn close_control_stream_after_type() {
5618        let mut s = Session::new().unwrap();
5619        s.handshake().unwrap();
5620
5621        s.pipe
5622            .client
5623            .stream_send(s.client.control_stream_id.unwrap(), &[], true)
5624            .unwrap();
5625
5626        s.advance().ok();
5627
5628        assert_eq!(
5629            Err(Error::ClosedCriticalStream),
5630            s.server.poll(&mut s.pipe.server)
5631        );
5632        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5633    }
5634
5635    #[test]
5636    /// Client closes the control stream after a frame is sent, which is
5637    /// forbidden.
5638    fn close_control_stream_after_frame() {
5639        let mut s = Session::new().unwrap();
5640        s.handshake().unwrap();
5641
5642        s.send_frame_client(
5643            frame::Frame::MaxPushId { push_id: 1 },
5644            s.client.control_stream_id.unwrap(),
5645            true,
5646        )
5647        .unwrap();
5648
5649        assert_eq!(
5650            Err(Error::ClosedCriticalStream),
5651            s.server.poll(&mut s.pipe.server)
5652        );
5653        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5654    }
5655
5656    #[test]
5657    /// Client resets the control stream, which is forbidden.
5658    fn reset_control_stream_after_type() {
5659        let mut s = Session::new().unwrap();
5660        s.handshake().unwrap();
5661
5662        s.pipe
5663            .client
5664            .stream_shutdown(
5665                s.client.control_stream_id.unwrap(),
5666                crate::Shutdown::Write,
5667                0,
5668            )
5669            .unwrap();
5670
5671        s.advance().ok();
5672
5673        assert_eq!(
5674            Err(Error::ClosedCriticalStream),
5675            s.server.poll(&mut s.pipe.server)
5676        );
5677        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5678    }
5679
5680    #[test]
5681    /// Client resets the control stream after a frame is sent, which is
5682    /// forbidden.
5683    fn reset_control_stream_after_frame() {
5684        let mut s = Session::new().unwrap();
5685        s.handshake().unwrap();
5686
5687        s.send_frame_client(
5688            frame::Frame::MaxPushId { push_id: 1 },
5689            s.client.control_stream_id.unwrap(),
5690            false,
5691        )
5692        .unwrap();
5693
5694        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5695
5696        s.pipe
5697            .client
5698            .stream_shutdown(
5699                s.client.control_stream_id.unwrap(),
5700                crate::Shutdown::Write,
5701                0,
5702            )
5703            .unwrap();
5704
5705        s.advance().ok();
5706
5707        assert_eq!(
5708            Err(Error::ClosedCriticalStream),
5709            s.server.poll(&mut s.pipe.server)
5710        );
5711        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5712    }
5713
5714    #[test]
5715    /// Client closes QPACK stream, which is forbidden.
5716    fn close_qpack_stream_after_type() {
5717        let mut s = Session::new().unwrap();
5718        s.handshake().unwrap();
5719
5720        s.pipe
5721            .client
5722            .stream_send(
5723                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
5724                &[],
5725                true,
5726            )
5727            .unwrap();
5728
5729        s.advance().ok();
5730
5731        assert_eq!(
5732            Err(Error::ClosedCriticalStream),
5733            s.server.poll(&mut s.pipe.server)
5734        );
5735        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5736    }
5737
5738    #[test]
5739    /// Client closes QPACK stream after sending some stuff, which is forbidden.
5740    fn close_qpack_stream_after_data() {
5741        let mut s = Session::new().unwrap();
5742        s.handshake().unwrap();
5743
5744        let stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5745        let d = [0; 1];
5746
5747        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5748        s.pipe.client.stream_send(stream_id, &d, true).unwrap();
5749
5750        s.advance().ok();
5751
5752        assert_eq!(
5753            Err(Error::ClosedCriticalStream),
5754            s.server.poll(&mut s.pipe.server)
5755        );
5756        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5757    }
5758
5759    #[test]
5760    /// Client resets QPACK stream, which is forbidden.
5761    fn reset_qpack_stream_after_type() {
5762        let mut s = Session::new().unwrap();
5763        s.handshake().unwrap();
5764
5765        s.pipe
5766            .client
5767            .stream_shutdown(
5768                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
5769                crate::Shutdown::Write,
5770                0,
5771            )
5772            .unwrap();
5773
5774        s.advance().ok();
5775
5776        assert_eq!(
5777            Err(Error::ClosedCriticalStream),
5778            s.server.poll(&mut s.pipe.server)
5779        );
5780        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5781    }
5782
5783    #[test]
5784    /// Client resets QPACK stream after sending some stuff, which is forbidden.
5785    fn reset_qpack_stream_after_data() {
5786        let mut s = Session::new().unwrap();
5787        s.handshake().unwrap();
5788
5789        let stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5790        let d = [0; 1];
5791
5792        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5793        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5794
5795        s.advance().ok();
5796
5797        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5798
5799        s.pipe
5800            .client
5801            .stream_shutdown(stream_id, crate::Shutdown::Write, 0)
5802            .unwrap();
5803
5804        s.advance().ok();
5805
5806        assert_eq!(
5807            Err(Error::ClosedCriticalStream),
5808            s.server.poll(&mut s.pipe.server)
5809        );
5810        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5811    }
5812
5813    #[test]
5814    /// Client sends QPACK data.
5815    fn qpack_data() {
5816        // TODO: QPACK instructions are ignored until dynamic table support is
5817        // added so we just test that the data is safely ignored.
5818        let mut s = Session::new().unwrap();
5819        s.handshake().unwrap();
5820
5821        let e_stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5822        let d_stream_id = s.client.local_qpack_streams.decoder_stream_id.unwrap();
5823        let d = [0; 20];
5824
5825        s.pipe.client.stream_send(e_stream_id, &d, false).unwrap();
5826        s.advance().ok();
5827
5828        s.pipe.client.stream_send(d_stream_id, &d, false).unwrap();
5829        s.advance().ok();
5830
5831        match s.server.poll(&mut s.pipe.server) {
5832            Ok(_) => panic!(),
5833
5834            Err(Error::Done) => {
5835                assert_eq!(s.server.peer_qpack_streams.encoder_stream_bytes, 20);
5836                assert_eq!(s.server.peer_qpack_streams.decoder_stream_bytes, 20);
5837            },
5838
5839            Err(_) => {
5840                panic!();
5841            },
5842        }
5843
5844        let stats = s.server.stats();
5845        assert_eq!(stats.qpack_encoder_stream_recv_bytes, 20);
5846        assert_eq!(stats.qpack_decoder_stream_recv_bytes, 20);
5847    }
5848
5849    #[test]
5850    /// Tests limits for the stream state buffer maximum size.
5851    fn max_state_buf_size() {
5852        let mut s = Session::new().unwrap();
5853        s.handshake().unwrap();
5854
5855        let req = vec![
5856            Header::new(b":method", b"GET"),
5857            Header::new(b":scheme", b"https"),
5858            Header::new(b":authority", b"quic.tech"),
5859            Header::new(b":path", b"/test"),
5860            Header::new(b"user-agent", b"quiche-test"),
5861        ];
5862
5863        assert_eq!(
5864            s.client.send_request(&mut s.pipe.client, &req, false),
5865            Ok(0)
5866        );
5867
5868        s.advance().ok();
5869
5870        let ev_headers = Event::Headers {
5871            list: req,
5872            more_frames: true,
5873        };
5874
5875        assert_eq!(s.server.poll(&mut s.pipe.server), Ok((0, ev_headers)));
5876
5877        // DATA frames don't consume the state buffer, so can be of any size.
5878        let mut d = [42; 128];
5879        let mut b = octets::OctetsMut::with_slice(&mut d);
5880
5881        let frame_type = b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
5882        s.pipe.client.stream_send(0, frame_type, false).unwrap();
5883
5884        let frame_len = b.put_varint(1 << 24).unwrap();
5885        s.pipe.client.stream_send(0, frame_len, false).unwrap();
5886
5887        s.pipe.client.stream_send(0, &d, false).unwrap();
5888
5889        s.advance().ok();
5890
5891        assert_eq!(s.server.poll(&mut s.pipe.server), Ok((0, Event::Data)));
5892
5893        // GREASE frames consume the state buffer, so need to be limited.
5894        let mut s = Session::new().unwrap();
5895        s.handshake().unwrap();
5896
5897        let mut d = [42; 128];
5898        let mut b = octets::OctetsMut::with_slice(&mut d);
5899
5900        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
5901        s.pipe.client.stream_send(0, frame_type, false).unwrap();
5902
5903        let frame_len = b.put_varint(1 << 24).unwrap();
5904        s.pipe.client.stream_send(0, frame_len, false).unwrap();
5905
5906        s.pipe.client.stream_send(0, &d, false).unwrap();
5907
5908        s.advance().ok();
5909
5910        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::ExcessiveLoad));
5911    }
5912
5913    #[test]
5914    /// Tests that DATA frames are properly truncated depending on the request
5915    /// stream's outgoing flow control capacity.
5916    fn stream_backpressure() {
5917        let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
5918
5919        let mut s = Session::new().unwrap();
5920        s.handshake().unwrap();
5921
5922        let (stream, req) = s.send_request(false).unwrap();
5923
5924        let total_data_frames = 6;
5925
5926        for _ in 0..total_data_frames {
5927            assert_eq!(
5928                s.client
5929                    .send_body(&mut s.pipe.client, stream, &bytes, false),
5930                Ok(bytes.len())
5931            );
5932
5933            s.advance().ok();
5934        }
5935
5936        assert_eq!(
5937            s.client.send_body(&mut s.pipe.client, stream, &bytes, true),
5938            Ok(bytes.len() - 2)
5939        );
5940
5941        s.advance().ok();
5942
5943        let mut recv_buf = vec![0; bytes.len()];
5944
5945        let ev_headers = Event::Headers {
5946            list: req,
5947            more_frames: true,
5948        };
5949
5950        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5951        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
5952        assert_eq!(s.poll_server(), Err(Error::Done));
5953
5954        for _ in 0..total_data_frames {
5955            assert_eq!(
5956                s.recv_body_server(stream, &mut recv_buf),
5957                Ok(bytes.len())
5958            );
5959        }
5960
5961        assert_eq!(
5962            s.recv_body_server(stream, &mut recv_buf),
5963            Ok(bytes.len() - 2)
5964        );
5965
5966        // Fin flag from last send_body() call was not sent as the buffer was
5967        // only partially written.
5968        assert_eq!(s.poll_server(), Err(Error::Done));
5969
5970        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
5971        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
5972        assert_eq!(s.pipe.server.data_blocked_recv_count, 0);
5973        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 1);
5974
5975        assert_eq!(s.pipe.client.data_blocked_sent_count, 0);
5976        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 1);
5977        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
5978        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
5979    }
5980
5981    #[test]
5982    /// Tests that the max header list size setting allows larger headers than
5983    /// default.
5984    fn request_max_header_size_limit_accepts_large_headers() {
5985        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5986        config
5987            .load_cert_chain_from_pem_file("examples/cert.crt")
5988            .unwrap();
5989        config
5990            .load_priv_key_from_pem_file("examples/cert.key")
5991            .unwrap();
5992        config.set_application_protos(&[b"h3"]).unwrap();
5993        config.set_initial_max_data(150000000);
5994        config.set_initial_max_stream_data_bidi_local(150000000);
5995        config.set_initial_max_stream_data_bidi_remote(150000000);
5996        config.set_initial_max_stream_data_uni(150000000);
5997        config.set_initial_max_streams_bidi(5);
5998        config.set_initial_max_streams_uni(5);
5999        config.verify_peer(false);
6000        config.set_initial_congestion_window_packets(100);
6001
6002        let mut h3_config = Config::new().unwrap();
6003        h3_config.set_max_field_section_size(256 * 1024);
6004
6005        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6006
6007        s.handshake().unwrap();
6008
6009        let mut req = vec![
6010            Header::new(b":method", b"GET"),
6011            Header::new(b":scheme", b"https"),
6012            Header::new(b":authority", b"quic.tech"),
6013            Header::new(b":path", b"/test"),
6014        ];
6015
6016        for _ in 1..5000 {
6017            req.push(Header::new(b"aaaaaaaaaa", b"aaaaaaaaa"));
6018        }
6019
6020        let ev_headers = Event::Headers {
6021            list: req.clone(),
6022            more_frames: false,
6023        };
6024
6025        let stream = s
6026            .client
6027            .send_request(&mut s.pipe.client, &req, true)
6028            .unwrap();
6029
6030        s.advance().ok();
6031
6032        assert_eq!(stream, 0);
6033
6034        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6035        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6036        assert_eq!(s.poll_server(), Err(Error::Done));
6037    }
6038
6039    #[test]
6040    /// Tests that the max header list size setting is enforced after decoding.
6041    fn request_max_header_size_limit_decoded_field_section() {
6042        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6043        config
6044            .load_cert_chain_from_pem_file("examples/cert.crt")
6045            .unwrap();
6046        config
6047            .load_priv_key_from_pem_file("examples/cert.key")
6048            .unwrap();
6049        config.set_application_protos(&[b"h3"]).unwrap();
6050        config.set_initial_max_data(1500);
6051        config.set_initial_max_stream_data_bidi_local(150);
6052        config.set_initial_max_stream_data_bidi_remote(150);
6053        config.set_initial_max_stream_data_uni(150);
6054        config.set_initial_max_streams_bidi(5);
6055        config.set_initial_max_streams_uni(5);
6056        config.verify_peer(false);
6057
6058        let mut h3_config = Config::new().unwrap();
6059        h3_config.set_max_field_section_size(65);
6060
6061        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6062
6063        s.handshake().unwrap();
6064
6065        let req = vec![
6066            Header::new(b":method", b"GET"),
6067            Header::new(b":scheme", b"https"),
6068            Header::new(b":authority", b"quic.tech"),
6069            Header::new(b":path", b"/test"),
6070            Header::new(b"aaaaaaa", b"aaaaaaaa"),
6071        ];
6072
6073        let stream = s
6074            .client
6075            .send_request(&mut s.pipe.client, &req, true)
6076            .unwrap();
6077
6078        s.advance().ok();
6079
6080        assert_eq!(stream, 0);
6081
6082        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
6083
6084        assert_eq!(
6085            s.pipe.server.local_error.as_ref().unwrap().error_code,
6086            Error::to_wire(Error::ExcessiveLoad)
6087        );
6088    }
6089
6090    #[test]
6091    /// Tests that the max header list size setting is enforced when observing
6092    /// frame size before decode.
6093    fn request_max_header_size_limit_default_abort_before_decode() {
6094        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6095        config
6096            .load_cert_chain_from_pem_file("examples/cert.crt")
6097            .unwrap();
6098        config
6099            .load_priv_key_from_pem_file("examples/cert.key")
6100            .unwrap();
6101        config.set_application_protos(&[b"h3"]).unwrap();
6102        config.set_initial_max_data(150000);
6103        config.set_initial_max_stream_data_bidi_local(150000);
6104        config.set_initial_max_stream_data_bidi_remote(150000);
6105        config.set_initial_max_stream_data_uni(150000);
6106        config.set_initial_max_streams_bidi(5);
6107        config.set_initial_max_streams_uni(5);
6108        config.verify_peer(false);
6109
6110        let h3_config = Config::new().unwrap();
6111
6112        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6113
6114        s.handshake().unwrap();
6115
6116        let mut d = vec![42; 200000];
6117        let mut b = octets::OctetsMut::with_slice(&mut d);
6118
6119        let hdrs = frame::Frame::Headers {
6120            header_block: vec![0; 65536],
6121        };
6122
6123        hdrs.to_bytes(&mut b).unwrap();
6124
6125        s.pipe.client.stream_send(0, &d, true).unwrap();
6126
6127        s.advance().ok();
6128
6129        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
6130
6131        assert_eq!(
6132            s.pipe.server.local_error.as_ref().unwrap().error_code,
6133            Error::to_wire(Error::ExcessiveLoad)
6134        );
6135    }
6136
6137    #[test]
6138    /// Tests that Error::TransportError contains a transport error.
6139    fn transport_error() {
6140        let mut s = Session::new().unwrap();
6141        s.handshake().unwrap();
6142
6143        let req = vec![
6144            Header::new(b":method", b"GET"),
6145            Header::new(b":scheme", b"https"),
6146            Header::new(b":authority", b"quic.tech"),
6147            Header::new(b":path", b"/test"),
6148            Header::new(b"user-agent", b"quiche-test"),
6149        ];
6150
6151        // We need to open all streams in the same flight, so we can't use the
6152        // Session::send_request() method because it also calls advance(),
6153        // otherwise the server would send a MAX_STREAMS frame and the client
6154        // wouldn't hit the streams limit.
6155        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6156        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(4));
6157        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(8));
6158        assert_eq!(
6159            s.client.send_request(&mut s.pipe.client, &req, true),
6160            Ok(12)
6161        );
6162        assert_eq!(
6163            s.client.send_request(&mut s.pipe.client, &req, true),
6164            Ok(16)
6165        );
6166
6167        assert_eq!(
6168            s.client.send_request(&mut s.pipe.client, &req, true),
6169            Err(Error::TransportError(crate::Error::StreamLimit))
6170        );
6171    }
6172
6173    #[test]
6174    /// Tests that sending DATA before HEADERS causes an error.
6175    fn data_before_headers() {
6176        let mut s = Session::new().unwrap();
6177        s.handshake().unwrap();
6178
6179        let mut d = [42; 128];
6180        let mut b = octets::OctetsMut::with_slice(&mut d);
6181
6182        let frame_type = b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
6183        s.pipe.client.stream_send(0, frame_type, false).unwrap();
6184
6185        let frame_len = b.put_varint(5).unwrap();
6186        s.pipe.client.stream_send(0, frame_len, false).unwrap();
6187
6188        s.pipe.client.stream_send(0, b"hello", false).unwrap();
6189
6190        s.advance().ok();
6191
6192        assert_eq!(
6193            s.server.poll(&mut s.pipe.server),
6194            Err(Error::FrameUnexpected)
6195        );
6196    }
6197
6198    #[test]
6199    /// Tests that calling poll() after an error occurred does nothing.
6200    fn poll_after_error() {
6201        let mut s = Session::new().unwrap();
6202        s.handshake().unwrap();
6203
6204        let mut d = [42; 128];
6205        let mut b = octets::OctetsMut::with_slice(&mut d);
6206
6207        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
6208        s.pipe.client.stream_send(0, frame_type, false).unwrap();
6209
6210        let frame_len = b.put_varint(1 << 24).unwrap();
6211        s.pipe.client.stream_send(0, frame_len, false).unwrap();
6212
6213        s.pipe.client.stream_send(0, &d, false).unwrap();
6214
6215        s.advance().ok();
6216
6217        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::ExcessiveLoad));
6218
6219        // Try to call poll() again after an error occurred.
6220        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
6221    }
6222
6223    #[test]
6224    /// Tests that we limit sending HEADERS based on the stream capacity.
6225    fn headers_blocked() {
6226        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6227        config
6228            .load_cert_chain_from_pem_file("examples/cert.crt")
6229            .unwrap();
6230        config
6231            .load_priv_key_from_pem_file("examples/cert.key")
6232            .unwrap();
6233        config.set_application_protos(&[b"h3"]).unwrap();
6234        config.set_initial_max_data(75);
6235        config.set_initial_max_stream_data_bidi_local(150);
6236        config.set_initial_max_stream_data_bidi_remote(150);
6237        config.set_initial_max_stream_data_uni(150);
6238        config.set_initial_max_streams_bidi(100);
6239        config.set_initial_max_streams_uni(5);
6240        config.verify_peer(false);
6241
6242        let h3_config = Config::new().unwrap();
6243
6244        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6245
6246        s.handshake().unwrap();
6247
6248        let req = vec![
6249            Header::new(b":method", b"GET"),
6250            Header::new(b":scheme", b"https"),
6251            Header::new(b":authority", b"quic.tech"),
6252            Header::new(b":path", b"/test"),
6253        ];
6254
6255        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6256
6257        assert_eq!(
6258            s.client.send_request(&mut s.pipe.client, &req, true),
6259            Err(Error::StreamBlocked)
6260        );
6261
6262        // Clear the writable stream queue.
6263        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6264        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6265        assert_eq!(s.pipe.client.stream_writable_next(), Some(10));
6266        assert_eq!(s.pipe.client.stream_writable_next(), None);
6267
6268        s.advance().ok();
6269
6270        // Once the server gives flow control credits back, we can send the
6271        // request.
6272        assert_eq!(s.pipe.client.stream_writable_next(), Some(4));
6273        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(4));
6274
6275        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
6276        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
6277        assert_eq!(s.pipe.server.data_blocked_recv_count, 1);
6278        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 0);
6279
6280        assert_eq!(s.pipe.client.data_blocked_sent_count, 1);
6281        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 0);
6282        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
6283        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
6284    }
6285
6286    #[test]
6287    /// Ensure StreamBlocked when connection flow control prevents headers.
6288    fn headers_blocked_on_conn() {
6289        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6290        config
6291            .load_cert_chain_from_pem_file("examples/cert.crt")
6292            .unwrap();
6293        config
6294            .load_priv_key_from_pem_file("examples/cert.key")
6295            .unwrap();
6296        config.set_application_protos(&[b"h3"]).unwrap();
6297        config.set_initial_max_data(75);
6298        config.set_initial_max_stream_data_bidi_local(150);
6299        config.set_initial_max_stream_data_bidi_remote(150);
6300        config.set_initial_max_stream_data_uni(150);
6301        config.set_initial_max_streams_bidi(100);
6302        config.set_initial_max_streams_uni(5);
6303        config.verify_peer(false);
6304
6305        let h3_config = Config::new().unwrap();
6306
6307        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6308
6309        s.handshake().unwrap();
6310
6311        // After the HTTP handshake, some bytes of connection flow control have
6312        // been consumed. Fill the connection with more grease data on the
6313        // control stream.
6314        let d = [42; 28];
6315        assert_eq!(s.pipe.client.stream_send(2, &d, false), Ok(23));
6316
6317        let req = vec![
6318            Header::new(b":method", b"GET"),
6319            Header::new(b":scheme", b"https"),
6320            Header::new(b":authority", b"quic.tech"),
6321            Header::new(b":path", b"/test"),
6322        ];
6323
6324        // There is 0 connection-level flow control, so sending a request is
6325        // blocked.
6326        assert_eq!(
6327            s.client.send_request(&mut s.pipe.client, &req, true),
6328            Err(Error::StreamBlocked)
6329        );
6330        assert_eq!(s.pipe.client.stream_writable_next(), None);
6331
6332        // Emit the control stream data and drain it at the server via poll() to
6333        // consumes it via poll() and gives back flow control.
6334        s.advance().ok();
6335        assert_eq!(s.poll_server(), Err(Error::Done));
6336        s.advance().ok();
6337
6338        // Now we can send the request.
6339        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6340        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6341        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6342
6343        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
6344        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
6345        assert_eq!(s.pipe.server.data_blocked_recv_count, 1);
6346        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 0);
6347
6348        assert_eq!(s.pipe.client.data_blocked_sent_count, 1);
6349        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 0);
6350        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
6351        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
6352    }
6353
6354    #[test]
6355    /// Ensure that the connection does not consume a stream ID when
6356    /// send_request fails with StreamBlocked due to hitting the
6357    /// MAX_DATA flow control limit when attempting to send headers.
6358    /// The headers are sent successfully after a MAX_DATA update.
6359    fn headers_blocked_by_max_data_success_on_retry() {
6360        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6361        config
6362            .load_cert_chain_from_pem_file("examples/cert.crt")
6363            .unwrap();
6364        config
6365            .load_priv_key_from_pem_file("examples/cert.key")
6366            .unwrap();
6367        config.set_application_protos(&[b"h3"]).unwrap();
6368        config.set_initial_max_data(70);
6369        config.set_initial_max_stream_data_bidi_local(150);
6370        config.set_initial_max_stream_data_bidi_remote(150);
6371        config.set_initial_max_stream_data_uni(150);
6372        config.set_initial_max_streams_bidi(100);
6373        config.set_initial_max_streams_uni(5);
6374        config.verify_peer(false);
6375
6376        let h3_config = Config::new().unwrap();
6377
6378        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6379
6380        s.handshake().unwrap();
6381
6382        let req = vec![
6383            Header::new(b":method", b"GET"),
6384            Header::new(b":scheme", b"https"),
6385            Header::new(b":authority", b"quic.tech"),
6386            Header::new(b":path", b"/test/with/long/url"),
6387        ];
6388
6389        // After the HTTP handshake, some bytes of connection flow
6390        // control have been consumed.  The serialized request does
6391        // not fit in the remaining connection-level flow control
6392        // limit.  send_request fails without creating a stream.
6393        assert_eq!(
6394            s.client.send_request(&mut s.pipe.client, &req, true),
6395            Err(Error::StreamBlocked)
6396        );
6397
6398        // Verify that stream 0 does not exist in the H3 stream map after the
6399        // failed attempt.
6400        assert!(!s.client.streams.contains_key(&0));
6401
6402        // Emit the control stream data and drain it at the server to give back
6403        // flow control.
6404        s.advance().ok();
6405        assert_eq!(s.poll_server(), Err(Error::Done));
6406        s.advance().ok();
6407
6408        // Now we can send the request. The stream ID should be 0 (not 4),
6409        // confirming that the blocked attempt did not consume the stream ID.
6410        let stream_id = s.client.send_request(&mut s.pipe.client, &req, true);
6411        assert_eq!(stream_id, Ok(0));
6412        assert!(s.client.streams.contains_key(&0));
6413        assert!(!s.client.streams.contains_key(&4));
6414
6415        // Subsequent request should use stream ID 4.
6416        let stream_id2 = s.client.send_request(&mut s.pipe.client, &req, true);
6417        assert_eq!(stream_id2, Ok(4));
6418        assert!(s.client.streams.contains_key(&0));
6419        assert!(s.client.streams.contains_key(&4));
6420
6421        s.advance().ok();
6422    }
6423
6424    #[test]
6425    /// Ensure STREAM_DATA_BLOCKED is not emitted multiple times with the same
6426    /// offset when trying to send large bodies.
6427    fn send_body_truncation_stream_blocked() {
6428        use crate::test_utils::decode_pkt;
6429
6430        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6431        config
6432            .load_cert_chain_from_pem_file("examples/cert.crt")
6433            .unwrap();
6434        config
6435            .load_priv_key_from_pem_file("examples/cert.key")
6436            .unwrap();
6437        config.set_application_protos(&[b"h3"]).unwrap();
6438        // Use generous connection-level flow control.
6439        config.set_initial_max_data(10000);
6440        config.set_initial_max_stream_data_bidi_local(80);
6441        config.set_initial_max_stream_data_bidi_remote(80);
6442        config.set_initial_max_stream_data_uni(150);
6443        config.set_initial_max_streams_bidi(100);
6444        config.set_initial_max_streams_uni(5);
6445        config.verify_peer(false);
6446
6447        let h3_config = Config::new().unwrap();
6448
6449        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6450
6451        s.handshake().unwrap();
6452
6453        let (stream, req) = s.send_request(true).unwrap();
6454
6455        let ev_headers = Event::Headers {
6456            list: req,
6457            more_frames: false,
6458        };
6459
6460        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6461        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6462
6463        let _ = s.send_response(stream, false).unwrap();
6464
6465        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6466
6467        // The body must be larger than the stream window would allow
6468        let d = [42; 500];
6469        let mut off = 0;
6470
6471        let sent = s
6472            .server
6473            .send_body(&mut s.pipe.server, stream, &d, true)
6474            .unwrap();
6475        assert_eq!(sent, 25);
6476        off += sent;
6477
6478        // send_body wrote as much as it could (sent < size of buff).
6479        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6480        assert_eq!(
6481            s.server
6482                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6483            Err(Error::Done)
6484        );
6485        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6486
6487        // Now read raw frames to see what the QUIC layer did
6488        let mut buf = [0; 65535];
6489        let (len, _) = s.pipe.server.send(&mut buf).unwrap();
6490
6491        let frames = decode_pkt(&mut s.pipe.client, &mut buf[..len]).unwrap();
6492
6493        let mut iter = frames.iter();
6494
6495        assert_eq!(
6496            iter.next(),
6497            Some(&crate::frame::Frame::StreamDataBlocked {
6498                stream_id: 0,
6499                limit: 80,
6500            })
6501        );
6502
6503        // At the server, after sending the STREAM_DATA_BLOCKED frame, we clear
6504        // the mark.
6505        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6506
6507        // Don't read any data from the client, so stream flow control is never
6508        // given back in the form of changing the stream's max offset.
6509        // Subsequent body send operations will still fail but no more
6510        // STREAM_DATA_BLOCKED frames should be submitted since the limit didn't
6511        // change. No frames means no packet to send.
6512        assert_eq!(
6513            s.server
6514                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6515            Err(Error::Done)
6516        );
6517        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6518        assert_eq!(s.pipe.server.send(&mut buf), Err(crate::Error::Done));
6519
6520        // Now update the client's max offset manually.
6521        let frames = [crate::frame::Frame::MaxStreamData {
6522            stream_id: 0,
6523            max: 100,
6524        }];
6525
6526        let pkt_type = crate::packet::Type::Short;
6527        assert_eq!(
6528            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
6529            Ok(39),
6530        );
6531
6532        let sent = s
6533            .server
6534            .send_body(&mut s.pipe.server, stream, &d[off..], true)
6535            .unwrap();
6536        assert_eq!(sent, 18);
6537
6538        // Same thing here...
6539        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6540        assert_eq!(
6541            s.server
6542                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6543            Err(Error::Done)
6544        );
6545        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6546
6547        let (len, _) = s.pipe.server.send(&mut buf).unwrap();
6548
6549        let frames = decode_pkt(&mut s.pipe.client, &mut buf[..len]).unwrap();
6550
6551        let mut iter = frames.iter();
6552
6553        assert_eq!(
6554            iter.next(),
6555            Some(&crate::frame::Frame::StreamDataBlocked {
6556                stream_id: 0,
6557                limit: 100,
6558            })
6559        );
6560    }
6561
6562    #[test]
6563    /// Ensure stream doesn't hang due to small cwnd.
6564    fn send_body_stream_blocked_by_small_cwnd() {
6565        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6566        config
6567            .load_cert_chain_from_pem_file("examples/cert.crt")
6568            .unwrap();
6569        config
6570            .load_priv_key_from_pem_file("examples/cert.key")
6571            .unwrap();
6572        config.set_application_protos(&[b"h3"]).unwrap();
6573        // Use generous connection-level flow control.
6574        config.set_initial_max_data(100000);
6575        config.set_initial_max_stream_data_bidi_local(100000);
6576        config.set_initial_max_stream_data_bidi_remote(50000);
6577        config.set_initial_max_stream_data_uni(150);
6578        config.set_initial_max_streams_bidi(100);
6579        config.set_initial_max_streams_uni(5);
6580        config.verify_peer(false);
6581
6582        let h3_config = Config::new().unwrap();
6583
6584        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6585
6586        s.handshake().unwrap();
6587
6588        let (stream, req) = s.send_request(true).unwrap();
6589
6590        let ev_headers = Event::Headers {
6591            list: req,
6592            more_frames: false,
6593        };
6594
6595        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6596        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6597
6598        let _ = s.send_response(stream, false).unwrap();
6599
6600        // Clear the writable stream queue.
6601        assert_eq!(s.pipe.server.stream_writable_next(), Some(3));
6602        assert_eq!(s.pipe.server.stream_writable_next(), Some(7));
6603        assert_eq!(s.pipe.server.stream_writable_next(), Some(11));
6604        assert_eq!(s.pipe.server.stream_writable_next(), Some(stream));
6605        assert_eq!(s.pipe.server.stream_writable_next(), None);
6606
6607        // The body must be larger than the cwnd would allow.
6608        let send_buf = [42; 80000];
6609
6610        let sent = s
6611            .server
6612            .send_body(&mut s.pipe.server, stream, &send_buf, true)
6613            .unwrap();
6614
6615        // send_body wrote as much as it could (sent < size of buff).
6616        assert_eq!(sent, 11995);
6617
6618        s.advance().ok();
6619
6620        // Client reads received headers and body.
6621        let mut recv_buf = [42; 80000];
6622        assert!(s.poll_client().is_ok());
6623        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6624        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(11995));
6625
6626        s.advance().ok();
6627
6628        // Server send cap is smaller than remaining body buffer.
6629        assert!(s.pipe.server.tx_cap < send_buf.len() - sent);
6630
6631        // Once the server cwnd opens up, we can send more body.
6632        assert_eq!(s.pipe.server.stream_writable_next(), Some(0));
6633    }
6634
6635    #[test]
6636    /// Ensure stream doesn't hang due to small cwnd.
6637    fn send_body_stream_blocked_zero_length() {
6638        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6639        config
6640            .load_cert_chain_from_pem_file("examples/cert.crt")
6641            .unwrap();
6642        config
6643            .load_priv_key_from_pem_file("examples/cert.key")
6644            .unwrap();
6645        config.set_application_protos(&[b"h3"]).unwrap();
6646        // Use generous connection-level flow control.
6647        config.set_initial_max_data(100000);
6648        config.set_initial_max_stream_data_bidi_local(100000);
6649        config.set_initial_max_stream_data_bidi_remote(50000);
6650        config.set_initial_max_stream_data_uni(150);
6651        config.set_initial_max_streams_bidi(100);
6652        config.set_initial_max_streams_uni(5);
6653        config.verify_peer(false);
6654
6655        let h3_config = Config::new().unwrap();
6656
6657        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6658
6659        s.handshake().unwrap();
6660
6661        let (stream, req) = s.send_request(true).unwrap();
6662
6663        let ev_headers = Event::Headers {
6664            list: req,
6665            more_frames: false,
6666        };
6667
6668        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6669        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6670
6671        let _ = s.send_response(stream, false).unwrap();
6672
6673        // Clear the writable stream queue.
6674        assert_eq!(s.pipe.server.stream_writable_next(), Some(3));
6675        assert_eq!(s.pipe.server.stream_writable_next(), Some(7));
6676        assert_eq!(s.pipe.server.stream_writable_next(), Some(11));
6677        assert_eq!(s.pipe.server.stream_writable_next(), Some(stream));
6678        assert_eq!(s.pipe.server.stream_writable_next(), None);
6679
6680        // The body is large enough to fill the cwnd, except for enough bytes
6681        // for another DATA frame header (but no payload).
6682        let send_buf = [42; 11994];
6683
6684        let sent = s
6685            .server
6686            .send_body(&mut s.pipe.server, stream, &send_buf, false)
6687            .unwrap();
6688
6689        assert_eq!(sent, 11994);
6690
6691        // There is only enough capacity left for the DATA frame header, but
6692        // no payload.
6693        assert_eq!(s.pipe.server.stream_capacity(stream).unwrap(), 3);
6694        assert_eq!(
6695            s.server
6696                .send_body(&mut s.pipe.server, stream, &send_buf, false),
6697            Err(Error::Done)
6698        );
6699
6700        s.advance().ok();
6701
6702        // Client reads received headers and body.
6703        let mut recv_buf = [42; 80000];
6704        assert!(s.poll_client().is_ok());
6705        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6706        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(11994));
6707
6708        s.advance().ok();
6709
6710        // Once the server cwnd opens up, we can send more body.
6711        assert_eq!(s.pipe.server.stream_writable_next(), Some(0));
6712    }
6713
6714    #[test]
6715    /// Test handling of 0-length DATA writes with and without fin.
6716    fn zero_length_data() {
6717        let mut s = Session::new().unwrap();
6718        s.handshake().unwrap();
6719
6720        let (stream, req) = s.send_request(false).unwrap();
6721
6722        assert_eq!(
6723            s.client.send_body(&mut s.pipe.client, 0, b"", false),
6724            Err(Error::Done)
6725        );
6726        assert_eq!(s.client.send_body(&mut s.pipe.client, 0, b"", true), Ok(0));
6727
6728        s.advance().ok();
6729
6730        let mut recv_buf = vec![0; 100];
6731
6732        let ev_headers = Event::Headers {
6733            list: req,
6734            more_frames: true,
6735        };
6736
6737        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6738
6739        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
6740        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Err(Error::Done));
6741
6742        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6743        assert_eq!(s.poll_server(), Err(Error::Done));
6744
6745        let resp = s.send_response(stream, false).unwrap();
6746
6747        assert_eq!(
6748            s.server.send_body(&mut s.pipe.server, 0, b"", false),
6749            Err(Error::Done)
6750        );
6751        assert_eq!(s.server.send_body(&mut s.pipe.server, 0, b"", true), Ok(0));
6752
6753        s.advance().ok();
6754
6755        let ev_headers = Event::Headers {
6756            list: resp,
6757            more_frames: true,
6758        };
6759
6760        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
6761
6762        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6763        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Err(Error::Done));
6764
6765        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
6766        assert_eq!(s.poll_client(), Err(Error::Done));
6767    }
6768
6769    #[test]
6770    /// Tests that blocked 0-length DATA writes are reported correctly.
6771    fn zero_length_data_blocked() {
6772        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6773        config
6774            .load_cert_chain_from_pem_file("examples/cert.crt")
6775            .unwrap();
6776        config
6777            .load_priv_key_from_pem_file("examples/cert.key")
6778            .unwrap();
6779        config.set_application_protos(&[b"h3"]).unwrap();
6780        config.set_initial_max_data(74);
6781        config.set_initial_max_stream_data_bidi_local(150);
6782        config.set_initial_max_stream_data_bidi_remote(150);
6783        config.set_initial_max_stream_data_uni(150);
6784        config.set_initial_max_streams_bidi(100);
6785        config.set_initial_max_streams_uni(5);
6786        config.verify_peer(false);
6787
6788        let h3_config = Config::new().unwrap();
6789
6790        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6791
6792        s.handshake().unwrap();
6793
6794        let req = vec![
6795            Header::new(b":method", b"GET"),
6796            Header::new(b":scheme", b"https"),
6797            Header::new(b":authority", b"quic.tech"),
6798            Header::new(b":path", b"/test"),
6799        ];
6800
6801        assert_eq!(
6802            s.client.send_request(&mut s.pipe.client, &req, false),
6803            Ok(0)
6804        );
6805
6806        assert_eq!(
6807            s.client.send_body(&mut s.pipe.client, 0, b"", true),
6808            Err(Error::Done)
6809        );
6810
6811        // Clear the writable stream queue.
6812        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6813        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6814        assert_eq!(s.pipe.client.stream_writable_next(), Some(10));
6815        assert_eq!(s.pipe.client.stream_writable_next(), None);
6816
6817        s.advance().ok();
6818
6819        // Flow-control credit from the server allows the body to be sent.
6820        assert_eq!(s.pipe.client.stream_writable_next(), Some(0));
6821        assert_eq!(s.client.send_body(&mut s.pipe.client, 0, b"", true), Ok(0));
6822    }
6823
6824    #[test]
6825    /// Tests that receiving an empty SETTINGS frame is handled and reported.
6826    fn empty_settings() {
6827        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6828        config
6829            .load_cert_chain_from_pem_file("examples/cert.crt")
6830            .unwrap();
6831        config
6832            .load_priv_key_from_pem_file("examples/cert.key")
6833            .unwrap();
6834        config.set_application_protos(&[b"h3"]).unwrap();
6835        config.set_initial_max_data(1500);
6836        config.set_initial_max_stream_data_bidi_local(150);
6837        config.set_initial_max_stream_data_bidi_remote(150);
6838        config.set_initial_max_stream_data_uni(150);
6839        config.set_initial_max_streams_bidi(5);
6840        config.set_initial_max_streams_uni(5);
6841        config.verify_peer(false);
6842        config.set_ack_delay_exponent(8);
6843        config.grease(false);
6844
6845        let h3_config = Config::new().unwrap();
6846        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6847
6848        s.handshake().unwrap();
6849
6850        assert!(s.client.peer_settings_raw().is_some());
6851        assert!(s.server.peer_settings_raw().is_some());
6852    }
6853
6854    #[test]
6855    /// Tests that receiving a H3_DATAGRAM setting is ok.
6856    fn dgram_setting() {
6857        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6858        config
6859            .load_cert_chain_from_pem_file("examples/cert.crt")
6860            .unwrap();
6861        config
6862            .load_priv_key_from_pem_file("examples/cert.key")
6863            .unwrap();
6864        config.set_application_protos(&[b"h3"]).unwrap();
6865        config.set_initial_max_data(70);
6866        config.set_initial_max_stream_data_bidi_local(150);
6867        config.set_initial_max_stream_data_bidi_remote(150);
6868        config.set_initial_max_stream_data_uni(150);
6869        config.set_initial_max_streams_bidi(100);
6870        config.set_initial_max_streams_uni(5);
6871        config.enable_dgram(true, 1000, 1000);
6872        config.verify_peer(false);
6873
6874        let h3_config = Config::new().unwrap();
6875
6876        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6877        assert_eq!(s.pipe.handshake(), Ok(()));
6878
6879        s.client.send_settings(&mut s.pipe.client).unwrap();
6880        assert_eq!(s.pipe.advance(), Ok(()));
6881
6882        // Before processing SETTINGS (via poll), HTTP/3 DATAGRAMS are not
6883        // enabled.
6884        assert!(!s.server.dgram_enabled_by_peer(&s.pipe.server));
6885
6886        // When everything is ok, poll returns Done and DATAGRAM is enabled.
6887        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
6888        assert!(s.server.dgram_enabled_by_peer(&s.pipe.server));
6889
6890        // Now detect things on the client
6891        s.server.send_settings(&mut s.pipe.server).unwrap();
6892        assert_eq!(s.pipe.advance(), Ok(()));
6893        assert!(!s.client.dgram_enabled_by_peer(&s.pipe.client));
6894        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::Done));
6895        assert!(s.client.dgram_enabled_by_peer(&s.pipe.client));
6896    }
6897
6898    #[test]
6899    /// Tests that receiving a H3_DATAGRAM setting when no TP is set generates
6900    /// an error.
6901    fn dgram_setting_no_tp() {
6902        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6903        config
6904            .load_cert_chain_from_pem_file("examples/cert.crt")
6905            .unwrap();
6906        config
6907            .load_priv_key_from_pem_file("examples/cert.key")
6908            .unwrap();
6909        config.set_application_protos(&[b"h3"]).unwrap();
6910        config.set_initial_max_data(70);
6911        config.set_initial_max_stream_data_bidi_local(150);
6912        config.set_initial_max_stream_data_bidi_remote(150);
6913        config.set_initial_max_stream_data_uni(150);
6914        config.set_initial_max_streams_bidi(100);
6915        config.set_initial_max_streams_uni(5);
6916        config.verify_peer(false);
6917
6918        let h3_config = Config::new().unwrap();
6919
6920        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6921        assert_eq!(s.pipe.handshake(), Ok(()));
6922
6923        s.client.control_stream_id = Some(
6924            s.client
6925                .open_uni_stream(
6926                    &mut s.pipe.client,
6927                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6928                )
6929                .unwrap(),
6930        );
6931
6932        let settings = frame::Frame::Settings {
6933            max_field_section_size: None,
6934            qpack_max_table_capacity: None,
6935            qpack_blocked_streams: None,
6936            connect_protocol_enabled: None,
6937            h3_datagram: Some(1),
6938            grease: None,
6939            additional_settings: Default::default(),
6940            raw: Default::default(),
6941        };
6942
6943        s.send_frame_client(settings, s.client.control_stream_id.unwrap(), false)
6944            .unwrap();
6945
6946        assert_eq!(s.pipe.advance(), Ok(()));
6947
6948        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::SettingsError));
6949    }
6950
6951    #[test]
6952    /// Tests that receiving SETTINGS with prohibited values generates an error.
6953    fn settings_h2_prohibited() {
6954        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6955        config
6956            .load_cert_chain_from_pem_file("examples/cert.crt")
6957            .unwrap();
6958        config
6959            .load_priv_key_from_pem_file("examples/cert.key")
6960            .unwrap();
6961        config.set_application_protos(&[b"h3"]).unwrap();
6962        config.set_initial_max_data(70);
6963        config.set_initial_max_stream_data_bidi_local(150);
6964        config.set_initial_max_stream_data_bidi_remote(150);
6965        config.set_initial_max_stream_data_uni(150);
6966        config.set_initial_max_streams_bidi(100);
6967        config.set_initial_max_streams_uni(5);
6968        config.verify_peer(false);
6969
6970        let h3_config = Config::new().unwrap();
6971
6972        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6973        assert_eq!(s.pipe.handshake(), Ok(()));
6974
6975        s.client.control_stream_id = Some(
6976            s.client
6977                .open_uni_stream(
6978                    &mut s.pipe.client,
6979                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6980                )
6981                .unwrap(),
6982        );
6983
6984        s.server.control_stream_id = Some(
6985            s.server
6986                .open_uni_stream(
6987                    &mut s.pipe.server,
6988                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6989                )
6990                .unwrap(),
6991        );
6992
6993        let frame_payload_len = 2u64;
6994        let settings = [
6995            frame::SETTINGS_FRAME_TYPE_ID as u8,
6996            frame_payload_len as u8,
6997            0x2, // 0x2 is a reserved setting type
6998            1,
6999        ];
7000
7001        s.send_arbitrary_stream_data_client(
7002            &settings,
7003            s.client.control_stream_id.unwrap(),
7004            false,
7005        )
7006        .unwrap();
7007
7008        s.send_arbitrary_stream_data_server(
7009            &settings,
7010            s.server.control_stream_id.unwrap(),
7011            false,
7012        )
7013        .unwrap();
7014
7015        assert_eq!(s.pipe.advance(), Ok(()));
7016
7017        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::SettingsError));
7018
7019        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::SettingsError));
7020    }
7021
7022    #[test]
7023    /// Tests that setting SETTINGS with prohibited values generates an error.
7024    fn set_prohibited_additional_settings() {
7025        let mut h3_config = Config::new().unwrap();
7026        assert_eq!(
7027            h3_config.set_additional_settings(vec![(
7028                frame::SETTINGS_QPACK_MAX_TABLE_CAPACITY,
7029                43
7030            )]),
7031            Err(Error::SettingsError)
7032        );
7033        assert_eq!(
7034            h3_config.set_additional_settings(vec![(
7035                frame::SETTINGS_MAX_FIELD_SECTION_SIZE,
7036                43
7037            )]),
7038            Err(Error::SettingsError)
7039        );
7040        assert_eq!(
7041            h3_config.set_additional_settings(vec![(
7042                frame::SETTINGS_QPACK_BLOCKED_STREAMS,
7043                43
7044            )]),
7045            Err(Error::SettingsError)
7046        );
7047        assert_eq!(
7048            h3_config.set_additional_settings(vec![(
7049                frame::SETTINGS_ENABLE_CONNECT_PROTOCOL,
7050                43
7051            )]),
7052            Err(Error::SettingsError)
7053        );
7054        assert_eq!(
7055            h3_config
7056                .set_additional_settings(vec![(frame::SETTINGS_H3_DATAGRAM, 43)]),
7057            Err(Error::SettingsError)
7058        );
7059    }
7060
7061    #[test]
7062    /// Tests that a client rejects a SETTINGS frame received on a request
7063    /// stream.
7064    fn settings_on_request_stream_client() {
7065        let mut s = Session::new().unwrap();
7066        s.handshake().unwrap();
7067
7068        let (stream, _req) = s.send_request(true).unwrap();
7069
7070        let settings = frame::Frame::Settings {
7071            max_field_section_size: None,
7072            qpack_max_table_capacity: None,
7073            qpack_blocked_streams: None,
7074            connect_protocol_enabled: None,
7075            h3_datagram: None,
7076            grease: None,
7077            additional_settings: Default::default(),
7078            raw: Default::default(),
7079        };
7080
7081        s.send_frame_server(settings, stream, false).unwrap();
7082
7083        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7084        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7085        assert_eq!(
7086            s.pipe.client.local_error(),
7087            Some(&crate::ConnectionError {
7088                is_app: true,
7089                error_code: WireErrorCode::FrameUnexpected as u64,
7090                reason: format!(
7091                    "Unexpected frame type {}",
7092                    frame::SETTINGS_FRAME_TYPE_ID
7093                )
7094                .into_bytes(),
7095            })
7096        );
7097    }
7098
7099    #[test]
7100    /// Tests that a client rejects a CANCEL_PUSH frame received on a request
7101    /// stream.
7102    fn cancel_push_on_request_stream_client() {
7103        let mut s = Session::new().unwrap();
7104        s.handshake().unwrap();
7105
7106        let (stream, _req) = s.send_request(true).unwrap();
7107        let cancel_push = frame::Frame::CancelPush { push_id: 0 };
7108        s.send_frame_server(cancel_push, stream, false).unwrap();
7109
7110        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7111        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7112        assert_eq!(
7113            s.pipe.client.local_error(),
7114            Some(&crate::ConnectionError {
7115                is_app: true,
7116                error_code: WireErrorCode::FrameUnexpected as u64,
7117                reason: format!(
7118                    "Unexpected frame type {}",
7119                    frame::CANCEL_PUSH_FRAME_TYPE_ID
7120                )
7121                .into_bytes(),
7122            })
7123        );
7124    }
7125
7126    #[test]
7127    /// Tests that a client rejects a GOAWAY frame received on a request
7128    /// stream.
7129    fn goaway_on_request_stream_client() {
7130        let mut s = Session::new().unwrap();
7131        s.handshake().unwrap();
7132
7133        let (stream, _req) = s.send_request(true).unwrap();
7134        let goaway = frame::Frame::GoAway { id: 0 };
7135
7136        s.send_frame_server(goaway, stream, false).unwrap();
7137
7138        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7139        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7140        assert_eq!(
7141            s.pipe.client.local_error(),
7142            Some(&crate::ConnectionError {
7143                is_app: true,
7144                error_code: WireErrorCode::FrameUnexpected as u64,
7145                reason: format!(
7146                    "Unexpected frame type {}",
7147                    frame::GOAWAY_FRAME_TYPE_ID
7148                )
7149                .into_bytes(),
7150            })
7151        );
7152    }
7153
7154    #[test]
7155    /// Tests that a client rejects a MAX_PUSH_ID frame received on a request
7156    /// stream.
7157    fn max_push_id_on_request_stream_client() {
7158        let mut s = Session::new().unwrap();
7159        s.handshake().unwrap();
7160
7161        let (stream, _req) = s.send_request(true).unwrap();
7162        let max_push_id = frame::Frame::MaxPushId { push_id: 0 };
7163
7164        s.send_frame_server(max_push_id, stream, false).unwrap();
7165
7166        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7167        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7168        assert_eq!(
7169            s.pipe.client.local_error(),
7170            Some(&crate::ConnectionError {
7171                is_app: true,
7172                error_code: WireErrorCode::FrameUnexpected as u64,
7173                reason: format!(
7174                    "Unexpected frame type {}",
7175                    frame::MAX_PUSH_FRAME_TYPE_ID
7176                )
7177                .into_bytes(),
7178            })
7179        );
7180    }
7181
7182    #[test]
7183    /// Tests additional settings are actually exchanged by the peers.
7184    fn set_additional_settings() {
7185        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7186        config
7187            .load_cert_chain_from_pem_file("examples/cert.crt")
7188            .unwrap();
7189        config
7190            .load_priv_key_from_pem_file("examples/cert.key")
7191            .unwrap();
7192        config.set_application_protos(&[b"h3"]).unwrap();
7193        config.set_initial_max_data(70);
7194        config.set_initial_max_stream_data_bidi_local(150);
7195        config.set_initial_max_stream_data_bidi_remote(150);
7196        config.set_initial_max_stream_data_uni(150);
7197        config.set_initial_max_streams_bidi(100);
7198        config.set_initial_max_streams_uni(5);
7199        config.verify_peer(false);
7200        config.grease(false);
7201
7202        let mut h3_config = Config::new().unwrap();
7203        h3_config
7204            .set_additional_settings(vec![(42, 43), (44, 45)])
7205            .unwrap();
7206
7207        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7208        assert_eq!(s.pipe.handshake(), Ok(()));
7209
7210        assert_eq!(s.pipe.advance(), Ok(()));
7211
7212        s.client.send_settings(&mut s.pipe.client).unwrap();
7213        assert_eq!(s.pipe.advance(), Ok(()));
7214        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
7215
7216        s.server.send_settings(&mut s.pipe.server).unwrap();
7217        assert_eq!(s.pipe.advance(), Ok(()));
7218        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::Done));
7219
7220        assert_eq!(
7221            s.server.peer_settings_raw(),
7222            Some(&[(6, 32_768), (42, 43), (44, 45)][..])
7223        );
7224        assert_eq!(
7225            s.client.peer_settings_raw(),
7226            Some(&[(6, 32_768), (42, 43), (44, 45)][..])
7227        );
7228    }
7229
7230    #[test]
7231    /// Send a single DATAGRAM.
7232    fn single_dgram() {
7233        let mut buf = [0; 65535];
7234        let mut s = Session::new().unwrap();
7235        s.handshake().unwrap();
7236
7237        // We'll send default data of 10 bytes on flow ID 0.
7238        let result = (11, 0, 1);
7239
7240        s.send_dgram_client(0).unwrap();
7241
7242        assert_eq!(s.poll_server(), Err(Error::Done));
7243        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7244
7245        s.send_dgram_server(0).unwrap();
7246        assert_eq!(s.poll_client(), Err(Error::Done));
7247        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7248    }
7249
7250    #[test]
7251    /// Send multiple DATAGRAMs.
7252    fn multiple_dgram() {
7253        let mut buf = [0; 65535];
7254        let mut s = Session::new().unwrap();
7255        s.handshake().unwrap();
7256
7257        // We'll send default data of 10 bytes on flow ID 0.
7258        let result = (11, 0, 1);
7259
7260        s.send_dgram_client(0).unwrap();
7261        s.send_dgram_client(0).unwrap();
7262        s.send_dgram_client(0).unwrap();
7263
7264        assert_eq!(s.poll_server(), Err(Error::Done));
7265        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7266        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7267        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7268        assert_eq!(s.recv_dgram_server(&mut buf), Err(Error::Done));
7269
7270        s.send_dgram_server(0).unwrap();
7271        s.send_dgram_server(0).unwrap();
7272        s.send_dgram_server(0).unwrap();
7273
7274        assert_eq!(s.poll_client(), Err(Error::Done));
7275        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7276        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7277        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7278        assert_eq!(s.recv_dgram_client(&mut buf), Err(Error::Done));
7279    }
7280
7281    #[test]
7282    /// Send more DATAGRAMs than the send queue allows.
7283    fn multiple_dgram_overflow() {
7284        let mut buf = [0; 65535];
7285        let mut s = Session::new().unwrap();
7286        s.handshake().unwrap();
7287
7288        // We'll send default data of 10 bytes on flow ID 0.
7289        let result = (11, 0, 1);
7290
7291        // Five DATAGRAMs
7292        s.send_dgram_client(0).unwrap();
7293        s.send_dgram_client(0).unwrap();
7294        s.send_dgram_client(0).unwrap();
7295        s.send_dgram_client(0).unwrap();
7296        s.send_dgram_client(0).unwrap();
7297
7298        // Only 3 independent DATAGRAMs to read events will fire.
7299        assert_eq!(s.poll_server(), Err(Error::Done));
7300        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7301        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7302        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7303        assert_eq!(s.recv_dgram_server(&mut buf), Err(Error::Done));
7304    }
7305
7306    #[test]
7307    /// Send a single DATAGRAM and request.
7308    fn poll_datagram_cycling_no_read() {
7309        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7310        config
7311            .load_cert_chain_from_pem_file("examples/cert.crt")
7312            .unwrap();
7313        config
7314            .load_priv_key_from_pem_file("examples/cert.key")
7315            .unwrap();
7316        config.set_application_protos(&[b"h3"]).unwrap();
7317        config.set_initial_max_data(1500);
7318        config.set_initial_max_stream_data_bidi_local(150);
7319        config.set_initial_max_stream_data_bidi_remote(150);
7320        config.set_initial_max_stream_data_uni(150);
7321        config.set_initial_max_streams_bidi(100);
7322        config.set_initial_max_streams_uni(5);
7323        config.verify_peer(false);
7324        config.enable_dgram(true, 100, 100);
7325
7326        let h3_config = Config::new().unwrap();
7327        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7328        s.handshake().unwrap();
7329
7330        // Send request followed by DATAGRAM on client side.
7331        let (stream, req) = s.send_request(false).unwrap();
7332
7333        s.send_body_client(stream, true).unwrap();
7334
7335        let ev_headers = Event::Headers {
7336            list: req,
7337            more_frames: true,
7338        };
7339
7340        s.send_dgram_client(0).unwrap();
7341
7342        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7343        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7344
7345        assert_eq!(s.poll_server(), Err(Error::Done));
7346    }
7347
7348    #[test]
7349    /// Send a single DATAGRAM and request.
7350    fn poll_datagram_single_read() {
7351        let mut buf = [0; 65535];
7352
7353        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7354        config
7355            .load_cert_chain_from_pem_file("examples/cert.crt")
7356            .unwrap();
7357        config
7358            .load_priv_key_from_pem_file("examples/cert.key")
7359            .unwrap();
7360        config.set_application_protos(&[b"h3"]).unwrap();
7361        config.set_initial_max_data(1500);
7362        config.set_initial_max_stream_data_bidi_local(150);
7363        config.set_initial_max_stream_data_bidi_remote(150);
7364        config.set_initial_max_stream_data_uni(150);
7365        config.set_initial_max_streams_bidi(100);
7366        config.set_initial_max_streams_uni(5);
7367        config.verify_peer(false);
7368        config.enable_dgram(true, 100, 100);
7369
7370        let h3_config = Config::new().unwrap();
7371        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7372        s.handshake().unwrap();
7373
7374        // We'll send default data of 10 bytes on flow ID 0.
7375        let result = (11, 0, 1);
7376
7377        // Send request followed by DATAGRAM on client side.
7378        let (stream, req) = s.send_request(false).unwrap();
7379
7380        let body = s.send_body_client(stream, true).unwrap();
7381
7382        let mut recv_buf = vec![0; body.len()];
7383
7384        let ev_headers = Event::Headers {
7385            list: req,
7386            more_frames: true,
7387        };
7388
7389        s.send_dgram_client(0).unwrap();
7390
7391        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7392        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7393
7394        assert_eq!(s.poll_server(), Err(Error::Done));
7395
7396        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7397
7398        assert_eq!(s.poll_server(), Err(Error::Done));
7399
7400        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7401        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7402        assert_eq!(s.poll_server(), Err(Error::Done));
7403
7404        // Send response followed by DATAGRAM on server side
7405        let resp = s.send_response(stream, false).unwrap();
7406
7407        let body = s.send_body_server(stream, true).unwrap();
7408
7409        let mut recv_buf = vec![0; body.len()];
7410
7411        let ev_headers = Event::Headers {
7412            list: resp,
7413            more_frames: true,
7414        };
7415
7416        s.send_dgram_server(0).unwrap();
7417
7418        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7419        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
7420
7421        assert_eq!(s.poll_client(), Err(Error::Done));
7422
7423        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7424
7425        assert_eq!(s.poll_client(), Err(Error::Done));
7426
7427        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
7428
7429        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7430        assert_eq!(s.poll_client(), Err(Error::Done));
7431    }
7432
7433    #[test]
7434    /// Send multiple DATAGRAMs and requests.
7435    fn poll_datagram_multi_read() {
7436        let mut buf = [0; 65535];
7437
7438        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7439        config
7440            .load_cert_chain_from_pem_file("examples/cert.crt")
7441            .unwrap();
7442        config
7443            .load_priv_key_from_pem_file("examples/cert.key")
7444            .unwrap();
7445        config.set_application_protos(&[b"h3"]).unwrap();
7446        config.set_initial_max_data(1500);
7447        config.set_initial_max_stream_data_bidi_local(150);
7448        config.set_initial_max_stream_data_bidi_remote(150);
7449        config.set_initial_max_stream_data_uni(150);
7450        config.set_initial_max_streams_bidi(100);
7451        config.set_initial_max_streams_uni(5);
7452        config.verify_peer(false);
7453        config.enable_dgram(true, 100, 100);
7454
7455        let h3_config = Config::new().unwrap();
7456        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7457        s.handshake().unwrap();
7458
7459        // 10 bytes on flow ID 0 and 2.
7460        let flow_0_result = (11, 0, 1);
7461        let flow_2_result = (11, 2, 1);
7462
7463        // Send requests followed by DATAGRAMs on client side.
7464        let (stream, req) = s.send_request(false).unwrap();
7465
7466        let body = s.send_body_client(stream, true).unwrap();
7467
7468        let mut recv_buf = vec![0; body.len()];
7469
7470        let ev_headers = Event::Headers {
7471            list: req,
7472            more_frames: true,
7473        };
7474
7475        s.send_dgram_client(0).unwrap();
7476        s.send_dgram_client(0).unwrap();
7477        s.send_dgram_client(0).unwrap();
7478        s.send_dgram_client(0).unwrap();
7479        s.send_dgram_client(0).unwrap();
7480        s.send_dgram_client(2).unwrap();
7481        s.send_dgram_client(2).unwrap();
7482        s.send_dgram_client(2).unwrap();
7483        s.send_dgram_client(2).unwrap();
7484        s.send_dgram_client(2).unwrap();
7485
7486        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7487        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7488
7489        assert_eq!(s.poll_server(), Err(Error::Done));
7490
7491        // Second cycle, start to read
7492        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7493        assert_eq!(s.poll_server(), Err(Error::Done));
7494        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7495        assert_eq!(s.poll_server(), Err(Error::Done));
7496        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7497        assert_eq!(s.poll_server(), Err(Error::Done));
7498
7499        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7500        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7501
7502        assert_eq!(s.poll_server(), Err(Error::Done));
7503
7504        // Third cycle.
7505        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7506        assert_eq!(s.poll_server(), Err(Error::Done));
7507        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7508        assert_eq!(s.poll_server(), Err(Error::Done));
7509        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7510        assert_eq!(s.poll_server(), Err(Error::Done));
7511        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7512        assert_eq!(s.poll_server(), Err(Error::Done));
7513        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7514        assert_eq!(s.poll_server(), Err(Error::Done));
7515        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7516        assert_eq!(s.poll_server(), Err(Error::Done));
7517        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7518        assert_eq!(s.poll_server(), Err(Error::Done));
7519
7520        // Send response followed by DATAGRAM on server side
7521        let resp = s.send_response(stream, false).unwrap();
7522
7523        let body = s.send_body_server(stream, true).unwrap();
7524
7525        let mut recv_buf = vec![0; body.len()];
7526
7527        let ev_headers = Event::Headers {
7528            list: resp,
7529            more_frames: true,
7530        };
7531
7532        s.send_dgram_server(0).unwrap();
7533        s.send_dgram_server(0).unwrap();
7534        s.send_dgram_server(0).unwrap();
7535        s.send_dgram_server(0).unwrap();
7536        s.send_dgram_server(0).unwrap();
7537        s.send_dgram_server(2).unwrap();
7538        s.send_dgram_server(2).unwrap();
7539        s.send_dgram_server(2).unwrap();
7540        s.send_dgram_server(2).unwrap();
7541        s.send_dgram_server(2).unwrap();
7542
7543        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7544        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
7545
7546        assert_eq!(s.poll_client(), Err(Error::Done));
7547
7548        // Second cycle, start to read
7549        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7550        assert_eq!(s.poll_client(), Err(Error::Done));
7551        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7552        assert_eq!(s.poll_client(), Err(Error::Done));
7553        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7554        assert_eq!(s.poll_client(), Err(Error::Done));
7555
7556        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
7557        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7558
7559        assert_eq!(s.poll_client(), Err(Error::Done));
7560
7561        // Third cycle.
7562        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7563        assert_eq!(s.poll_client(), Err(Error::Done));
7564        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7565        assert_eq!(s.poll_client(), Err(Error::Done));
7566        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7567        assert_eq!(s.poll_client(), Err(Error::Done));
7568        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7569        assert_eq!(s.poll_client(), Err(Error::Done));
7570        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7571        assert_eq!(s.poll_client(), Err(Error::Done));
7572        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7573        assert_eq!(s.poll_client(), Err(Error::Done));
7574        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7575        assert_eq!(s.poll_client(), Err(Error::Done));
7576    }
7577
7578    #[test]
7579    /// Tests that the Finished event is not issued for streams of unknown type
7580    /// (e.g. GREASE).
7581    fn finished_is_for_requests() {
7582        let mut s = Session::new().unwrap();
7583        s.handshake().unwrap();
7584
7585        assert_eq!(s.poll_client(), Err(Error::Done));
7586        assert_eq!(s.poll_server(), Err(Error::Done));
7587
7588        assert_eq!(s.client.open_grease_stream(&mut s.pipe.client), Ok(()));
7589        assert_eq!(s.pipe.advance(), Ok(()));
7590
7591        assert_eq!(s.poll_client(), Err(Error::Done));
7592        assert_eq!(s.poll_server(), Err(Error::Done));
7593    }
7594
7595    #[test]
7596    fn unknown_uni_stream_leaks_past_max_streams_uni() {
7597        let (mut config, h3_config) = Session::default_configs().unwrap();
7598        config.set_initial_max_data(100_000);
7599        config.set_initial_max_stream_data_uni(100_000);
7600        config.set_initial_max_streams_uni(5);
7601        config.grease(false);
7602
7603        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7604        s.handshake().unwrap();
7605
7606        let baseline = s.server.streams.len();
7607        let mut leaked_streams = Vec::new();
7608        let mut id = 14;
7609
7610        let n = 500;
7611        for i in 0..n {
7612            s.pipe
7613                .client
7614                .stream_send(id, &[0x21], true)
7615                .unwrap_or_else(|e| {
7616                    panic!(
7617                        "stream credit was not re-issued after {} streams: {:?}",
7618                        i, e
7619                    )
7620                });
7621
7622            s.pipe.advance().unwrap();
7623            assert_eq!(s.poll_server(), Err(Error::Done));
7624            assert!(s.pipe.server.streams.is_collected(id));
7625            s.pipe.advance().unwrap();
7626
7627            leaked_streams.push(id);
7628            id += 4;
7629        }
7630
7631        for id in leaked_streams {
7632            assert!(s.pipe.server.streams.is_collected(id));
7633        }
7634
7635        assert_eq!(
7636            s.server.streams.len(),
7637            baseline,
7638            "expected {} allocated streams in stream map after {} streams with unknown type",
7639            baseline,
7640            n,
7641        );
7642    }
7643
7644    #[test]
7645    fn empty_uni_stream_leaks_past_max_streams_uni() {
7646        let (mut config, h3_config) = Session::default_configs().unwrap();
7647        config.set_initial_max_data(100_000);
7648        config.set_initial_max_stream_data_uni(100_000);
7649        config.set_initial_max_streams_uni(5);
7650        config.grease(false);
7651
7652        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7653        s.handshake().unwrap();
7654
7655        let baseline = s.server.streams.len();
7656        let mut leaked_streams = Vec::new();
7657        let mut id = 14;
7658
7659        let n = 500;
7660        for i in 0..n {
7661            s.pipe
7662                .client
7663                .stream_send(id, &[], true)
7664                .unwrap_or_else(|e| {
7665                    panic!(
7666                        "stream credit was not re-issued after {} streams: {:?}",
7667                        i, e
7668                    )
7669                });
7670
7671            s.pipe.advance().unwrap();
7672            assert_eq!(s.poll_server(), Err(Error::Done));
7673            assert!(s.pipe.server.streams.is_collected(id));
7674            s.pipe.advance().unwrap();
7675
7676            leaked_streams.push(id);
7677            id += 4;
7678        }
7679
7680        for id in leaked_streams {
7681            assert!(s.pipe.server.streams.is_collected(id));
7682        }
7683
7684        assert_eq!(
7685            s.server.streams.len(),
7686            baseline,
7687            "expected {} allocated streams in stream map after {} streams without stream type",
7688            baseline,
7689            n,
7690        );
7691    }
7692
7693    #[test]
7694    /// Tests that streams are marked as finished only once.
7695    fn finished_once() {
7696        let mut s = Session::new().unwrap();
7697        s.handshake().unwrap();
7698
7699        let (stream, req) = s.send_request(false).unwrap();
7700        let body = s.send_body_client(stream, true).unwrap();
7701
7702        let mut recv_buf = vec![0; body.len()];
7703
7704        let ev_headers = Event::Headers {
7705            list: req,
7706            more_frames: true,
7707        };
7708
7709        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7710        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7711
7712        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7713        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7714
7715        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Err(Error::Done));
7716        assert_eq!(s.poll_server(), Err(Error::Done));
7717    }
7718
7719    #[test]
7720    /// Tests that the Data event is properly re-armed.
7721    fn data_event_rearm() {
7722        let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
7723
7724        let mut s = Session::new().unwrap();
7725        s.handshake().unwrap();
7726
7727        let (r1_id, r1_hdrs) = s.send_request(false).unwrap();
7728
7729        let mut recv_buf = vec![0; bytes.len()];
7730
7731        let r1_ev_headers = Event::Headers {
7732            list: r1_hdrs,
7733            more_frames: true,
7734        };
7735
7736        // Manually send an incomplete DATA frame (i.e. the frame size is longer
7737        // than the actual data sent).
7738        {
7739            let mut d = [42; 10];
7740            let mut b = octets::OctetsMut::with_slice(&mut d);
7741
7742            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7743            b.put_varint(bytes.len() as u64).unwrap();
7744            let off = b.off();
7745            s.pipe.client.stream_send(r1_id, &d[..off], false).unwrap();
7746
7747            assert_eq!(
7748                s.pipe.client.stream_send(r1_id, &bytes[..5], false),
7749                Ok(5)
7750            );
7751
7752            s.advance().ok();
7753        }
7754
7755        assert_eq!(s.poll_server(), Ok((r1_id, r1_ev_headers)));
7756        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7757        assert_eq!(s.poll_server(), Err(Error::Done));
7758
7759        // Read the available body data.
7760        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(5));
7761
7762        // Send the remaining DATA payload.
7763        assert_eq!(s.pipe.client.stream_send(r1_id, &bytes[5..], false), Ok(5));
7764        s.advance().ok();
7765
7766        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7767        assert_eq!(s.poll_server(), Err(Error::Done));
7768
7769        // Read the rest of the body data.
7770        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(5));
7771        assert_eq!(s.poll_server(), Err(Error::Done));
7772
7773        // Send more data.
7774        let r1_body = s.send_body_client(r1_id, false).unwrap();
7775
7776        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7777        assert_eq!(s.poll_server(), Err(Error::Done));
7778
7779        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(r1_body.len()));
7780
7781        // Send a new request to test cross-stream event rearming.
7782        let (r2_id, r2_hdrs) = s.send_request(false).unwrap();
7783        let r2_ev_headers = Event::Headers {
7784            list: r2_hdrs,
7785            more_frames: true,
7786        };
7787        let r2_body = s.send_body_client(r2_id, false).unwrap();
7788
7789        s.advance().ok();
7790
7791        assert_eq!(s.poll_server(), Ok((r2_id, r2_ev_headers)));
7792        assert_eq!(s.poll_server(), Ok((r2_id, Event::Data)));
7793        assert_eq!(s.recv_body_server(r2_id, &mut recv_buf), Ok(r2_body.len()));
7794        assert_eq!(s.poll_server(), Err(Error::Done));
7795
7796        // Send more data on request 1, then trailing HEADERS.
7797        let r1_body = s.send_body_client(r1_id, false).unwrap();
7798
7799        let trailers = vec![Header::new(b"hello", b"world")];
7800
7801        s.client
7802            .send_headers(&mut s.pipe.client, r1_id, &trailers, true)
7803            .unwrap();
7804
7805        let r1_ev_trailers = Event::Headers {
7806            list: trailers.clone(),
7807            more_frames: false,
7808        };
7809
7810        s.advance().ok();
7811
7812        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7813        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(r1_body.len()));
7814
7815        assert_eq!(s.poll_server(), Ok((r1_id, r1_ev_trailers)));
7816        assert_eq!(s.poll_server(), Ok((r1_id, Event::Finished)));
7817        assert_eq!(s.poll_server(), Err(Error::Done));
7818
7819        // Send more data on request 2, then trailing HEADERS.
7820        let r2_body = s.send_body_client(r2_id, false).unwrap();
7821
7822        s.client
7823            .send_headers(&mut s.pipe.client, r2_id, &trailers, false)
7824            .unwrap();
7825
7826        let r2_ev_trailers = Event::Headers {
7827            list: trailers,
7828            more_frames: true,
7829        };
7830
7831        s.advance().ok();
7832
7833        assert_eq!(s.poll_server(), Ok((r2_id, Event::Data)));
7834        assert_eq!(s.recv_body_server(r2_id, &mut recv_buf), Ok(r2_body.len()));
7835        assert_eq!(s.poll_server(), Ok((r2_id, r2_ev_trailers)));
7836        assert_eq!(s.poll_server(), Err(Error::Done));
7837
7838        let (r3_id, r3_hdrs) = s.send_request(false).unwrap();
7839
7840        let r3_ev_headers = Event::Headers {
7841            list: r3_hdrs,
7842            more_frames: true,
7843        };
7844
7845        // Manually send an incomplete DATA frame containing only its header.
7846        {
7847            let mut d = [42; 10];
7848            let mut b = octets::OctetsMut::with_slice(&mut d);
7849
7850            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7851            b.put_varint(bytes.len() as u64).unwrap();
7852            let off = b.off();
7853            s.pipe.client.stream_send(r3_id, &d[..off], false).unwrap();
7854
7855            s.advance().ok();
7856        }
7857
7858        assert_eq!(s.poll_server(), Ok((r3_id, r3_ev_headers)));
7859        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7860        assert_eq!(s.poll_server(), Err(Error::Done));
7861
7862        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Err(Error::Done));
7863
7864        assert_eq!(s.pipe.client.stream_send(r3_id, &bytes[..5], false), Ok(5));
7865
7866        s.advance().ok();
7867
7868        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7869        assert_eq!(s.poll_server(), Err(Error::Done));
7870
7871        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(5));
7872
7873        assert_eq!(s.pipe.client.stream_send(r3_id, &bytes[5..], false), Ok(5));
7874        s.advance().ok();
7875
7876        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7877        assert_eq!(s.poll_server(), Err(Error::Done));
7878
7879        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(5));
7880
7881        // Buffer multiple data frames.
7882        let body = s.send_body_client(r3_id, false).unwrap();
7883        s.send_body_client(r3_id, false).unwrap();
7884        s.send_body_client(r3_id, false).unwrap();
7885
7886        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7887        assert_eq!(s.poll_server(), Err(Error::Done));
7888
7889        {
7890            let mut d = [42; 10];
7891            let mut b = octets::OctetsMut::with_slice(&mut d);
7892
7893            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7894            b.put_varint(0).unwrap();
7895            let off = b.off();
7896            s.pipe.client.stream_send(r3_id, &d[..off], true).unwrap();
7897
7898            s.advance().ok();
7899        }
7900
7901        let mut recv_buf = vec![0; bytes.len() * 3];
7902
7903        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(body.len() * 3));
7904    }
7905
7906    #[test]
7907    /// Tests that the Datagram event is properly re-armed.
7908    fn dgram_event_rearm() {
7909        let mut buf = [0; 65535];
7910
7911        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7912        config
7913            .load_cert_chain_from_pem_file("examples/cert.crt")
7914            .unwrap();
7915        config
7916            .load_priv_key_from_pem_file("examples/cert.key")
7917            .unwrap();
7918        config.set_application_protos(&[b"h3"]).unwrap();
7919        config.set_initial_max_data(1500);
7920        config.set_initial_max_stream_data_bidi_local(150);
7921        config.set_initial_max_stream_data_bidi_remote(150);
7922        config.set_initial_max_stream_data_uni(150);
7923        config.set_initial_max_streams_bidi(100);
7924        config.set_initial_max_streams_uni(5);
7925        config.verify_peer(false);
7926        config.enable_dgram(true, 100, 100);
7927
7928        let h3_config = Config::new().unwrap();
7929        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7930        s.handshake().unwrap();
7931
7932        // 10 bytes on flow ID 0 and 2.
7933        let flow_0_result = (11, 0, 1);
7934        let flow_2_result = (11, 2, 1);
7935
7936        // Send requests followed by DATAGRAMs on client side.
7937        let (stream, req) = s.send_request(false).unwrap();
7938
7939        let body = s.send_body_client(stream, true).unwrap();
7940
7941        let mut recv_buf = vec![0; body.len()];
7942
7943        let ev_headers = Event::Headers {
7944            list: req,
7945            more_frames: true,
7946        };
7947
7948        s.send_dgram_client(0).unwrap();
7949        s.send_dgram_client(0).unwrap();
7950        s.send_dgram_client(2).unwrap();
7951        s.send_dgram_client(2).unwrap();
7952
7953        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7954        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7955
7956        assert_eq!(s.poll_server(), Err(Error::Done));
7957        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7958
7959        assert_eq!(s.poll_server(), Err(Error::Done));
7960        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7961
7962        assert_eq!(s.poll_server(), Err(Error::Done));
7963        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7964
7965        assert_eq!(s.poll_server(), Err(Error::Done));
7966        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7967
7968        assert_eq!(s.poll_server(), Err(Error::Done));
7969
7970        s.send_dgram_client(0).unwrap();
7971        s.send_dgram_client(2).unwrap();
7972
7973        assert_eq!(s.poll_server(), Err(Error::Done));
7974
7975        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7976        assert_eq!(s.poll_server(), Err(Error::Done));
7977
7978        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7979        assert_eq!(s.poll_server(), Err(Error::Done));
7980
7981        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7982        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7983
7984        // Verify that dgram counts are incremented.
7985        assert_eq!(s.pipe.client.dgram_sent_count, 6);
7986        assert_eq!(s.pipe.client.dgram_recv_count, 0);
7987        assert_eq!(s.pipe.server.dgram_sent_count, 0);
7988        assert_eq!(s.pipe.server.dgram_recv_count, 6);
7989
7990        let server_path = s.pipe.server.paths.get_active().expect("no active");
7991        let client_path = s.pipe.client.paths.get_active().expect("no active");
7992        assert_eq!(client_path.dgram_sent_count, 6);
7993        assert_eq!(client_path.dgram_recv_count, 0);
7994        assert_eq!(server_path.dgram_sent_count, 0);
7995        assert_eq!(server_path.dgram_recv_count, 6);
7996    }
7997
7998    #[test]
7999    fn reset_stream() {
8000        let mut buf = [0; 65535];
8001
8002        let mut s = Session::new().unwrap();
8003        s.handshake().unwrap();
8004
8005        // Client sends request.
8006        let (stream, req) = s.send_request(false).unwrap();
8007
8008        let ev_headers = Event::Headers {
8009            list: req,
8010            more_frames: true,
8011        };
8012
8013        // Server sends response and closes stream.
8014        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8015        assert_eq!(s.poll_server(), Err(Error::Done));
8016
8017        let resp = s.send_response(stream, true).unwrap();
8018
8019        let ev_headers = Event::Headers {
8020            list: resp,
8021            more_frames: false,
8022        };
8023
8024        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8025        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8026        assert_eq!(s.poll_client(), Err(Error::Done));
8027
8028        // Client sends RESET_STREAM, closing stream.
8029        let frames = [crate::frame::Frame::ResetStream {
8030            stream_id: stream,
8031            error_code: 42,
8032            final_size: 68,
8033        }];
8034
8035        let pkt_type = crate::packet::Type::Short;
8036        assert_eq!(
8037            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
8038            Ok(39)
8039        );
8040
8041        // Server issues Reset event for the stream.
8042        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
8043        assert_eq!(s.poll_server(), Err(Error::Done));
8044
8045        // Sending RESET_STREAM again shouldn't trigger another Reset event.
8046        assert_eq!(
8047            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
8048            Ok(39)
8049        );
8050
8051        assert_eq!(s.poll_server(), Err(Error::Done));
8052    }
8053
8054    /// The client shuts down the stream's write direction, the server
8055    /// shuts down its side with fin
8056    #[test]
8057    fn client_shutdown_write_server_fin() {
8058        let mut buf = [0; 65535];
8059        let mut s = Session::new().unwrap();
8060        s.handshake().unwrap();
8061
8062        // Client sends request.
8063        let (stream, req) = s.send_request(false).unwrap();
8064
8065        let ev_headers = Event::Headers {
8066            list: req,
8067            more_frames: true,
8068        };
8069
8070        // Server sends response and closes stream.
8071        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8072        assert_eq!(s.poll_server(), Err(Error::Done));
8073
8074        let resp = s.send_response(stream, true).unwrap();
8075
8076        let ev_headers = Event::Headers {
8077            list: resp,
8078            more_frames: false,
8079        };
8080
8081        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8082        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8083        assert_eq!(s.poll_client(), Err(Error::Done));
8084
8085        // Client shuts down stream ==> sends RESET_STREAM
8086        assert_eq!(
8087            s.pipe
8088                .client
8089                .stream_shutdown(stream, crate::Shutdown::Write, 42),
8090            Ok(())
8091        );
8092        assert_eq!(s.advance(), Ok(()));
8093
8094        // Server sees the Reset event for the stream.
8095        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
8096        assert_eq!(s.poll_server(), Err(Error::Done));
8097
8098        // Streams have been collected by quiche
8099        assert!(s.pipe.server.streams.is_collected(stream));
8100        assert!(s.pipe.client.streams.is_collected(stream));
8101
8102        // Client sends another request, server sends response without fin
8103        //
8104        let (stream, req) = s.send_request(false).unwrap();
8105
8106        let ev_headers = Event::Headers {
8107            list: req,
8108            more_frames: true,
8109        };
8110
8111        // Check that server has received the request.
8112        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8113        assert_eq!(s.poll_server(), Err(Error::Done));
8114
8115        // Server sends reponse without closing the stream.
8116        let resp = s.send_response(stream, false).unwrap();
8117
8118        let ev_headers = Event::Headers {
8119            list: resp,
8120            more_frames: true,
8121        };
8122
8123        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8124        assert_eq!(s.poll_client(), Err(Error::Done));
8125
8126        // Client shuts down stream ==> sends RESET_STREAM
8127        assert_eq!(
8128            s.pipe
8129                .client
8130                .stream_shutdown(stream, crate::Shutdown::Write, 42),
8131            Ok(())
8132        );
8133        assert_eq!(s.advance(), Ok(()));
8134
8135        // Server sees the Reset event for the stream.
8136        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
8137        assert_eq!(s.poll_server(), Err(Error::Done));
8138
8139        // Server sends body and closes the stream.
8140        s.send_body_server(stream, true).unwrap();
8141
8142        // Stream has been collected on server by quiche
8143        assert!(s.pipe.server.streams.is_collected(stream));
8144        // Client stream has not been collected, the client needs to
8145        // read the fin from the stream first.
8146        assert!(!s.pipe.client.streams.is_collected(stream));
8147        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8148        s.recv_body_client(stream, &mut buf).unwrap();
8149        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8150        assert_eq!(s.poll_client(), Err(Error::Done));
8151        assert!(s.pipe.client.streams.is_collected(stream));
8152    }
8153
8154    #[test]
8155    fn client_shutdown_read() {
8156        let mut buf = [0; 65535];
8157        let mut s = Session::new().unwrap();
8158        s.handshake().unwrap();
8159
8160        // Client sends request and leaves stream open.
8161        let (stream, req) = s.send_request(false).unwrap();
8162
8163        let ev_headers = Event::Headers {
8164            list: req,
8165            more_frames: true,
8166        };
8167
8168        // Server sends response and leaves stream open.
8169        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8170        assert_eq!(s.poll_server(), Err(Error::Done));
8171
8172        let resp = s.send_response(stream, false).unwrap();
8173
8174        let ev_headers = Event::Headers {
8175            list: resp,
8176            more_frames: true,
8177        };
8178
8179        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8180        assert_eq!(s.poll_client(), Err(Error::Done));
8181        // Client shuts down read
8182        assert_eq!(
8183            s.pipe
8184                .client
8185                .stream_shutdown(stream, crate::Shutdown::Read, 42),
8186            Ok(())
8187        );
8188        assert_eq!(s.advance(), Ok(()));
8189
8190        // Stream is writable on server side, but returns StreamStopped
8191        assert_eq!(s.poll_server(), Err(Error::Done));
8192        let writables: Vec<u64> = s.pipe.server.writable().collect();
8193        assert!(writables.contains(&stream));
8194        assert_eq!(
8195            s.send_body_server(stream, false),
8196            Err(Error::TransportError(crate::Error::StreamStopped(42)))
8197        );
8198
8199        // Client needs to finish its side by sending a fin
8200        assert_eq!(
8201            s.client.send_body(&mut s.pipe.client, stream, &[], true),
8202            Ok(0)
8203        );
8204        assert_eq!(s.advance(), Ok(()));
8205        // Note, we get an Event::Data for an empty buffer today. But it
8206        // would also be fine to not get it.
8207        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8208        assert_eq!(s.recv_body_server(stream, &mut buf), Err(Error::Done));
8209        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8210        assert_eq!(s.poll_server(), Err(Error::Done));
8211
8212        // Since the client has already send a fin, the stream is collected
8213        // on both client and server
8214        assert!(s.pipe.client.streams.is_collected(stream));
8215        assert!(s.pipe.server.streams.is_collected(stream));
8216    }
8217
8218    #[test]
8219    fn reset_finished_at_server() {
8220        let mut s = Session::new().unwrap();
8221        s.handshake().unwrap();
8222
8223        // Client sends HEADERS and doesn't fin
8224        let (stream, _req) = s.send_request(false).unwrap();
8225
8226        // ..then Client sends RESET_STREAM
8227        assert_eq!(
8228            s.pipe.client.stream_shutdown(0, crate::Shutdown::Write, 0),
8229            Ok(())
8230        );
8231
8232        assert_eq!(s.pipe.advance(), Ok(()));
8233
8234        // Server receives just a reset
8235        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(0))));
8236        assert_eq!(s.poll_server(), Err(Error::Done));
8237
8238        // Client sends HEADERS and fin
8239        let (stream, req) = s.send_request(true).unwrap();
8240
8241        // ..then Client sends RESET_STREAM
8242        assert_eq!(
8243            s.pipe.client.stream_shutdown(4, crate::Shutdown::Write, 0),
8244            Ok(())
8245        );
8246
8247        let ev_headers = Event::Headers {
8248            list: req,
8249            more_frames: false,
8250        };
8251
8252        // Server receives headers and fin.
8253        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8254        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8255        assert_eq!(s.poll_server(), Err(Error::Done));
8256    }
8257
8258    #[test]
8259    fn reset_finished_at_server_with_data_pending() {
8260        let mut s = Session::new().unwrap();
8261        s.handshake().unwrap();
8262
8263        // Client sends HEADERS and doesn't fin.
8264        let (stream, req) = s.send_request(false).unwrap();
8265
8266        assert!(s.send_body_client(stream, false).is_ok());
8267
8268        assert_eq!(s.pipe.advance(), Ok(()));
8269
8270        let ev_headers = Event::Headers {
8271            list: req,
8272            more_frames: true,
8273        };
8274
8275        // Server receives headers and data...
8276        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8277        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8278
8279        // ..then Client sends RESET_STREAM.
8280        assert_eq!(
8281            s.pipe
8282                .client
8283                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8284            Ok(())
8285        );
8286
8287        assert_eq!(s.pipe.advance(), Ok(()));
8288
8289        // The server does *not* attempt to read from the stream,
8290        // but polls and receives the reset and there are no more
8291        // readable streams.
8292        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(0))));
8293        assert_eq!(s.poll_server(), Err(Error::Done));
8294        assert_eq!(s.pipe.server.readable().len(), 0);
8295    }
8296
8297    #[test]
8298    fn reset_finished_at_server_with_data_pending_2() {
8299        let mut s = Session::new().unwrap();
8300        s.handshake().unwrap();
8301
8302        // Client sends HEADERS and doesn't fin.
8303        let (stream, req) = s.send_request(false).unwrap();
8304
8305        assert!(s.send_body_client(stream, false).is_ok());
8306
8307        assert_eq!(s.pipe.advance(), Ok(()));
8308
8309        let ev_headers = Event::Headers {
8310            list: req,
8311            more_frames: true,
8312        };
8313
8314        // Server receives headers and data...
8315        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8316        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8317
8318        // ..then Client sends RESET_STREAM.
8319        assert_eq!(
8320            s.pipe
8321                .client
8322                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8323            Ok(())
8324        );
8325
8326        assert_eq!(s.pipe.advance(), Ok(()));
8327
8328        // Server reads from the stream and receives the reset while
8329        // attempting to read.
8330        assert_eq!(
8331            s.recv_body_server(stream, &mut [0; 100]),
8332            Err(Error::TransportError(crate::Error::StreamReset(0)))
8333        );
8334
8335        // No more events and there are no more readable streams.
8336        assert_eq!(s.poll_server(), Err(Error::Done));
8337        assert_eq!(s.pipe.server.readable().len(), 0);
8338    }
8339
8340    #[test]
8341    fn reset_finished_at_client() {
8342        let mut buf = [0; 65535];
8343        let mut s = Session::new().unwrap();
8344        s.handshake().unwrap();
8345
8346        // Client sends HEADERS and doesn't fin
8347        let (stream, req) = s.send_request(false).unwrap();
8348
8349        let ev_headers = Event::Headers {
8350            list: req,
8351            more_frames: true,
8352        };
8353
8354        // Server receives headers.
8355        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8356        assert_eq!(s.poll_server(), Err(Error::Done));
8357
8358        // Server sends response and doesn't fin
8359        s.send_response(stream, false).unwrap();
8360
8361        assert_eq!(s.pipe.advance(), Ok(()));
8362
8363        // .. then Server sends RESET_STREAM
8364        assert_eq!(
8365            s.pipe
8366                .server
8367                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8368            Ok(())
8369        );
8370
8371        assert_eq!(s.pipe.advance(), Ok(()));
8372
8373        // Client receives Reset only
8374        assert_eq!(s.poll_client(), Ok((stream, Event::Reset(0))));
8375        assert_eq!(s.poll_server(), Err(Error::Done));
8376
8377        // Client sends headers and fin.
8378        let (stream, req) = s.send_request(true).unwrap();
8379
8380        let ev_headers = Event::Headers {
8381            list: req,
8382            more_frames: false,
8383        };
8384
8385        // Server receives headers and fin.
8386        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8387        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8388        assert_eq!(s.poll_server(), Err(Error::Done));
8389
8390        // Server sends response and fin
8391        let resp = s.send_response(stream, true).unwrap();
8392
8393        assert_eq!(s.pipe.advance(), Ok(()));
8394
8395        // ..then Server sends RESET_STREAM
8396        let frames = [crate::frame::Frame::ResetStream {
8397            stream_id: stream,
8398            error_code: 42,
8399            final_size: 68,
8400        }];
8401
8402        let pkt_type = crate::packet::Type::Short;
8403        assert_eq!(
8404            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
8405            Ok(39)
8406        );
8407
8408        assert_eq!(s.pipe.advance(), Ok(()));
8409
8410        let ev_headers = Event::Headers {
8411            list: resp,
8412            more_frames: false,
8413        };
8414
8415        // Client receives headers and fin.
8416        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8417        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8418        assert_eq!(s.poll_client(), Err(Error::Done));
8419    }
8420
8421    #[test]
8422    fn collect_completed_streams() {
8423        let mut s = Session::new().unwrap();
8424        s.handshake().unwrap();
8425
8426        let init_streams_client = s.client.streams.len();
8427        let init_streams_server = s.server.streams.len();
8428
8429        // Client sends HEADERS and doesn't fin
8430        let (stream, req) = s.send_request(false).unwrap();
8431
8432        let ev_headers = Event::Headers {
8433            list: req,
8434            more_frames: true,
8435        };
8436
8437        // Server receives headers.
8438        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8439        assert_eq!(s.poll_server(), Err(Error::Done));
8440
8441        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8442        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8443
8444        // Client sends body and fin
8445        let body = s.send_body_client(stream, true).unwrap();
8446
8447        let mut recv_buf = vec![0; body.len()];
8448
8449        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8450        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
8451
8452        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8453
8454        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8455        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8456
8457        // Server sends response and finishes the stream
8458        let resp_headers = s.send_response(stream, false).unwrap();
8459        s.send_body_server(stream, true).unwrap();
8460
8461        let ev_headers = Event::Headers {
8462            list: resp_headers,
8463            more_frames: true,
8464        };
8465
8466        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8467        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8468        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
8469
8470        // The server stream should be gone now
8471        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8472        assert_eq!(s.server.streams.len(), init_streams_server);
8473
8474        // Polling again should clean up the client
8475        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8476        assert_eq!(s.poll_client(), Err(Error::Done));
8477
8478        assert_eq!(s.client.streams.len(), init_streams_client);
8479    }
8480
8481    #[test]
8482    fn collect_reset_streams() {
8483        let mut s = Session::new().unwrap();
8484        s.handshake().unwrap();
8485
8486        let init_streams_client = s.client.streams.len();
8487        let init_streams_server = s.server.streams.len();
8488
8489        // Client sends HEADERS and doesn't fin
8490        let (stream, req) = s.send_request(false).unwrap();
8491
8492        let ev_headers = Event::Headers {
8493            list: req,
8494            more_frames: true,
8495        };
8496
8497        // Server receives headers.
8498        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8499        assert_eq!(s.poll_server(), Err(Error::Done));
8500
8501        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8502        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8503
8504        // Client sends body and fin
8505        let body = s.send_body_client(stream, true).unwrap();
8506
8507        let mut recv_buf = vec![0; body.len()];
8508
8509        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8510        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
8511
8512        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8513
8514        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8515        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8516
8517        // Server sends response and resets the stream.
8518        s.send_response(stream, false).unwrap();
8519        s.pipe
8520            .server
8521            .stream_shutdown(stream, crate::Shutdown::Write, 0)
8522            .unwrap();
8523
8524        s.advance().ok();
8525
8526        // TODO: need to notify resets better.
8527        //
8528        // This will trigger the h3 layer to check for the stream resets,
8529        // otherwise it wouldn't know that a reset happened.
8530        //
8531        // We will need to figure out a way to do this automatically to avoid
8532        // requiring applications to do this manually. For now just keep this
8533        // for testing purposes.
8534        let _ = s.send_body_server(stream, true);
8535
8536        assert_eq!(s.poll_server(), Err(Error::Done));
8537
8538        assert_eq!(s.poll_client(), Ok((stream, Event::Reset(0))));
8539        assert_eq!(s.poll_client(), Err(Error::Done));
8540
8541        // The server stream should be gone now
8542        assert_eq!(s.client.streams.len(), init_streams_client);
8543        assert_eq!(s.server.streams.len(), init_streams_server);
8544    }
8545}
8546
8547#[cfg(feature = "ffi")]
8548mod ffi;
8549#[cfg(feature = "internal")]
8550#[doc(hidden)]
8551pub mod frame;
8552#[cfg(not(feature = "internal"))]
8553mod frame;
8554#[doc(hidden)]
8555pub mod qpack;
8556mod stream;