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                    // `rem` should always be None or empty.
1654                    // `do_send_body()` should have checked the capacity and
1655                    // ensured that there is enough capacity to write the header +
1656                    // fully body.
1657                    debug_assert!(false);
1658                    return Err(Error::InternalError);
1659                }
1660
1661                if with_prefix {
1662                    n -= header.len();
1663                }
1664
1665                if !remainder.as_ref().is_empty() {
1666                    let _ = std::mem::replace(body, remainder);
1667                }
1668
1669                Ok((n, n))
1670            },
1671        )
1672    }
1673
1674    fn do_send_body<F, B, R, SND>(
1675        &mut self, conn: &mut super::Connection<F>, stream_id: u64, body: B,
1676        fin: bool, write_fn: SND,
1677    ) -> Result<R>
1678    where
1679        F: BufFactory,
1680        B: AsRef<[u8]>,
1681        SND: FnOnce(
1682            &mut super::Connection<F>,
1683            &[u8],
1684            u64,
1685            B,
1686            usize,
1687            bool,
1688        ) -> Result<(usize, R)>,
1689    {
1690        let mut d = [42; 10];
1691        let mut b = octets::OctetsMut::with_slice(&mut d);
1692
1693        let len = body.as_ref().len();
1694
1695        // Validate that it is sane to send data on the stream.
1696        if !stream_id.is_multiple_of(4) {
1697            return Err(Error::FrameUnexpected);
1698        }
1699
1700        match self.streams.get_mut(&stream_id) {
1701            Some(s) => {
1702                if !s.local_initialized() {
1703                    return Err(Error::FrameUnexpected);
1704                }
1705
1706                if s.trailers_sent() {
1707                    return Err(Error::FrameUnexpected);
1708                }
1709            },
1710
1711            None => {
1712                return Err(Error::FrameUnexpected);
1713            },
1714        };
1715
1716        // Avoid sending 0-length DATA frames when the fin flag is false.
1717        if len == 0 && !fin {
1718            return Err(Error::Done);
1719        }
1720
1721        let overhead = octets::varint_len(frame::DATA_FRAME_TYPE_ID) +
1722            octets::varint_len(len as u64);
1723
1724        let stream_cap = match conn.stream_capacity(stream_id) {
1725            Ok(v) => v,
1726
1727            Err(e) => {
1728                if conn.stream_finished(stream_id) {
1729                    self.streams.remove(&stream_id);
1730                }
1731
1732                return Err(e.into());
1733            },
1734        };
1735
1736        // Make sure there is enough capacity to send the DATA frame header.
1737        if stream_cap < overhead {
1738            let _ = conn.stream_writable(stream_id, overhead + 1);
1739            return Err(Error::Done);
1740        }
1741
1742        // Cap the frame payload length to the stream's capacity.
1743        let body_len = std::cmp::min(len, stream_cap - overhead);
1744
1745        // If we can't send the entire body, set the fin flag to false so the
1746        // application can try again later.
1747        let fin = if body_len != len { false } else { fin };
1748
1749        // Again, avoid sending 0-length DATA frames when the fin flag is false.
1750        if body_len == 0 && !fin {
1751            let _ = conn.stream_writable(stream_id, overhead + 1);
1752            return Err(Error::Done);
1753        }
1754
1755        b.put_varint(frame::DATA_FRAME_TYPE_ID)?;
1756        b.put_varint(body_len as u64)?;
1757        let off = b.off();
1758
1759        // Return how many bytes were written, excluding the frame header.
1760        // Sending body separately avoids unnecessary copy.
1761        let (written, ret) =
1762            write_fn(conn, &d[..off], stream_id, body, body_len, fin)?;
1763        if written != body_len {
1764            // This should never happen. If it does, it means we wrote an
1765            // incorrect frame length and thus we can't really continue.
1766            debug_assert!(false);
1767            return Err(Error::InternalError);
1768        }
1769
1770        trace!(
1771            "{} tx frm DATA stream={} len={} fin={}",
1772            conn.trace_id(),
1773            stream_id,
1774            written,
1775            fin
1776        );
1777
1778        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
1779            let frame = Http3Frame::Data { raw: None };
1780            let ev_data = EventData::Http3FrameCreated(FrameCreated {
1781                stream_id,
1782                length: Some(written as u64),
1783                frame,
1784                ..Default::default()
1785            });
1786
1787            q.add_event_data_now(ev_data).ok();
1788        });
1789
1790        if written < len {
1791            // Ensure the peer is notified that the connection or stream is
1792            // blocked when the stream's capacity is limited by flow control.
1793            //
1794            // We only need enough capacity to send a few bytes, to make sure
1795            // the stream doesn't hang due to congestion window not growing
1796            // enough.
1797            let _ = conn.stream_writable(stream_id, overhead + 1);
1798        }
1799
1800        if fin && written == len {
1801            self.finish_local_stream(conn, stream_id, false);
1802        }
1803
1804        Ok(ret)
1805    }
1806
1807    /// Returns whether the peer enabled HTTP/3 DATAGRAM frame support.
1808    ///
1809    /// Support is signalled by the peer's SETTINGS, so this method always
1810    /// returns false until they have been processed using the [`poll()`]
1811    /// method.
1812    ///
1813    /// [`poll()`]: struct.Connection.html#method.poll
1814    pub fn dgram_enabled_by_peer<F: BufFactory>(
1815        &self, conn: &super::Connection<F>,
1816    ) -> bool {
1817        self.peer_settings.h3_datagram == Some(1) &&
1818            conn.dgram_max_writable_len().is_some()
1819    }
1820
1821    /// Returns whether the peer enabled extended CONNECT support.
1822    ///
1823    /// Support is signalled by the peer's SETTINGS, so this method always
1824    /// returns false until they have been processed using the [`poll()`]
1825    /// method.
1826    ///
1827    /// [`poll()`]: struct.Connection.html#method.poll
1828    pub fn extended_connect_enabled_by_peer(&self) -> bool {
1829        self.peer_settings.connect_protocol_enabled == Some(1)
1830    }
1831
1832    /// Reads request or response body data into the provided buffer.
1833    ///
1834    /// Applications should call this method whenever the [`poll()`] method
1835    /// returns a [`Data`] event.
1836    ///
1837    /// On success the amount of bytes read is returned, or [`Done`] if there
1838    /// is no data to read.
1839    ///
1840    /// [`poll()`]: struct.Connection.html#method.poll
1841    /// [`Data`]: enum.Event.html#variant.Data
1842    /// [`Done`]: enum.Error.html#variant.Done
1843    pub fn recv_body<F: BufFactory>(
1844        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1845        out: &mut [u8],
1846    ) -> Result<usize> {
1847        self.recv_body_buf(conn, stream_id, out)
1848    }
1849
1850    /// Reads request or response body data into the provided BufMut buffer.
1851    ///
1852    /// **NOTE**:
1853    /// The BufMut will be populated with all available data up to its capacity.
1854    /// Since some BufMut implementations, e.g., [`Vec<u8>`], dynamically
1855    /// allocate additional memory, the caller may use
1856    /// [`BufMut::limit()`] to limit the maximum amount of data that
1857    /// can be written.
1858    ///
1859    /// Applications should call this method (or [`recv_body()`]) whenever
1860    /// the [`poll()`] method returns a [`Data`] event.
1861    ///
1862    /// On success the amount of bytes read is returned, or [`Done`] if there
1863    /// is no data to read.
1864    ///
1865    /// [`BufMut::limit()`]: bytes::BufMut::limit()
1866    /// [`recv_body()`]: Self::recv_body()
1867    /// [`poll()`]: struct.Connection.html#method.poll
1868    /// [`Data`]: enum.Event.html#variant.Data
1869    /// [`Done`]: enum.Error.html#variant.Done
1870    ///
1871    /// ## Example:
1872    /// ```no_run
1873    /// # use quiche::h3;
1874    /// fn receive(
1875    ///     qconn: &mut quiche::Connection, h3conn: &mut h3::Connection,
1876    /// ) -> Result<Vec<u8>, h3::Error> {
1877    ///     use bytes::BufMut as _;
1878    ///     let mut buffer = Vec::with_capacity(2048).limit(2048);
1879    ///     let bytes = h3conn.recv_body_buf(qconn, 0, &mut buffer)?;
1880    ///     let buffer = buffer.into_inner();
1881    ///     // The vec has been filled with exactly `bytes` number of bytes
1882    ///     assert_eq!(buffer.len(), bytes);
1883    ///     Ok(buffer)
1884    /// }
1885    /// ```
1886    pub fn recv_body_buf<F: BufFactory, OUT: bytes::BufMut>(
1887        &mut self, conn: &mut super::Connection<F>, stream_id: u64, mut out: OUT,
1888    ) -> Result<usize> {
1889        let mut total = 0;
1890
1891        // Try to consume all buffered data for the stream, even across multiple
1892        // DATA frames.
1893        // Note, that even if the BufMut does not have a limit defined, we are
1894        // inherently limited by how much data is in quiche's receive buffer for
1895        // that stream, so the BufMut cannot grow unbounded.
1896        while out.has_remaining_mut() {
1897            let stream = self.streams.get_mut(&stream_id).ok_or(Error::Done)?;
1898
1899            if stream.state() != stream::State::Data {
1900                break;
1901            }
1902
1903            let (read, fin) = match stream.try_consume_data(conn, &mut out) {
1904                Ok(v) => v,
1905
1906                Err(Error::Done) => break,
1907
1908                Err(e) => return Err(e),
1909            };
1910
1911            total += read;
1912
1913            // No more data to read, we are done.
1914            if read == 0 || fin {
1915                break;
1916            }
1917
1918            // Process incoming data from the stream. For example, if a whole
1919            // DATA frame was consumed, and another one is queued behind it,
1920            // this will ensure the additional data will also be returned to
1921            // the application.
1922            match self.process_readable_stream(conn, stream_id, false) {
1923                Ok(_) => unreachable!(),
1924
1925                Err(Error::Done) => (),
1926
1927                Err(e) => return Err(e),
1928            };
1929
1930            if conn.stream_finished(stream_id) {
1931                break;
1932            }
1933        }
1934
1935        // While body is being received, the stream is marked as finished only
1936        // when all data is read by the application.
1937        if conn.stream_finished(stream_id) {
1938            self.process_finished_stream(stream_id);
1939        }
1940
1941        if total == 0 {
1942            return Err(Error::Done);
1943        }
1944
1945        Ok(total)
1946    }
1947
1948    /// Sends a PRIORITY_UPDATE frame on the control stream with specified
1949    /// request stream ID and priority.
1950    ///
1951    /// The `priority` parameter represents [Extensible Priority]
1952    /// parameters. If the urgency is outside the range 0-7, it will be clamped
1953    /// to 7.
1954    ///
1955    /// The [`StreamBlocked`] error is returned when the underlying QUIC stream
1956    /// doesn't have enough capacity for the operation to complete. When this
1957    /// happens the application should retry the operation once the stream is
1958    /// reported as writable again.
1959    ///
1960    /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1961    /// [Extensible Priority]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
1962    pub fn send_priority_update_for_request<F: BufFactory>(
1963        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1964        priority: &Priority,
1965    ) -> Result<()> {
1966        let mut d = [42; 20];
1967        let mut b = octets::OctetsMut::with_slice(&mut d);
1968
1969        // Validate that it is sane to send PRIORITY_UPDATE.
1970        if self.is_server {
1971            return Err(Error::FrameUnexpected);
1972        }
1973
1974        if !stream_id.is_multiple_of(4) {
1975            return Err(Error::FrameUnexpected);
1976        }
1977
1978        let control_stream_id =
1979            self.control_stream_id.ok_or(Error::FrameUnexpected)?;
1980
1981        let urgency = priority
1982            .urgency
1983            .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND);
1984
1985        let mut field_value = format!("u={urgency}");
1986
1987        if priority.incremental {
1988            field_value.push_str(",i");
1989        }
1990
1991        let priority_field_value = field_value.as_bytes();
1992        let frame_payload_len =
1993            octets::varint_len(stream_id) + priority_field_value.len();
1994
1995        let overhead =
1996            octets::varint_len(frame::PRIORITY_UPDATE_FRAME_REQUEST_TYPE_ID) +
1997                octets::varint_len(stream_id) +
1998                octets::varint_len(frame_payload_len as u64);
1999
2000        // Make sure the control stream has enough capacity.
2001        match conn.stream_writable(
2002            control_stream_id,
2003            overhead + priority_field_value.len(),
2004        ) {
2005            Ok(true) => (),
2006
2007            Ok(false) => return Err(Error::StreamBlocked),
2008
2009            Err(e) => {
2010                return Err(e.into());
2011            },
2012        }
2013
2014        b.put_varint(frame::PRIORITY_UPDATE_FRAME_REQUEST_TYPE_ID)?;
2015        b.put_varint(frame_payload_len as u64)?;
2016        b.put_varint(stream_id)?;
2017        let off = b.off();
2018        conn.stream_send(control_stream_id, &d[..off], false)?;
2019
2020        // Sending field value separately avoids unnecessary copy.
2021        conn.stream_send(control_stream_id, priority_field_value, false)?;
2022
2023        trace!(
2024            "{} tx frm PRIORITY_UPDATE request_stream={} priority_field_value={}",
2025            conn.trace_id(),
2026            stream_id,
2027            field_value,
2028        );
2029
2030        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2031            let frame = Http3Frame::PriorityUpdate {
2032                stream_id: Some(stream_id),
2033                push_id: None,
2034                priority_field_value: field_value.clone(),
2035                raw: None,
2036            };
2037
2038            let ev_data = EventData::Http3FrameCreated(FrameCreated {
2039                stream_id,
2040                length: Some(priority_field_value.len() as u64),
2041                frame,
2042                ..Default::default()
2043            });
2044
2045            q.add_event_data_now(ev_data).ok();
2046        });
2047
2048        Ok(())
2049    }
2050
2051    /// Take the last PRIORITY_UPDATE for a prioritized element ID.
2052    ///
2053    /// When the [`poll()`] method returns a [`PriorityUpdate`] event for a
2054    /// prioritized element, the event has triggered and will not rearm until
2055    /// applications call this method. It is recommended that applications defer
2056    /// taking the PRIORITY_UPDATE until after [`poll()`] returns [`Done`].
2057    ///
2058    /// On success the Priority Field Value is returned, or [`Done`] if there is
2059    /// no PRIORITY_UPDATE to read (either because there is no value to take, or
2060    /// because the prioritized element does not exist).
2061    ///
2062    /// [`poll()`]: struct.Connection.html#method.poll
2063    /// [`PriorityUpdate`]: enum.Event.html#variant.PriorityUpdate
2064    /// [`Done`]: enum.Error.html#variant.Done
2065    pub fn take_last_priority_update(
2066        &mut self, prioritized_element_id: u64,
2067    ) -> Result<Vec<u8>> {
2068        if let Some(stream) = self.streams.get_mut(&prioritized_element_id) {
2069            return stream.take_last_priority_update().ok_or(Error::Done);
2070        }
2071
2072        Err(Error::Done)
2073    }
2074
2075    /// Processes HTTP/3 data received from the peer.
2076    ///
2077    /// On success it returns an [`Event`] and an ID, or [`Done`] when there are
2078    /// no events to report.
2079    ///
2080    /// Note that all events are edge-triggered, meaning that once reported they
2081    /// will not be reported again by calling this method again, until the event
2082    /// is re-armed.
2083    ///
2084    /// The events [`Headers`], [`Data`] and [`Finished`] return a stream ID,
2085    /// which is used in methods [`recv_body()`], [`send_response()`] or
2086    /// [`send_body()`].
2087    ///
2088    /// The event [`GoAway`] returns an ID that depends on the connection role.
2089    /// A client receives the largest processed stream ID. A server receives the
2090    /// the largest permitted push ID.
2091    ///
2092    /// The event [`PriorityUpdate`] only occurs at servers. It returns a
2093    /// prioritized element ID that is used in the method
2094    /// [`take_last_priority_update()`], which rearms the event for that ID.
2095    ///
2096    /// If an error occurs while processing data, the connection is closed with
2097    /// the appropriate error code, using the transport's [`close()`] method.
2098    ///
2099    /// [`Event`]: enum.Event.html
2100    /// [`Done`]: enum.Error.html#variant.Done
2101    /// [`Headers`]: enum.Event.html#variant.Headers
2102    /// [`Data`]: enum.Event.html#variant.Data
2103    /// [`Finished`]: enum.Event.html#variant.Finished
2104    /// [`GoAway`]: enum.Event.html#variant.GoAWay
2105    /// [`PriorityUpdate`]: enum.Event.html#variant.PriorityUpdate
2106    /// [`recv_body()`]: struct.Connection.html#method.recv_body
2107    /// [`send_response()`]: struct.Connection.html#method.send_response
2108    /// [`send_body()`]: struct.Connection.html#method.send_body
2109    /// [`recv_dgram()`]: struct.Connection.html#method.recv_dgram
2110    /// [`take_last_priority_update()`]: struct.Connection.html#method.take_last_priority_update
2111    /// [`close()`]: ../struct.Connection.html#method.close
2112    pub fn poll<F: BufFactory>(
2113        &mut self, conn: &mut super::Connection<F>,
2114    ) -> Result<(u64, Event)> {
2115        // When connection close is initiated by the local application (e.g. due
2116        // to a protocol error), the connection itself might be in a broken
2117        // state, so return early.
2118        if conn.local_error.is_some() {
2119            return Err(Error::Done);
2120        }
2121
2122        // Process control streams first.
2123        if let Some(stream_id) = self.peer_control_stream_id {
2124            match self.process_control_stream(conn, stream_id) {
2125                Ok(ev) => return Ok(ev),
2126
2127                Err(Error::Done) => (),
2128
2129                Err(e) => return Err(e),
2130            };
2131        }
2132
2133        if let Some(stream_id) = self.peer_qpack_streams.encoder_stream_id {
2134            match self.process_control_stream(conn, stream_id) {
2135                Ok(ev) => return Ok(ev),
2136
2137                Err(Error::Done) => (),
2138
2139                Err(e) => return Err(e),
2140            };
2141        }
2142
2143        if let Some(stream_id) = self.peer_qpack_streams.decoder_stream_id {
2144            match self.process_control_stream(conn, stream_id) {
2145                Ok(ev) => return Ok(ev),
2146
2147                Err(Error::Done) => (),
2148
2149                Err(e) => return Err(e),
2150            };
2151        }
2152
2153        // Process finished streams list.
2154        if let Some(ev) = self.pop_finished_stream(conn) {
2155            return Ok(ev);
2156        }
2157
2158        // Process HTTP/3 data from readable streams.
2159        for s in conn.readable() {
2160            trace!("{} stream id {} is readable", conn.trace_id(), s);
2161
2162            let ev = match self.process_readable_stream(conn, s, true) {
2163                Ok(v) => Some(v),
2164
2165                Err(Error::Done) => None,
2166
2167                // Return early if the stream was reset, to avoid returning
2168                // a Finished event later as well.
2169                Err(Error::TransportError(crate::Error::StreamReset(e))) => {
2170                    self.remove_local_finished_stream(s);
2171
2172                    return Ok((s, Event::Reset(e)));
2173                },
2174
2175                Err(e) => return Err(e),
2176            };
2177
2178            if conn.stream_finished(s) {
2179                self.process_finished_stream(s);
2180            }
2181
2182            // TODO: check if stream is completed so it can be freed
2183            if let Some(ev) = ev {
2184                return Ok(ev);
2185            }
2186        }
2187
2188        // Process finished streams list once again, to make sure `Finished`
2189        // events are returned when receiving empty stream frames with the fin
2190        // flag set.
2191        if let Some(ev) = self.pop_finished_stream(conn) {
2192            return Ok(ev);
2193        }
2194
2195        Err(Error::Done)
2196    }
2197
2198    /// Sends a GOAWAY frame to initiate graceful connection closure.
2199    ///
2200    /// When quiche is used in the server role, the `id` parameter is the stream
2201    /// ID of the highest processed request. This can be any valid ID between 0
2202    /// and 2^62-4. However, the ID cannot be increased. Failure to satisfy
2203    /// these conditions will return an error.
2204    ///
2205    /// This method does not close the QUIC connection. Applications are
2206    /// required to call [`close()`] themselves.
2207    ///
2208    /// [`close()`]: ../struct.Connection.html#method.close
2209    pub fn send_goaway<F: BufFactory>(
2210        &mut self, conn: &mut super::Connection<F>, id: u64,
2211    ) -> Result<()> {
2212        let mut id = id;
2213
2214        // TODO: server push
2215        //
2216        // In the meantime always send 0 from client.
2217        if !self.is_server {
2218            id = 0;
2219        }
2220
2221        if self.is_server && !id.is_multiple_of(4) {
2222            return Err(Error::IdError);
2223        }
2224
2225        if let Some(sent_id) = self.local_goaway_id {
2226            if id > sent_id {
2227                return Err(Error::IdError);
2228            }
2229        }
2230
2231        if let Some(stream_id) = self.control_stream_id {
2232            let mut d = [42; 10];
2233            let mut b = octets::OctetsMut::with_slice(&mut d);
2234
2235            let frame = frame::Frame::GoAway { id };
2236
2237            let wire_len = frame.to_bytes(&mut b)?;
2238            let stream_cap = conn.stream_capacity(stream_id)?;
2239
2240            if stream_cap < wire_len {
2241                return Err(Error::StreamBlocked);
2242            }
2243
2244            trace!("{} tx frm {:?}", conn.trace_id(), frame);
2245
2246            qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2247                let ev_data = EventData::Http3FrameCreated(FrameCreated {
2248                    stream_id,
2249                    length: Some(octets::varint_len(id) as u64),
2250                    frame: frame.to_qlog(),
2251                    ..Default::default()
2252                });
2253
2254                q.add_event_data_now(ev_data).ok();
2255            });
2256
2257            let off = b.off();
2258            conn.stream_send(stream_id, &d[..off], false)?;
2259
2260            self.local_goaway_id = Some(id);
2261        }
2262
2263        Ok(())
2264    }
2265
2266    /// Gets the raw settings from peer including unknown and reserved types.
2267    ///
2268    /// The order of settings is the same as received in the SETTINGS frame.
2269    pub fn peer_settings_raw(&self) -> Option<&[(u64, u64)]> {
2270        self.peer_settings.raw.as_deref()
2271    }
2272
2273    fn open_uni_stream<F: BufFactory>(
2274        &mut self, conn: &mut super::Connection<F>, ty: u64,
2275    ) -> Result<u64> {
2276        let stream_id = self.next_uni_stream_id;
2277
2278        let mut d = [0; 8];
2279        let mut b = octets::OctetsMut::with_slice(&mut d);
2280
2281        match ty {
2282            // Control and QPACK streams are the most important to schedule.
2283            stream::HTTP3_CONTROL_STREAM_TYPE_ID |
2284            stream::QPACK_ENCODER_STREAM_TYPE_ID |
2285            stream::QPACK_DECODER_STREAM_TYPE_ID => {
2286                conn.stream_priority(stream_id, 0, false)?;
2287            },
2288
2289            // TODO: Server push
2290            stream::HTTP3_PUSH_STREAM_TYPE_ID => (),
2291
2292            // Anything else is a GREASE stream, so make it the least important.
2293            _ => {
2294                conn.stream_priority(stream_id, 255, false)?;
2295            },
2296        }
2297
2298        conn.stream_send(stream_id, b.put_varint(ty)?, false)?;
2299
2300        // To avoid skipping stream IDs, we only calculate the next available
2301        // stream ID when data has been successfully buffered.
2302        self.next_uni_stream_id = self
2303            .next_uni_stream_id
2304            .checked_add(4)
2305            .ok_or(Error::IdError)?;
2306
2307        Ok(stream_id)
2308    }
2309
2310    fn open_qpack_encoder_stream<F: BufFactory>(
2311        &mut self, conn: &mut super::Connection<F>,
2312    ) -> Result<()> {
2313        let stream_id =
2314            self.open_uni_stream(conn, stream::QPACK_ENCODER_STREAM_TYPE_ID)?;
2315
2316        self.local_qpack_streams.encoder_stream_id = Some(stream_id);
2317
2318        qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2319            let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2320                stream_id,
2321                initiator: Some(Initiator::Local),
2322                stream_type: StreamType::QpackEncode,
2323                ..Default::default()
2324            });
2325
2326            q.add_event_data_now(ev_data).ok();
2327        });
2328
2329        Ok(())
2330    }
2331
2332    fn open_qpack_decoder_stream<F: BufFactory>(
2333        &mut self, conn: &mut super::Connection<F>,
2334    ) -> Result<()> {
2335        let stream_id =
2336            self.open_uni_stream(conn, stream::QPACK_DECODER_STREAM_TYPE_ID)?;
2337
2338        self.local_qpack_streams.decoder_stream_id = Some(stream_id);
2339
2340        qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2341            let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2342                stream_id,
2343                initiator: Some(Initiator::Local),
2344                stream_type: StreamType::QpackDecode,
2345                ..Default::default()
2346            });
2347
2348            q.add_event_data_now(ev_data).ok();
2349        });
2350
2351        Ok(())
2352    }
2353
2354    /// Send GREASE frames on the provided stream ID.
2355    fn send_grease_frames<F: BufFactory>(
2356        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
2357    ) -> Result<()> {
2358        let mut d = [0; 8];
2359
2360        let stream_cap = match conn.stream_capacity(stream_id) {
2361            Ok(v) => v,
2362
2363            Err(e) => {
2364                if conn.stream_finished(stream_id) {
2365                    self.streams.remove(&stream_id);
2366                }
2367
2368                return Err(e.into());
2369            },
2370        };
2371
2372        let grease_frame1 = grease_value();
2373        let grease_frame2 = grease_value();
2374        let grease_payload = b"GREASE is the word";
2375
2376        let overhead = octets::varint_len(grease_frame1) + // frame type
2377            1 + // payload len
2378            octets::varint_len(grease_frame2) + // frame type
2379            1 + // payload len
2380            grease_payload.len(); // payload
2381
2382        // Don't send GREASE if there is not enough capacity for it. Greasing
2383        // will _not_ be attempted again later on.
2384        if stream_cap < overhead {
2385            return Ok(());
2386        }
2387
2388        // Empty GREASE frame.
2389        let mut b = octets::OctetsMut::with_slice(&mut d);
2390        conn.stream_send(stream_id, b.put_varint(grease_frame1)?, false)?;
2391
2392        let mut b = octets::OctetsMut::with_slice(&mut d);
2393        conn.stream_send(stream_id, b.put_varint(0)?, false)?;
2394
2395        trace!(
2396            "{} tx frm GREASE stream={} len=0",
2397            conn.trace_id(),
2398            stream_id
2399        );
2400
2401        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2402            let frame = Http3Frame::Reserved {
2403                frame_type_bytes: grease_frame1,
2404                raw: None,
2405            };
2406            let ev_data = EventData::Http3FrameCreated(FrameCreated {
2407                stream_id,
2408                length: Some(0),
2409                frame,
2410                ..Default::default()
2411            });
2412
2413            q.add_event_data_now(ev_data).ok();
2414        });
2415
2416        // GREASE frame with payload.
2417        let mut b = octets::OctetsMut::with_slice(&mut d);
2418        conn.stream_send(stream_id, b.put_varint(grease_frame2)?, false)?;
2419
2420        let mut b = octets::OctetsMut::with_slice(&mut d);
2421        conn.stream_send(stream_id, b.put_varint(18)?, false)?;
2422
2423        conn.stream_send(stream_id, grease_payload, false)?;
2424
2425        trace!(
2426            "{} tx frm GREASE stream={} len={}",
2427            conn.trace_id(),
2428            stream_id,
2429            grease_payload.len()
2430        );
2431
2432        qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2433            let frame = Http3Frame::Reserved {
2434                frame_type_bytes: grease_frame2,
2435                raw: None,
2436            };
2437            let ev_data = EventData::Http3FrameCreated(FrameCreated {
2438                stream_id,
2439                length: Some(grease_payload.len() as u64),
2440                frame,
2441                ..Default::default()
2442            });
2443
2444            q.add_event_data_now(ev_data).ok();
2445        });
2446
2447        Ok(())
2448    }
2449
2450    /// Opens a new unidirectional stream with a GREASE type and sends some
2451    /// unframed payload.
2452    fn open_grease_stream<F: BufFactory>(
2453        &mut self, conn: &mut super::Connection<F>,
2454    ) -> Result<()> {
2455        let ty = grease_value();
2456        match self.open_uni_stream(conn, ty) {
2457            Ok(stream_id) => {
2458                conn.stream_send(stream_id, b"GREASE is the word", true)?;
2459
2460                trace!("{} open GREASE stream {}", conn.trace_id(), stream_id);
2461
2462                qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2463                    let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2464                        stream_id,
2465                        initiator: Some(Initiator::Local),
2466                        stream_type: StreamType::Unknown,
2467                        stream_type_bytes: Some(ty),
2468                        ..Default::default()
2469                    });
2470
2471                    q.add_event_data_now(ev_data).ok();
2472                });
2473            },
2474
2475            Err(Error::IdError) => {
2476                trace!("{} GREASE stream blocked", conn.trace_id(),);
2477
2478                return Ok(());
2479            },
2480
2481            Err(e) => return Err(e),
2482        };
2483
2484        Ok(())
2485    }
2486
2487    /// Sends SETTINGS frame based on HTTP/3 configuration.
2488    fn send_settings<F: BufFactory>(
2489        &mut self, conn: &mut super::Connection<F>,
2490    ) -> Result<()> {
2491        let stream_id = match self
2492            .open_uni_stream(conn, stream::HTTP3_CONTROL_STREAM_TYPE_ID)
2493        {
2494            Ok(v) => v,
2495
2496            Err(e) => {
2497                trace!("{} Control stream blocked", conn.trace_id(),);
2498
2499                if e == Error::Done {
2500                    return Err(Error::InternalError);
2501                }
2502
2503                return Err(e);
2504            },
2505        };
2506
2507        self.control_stream_id = Some(stream_id);
2508
2509        qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2510            let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2511                stream_id,
2512                initiator: Some(Initiator::Local),
2513                stream_type: StreamType::Control,
2514                ..Default::default()
2515            });
2516
2517            q.add_event_data_now(ev_data).ok();
2518        });
2519
2520        let grease = if conn.grease {
2521            Some((grease_value(), grease_value()))
2522        } else {
2523            None
2524        };
2525
2526        let frame = frame::Frame::Settings {
2527            max_field_section_size: self.local_settings.max_field_section_size,
2528            qpack_max_table_capacity: self
2529                .local_settings
2530                .qpack_max_table_capacity,
2531            qpack_blocked_streams: self.local_settings.qpack_blocked_streams,
2532            connect_protocol_enabled: self
2533                .local_settings
2534                .connect_protocol_enabled,
2535            h3_datagram: self.local_settings.h3_datagram,
2536            grease,
2537            additional_settings: self.local_settings.additional_settings.clone(),
2538            raw: Default::default(),
2539        };
2540
2541        let mut d = [42; 128];
2542        let mut b = octets::OctetsMut::with_slice(&mut d);
2543
2544        frame.to_bytes(&mut b)?;
2545
2546        let off = b.off();
2547
2548        if let Some(id) = self.control_stream_id {
2549            conn.stream_send(id, &d[..off], false)?;
2550
2551            trace!(
2552                "{} tx frm SETTINGS stream={} len={}",
2553                conn.trace_id(),
2554                id,
2555                off
2556            );
2557
2558            qlog_with_type!(QLOG_FRAME_CREATED, conn.qlog, q, {
2559                let frame = frame.to_qlog();
2560                let ev_data = EventData::Http3FrameCreated(FrameCreated {
2561                    stream_id: id,
2562                    length: Some(off as u64),
2563                    frame,
2564                    ..Default::default()
2565                });
2566
2567                q.add_event_data_now(ev_data).ok();
2568            });
2569        }
2570
2571        Ok(())
2572    }
2573
2574    fn process_control_stream<F: BufFactory>(
2575        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
2576    ) -> Result<(u64, Event)> {
2577        close_conn_if_critical_stream_finished(conn, stream_id)?;
2578
2579        if !conn.stream_readable(stream_id) {
2580            return Err(Error::Done);
2581        }
2582
2583        match self.process_readable_stream(conn, stream_id, true) {
2584            Ok(ev) => return Ok(ev),
2585
2586            Err(Error::Done) => (),
2587
2588            Err(e) => return Err(e),
2589        };
2590
2591        close_conn_if_critical_stream_finished(conn, stream_id)?;
2592
2593        Err(Error::Done)
2594    }
2595
2596    fn process_readable_stream<F: BufFactory>(
2597        &mut self, conn: &mut super::Connection<F>, stream_id: u64, polling: bool,
2598    ) -> Result<(u64, Event)> {
2599        self.streams.entry(stream_id).or_insert_with(|| {
2600            <stream::Stream>::new(
2601                stream_id,
2602                false,
2603                self.local_settings
2604                    .max_field_section_size
2605                    .unwrap_or(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
2606                self.max_priority_update_size,
2607            )
2608        });
2609
2610        // We need to get a fresh reference to the stream for each
2611        // iteration, to avoid borrowing `self` for the entire duration
2612        // of the loop, because we'll need to borrow it again in the
2613        // `State::FramePayload` case below.
2614        while let Some(stream) = self.streams.get_mut(&stream_id) {
2615            match stream.state() {
2616                stream::State::StreamType => {
2617                    stream.try_fill_buffer(conn)?;
2618
2619                    let varint = match stream.try_consume_varint() {
2620                        Ok(v) => v,
2621
2622                        Err(_) => continue,
2623                    };
2624
2625                    let ty = stream::Type::deserialize(varint)?;
2626
2627                    if let Err(e) = stream.set_ty(ty) {
2628                        conn.close(true, e.to_wire(), b"")?;
2629                        return Err(e);
2630                    }
2631
2632                    qlog_with_type!(QLOG_STREAM_TYPE_SET, conn.qlog, q, {
2633                        let ty_val = if matches!(ty, stream::Type::Unknown) {
2634                            Some(varint)
2635                        } else {
2636                            None
2637                        };
2638
2639                        let ev_data =
2640                            EventData::Http3StreamTypeSet(StreamTypeSet {
2641                                stream_id,
2642                                initiator: Some(Initiator::Remote),
2643                                stream_type: ty.to_qlog(),
2644                                stream_type_bytes: ty_val,
2645                                ..Default::default()
2646                            });
2647
2648                        q.add_event_data_now(ev_data).ok();
2649                    });
2650
2651                    match &ty {
2652                        stream::Type::Control => {
2653                            // Only one control stream allowed.
2654                            if self.peer_control_stream_id.is_some() {
2655                                conn.close(
2656                                    true,
2657                                    Error::StreamCreationError.to_wire(),
2658                                    b"Received multiple control streams",
2659                                )?;
2660
2661                                return Err(Error::StreamCreationError);
2662                            }
2663
2664                            trace!(
2665                                "{} open peer's control stream {}",
2666                                conn.trace_id(),
2667                                stream_id
2668                            );
2669
2670                            close_conn_if_critical_stream_finished(
2671                                conn, stream_id,
2672                            )?;
2673
2674                            self.peer_control_stream_id = Some(stream_id);
2675                        },
2676
2677                        stream::Type::Push => {
2678                            // Server push is not supported, so push streams at
2679                            // either client or server is a critical protocol
2680                            // error.
2681                            conn.close(
2682                                true,
2683                                Error::StreamCreationError.to_wire(),
2684                                b"Received push stream.",
2685                            )?;
2686
2687                            return Err(Error::StreamCreationError);
2688                        },
2689
2690                        stream::Type::QpackEncoder => {
2691                            // Only one qpack encoder stream allowed.
2692                            if self.peer_qpack_streams.encoder_stream_id.is_some()
2693                            {
2694                                conn.close(
2695                                    true,
2696                                    Error::StreamCreationError.to_wire(),
2697                                    b"Received multiple QPACK encoder streams",
2698                                )?;
2699
2700                                return Err(Error::StreamCreationError);
2701                            }
2702
2703                            close_conn_if_critical_stream_finished(
2704                                conn, stream_id,
2705                            )?;
2706
2707                            self.peer_qpack_streams.encoder_stream_id =
2708                                Some(stream_id);
2709                        },
2710
2711                        stream::Type::QpackDecoder => {
2712                            // Only one qpack decoder allowed.
2713                            if self.peer_qpack_streams.decoder_stream_id.is_some()
2714                            {
2715                                conn.close(
2716                                    true,
2717                                    Error::StreamCreationError.to_wire(),
2718                                    b"Received multiple QPACK decoder streams",
2719                                )?;
2720
2721                                return Err(Error::StreamCreationError);
2722                            }
2723
2724                            close_conn_if_critical_stream_finished(
2725                                conn, stream_id,
2726                            )?;
2727
2728                            self.peer_qpack_streams.decoder_stream_id =
2729                                Some(stream_id);
2730                        },
2731
2732                        stream::Type::Unknown => {
2733                            // Unknown stream types are ignored.
2734                            // TODO: we MAY send STOP_SENDING
2735                        },
2736
2737                        stream::Type::Request => unreachable!(),
2738                    }
2739                },
2740
2741                stream::State::PushId => {
2742                    stream.try_fill_buffer(conn)?;
2743
2744                    let varint = match stream.try_consume_varint() {
2745                        Ok(v) => v,
2746
2747                        Err(_) => continue,
2748                    };
2749
2750                    if let Err(e) = stream.set_push_id(varint) {
2751                        conn.close(true, e.to_wire(), b"")?;
2752                        return Err(e);
2753                    }
2754                },
2755
2756                stream::State::FrameType => {
2757                    stream.try_fill_buffer(conn)?;
2758
2759                    let varint = match stream.try_consume_varint() {
2760                        Ok(v) => v,
2761
2762                        Err(_) => continue,
2763                    };
2764
2765                    match stream.set_frame_type(varint) {
2766                        Err(Error::FrameUnexpected) => {
2767                            let msg = format!("Unexpected frame type {varint}");
2768
2769                            conn.close(
2770                                true,
2771                                Error::FrameUnexpected.to_wire(),
2772                                msg.as_bytes(),
2773                            )?;
2774
2775                            return Err(Error::FrameUnexpected);
2776                        },
2777
2778                        Err(e) => {
2779                            conn.close(
2780                                true,
2781                                e.to_wire(),
2782                                b"Error handling frame.",
2783                            )?;
2784
2785                            return Err(e);
2786                        },
2787
2788                        _ => (),
2789                    }
2790                },
2791
2792                stream::State::FramePayloadLen => {
2793                    stream.try_fill_buffer(conn)?;
2794
2795                    let payload_len = match stream.try_consume_varint() {
2796                        Ok(v) => v,
2797
2798                        Err(_) => continue,
2799                    };
2800
2801                    // DATA frames are handled uniquely. After this point we lose
2802                    // visibility of DATA framing, so just log here.
2803                    if Some(frame::DATA_FRAME_TYPE_ID) == stream.frame_type() {
2804                        trace!(
2805                            "{} rx frm DATA stream={} wire_payload_len={}",
2806                            conn.trace_id(),
2807                            stream_id,
2808                            payload_len
2809                        );
2810
2811                        qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
2812                            let frame = Http3Frame::Data { raw: None };
2813
2814                            let ev_data =
2815                                EventData::Http3FrameParsed(FrameParsed {
2816                                    stream_id,
2817                                    length: Some(payload_len),
2818                                    frame,
2819                                    ..Default::default()
2820                                });
2821
2822                            q.add_event_data_now(ev_data).ok();
2823                        });
2824                    }
2825
2826                    let res = stream.set_frame_payload_len(payload_len);
2827
2828                    if let Err(e) = res {
2829                        conn.close(true, e.to_wire(), b"")?;
2830                        return Err(e);
2831                    }
2832                },
2833
2834                stream::State::FramePayload => {
2835                    // Do not emit events when not polling.
2836                    if !polling {
2837                        break;
2838                    }
2839
2840                    stream.try_fill_buffer(conn)?;
2841
2842                    let (frame, payload_len) = match stream.try_consume_frame() {
2843                        Ok(frame) => frame,
2844
2845                        Err(Error::Done) => return Err(Error::Done),
2846
2847                        Err(e) => {
2848                            conn.close(
2849                                true,
2850                                e.to_wire(),
2851                                b"Error handling frame.",
2852                            )?;
2853
2854                            return Err(e);
2855                        },
2856                    };
2857
2858                    match self.process_frame(conn, stream_id, frame, payload_len)
2859                    {
2860                        Ok(ev) => return Ok(ev),
2861
2862                        Err(Error::Done) => {
2863                            // This might be a frame that is processed internally
2864                            // without needing to bubble up to the user as an
2865                            // event. Check whether the frame has FIN'd by QUIC
2866                            // to prevent trying to read again on a closed stream.
2867                            if conn.stream_finished(stream_id) {
2868                                break;
2869                            }
2870                        },
2871
2872                        Err(e) => return Err(e),
2873                    };
2874                },
2875
2876                stream::State::Data => {
2877                    // Do not emit events when not polling.
2878                    if !polling {
2879                        break;
2880                    }
2881
2882                    if !stream.try_trigger_data_event() {
2883                        break;
2884                    }
2885
2886                    return Ok((stream_id, Event::Data));
2887                },
2888
2889                stream::State::QpackInstruction => {
2890                    let mut d = [0; 4096];
2891
2892                    // Read data from the stream and discard immediately.
2893                    loop {
2894                        let (recv, fin) = conn.stream_recv(stream_id, &mut d)?;
2895
2896                        match stream.ty() {
2897                            Some(stream::Type::QpackEncoder) =>
2898                                self.peer_qpack_streams.encoder_stream_bytes +=
2899                                    recv as u64,
2900                            Some(stream::Type::QpackDecoder) =>
2901                                self.peer_qpack_streams.decoder_stream_bytes +=
2902                                    recv as u64,
2903                            _ => unreachable!(),
2904                        };
2905
2906                        if fin {
2907                            close_conn_critical_stream(conn)?;
2908                        }
2909                    }
2910                },
2911
2912                stream::State::SkipFramePayload => {
2913                    stream.try_skip_frame(conn)?;
2914
2915                    // Check whether the frame has FIN'd by QUIC to prevent
2916                    // trying to read again on a closed stream.
2917                    if conn.stream_finished(stream_id) {
2918                        break;
2919                    }
2920                },
2921
2922                stream::State::Drain => {
2923                    // Discard incoming data on the stream.
2924                    conn.stream_shutdown(
2925                        stream_id,
2926                        crate::Shutdown::Read,
2927                        0x100,
2928                    )?;
2929
2930                    break;
2931                },
2932
2933                stream::State::Finished => break,
2934            }
2935        }
2936
2937        Err(Error::Done)
2938    }
2939
2940    fn process_finished_stream(&mut self, stream_id: u64) {
2941        let stream = match self.streams.get_mut(&stream_id) {
2942            Some(v) => v,
2943
2944            None => return,
2945        };
2946
2947        if stream.state() == stream::State::Finished {
2948            return;
2949        }
2950
2951        match stream.ty() {
2952            Some(stream::Type::Request) | Some(stream::Type::Push) => {
2953                stream.finished();
2954
2955                self.finished_streams.push_back(stream_id);
2956            },
2957            Some(stream::Type::Unknown) | None => {
2958                self.streams.remove(&stream_id);
2959            },
2960            // Closing any of the critical streams leads to connection close,
2961            // so there is no need for any cleanup actions here.
2962            Some(stream::Type::Control) |
2963            Some(stream::Type::QpackEncoder) |
2964            Some(stream::Type::QpackDecoder) => (),
2965        };
2966    }
2967
2968    fn finish_local_stream<F: BufFactory>(
2969        &mut self, conn: &super::Connection<F>, stream_id: u64,
2970        initialize_local: bool,
2971    ) {
2972        let hash_map::Entry::Occupied(mut stream) = self.streams.entry(stream_id)
2973        else {
2974            return;
2975        };
2976
2977        {
2978            let stream = stream.get_mut();
2979
2980            if initialize_local {
2981                stream.initialize_local();
2982            }
2983
2984            stream.finish_local();
2985        }
2986
2987        if conn.stream_finished(stream_id) {
2988            stream.remove();
2989        }
2990    }
2991
2992    fn remove_local_finished_stream(&mut self, stream_id: u64) {
2993        if let hash_map::Entry::Occupied(stream) = self.streams.entry(stream_id) {
2994            if stream.get().local_finished() {
2995                stream.remove();
2996            }
2997        }
2998    }
2999
3000    fn pop_finished_stream<F: BufFactory>(
3001        &mut self, conn: &mut super::Connection<F>,
3002    ) -> Option<(u64, Event)> {
3003        let finished = self.finished_streams.pop_front()?;
3004
3005        self.remove_local_finished_stream(finished);
3006
3007        if conn.stream_readable(finished) {
3008            // The stream is finished, but is still readable, it may indicate
3009            // that there is a pending error, such as reset.
3010            if let Err(crate::Error::StreamReset(e)) =
3011                conn.stream_recv(finished, &mut [])
3012            {
3013                return Some((finished, Event::Reset(e)));
3014            }
3015        }
3016
3017        Some((finished, Event::Finished))
3018    }
3019
3020    fn process_frame<F: BufFactory>(
3021        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
3022        frame: frame::Frame, payload_len: u64,
3023    ) -> Result<(u64, Event)> {
3024        trace!(
3025            "{} rx frm {:?} stream={} payload_len={}",
3026            conn.trace_id(),
3027            frame,
3028            stream_id,
3029            payload_len
3030        );
3031
3032        qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
3033            // HEADERS frames are special case and will be logged below.
3034            if !matches!(frame, frame::Frame::Headers { .. }) {
3035                let frame = frame.to_qlog();
3036                let ev_data = EventData::Http3FrameParsed(FrameParsed {
3037                    stream_id,
3038                    length: Some(payload_len),
3039                    frame,
3040                    ..Default::default()
3041                });
3042
3043                q.add_event_data_now(ev_data).ok();
3044            }
3045        });
3046
3047        match frame {
3048            frame::Frame::Settings {
3049                max_field_section_size,
3050                qpack_max_table_capacity,
3051                qpack_blocked_streams,
3052                connect_protocol_enabled,
3053                h3_datagram,
3054                additional_settings,
3055                raw,
3056                ..
3057            } => {
3058                self.peer_settings = ConnectionSettings {
3059                    max_field_section_size,
3060                    qpack_max_table_capacity,
3061                    qpack_blocked_streams,
3062                    connect_protocol_enabled,
3063                    h3_datagram,
3064                    additional_settings,
3065                    raw,
3066                };
3067
3068                if let Some(1) = h3_datagram {
3069                    // The peer MUST have also enabled DATAGRAM with a TP
3070                    if conn.dgram_max_writable_len().is_none() {
3071                        conn.close(
3072                            true,
3073                            Error::SettingsError.to_wire(),
3074                            b"H3_DATAGRAM sent with value 1 but max_datagram_frame_size TP not set.",
3075                        )?;
3076
3077                        return Err(Error::SettingsError);
3078                    }
3079                }
3080            },
3081
3082            frame::Frame::Headers { header_block } => {
3083                // Servers reject too many HEADERS frames.
3084                if let Some(s) = self.streams.get_mut(&stream_id) {
3085                    if self.is_server && s.headers_received_count() == 2 {
3086                        conn.close(
3087                            true,
3088                            Error::FrameUnexpected.to_wire(),
3089                            b"Too many HEADERS frames",
3090                        )?;
3091                        return Err(Error::FrameUnexpected);
3092                    }
3093
3094                    s.increment_headers_received();
3095                }
3096
3097                // Use "infinite" as default value for max_field_section_size if
3098                // it is not configured by the application.
3099                let max_size = self
3100                    .local_settings
3101                    .max_field_section_size
3102                    .unwrap_or(u64::MAX);
3103
3104                let headers = match self
3105                    .qpack_decoder
3106                    .decode(&header_block[..], max_size)
3107                {
3108                    Ok(v) => v,
3109
3110                    Err(e) => {
3111                        let e = match e {
3112                            qpack::Error::HeaderListTooLarge =>
3113                                Error::ExcessiveLoad,
3114
3115                            _ => Error::QpackDecompressionFailed,
3116                        };
3117
3118                        conn.close(true, e.to_wire(), b"Error parsing headers.")?;
3119
3120                        return Err(e);
3121                    },
3122                };
3123
3124                qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
3125                    let qlog_headers = headers
3126                        .iter()
3127                        .map(|h| qlog::events::http3::HttpHeader {
3128                            name: Some(
3129                                String::from_utf8_lossy(h.name()).into_owned(),
3130                            ),
3131                            name_bytes: None,
3132                            value: Some(
3133                                String::from_utf8_lossy(h.value()).into_owned(),
3134                            ),
3135                            value_bytes: None,
3136                        })
3137                        .collect();
3138
3139                    let frame = Http3Frame::Headers {
3140                        headers: qlog_headers,
3141                        raw: None,
3142                    };
3143
3144                    let ev_data = EventData::Http3FrameParsed(FrameParsed {
3145                        stream_id,
3146                        length: Some(payload_len),
3147                        frame,
3148                        ..Default::default()
3149                    });
3150
3151                    q.add_event_data_now(ev_data).ok();
3152                });
3153
3154                let more_frames = !conn.stream_finished(stream_id);
3155
3156                return Ok((stream_id, Event::Headers {
3157                    list: headers,
3158                    more_frames,
3159                }));
3160            },
3161
3162            frame::Frame::Data { .. } => {
3163                // Do nothing. The Data event is returned separately.
3164            },
3165
3166            frame::Frame::GoAway { id } => {
3167                if !self.is_server && id % 4 != 0 {
3168                    conn.close(
3169                        true,
3170                        Error::FrameUnexpected.to_wire(),
3171                        b"GOAWAY received with ID of non-request stream",
3172                    )?;
3173
3174                    return Err(Error::IdError);
3175                }
3176
3177                if let Some(received_id) = self.peer_goaway_id {
3178                    if id > received_id {
3179                        conn.close(
3180                            true,
3181                            Error::IdError.to_wire(),
3182                            b"GOAWAY received with ID larger than previously received",
3183                        )?;
3184
3185                        return Err(Error::IdError);
3186                    }
3187                }
3188
3189                self.peer_goaway_id = Some(id);
3190
3191                return Ok((id, Event::GoAway));
3192            },
3193
3194            frame::Frame::MaxPushId { push_id } => {
3195                if !self.is_server {
3196                    conn.close(
3197                        true,
3198                        Error::FrameUnexpected.to_wire(),
3199                        b"MAX_PUSH_ID received by client",
3200                    )?;
3201
3202                    return Err(Error::FrameUnexpected);
3203                }
3204
3205                if push_id < self.max_push_id {
3206                    conn.close(
3207                        true,
3208                        Error::IdError.to_wire(),
3209                        b"MAX_PUSH_ID reduced limit",
3210                    )?;
3211
3212                    return Err(Error::IdError);
3213                }
3214
3215                self.max_push_id = push_id;
3216            },
3217
3218            frame::Frame::PushPromise { .. } => {
3219                if self.is_server {
3220                    conn.close(
3221                        true,
3222                        Error::FrameUnexpected.to_wire(),
3223                        b"PUSH_PROMISE received by server",
3224                    )?;
3225
3226                    return Err(Error::FrameUnexpected);
3227                }
3228
3229                if !stream_id.is_multiple_of(4) {
3230                    conn.close(
3231                        true,
3232                        Error::FrameUnexpected.to_wire(),
3233                        b"PUSH_PROMISE received on non-request stream",
3234                    )?;
3235
3236                    return Err(Error::FrameUnexpected);
3237                }
3238
3239                // TODO: implement more checks and PUSH_PROMISE event
3240            },
3241
3242            frame::Frame::CancelPush { .. } => {
3243                // TODO: implement CANCEL_PUSH frame
3244            },
3245
3246            frame::Frame::PriorityUpdateRequest {
3247                prioritized_element_id,
3248                priority_field_value,
3249            } => {
3250                if !self.is_server {
3251                    conn.close(
3252                        true,
3253                        Error::FrameUnexpected.to_wire(),
3254                        b"PRIORITY_UPDATE received by client",
3255                    )?;
3256
3257                    return Err(Error::FrameUnexpected);
3258                }
3259
3260                if prioritized_element_id % 4 != 0 {
3261                    conn.close(
3262                        true,
3263                        Error::FrameUnexpected.to_wire(),
3264                        b"PRIORITY_UPDATE for request stream type with wrong ID",
3265                    )?;
3266
3267                    return Err(Error::FrameUnexpected);
3268                }
3269
3270                if prioritized_element_id > conn.streams.max_streams_bidi() * 4 {
3271                    conn.close(
3272                        true,
3273                        Error::IdError.to_wire(),
3274                        b"PRIORITY_UPDATE for request stream beyond max streams limit",
3275                    )?;
3276
3277                    return Err(Error::IdError);
3278                }
3279
3280                // PRIORITY_UPDATE can arrive before the request stream exists,
3281                // so a missing transport stream is allowed. Ignore updates only
3282                // once the transport stream was collected or both transport
3283                // directions are finished.
3284                if conn.stream_closed(prioritized_element_id) {
3285                    return Err(Error::Done);
3286                }
3287
3288                // If the stream did not yet exist, create it and store.
3289                let stream = self
3290                    .streams
3291                    .entry(prioritized_element_id)
3292                    .or_insert_with(|| {
3293                        <stream::Stream>::new(
3294                            prioritized_element_id,
3295                            false,
3296                            self.local_settings.max_field_section_size.unwrap_or(
3297                                SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT,
3298                            ),
3299                            self.max_priority_update_size,
3300                        )
3301                    });
3302
3303                let had_priority_update = stream.has_last_priority_update();
3304                stream.set_last_priority_update(Some(priority_field_value));
3305
3306                // Only trigger the event when there wasn't already a stored
3307                // PRIORITY_UPDATE.
3308                if !had_priority_update {
3309                    return Ok((prioritized_element_id, Event::PriorityUpdate));
3310                } else {
3311                    return Err(Error::Done);
3312                }
3313            },
3314
3315            frame::Frame::PriorityUpdatePush {
3316                prioritized_element_id,
3317                ..
3318            } => {
3319                if !self.is_server {
3320                    conn.close(
3321                        true,
3322                        Error::FrameUnexpected.to_wire(),
3323                        b"PRIORITY_UPDATE received by client",
3324                    )?;
3325
3326                    return Err(Error::FrameUnexpected);
3327                }
3328
3329                if prioritized_element_id % 3 != 0 {
3330                    conn.close(
3331                        true,
3332                        Error::FrameUnexpected.to_wire(),
3333                        b"PRIORITY_UPDATE for push stream type with wrong ID",
3334                    )?;
3335
3336                    return Err(Error::FrameUnexpected);
3337                }
3338
3339                // TODO: we only implement this if we implement server push
3340            },
3341
3342            frame::Frame::Unknown { .. } => (),
3343        }
3344
3345        Err(Error::Done)
3346    }
3347
3348    /// Collects and returns statistics about the connection.
3349    #[inline]
3350    pub fn stats(&self) -> Stats {
3351        Stats {
3352            qpack_encoder_stream_recv_bytes: self
3353                .peer_qpack_streams
3354                .encoder_stream_bytes,
3355            qpack_decoder_stream_recv_bytes: self
3356                .peer_qpack_streams
3357                .decoder_stream_bytes,
3358        }
3359    }
3360}
3361
3362/// Generates an HTTP/3 GREASE variable length integer.
3363pub fn grease_value() -> u64 {
3364    let n = super::rand::rand_u64_uniform(148_764_065_110_560_899);
3365    31 * n + 33
3366}
3367
3368#[doc(hidden)]
3369#[cfg(any(test, feature = "internal"))]
3370pub mod testing {
3371    use super::*;
3372
3373    use crate::test_utils;
3374    use crate::DefaultBufFactory;
3375
3376    /// Session is an HTTP/3 test helper structure. It holds a client, server
3377    /// and pipe that allows them to communicate.
3378    ///
3379    /// `default()` creates a session with some sensible default
3380    /// configuration. `with_configs()` allows for providing a specific
3381    /// configuration.
3382    ///
3383    /// `handshake()` performs all the steps needed to establish an HTTP/3
3384    /// connection.
3385    ///
3386    /// Some utility functions are provided that make it less verbose to send
3387    /// request, responses and individual headers. The full quiche API remains
3388    /// available for any test that need to do unconventional things (such as
3389    /// bad behaviour that triggers errors).
3390    pub struct Session<F = DefaultBufFactory>
3391    where
3392        F: BufFactory,
3393    {
3394        pub pipe: test_utils::Pipe<F>,
3395        pub client: Connection,
3396        pub server: Connection,
3397    }
3398
3399    impl Session {
3400        pub fn new() -> Result<Session> {
3401            Session::<DefaultBufFactory>::new_with_buf()
3402        }
3403
3404        pub fn with_configs(
3405            config: &mut crate::Config, h3_config: &Config,
3406        ) -> Result<Session> {
3407            Session::<DefaultBufFactory>::with_configs_and_buf(config, h3_config)
3408        }
3409
3410        pub fn default_configs() -> Result<(crate::Config, Config)> {
3411            fn path_relative_to_manifest_dir(path: &str) -> String {
3412                std::fs::canonicalize(
3413                    std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(path),
3414                )
3415                .unwrap()
3416                .to_string_lossy()
3417                .into_owned()
3418            }
3419
3420            let mut config = crate::Config::new(crate::PROTOCOL_VERSION)?;
3421            config.load_cert_chain_from_pem_file(
3422                &path_relative_to_manifest_dir("examples/cert.crt"),
3423            )?;
3424            config.load_priv_key_from_pem_file(
3425                &path_relative_to_manifest_dir("examples/cert.key"),
3426            )?;
3427            config.set_application_protos(&[b"h3"])?;
3428            config.set_initial_max_data(1500);
3429            config.set_initial_max_stream_data_bidi_local(150);
3430            config.set_initial_max_stream_data_bidi_remote(150);
3431            config.set_initial_max_stream_data_uni(150);
3432            config.set_initial_max_streams_bidi(5);
3433            config.set_initial_max_streams_uni(5);
3434            config.verify_peer(false);
3435            config.enable_dgram(true, 3, 3);
3436            config.set_ack_delay_exponent(8);
3437
3438            let h3_config = Config::new()?;
3439            Ok((config, h3_config))
3440        }
3441    }
3442
3443    impl<F: BufFactory> Session<F> {
3444        pub fn new_with_buf() -> Result<Session<F>> {
3445            let (mut config, h3_config) = Session::default_configs()?;
3446            Session::with_configs_and_buf(&mut config, &h3_config)
3447        }
3448
3449        pub fn with_configs_and_buf(
3450            config: &mut crate::Config, h3_config: &Config,
3451        ) -> Result<Session<F>> {
3452            let pipe = test_utils::Pipe::with_config_and_buf(config)?;
3453            let client_dgram = pipe.client.dgram_enabled();
3454            let server_dgram = pipe.server.dgram_enabled();
3455            Ok(Session {
3456                pipe,
3457                client: Connection::new(h3_config, false, client_dgram)?,
3458                server: Connection::new(h3_config, true, server_dgram)?,
3459            })
3460        }
3461
3462        /// Do the HTTP/3 handshake so both ends are in sane initial state.
3463        pub fn handshake(&mut self) -> Result<()> {
3464            self.pipe.handshake()?;
3465
3466            // Client streams.
3467            self.client.send_settings(&mut self.pipe.client)?;
3468            self.pipe.advance().ok();
3469
3470            self.client
3471                .open_qpack_encoder_stream(&mut self.pipe.client)?;
3472            self.pipe.advance().ok();
3473
3474            self.client
3475                .open_qpack_decoder_stream(&mut self.pipe.client)?;
3476            self.pipe.advance().ok();
3477
3478            if self.pipe.client.grease {
3479                self.client.open_grease_stream(&mut self.pipe.client)?;
3480            }
3481
3482            self.pipe.advance().ok();
3483
3484            // Server streams.
3485            self.server.send_settings(&mut self.pipe.server)?;
3486            self.pipe.advance().ok();
3487
3488            self.server
3489                .open_qpack_encoder_stream(&mut self.pipe.server)?;
3490            self.pipe.advance().ok();
3491
3492            self.server
3493                .open_qpack_decoder_stream(&mut self.pipe.server)?;
3494            self.pipe.advance().ok();
3495
3496            if self.pipe.server.grease {
3497                self.server.open_grease_stream(&mut self.pipe.server)?;
3498            }
3499
3500            self.advance().ok();
3501
3502            while self.client.poll(&mut self.pipe.client).is_ok() {
3503                // Do nothing.
3504            }
3505
3506            while self.server.poll(&mut self.pipe.server).is_ok() {
3507                // Do nothing.
3508            }
3509
3510            Ok(())
3511        }
3512
3513        /// Advances the session pipe over the buffer.
3514        pub fn advance(&mut self) -> crate::Result<()> {
3515            self.pipe.advance()
3516        }
3517
3518        /// Polls the client for events.
3519        pub fn poll_client(&mut self) -> Result<(u64, Event)> {
3520            self.client.poll(&mut self.pipe.client)
3521        }
3522
3523        /// Polls the server for events.
3524        pub fn poll_server(&mut self) -> Result<(u64, Event)> {
3525            self.server.poll(&mut self.pipe.server)
3526        }
3527
3528        /// Sends a request from client with default headers.
3529        ///
3530        /// On success it returns the newly allocated stream and the headers.
3531        pub fn send_request(&mut self, fin: bool) -> Result<(u64, Vec<Header>)> {
3532            let req = vec![
3533                Header::new(b":method", b"GET"),
3534                Header::new(b":scheme", b"https"),
3535                Header::new(b":authority", b"quic.tech"),
3536                Header::new(b":path", b"/test"),
3537                Header::new(b"user-agent", b"quiche-test"),
3538            ];
3539
3540            let stream =
3541                self.client.send_request(&mut self.pipe.client, &req, fin)?;
3542
3543            self.advance().ok();
3544
3545            Ok((stream, req))
3546        }
3547
3548        /// Sends a response from server with default headers.
3549        ///
3550        /// On success it returns the headers.
3551        pub fn send_response(
3552            &mut self, stream: u64, fin: bool,
3553        ) -> Result<Vec<Header>> {
3554            let resp = vec![
3555                Header::new(b":status", b"200"),
3556                Header::new(b"server", b"quiche-test"),
3557            ];
3558
3559            self.server.send_response(
3560                &mut self.pipe.server,
3561                stream,
3562                &resp,
3563                fin,
3564            )?;
3565
3566            self.advance().ok();
3567
3568            Ok(resp)
3569        }
3570
3571        /// Sends some default payload from client.
3572        ///
3573        /// On success it returns the payload.
3574        pub fn send_body_client(
3575            &mut self, stream: u64, fin: bool,
3576        ) -> Result<Vec<u8>> {
3577            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3578
3579            self.client
3580                .send_body(&mut self.pipe.client, stream, &bytes, fin)?;
3581
3582            self.advance().ok();
3583
3584            Ok(bytes)
3585        }
3586
3587        /// Fetches DATA payload from the server.
3588        ///
3589        /// On success it returns the number of bytes received.
3590        pub fn recv_body_client(
3591            &mut self, stream: u64, buf: &mut [u8],
3592        ) -> Result<usize> {
3593            self.client.recv_body(&mut self.pipe.client, stream, buf)
3594        }
3595
3596        /// Fetches DATA payload from the server.
3597        ///
3598        /// On success it returns the number of bytes received.
3599        pub fn recv_body_buf_client<B: bytes::BufMut>(
3600            &mut self, stream: u64, buf: B,
3601        ) -> Result<usize> {
3602            self.client
3603                .recv_body_buf(&mut self.pipe.client, stream, buf)
3604        }
3605
3606        /// Sends some default payload from server.
3607        ///
3608        /// On success it returns the payload.
3609        pub fn send_body_server(
3610            &mut self, stream: u64, fin: bool,
3611        ) -> Result<Vec<u8>> {
3612            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3613
3614            self.server
3615                .send_body(&mut self.pipe.server, stream, &bytes, fin)?;
3616
3617            self.advance().ok();
3618
3619            Ok(bytes)
3620        }
3621
3622        /// Fetches DATA payload from the client.
3623        ///
3624        /// On success it returns the number of bytes received.
3625        pub fn recv_body_server(
3626            &mut self, stream: u64, buf: &mut [u8],
3627        ) -> Result<usize> {
3628            self.server.recv_body(&mut self.pipe.server, stream, buf)
3629        }
3630
3631        /// Fetches DATA payload from the client.
3632        ///
3633        /// On success it returns the number of bytes received.
3634        pub fn recv_body_buf_server<B: bytes::BufMut>(
3635            &mut self, stream: u64, buf: B,
3636        ) -> Result<usize> {
3637            self.server
3638                .recv_body_buf(&mut self.pipe.server, stream, buf)
3639        }
3640
3641        /// Sends a single HTTP/3 frame from the client.
3642        pub fn send_frame_client(
3643            &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3644        ) -> Result<()> {
3645            let mut d = [42; 65535];
3646
3647            let mut b = octets::OctetsMut::with_slice(&mut d);
3648
3649            frame.to_bytes(&mut b)?;
3650
3651            let off = b.off();
3652            self.pipe.client.stream_send(stream_id, &d[..off], fin)?;
3653
3654            self.advance().ok();
3655
3656            Ok(())
3657        }
3658
3659        /// Send an HTTP/3 DATAGRAM with default data from the client.
3660        ///
3661        /// On success it returns the data.
3662        pub fn send_dgram_client(&mut self, flow_id: u64) -> Result<Vec<u8>> {
3663            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3664            let len = octets::varint_len(flow_id) + bytes.len();
3665            let mut d = vec![0; len];
3666            let mut b = octets::OctetsMut::with_slice(&mut d);
3667
3668            b.put_varint(flow_id)?;
3669            b.put_bytes(&bytes)?;
3670
3671            self.pipe.client.dgram_send(&d)?;
3672
3673            self.advance().ok();
3674
3675            Ok(bytes)
3676        }
3677
3678        /// Receives an HTTP/3 DATAGRAM from the server.
3679        ///
3680        /// On success it returns the DATAGRAM length, flow ID and flow ID
3681        /// length.
3682        pub fn recv_dgram_client(
3683            &mut self, buf: &mut [u8],
3684        ) -> Result<(usize, u64, usize)> {
3685            let len = self.pipe.client.dgram_recv(buf)?;
3686            let mut b = octets::Octets::with_slice(buf);
3687            let flow_id = b.get_varint()?;
3688
3689            Ok((len, flow_id, b.off()))
3690        }
3691
3692        /// Send an HTTP/3 DATAGRAM with default data from the server
3693        ///
3694        /// On success it returns the data.
3695        pub fn send_dgram_server(&mut self, flow_id: u64) -> Result<Vec<u8>> {
3696            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3697            let len = octets::varint_len(flow_id) + bytes.len();
3698            let mut d = vec![0; len];
3699            let mut b = octets::OctetsMut::with_slice(&mut d);
3700
3701            b.put_varint(flow_id)?;
3702            b.put_bytes(&bytes)?;
3703
3704            self.pipe.server.dgram_send(&d)?;
3705
3706            self.advance().ok();
3707
3708            Ok(bytes)
3709        }
3710
3711        /// Receives an HTTP/3 DATAGRAM from the client.
3712        ///
3713        /// On success it returns the DATAGRAM length, flow ID and flow ID
3714        /// length.
3715        pub fn recv_dgram_server(
3716            &mut self, buf: &mut [u8],
3717        ) -> Result<(usize, u64, usize)> {
3718            let len = self.pipe.server.dgram_recv(buf)?;
3719            let mut b = octets::Octets::with_slice(buf);
3720            let flow_id = b.get_varint()?;
3721
3722            Ok((len, flow_id, b.off()))
3723        }
3724
3725        /// Sends a single HTTP/3 frame from the server.
3726        pub fn send_frame_server(
3727            &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3728        ) -> Result<()> {
3729            let mut d = [42; 65535];
3730
3731            let mut b = octets::OctetsMut::with_slice(&mut d);
3732
3733            frame.to_bytes(&mut b)?;
3734
3735            let off = b.off();
3736            self.pipe.server.stream_send(stream_id, &d[..off], fin)?;
3737
3738            self.advance().ok();
3739
3740            Ok(())
3741        }
3742
3743        /// Sends an arbitrary buffer of HTTP/3 stream data from the client.
3744        pub fn send_arbitrary_stream_data_client(
3745            &mut self, data: &[u8], stream_id: u64, fin: bool,
3746        ) -> Result<()> {
3747            self.pipe.client.stream_send(stream_id, data, fin)?;
3748
3749            self.advance().ok();
3750
3751            Ok(())
3752        }
3753
3754        /// Sends an arbitrary buffer of HTTP/3 stream data from the server.
3755        pub fn send_arbitrary_stream_data_server(
3756            &mut self, data: &[u8], stream_id: u64, fin: bool,
3757        ) -> Result<()> {
3758            self.pipe.server.stream_send(stream_id, data, fin)?;
3759
3760            self.advance().ok();
3761
3762            Ok(())
3763        }
3764    }
3765}
3766
3767#[cfg(test)]
3768mod tests {
3769    use bytes::BufMut as _;
3770
3771    use super::*;
3772
3773    use super::testing::*;
3774
3775    #[test]
3776    /// Make sure that random GREASE values is within the specified limit.
3777    fn grease_value_in_varint_limit() {
3778        assert!(grease_value() < 2u64.pow(62) - 1);
3779    }
3780
3781    #[test]
3782    fn h3_handshake_0rtt() {
3783        let mut buf = [0; 65535];
3784
3785        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
3786        config
3787            .load_cert_chain_from_pem_file("examples/cert.crt")
3788            .unwrap();
3789        config
3790            .load_priv_key_from_pem_file("examples/cert.key")
3791            .unwrap();
3792        config
3793            .set_application_protos(&[b"proto1", b"proto2"])
3794            .unwrap();
3795        config.set_initial_max_data(30);
3796        config.set_initial_max_stream_data_bidi_local(15);
3797        config.set_initial_max_stream_data_bidi_remote(15);
3798        config.set_initial_max_stream_data_uni(15);
3799        config.set_initial_max_streams_bidi(3);
3800        config.set_initial_max_streams_uni(3);
3801        config.enable_early_data();
3802        config.verify_peer(false);
3803
3804        let h3_config = Config::new().unwrap();
3805
3806        // Perform initial handshake.
3807        let mut pipe = crate::test_utils::Pipe::with_config(&mut config).unwrap();
3808        assert_eq!(pipe.handshake(), Ok(()));
3809
3810        // Extract session,
3811        let session = pipe.client.session().unwrap();
3812
3813        // Configure session on new connection.
3814        let mut pipe = crate::test_utils::Pipe::with_config(&mut config).unwrap();
3815        assert_eq!(pipe.client.set_session(session), Ok(()));
3816
3817        // Can't create an H3 connection until the QUIC connection is determined
3818        // to have made sufficient early data progress.
3819        assert!(matches!(
3820            Connection::with_transport(&mut pipe.client, &h3_config),
3821            Err(Error::InternalError)
3822        ));
3823
3824        // Client sends initial flight.
3825        let (len, _) = pipe.client.send(&mut buf).unwrap();
3826
3827        // Now an H3 connection can be created.
3828        assert!(Connection::with_transport(&mut pipe.client, &h3_config).is_ok());
3829        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
3830
3831        // Client sends 0-RTT packet.
3832        let pkt_type = crate::packet::Type::ZeroRTT;
3833
3834        let frames = [crate::frame::Frame::Stream {
3835            stream_id: 6,
3836            data: <crate::range_buf::RangeBuf>::from(b"aaaaa", 0, true),
3837        }];
3838
3839        assert_eq!(
3840            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
3841            Ok(1200)
3842        );
3843
3844        assert_eq!(pipe.server.undecryptable_pkts.len(), 0);
3845
3846        // 0-RTT stream data is readable.
3847        let mut r = pipe.server.readable();
3848        assert_eq!(r.next(), Some(6));
3849        assert_eq!(r.next(), None);
3850
3851        let mut b = [0; 15];
3852        assert_eq!(pipe.server.stream_recv(6, &mut b), Ok((5, true)));
3853        assert_eq!(&b[..5], b"aaaaa");
3854    }
3855
3856    #[test]
3857    /// Send a request with no body, get a response with no body.
3858    fn request_no_body_response_no_body() {
3859        let mut s = Session::new().unwrap();
3860        s.handshake().unwrap();
3861
3862        let (stream, req) = s.send_request(true).unwrap();
3863
3864        assert_eq!(stream, 0);
3865
3866        let ev_headers = Event::Headers {
3867            list: req,
3868            more_frames: false,
3869        };
3870
3871        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3872        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3873
3874        let resp = s.send_response(stream, true).unwrap();
3875
3876        let ev_headers = Event::Headers {
3877            list: resp,
3878            more_frames: false,
3879        };
3880
3881        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3882        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3883        assert_eq!(s.poll_client(), Err(Error::Done));
3884    }
3885
3886    #[test]
3887    /// Send a request with no body, get a response with one DATA frame.
3888    fn request_no_body_response_one_chunk() {
3889        let mut s = Session::new().unwrap();
3890        s.handshake().unwrap();
3891
3892        let (stream, req) = s.send_request(true).unwrap();
3893        assert_eq!(stream, 0);
3894
3895        let ev_headers = Event::Headers {
3896            list: req,
3897            more_frames: false,
3898        };
3899
3900        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3901
3902        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3903
3904        let resp = s.send_response(stream, false).unwrap();
3905
3906        let body = s.send_body_server(stream, true).unwrap();
3907
3908        let mut recv_buf = vec![0; body.len()];
3909
3910        let ev_headers = Event::Headers {
3911            list: resp,
3912            more_frames: true,
3913        };
3914
3915        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3916
3917        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
3918        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
3919
3920        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3921        assert_eq!(s.poll_client(), Err(Error::Done));
3922    }
3923
3924    #[test]
3925    /// Send a request with no body, get a response with multiple DATA frames.
3926    fn request_no_body_response_many_chunks() {
3927        let mut s = Session::new().unwrap();
3928        s.handshake().unwrap();
3929
3930        let (stream, req) = s.send_request(true).unwrap();
3931
3932        let ev_headers = Event::Headers {
3933            list: req,
3934            more_frames: false,
3935        };
3936
3937        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3938        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3939
3940        let total_data_frames = 4;
3941
3942        let resp = s.send_response(stream, false).unwrap();
3943
3944        for _ in 0..total_data_frames - 1 {
3945            s.send_body_server(stream, false).unwrap();
3946        }
3947
3948        let body = s.send_body_server(stream, true).unwrap();
3949
3950        let mut recv_buf = vec![0; body.len()];
3951
3952        let ev_headers = Event::Headers {
3953            list: resp,
3954            more_frames: true,
3955        };
3956
3957        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3958        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
3959        assert_eq!(s.poll_client(), Err(Error::Done));
3960
3961        for _ in 0..total_data_frames {
3962            assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
3963        }
3964
3965        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3966        assert_eq!(s.poll_client(), Err(Error::Done));
3967    }
3968
3969    #[test]
3970    /// Send a request with no body, get a response with multiple DATA frames.
3971    fn request_no_body_response_many_chunks_with_buf() {
3972        let (mut config, h3_config) = Session::default_configs().unwrap();
3973        // we don't want to be limited by flow or cong. control
3974        config.set_initial_congestion_window_packets(100);
3975        config.set_initial_max_data(200_000);
3976        config.set_initial_max_stream_data_bidi_local(200_000);
3977        config.set_initial_max_stream_data_bidi_remote(200_000);
3978        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
3979        s.handshake().unwrap();
3980
3981        let (stream, req) = s.send_request(true).unwrap();
3982
3983        let ev_headers = Event::Headers {
3984            list: req,
3985            more_frames: false,
3986        };
3987
3988        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3989        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3990
3991        let total_data_frames = 4;
3992
3993        // Use a large body
3994        let data = vec![0xab_u8; 16 * 1024];
3995
3996        let resp = s.send_response(stream, false).unwrap();
3997
3998        for _ in 0..total_data_frames - 1 {
3999            assert_eq!(
4000                s.server.send_body(&mut s.pipe.server, stream, &data, false),
4001                Ok(data.len())
4002            );
4003            s.advance().ok();
4004        }
4005
4006        s.server
4007            .send_body(&mut s.pipe.server, stream, &data, true)
4008            .unwrap();
4009        s.advance().ok();
4010
4011        let ev_headers = Event::Headers {
4012            list: resp,
4013            more_frames: true,
4014        };
4015
4016        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4017        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
4018        assert_eq!(s.poll_client(), Err(Error::Done));
4019
4020        // We expect to be able to read multiple data frames in a single call and
4021        // reads don't have to end on frame boundaries. So let's try to read
4022        // 1.5 times the amount we sent in one frame.
4023        let how_much_to_read_per_call = data.len() * 2 / 3;
4024        let mut remaining_to_read = total_data_frames * data.len();
4025        let mut recv_buf = Vec::new().limit(how_much_to_read_per_call);
4026        assert_eq!(
4027            s.recv_body_buf_client(stream, &mut recv_buf),
4028            Ok(how_much_to_read_per_call)
4029        );
4030        remaining_to_read -= how_much_to_read_per_call;
4031        assert_eq!(recv_buf.get_ref().len(), how_much_to_read_per_call);
4032
4033        while remaining_to_read > 0 {
4034            // Set a different limit for the following reads.
4035            recv_buf.set_limit(data.len());
4036            // We should either read up to the limit we set above, or to
4037            // the end of buffered data.
4038            let expected = std::cmp::min(data.len(), remaining_to_read);
4039            assert_eq!(
4040                s.recv_body_buf_client(stream, &mut recv_buf),
4041                Ok(expected)
4042            );
4043            remaining_to_read -= expected;
4044        }
4045        // We've read everything now. Ensure the Vec reflects that
4046        assert_eq!(recv_buf.get_ref().len(), total_data_frames * data.len());
4047
4048        // No more data to read.
4049        assert_eq!(
4050            s.recv_body_buf_client(stream, &mut recv_buf),
4051            Err(Error::Done)
4052        );
4053
4054        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4055        assert_eq!(s.poll_client(), Err(Error::Done));
4056    }
4057
4058    #[test]
4059    /// Send a request with one DATA frame, get a response with no body.
4060    fn request_one_chunk_response_no_body() {
4061        let mut s = Session::new().unwrap();
4062        s.handshake().unwrap();
4063
4064        let (stream, req) = s.send_request(false).unwrap();
4065
4066        let body = s.send_body_client(stream, true).unwrap();
4067
4068        let mut recv_buf = vec![0; body.len()];
4069
4070        let ev_headers = Event::Headers {
4071            list: req,
4072            more_frames: true,
4073        };
4074
4075        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4076
4077        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4078        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4079
4080        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4081
4082        let resp = s.send_response(stream, true).unwrap();
4083
4084        let ev_headers = Event::Headers {
4085            list: resp,
4086            more_frames: false,
4087        };
4088
4089        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4090        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4091    }
4092
4093    #[test]
4094    /// Send a request with multiple DATA frames, get a response with no body.
4095    fn request_many_chunks_response_no_body() {
4096        let mut s = Session::new().unwrap();
4097        s.handshake().unwrap();
4098
4099        let (stream, req) = s.send_request(false).unwrap();
4100
4101        let total_data_frames = 4;
4102
4103        for _ in 0..total_data_frames - 1 {
4104            s.send_body_client(stream, false).unwrap();
4105        }
4106
4107        let body = s.send_body_client(stream, true).unwrap();
4108
4109        let mut recv_buf = vec![0; body.len()];
4110
4111        let ev_headers = Event::Headers {
4112            list: req,
4113            more_frames: true,
4114        };
4115
4116        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4117        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4118        assert_eq!(s.poll_server(), Err(Error::Done));
4119
4120        for _ in 0..total_data_frames {
4121            assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4122        }
4123
4124        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4125
4126        let resp = s.send_response(stream, true).unwrap();
4127
4128        let ev_headers = Event::Headers {
4129            list: resp,
4130            more_frames: false,
4131        };
4132
4133        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4134        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4135    }
4136
4137    #[test]
4138    /// Send a request with multiple DATA frames, get a response with one DATA
4139    /// frame.
4140    fn many_requests_many_chunks_response_one_chunk() {
4141        let mut s = Session::new().unwrap();
4142        s.handshake().unwrap();
4143
4144        let mut reqs = Vec::new();
4145
4146        let (stream1, req1) = s.send_request(false).unwrap();
4147        assert_eq!(stream1, 0);
4148        reqs.push(req1);
4149
4150        let (stream2, req2) = s.send_request(false).unwrap();
4151        assert_eq!(stream2, 4);
4152        reqs.push(req2);
4153
4154        let (stream3, req3) = s.send_request(false).unwrap();
4155        assert_eq!(stream3, 8);
4156        reqs.push(req3);
4157
4158        let body = s.send_body_client(stream1, false).unwrap();
4159        s.send_body_client(stream2, false).unwrap();
4160        s.send_body_client(stream3, false).unwrap();
4161
4162        let mut recv_buf = vec![0; body.len()];
4163
4164        // Reverse order of writes.
4165
4166        s.send_body_client(stream3, true).unwrap();
4167        s.send_body_client(stream2, true).unwrap();
4168        s.send_body_client(stream1, true).unwrap();
4169
4170        let (_, ev) = s.poll_server().unwrap();
4171        let ev_headers = Event::Headers {
4172            list: reqs[0].clone(),
4173            more_frames: true,
4174        };
4175        assert_eq!(ev, ev_headers);
4176
4177        let (_, ev) = s.poll_server().unwrap();
4178        let ev_headers = Event::Headers {
4179            list: reqs[1].clone(),
4180            more_frames: true,
4181        };
4182        assert_eq!(ev, ev_headers);
4183
4184        let (_, ev) = s.poll_server().unwrap();
4185        let ev_headers = Event::Headers {
4186            list: reqs[2].clone(),
4187            more_frames: true,
4188        };
4189        assert_eq!(ev, ev_headers);
4190
4191        assert_eq!(s.poll_server(), Ok((0, Event::Data)));
4192        assert_eq!(s.recv_body_server(0, &mut recv_buf), Ok(body.len()));
4193        assert_eq!(s.poll_client(), Err(Error::Done));
4194        assert_eq!(s.recv_body_server(0, &mut recv_buf), Ok(body.len()));
4195        assert_eq!(s.poll_server(), Ok((0, Event::Finished)));
4196
4197        assert_eq!(s.poll_server(), Ok((4, Event::Data)));
4198        assert_eq!(s.recv_body_server(4, &mut recv_buf), Ok(body.len()));
4199        assert_eq!(s.poll_client(), Err(Error::Done));
4200        assert_eq!(s.recv_body_server(4, &mut recv_buf), Ok(body.len()));
4201        assert_eq!(s.poll_server(), Ok((4, Event::Finished)));
4202
4203        assert_eq!(s.poll_server(), Ok((8, Event::Data)));
4204        assert_eq!(s.recv_body_server(8, &mut recv_buf), Ok(body.len()));
4205        assert_eq!(s.poll_client(), Err(Error::Done));
4206        assert_eq!(s.recv_body_server(8, &mut recv_buf), Ok(body.len()));
4207        assert_eq!(s.poll_server(), Ok((8, Event::Finished)));
4208
4209        assert_eq!(s.poll_server(), Err(Error::Done));
4210
4211        let mut resps = Vec::new();
4212
4213        let resp1 = s.send_response(stream1, true).unwrap();
4214        resps.push(resp1);
4215
4216        let resp2 = s.send_response(stream2, true).unwrap();
4217        resps.push(resp2);
4218
4219        let resp3 = s.send_response(stream3, true).unwrap();
4220        resps.push(resp3);
4221
4222        for _ in 0..resps.len() {
4223            let (stream, ev) = s.poll_client().unwrap();
4224            let ev_headers = Event::Headers {
4225                list: resps[(stream / 4) as usize].clone(),
4226                more_frames: false,
4227            };
4228            assert_eq!(ev, ev_headers);
4229            assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4230        }
4231
4232        assert_eq!(s.poll_client(), Err(Error::Done));
4233    }
4234
4235    #[test]
4236    /// Send a request with no body, get a response with one DATA frame and an
4237    /// empty FIN after reception from the client.
4238    fn request_no_body_response_one_chunk_empty_fin() {
4239        let mut s = Session::new().unwrap();
4240        s.handshake().unwrap();
4241
4242        let (stream, req) = s.send_request(true).unwrap();
4243
4244        let ev_headers = Event::Headers {
4245            list: req,
4246            more_frames: false,
4247        };
4248
4249        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4250        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4251
4252        let resp = s.send_response(stream, false).unwrap();
4253
4254        let body = s.send_body_server(stream, false).unwrap();
4255
4256        let mut recv_buf = vec![0; body.len()];
4257
4258        let ev_headers = Event::Headers {
4259            list: resp,
4260            more_frames: true,
4261        };
4262
4263        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4264
4265        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
4266        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
4267
4268        assert_eq!(s.pipe.server.stream_send(stream, &[], true), Ok(0));
4269        s.advance().ok();
4270
4271        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4272        assert_eq!(s.poll_client(), Err(Error::Done));
4273    }
4274
4275    #[test]
4276    /// Send a request with no body, get a response with no body followed by
4277    /// GREASE that is STREAM frame with a FIN.
4278    fn request_no_body_response_no_body_with_grease() {
4279        let mut s = Session::new().unwrap();
4280        s.handshake().unwrap();
4281
4282        let (stream, req) = s.send_request(true).unwrap();
4283
4284        assert_eq!(stream, 0);
4285
4286        let ev_headers = Event::Headers {
4287            list: req,
4288            more_frames: false,
4289        };
4290
4291        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4292        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4293
4294        let resp = s.send_response(stream, false).unwrap();
4295
4296        let ev_headers = Event::Headers {
4297            list: resp,
4298            more_frames: true,
4299        };
4300
4301        // Inject a GREASE frame.
4302        let mut d = [42; 10];
4303        let mut b = octets::OctetsMut::with_slice(&mut d);
4304
4305        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
4306        s.pipe.server.stream_send(0, frame_type, false).unwrap();
4307
4308        let frame_len = b.put_varint(10).unwrap();
4309        s.pipe.server.stream_send(0, frame_len, false).unwrap();
4310
4311        s.pipe.server.stream_send(0, &d, true).unwrap();
4312
4313        s.advance().ok();
4314
4315        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4316        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4317        assert_eq!(s.poll_client(), Err(Error::Done));
4318    }
4319
4320    #[test]
4321    /// Try to send DATA frames before HEADERS.
4322    fn body_response_before_headers() {
4323        let mut s = Session::new().unwrap();
4324        s.handshake().unwrap();
4325
4326        let (stream, req) = s.send_request(true).unwrap();
4327        assert_eq!(stream, 0);
4328
4329        let ev_headers = Event::Headers {
4330            list: req,
4331            more_frames: false,
4332        };
4333
4334        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4335
4336        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4337
4338        assert_eq!(
4339            s.send_body_server(stream, true),
4340            Err(Error::FrameUnexpected)
4341        );
4342
4343        assert_eq!(s.poll_client(), Err(Error::Done));
4344    }
4345
4346    #[test]
4347    /// Try to send DATA frames on wrong streams, ensure the API returns an
4348    /// error before anything hits the transport layer.
4349    fn send_body_invalid_client_stream() {
4350        let mut s = Session::new().unwrap();
4351        s.handshake().unwrap();
4352
4353        assert_eq!(s.send_body_client(0, true), Err(Error::FrameUnexpected));
4354
4355        assert_eq!(
4356            s.send_body_client(s.client.control_stream_id.unwrap(), true),
4357            Err(Error::FrameUnexpected)
4358        );
4359
4360        assert_eq!(
4361            s.send_body_client(
4362                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
4363                true
4364            ),
4365            Err(Error::FrameUnexpected)
4366        );
4367
4368        assert_eq!(
4369            s.send_body_client(
4370                s.client.local_qpack_streams.decoder_stream_id.unwrap(),
4371                true
4372            ),
4373            Err(Error::FrameUnexpected)
4374        );
4375
4376        assert_eq!(
4377            s.send_body_client(s.client.peer_control_stream_id.unwrap(), true),
4378            Err(Error::FrameUnexpected)
4379        );
4380
4381        assert_eq!(
4382            s.send_body_client(
4383                s.client.peer_qpack_streams.encoder_stream_id.unwrap(),
4384                true
4385            ),
4386            Err(Error::FrameUnexpected)
4387        );
4388
4389        assert_eq!(
4390            s.send_body_client(
4391                s.client.peer_qpack_streams.decoder_stream_id.unwrap(),
4392                true
4393            ),
4394            Err(Error::FrameUnexpected)
4395        );
4396    }
4397
4398    #[test]
4399    /// Try to send DATA frames on wrong streams, ensure the API returns an
4400    /// error before anything hits the transport layer.
4401    fn send_body_invalid_server_stream() {
4402        let mut s = Session::new().unwrap();
4403        s.handshake().unwrap();
4404
4405        assert_eq!(s.send_body_server(0, true), Err(Error::FrameUnexpected));
4406
4407        assert_eq!(
4408            s.send_body_server(s.server.control_stream_id.unwrap(), true),
4409            Err(Error::FrameUnexpected)
4410        );
4411
4412        assert_eq!(
4413            s.send_body_server(
4414                s.server.local_qpack_streams.encoder_stream_id.unwrap(),
4415                true
4416            ),
4417            Err(Error::FrameUnexpected)
4418        );
4419
4420        assert_eq!(
4421            s.send_body_server(
4422                s.server.local_qpack_streams.decoder_stream_id.unwrap(),
4423                true
4424            ),
4425            Err(Error::FrameUnexpected)
4426        );
4427
4428        assert_eq!(
4429            s.send_body_server(s.server.peer_control_stream_id.unwrap(), true),
4430            Err(Error::FrameUnexpected)
4431        );
4432
4433        assert_eq!(
4434            s.send_body_server(
4435                s.server.peer_qpack_streams.encoder_stream_id.unwrap(),
4436                true
4437            ),
4438            Err(Error::FrameUnexpected)
4439        );
4440
4441        assert_eq!(
4442            s.send_body_server(
4443                s.server.peer_qpack_streams.decoder_stream_id.unwrap(),
4444                true
4445            ),
4446            Err(Error::FrameUnexpected)
4447        );
4448    }
4449
4450    #[test]
4451    /// Client sends request with body and trailers.
4452    fn trailers() {
4453        let mut s = Session::new().unwrap();
4454        s.handshake().unwrap();
4455
4456        let (stream, req) = s.send_request(false).unwrap();
4457
4458        let body = s.send_body_client(stream, false).unwrap();
4459
4460        let mut recv_buf = vec![0; body.len()];
4461
4462        let req_trailers = vec![Header::new(b"foo", b"bar")];
4463
4464        s.client
4465            .send_additional_headers(
4466                &mut s.pipe.client,
4467                stream,
4468                &req_trailers,
4469                true,
4470                true,
4471            )
4472            .unwrap();
4473
4474        s.advance().ok();
4475
4476        let ev_headers = Event::Headers {
4477            list: req,
4478            more_frames: true,
4479        };
4480
4481        let ev_trailers = Event::Headers {
4482            list: req_trailers,
4483            more_frames: false,
4484        };
4485
4486        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4487
4488        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4489        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4490
4491        assert_eq!(s.poll_server(), Ok((stream, ev_trailers)));
4492        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4493    }
4494
4495    #[test]
4496    /// Server responds with a 103, then a 200 with no body.
4497    fn informational_response() {
4498        let mut s = Session::new().unwrap();
4499        s.handshake().unwrap();
4500
4501        let (stream, req) = s.send_request(true).unwrap();
4502
4503        assert_eq!(stream, 0);
4504
4505        let ev_headers = Event::Headers {
4506            list: req,
4507            more_frames: false,
4508        };
4509
4510        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4511        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4512
4513        let info_resp = vec![
4514            Header::new(b":status", b"103"),
4515            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4516        ];
4517
4518        let resp = vec![
4519            Header::new(b":status", b"200"),
4520            Header::new(b"server", b"quiche-test"),
4521        ];
4522
4523        s.server
4524            .send_response(&mut s.pipe.server, stream, &info_resp, false)
4525            .unwrap();
4526
4527        s.server
4528            .send_additional_headers(
4529                &mut s.pipe.server,
4530                stream,
4531                &resp,
4532                false,
4533                true,
4534            )
4535            .unwrap();
4536
4537        s.advance().ok();
4538
4539        let ev_info_headers = Event::Headers {
4540            list: info_resp,
4541            more_frames: true,
4542        };
4543
4544        let ev_headers = Event::Headers {
4545            list: resp,
4546            more_frames: false,
4547        };
4548
4549        assert_eq!(s.poll_client(), Ok((stream, ev_info_headers)));
4550        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4551        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4552        assert_eq!(s.poll_client(), Err(Error::Done));
4553    }
4554
4555    #[test]
4556    /// Server responds with a 103, then attempts to send a 200 using
4557    /// send_response again, which should fail.
4558    fn no_multiple_response() {
4559        let mut s = Session::new().unwrap();
4560        s.handshake().unwrap();
4561
4562        let (stream, req) = s.send_request(true).unwrap();
4563
4564        assert_eq!(stream, 0);
4565
4566        let ev_headers = Event::Headers {
4567            list: req,
4568            more_frames: false,
4569        };
4570
4571        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4572        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4573
4574        let info_resp = vec![
4575            Header::new(b":status", b"103"),
4576            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4577        ];
4578
4579        let resp = vec![
4580            Header::new(b":status", b"200"),
4581            Header::new(b"server", b"quiche-test"),
4582        ];
4583
4584        s.server
4585            .send_response(&mut s.pipe.server, stream, &info_resp, false)
4586            .unwrap();
4587
4588        assert_eq!(
4589            Err(Error::FrameUnexpected),
4590            s.server
4591                .send_response(&mut s.pipe.server, stream, &resp, true)
4592        );
4593
4594        s.advance().ok();
4595
4596        let ev_info_headers = Event::Headers {
4597            list: info_resp,
4598            more_frames: true,
4599        };
4600
4601        assert_eq!(s.poll_client(), Ok((stream, ev_info_headers)));
4602        assert_eq!(s.poll_client(), Err(Error::Done));
4603    }
4604
4605    #[test]
4606    /// Server attempts to use send_additional_headers before initial response.
4607    fn no_send_additional_before_initial_response() {
4608        let mut s = Session::new().unwrap();
4609        s.handshake().unwrap();
4610
4611        let (stream, req) = s.send_request(true).unwrap();
4612
4613        assert_eq!(stream, 0);
4614
4615        let ev_headers = Event::Headers {
4616            list: req,
4617            more_frames: false,
4618        };
4619
4620        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4621        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4622
4623        let info_resp = vec![
4624            Header::new(b":status", b"103"),
4625            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4626        ];
4627
4628        assert_eq!(
4629            Err(Error::FrameUnexpected),
4630            s.server.send_additional_headers(
4631                &mut s.pipe.server,
4632                stream,
4633                &info_resp,
4634                false,
4635                false
4636            )
4637        );
4638
4639        s.advance().ok();
4640
4641        assert_eq!(s.poll_client(), Err(Error::Done));
4642    }
4643
4644    #[test]
4645    /// Client sends multiple HEADERS before data.
4646    fn additional_headers_before_data_client() {
4647        let mut s = Session::new().unwrap();
4648        s.handshake().unwrap();
4649
4650        let (stream, req) = s.send_request(false).unwrap();
4651
4652        let req_trailer = vec![Header::new(b"goodbye", b"world")];
4653
4654        assert_eq!(
4655            s.client.send_additional_headers(
4656                &mut s.pipe.client,
4657                stream,
4658                &req_trailer,
4659                true,
4660                false
4661            ),
4662            Ok(())
4663        );
4664
4665        s.advance().ok();
4666
4667        let ev_initial_headers = Event::Headers {
4668            list: req,
4669            more_frames: true,
4670        };
4671
4672        let ev_trailing_headers = Event::Headers {
4673            list: req_trailer,
4674            more_frames: true,
4675        };
4676
4677        assert_eq!(s.poll_server(), Ok((stream, ev_initial_headers)));
4678        assert_eq!(s.poll_server(), Ok((stream, ev_trailing_headers)));
4679        assert_eq!(s.poll_server(), Err(Error::Done));
4680    }
4681
4682    #[test]
4683    /// Client sends multiple HEADERS before data.
4684    fn data_after_trailers_client() {
4685        let mut s = Session::new().unwrap();
4686        s.handshake().unwrap();
4687
4688        let (stream, req) = s.send_request(false).unwrap();
4689
4690        let body = s.send_body_client(stream, false).unwrap();
4691
4692        let mut recv_buf = vec![0; body.len()];
4693
4694        let req_trailers = vec![Header::new(b"foo", b"bar")];
4695
4696        s.client
4697            .send_additional_headers(
4698                &mut s.pipe.client,
4699                stream,
4700                &req_trailers,
4701                true,
4702                false,
4703            )
4704            .unwrap();
4705
4706        s.advance().ok();
4707
4708        s.send_frame_client(
4709            frame::Frame::Data {
4710                payload: vec![1, 2, 3, 4],
4711            },
4712            stream,
4713            true,
4714        )
4715        .unwrap();
4716
4717        let ev_headers = Event::Headers {
4718            list: req,
4719            more_frames: true,
4720        };
4721
4722        let ev_trailers = Event::Headers {
4723            list: req_trailers,
4724            more_frames: true,
4725        };
4726
4727        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4728        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4729        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4730        assert_eq!(s.poll_server(), Ok((stream, ev_trailers)));
4731        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4732    }
4733
4734    #[test]
4735    /// Send a MAX_PUSH_ID frame from the client on a valid stream.
4736    fn max_push_id_from_client_good() {
4737        let mut s = Session::new().unwrap();
4738        s.handshake().unwrap();
4739
4740        s.send_frame_client(
4741            frame::Frame::MaxPushId { push_id: 1 },
4742            s.client.control_stream_id.unwrap(),
4743            false,
4744        )
4745        .unwrap();
4746
4747        assert_eq!(s.poll_server(), Err(Error::Done));
4748    }
4749
4750    #[test]
4751    /// Send a MAX_PUSH_ID frame from the client on an invalid stream.
4752    fn max_push_id_from_client_bad_stream() {
4753        let mut s = Session::new().unwrap();
4754        s.handshake().unwrap();
4755
4756        let (stream, req) = s.send_request(false).unwrap();
4757
4758        s.send_frame_client(
4759            frame::Frame::MaxPushId { push_id: 2 },
4760            stream,
4761            false,
4762        )
4763        .unwrap();
4764
4765        let ev_headers = Event::Headers {
4766            list: req,
4767            more_frames: true,
4768        };
4769
4770        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4771        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4772    }
4773
4774    #[test]
4775    /// Send a sequence of MAX_PUSH_ID frames from the client that attempt to
4776    /// reduce the limit.
4777    fn max_push_id_from_client_limit_reduction() {
4778        let mut s = Session::new().unwrap();
4779        s.handshake().unwrap();
4780
4781        s.send_frame_client(
4782            frame::Frame::MaxPushId { push_id: 2 },
4783            s.client.control_stream_id.unwrap(),
4784            false,
4785        )
4786        .unwrap();
4787
4788        s.send_frame_client(
4789            frame::Frame::MaxPushId { push_id: 1 },
4790            s.client.control_stream_id.unwrap(),
4791            false,
4792        )
4793        .unwrap();
4794
4795        assert_eq!(s.poll_server(), Err(Error::IdError));
4796    }
4797
4798    #[test]
4799    /// Send a MAX_PUSH_ID frame from the server, which is forbidden.
4800    fn max_push_id_from_server() {
4801        let mut s = Session::new().unwrap();
4802        s.handshake().unwrap();
4803
4804        s.send_frame_server(
4805            frame::Frame::MaxPushId { push_id: 1 },
4806            s.server.control_stream_id.unwrap(),
4807            false,
4808        )
4809        .unwrap();
4810
4811        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
4812    }
4813
4814    #[test]
4815    /// Send a PUSH_PROMISE frame from the client, which is forbidden.
4816    fn push_promise_from_client() {
4817        let mut s = Session::new().unwrap();
4818        s.handshake().unwrap();
4819
4820        let (stream, req) = s.send_request(false).unwrap();
4821
4822        let header_block = s.client.encode_header_block(&req).unwrap();
4823
4824        s.send_frame_client(
4825            frame::Frame::PushPromise {
4826                push_id: 1,
4827                header_block,
4828            },
4829            stream,
4830            false,
4831        )
4832        .unwrap();
4833
4834        let ev_headers = Event::Headers {
4835            list: req,
4836            more_frames: true,
4837        };
4838
4839        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4840        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4841    }
4842
4843    #[test]
4844    /// Server push streams from client are not allowed by the protocol.
4845    fn push_stream_from_client() {
4846        let mut s = Session::new().unwrap();
4847        s.handshake().unwrap();
4848
4849        s.client
4850            .open_uni_stream(
4851                &mut s.pipe.client,
4852                stream::HTTP3_PUSH_STREAM_TYPE_ID,
4853            )
4854            .unwrap();
4855
4856        s.advance().ok();
4857
4858        assert_eq!(s.poll_server(), Err(Error::StreamCreationError));
4859    }
4860
4861    #[test]
4862    /// Server push streams from server are not allowed since the client does
4863    /// not advertise server push support by default.
4864    fn push_stream_from_server() {
4865        let mut s = Session::new().unwrap();
4866        s.handshake().unwrap();
4867
4868        s.server
4869            .open_uni_stream(
4870                &mut s.pipe.server,
4871                stream::HTTP3_PUSH_STREAM_TYPE_ID,
4872            )
4873            .unwrap();
4874
4875        s.advance().ok();
4876
4877        assert_eq!(s.poll_client(), Err(Error::StreamCreationError));
4878    }
4879
4880    #[test]
4881    /// Send a CANCEL_PUSH frame from the client.
4882    fn cancel_push_from_client() {
4883        let mut s = Session::new().unwrap();
4884        s.handshake().unwrap();
4885
4886        s.send_frame_client(
4887            frame::Frame::CancelPush { push_id: 1 },
4888            s.client.control_stream_id.unwrap(),
4889            false,
4890        )
4891        .unwrap();
4892
4893        assert_eq!(s.poll_server(), Err(Error::Done));
4894    }
4895
4896    #[test]
4897    /// Send a CANCEL_PUSH frame from the client on an invalid stream.
4898    fn cancel_push_from_client_bad_stream() {
4899        let mut s = Session::new().unwrap();
4900        s.handshake().unwrap();
4901
4902        let (stream, req) = s.send_request(false).unwrap();
4903
4904        s.send_frame_client(
4905            frame::Frame::CancelPush { push_id: 2 },
4906            stream,
4907            false,
4908        )
4909        .unwrap();
4910
4911        let ev_headers = Event::Headers {
4912            list: req,
4913            more_frames: true,
4914        };
4915
4916        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4917        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4918    }
4919
4920    #[test]
4921    /// Send a CANCEL_PUSH frame from the client.
4922    fn cancel_push_from_server() {
4923        let mut s = Session::new().unwrap();
4924        s.handshake().unwrap();
4925
4926        s.send_frame_server(
4927            frame::Frame::CancelPush { push_id: 1 },
4928            s.server.control_stream_id.unwrap(),
4929            false,
4930        )
4931        .unwrap();
4932
4933        assert_eq!(s.poll_client(), Err(Error::Done));
4934    }
4935
4936    #[test]
4937    /// Send a GOAWAY frame from the client.
4938    fn goaway_from_client_good() {
4939        let mut s = Session::new().unwrap();
4940        s.handshake().unwrap();
4941
4942        s.client.send_goaway(&mut s.pipe.client, 100).unwrap();
4943
4944        s.advance().ok();
4945
4946        // TODO: server push
4947        assert_eq!(s.poll_server(), Ok((0, Event::GoAway)));
4948    }
4949
4950    #[test]
4951    /// Send a GOAWAY frame from the server.
4952    fn goaway_from_server_good() {
4953        let mut s = Session::new().unwrap();
4954        s.handshake().unwrap();
4955
4956        s.server.send_goaway(&mut s.pipe.server, 4000).unwrap();
4957
4958        s.advance().ok();
4959
4960        assert_eq!(s.poll_client(), Ok((4000, Event::GoAway)));
4961    }
4962
4963    #[test]
4964    /// A client MUST NOT send a request after it receives GOAWAY.
4965    fn client_request_after_goaway() {
4966        let mut s = Session::new().unwrap();
4967        s.handshake().unwrap();
4968
4969        s.server.send_goaway(&mut s.pipe.server, 4000).unwrap();
4970
4971        s.advance().ok();
4972
4973        assert_eq!(s.poll_client(), Ok((4000, Event::GoAway)));
4974
4975        assert_eq!(s.send_request(true), Err(Error::FrameUnexpected));
4976    }
4977
4978    #[test]
4979    /// Send a GOAWAY frame from the server, using an invalid goaway ID.
4980    fn goaway_from_server_invalid_id() {
4981        let mut s = Session::new().unwrap();
4982        s.handshake().unwrap();
4983
4984        s.send_frame_server(
4985            frame::Frame::GoAway { id: 1 },
4986            s.server.control_stream_id.unwrap(),
4987            false,
4988        )
4989        .unwrap();
4990
4991        assert_eq!(s.poll_client(), Err(Error::IdError));
4992    }
4993
4994    #[test]
4995    /// Send multiple GOAWAY frames from the server, that increase the goaway
4996    /// ID.
4997    fn goaway_from_server_increase_id() {
4998        let mut s = Session::new().unwrap();
4999        s.handshake().unwrap();
5000
5001        s.send_frame_server(
5002            frame::Frame::GoAway { id: 0 },
5003            s.server.control_stream_id.unwrap(),
5004            false,
5005        )
5006        .unwrap();
5007
5008        s.send_frame_server(
5009            frame::Frame::GoAway { id: 4 },
5010            s.server.control_stream_id.unwrap(),
5011            false,
5012        )
5013        .unwrap();
5014
5015        assert_eq!(s.poll_client(), Ok((0, Event::GoAway)));
5016
5017        assert_eq!(s.poll_client(), Err(Error::IdError));
5018    }
5019
5020    #[test]
5021    #[cfg(feature = "sfv")]
5022    fn parse_priority_field_value() {
5023        // Legal dicts
5024        assert_eq!(
5025            Ok(Priority::new(0, false)),
5026            Priority::try_from(b"u=0".as_slice())
5027        );
5028        assert_eq!(
5029            Ok(Priority::new(3, false)),
5030            Priority::try_from(b"u=3".as_slice())
5031        );
5032        assert_eq!(
5033            Ok(Priority::new(7, false)),
5034            Priority::try_from(b"u=7".as_slice())
5035        );
5036
5037        assert_eq!(
5038            Ok(Priority::new(0, true)),
5039            Priority::try_from(b"u=0, i".as_slice())
5040        );
5041        assert_eq!(
5042            Ok(Priority::new(3, true)),
5043            Priority::try_from(b"u=3, i".as_slice())
5044        );
5045        assert_eq!(
5046            Ok(Priority::new(7, true)),
5047            Priority::try_from(b"u=7, i".as_slice())
5048        );
5049
5050        assert_eq!(
5051            Ok(Priority::new(0, true)),
5052            Priority::try_from(b"u=0, i=?1".as_slice())
5053        );
5054        assert_eq!(
5055            Ok(Priority::new(3, true)),
5056            Priority::try_from(b"u=3, i=?1".as_slice())
5057        );
5058        assert_eq!(
5059            Ok(Priority::new(7, true)),
5060            Priority::try_from(b"u=7, i=?1".as_slice())
5061        );
5062
5063        assert_eq!(
5064            Ok(Priority::new(3, false)),
5065            Priority::try_from(b"".as_slice())
5066        );
5067
5068        assert_eq!(
5069            Ok(Priority::new(0, true)),
5070            Priority::try_from(b"u=0;foo, i;bar".as_slice())
5071        );
5072        assert_eq!(
5073            Ok(Priority::new(3, true)),
5074            Priority::try_from(b"u=3;hello, i;world".as_slice())
5075        );
5076        assert_eq!(
5077            Ok(Priority::new(7, true)),
5078            Priority::try_from(b"u=7;croeso, i;gymru".as_slice())
5079        );
5080
5081        assert_eq!(
5082            Ok(Priority::new(0, true)),
5083            Priority::try_from(b"u=0, i, spinaltap=11".as_slice())
5084        );
5085
5086        // Illegal formats
5087        assert_eq!(Err(Error::Done), Priority::try_from(b"0".as_slice()));
5088        assert_eq!(
5089            Ok(Priority::new(7, false)),
5090            Priority::try_from(b"u=-1".as_slice())
5091        );
5092        assert_eq!(Err(Error::Done), Priority::try_from(b"u=0.2".as_slice()));
5093        assert_eq!(
5094            Ok(Priority::new(7, false)),
5095            Priority::try_from(b"u=100".as_slice())
5096        );
5097        assert_eq!(
5098            Err(Error::Done),
5099            Priority::try_from(b"u=3, i=true".as_slice())
5100        );
5101
5102        // Trailing comma in dict is malformed
5103        assert_eq!(Err(Error::Done), Priority::try_from(b"u=7, ".as_slice()));
5104    }
5105
5106    #[test]
5107    /// Send a PRIORITY_UPDATE for request stream from the client.
5108    fn priority_update_request() {
5109        let mut s = Session::new().unwrap();
5110        s.handshake().unwrap();
5111
5112        s.client
5113            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5114                urgency: 3,
5115                incremental: false,
5116            })
5117            .unwrap();
5118        s.advance().ok();
5119
5120        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5121        assert_eq!(s.poll_server(), Err(Error::Done));
5122    }
5123
5124    #[test]
5125    /// Send a PRIORITY_UPDATE for request stream from the client that is too
5126    /// large.
5127    fn priority_update_request_max_size_limit_default() {
5128        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5129        config
5130            .load_cert_chain_from_pem_file("examples/cert.crt")
5131            .unwrap();
5132        config
5133            .load_priv_key_from_pem_file("examples/cert.key")
5134            .unwrap();
5135        config.set_application_protos(&[b"h3"]).unwrap();
5136        config.set_initial_max_data(1500);
5137        config.set_initial_max_stream_data_bidi_local(1500);
5138        config.set_initial_max_stream_data_bidi_remote(1500);
5139        config.set_initial_max_stream_data_uni(1500);
5140        config.set_initial_max_streams_bidi(5);
5141        config.set_initial_max_streams_uni(5);
5142        config.verify_peer(false);
5143
5144        let h3_config = Config::new().unwrap();
5145
5146        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
5147
5148        s.handshake().unwrap();
5149
5150        let mut d = vec![42; 600];
5151        let mut b = octets::OctetsMut::with_slice(&mut d);
5152
5153        let pu = frame::Frame::PriorityUpdateRequest {
5154            prioritized_element_id: 0,
5155            priority_field_value: vec![0; 512],
5156        };
5157
5158        pu.to_bytes(&mut b).unwrap();
5159
5160        s.pipe.client.stream_send(2, &d, true).unwrap();
5161
5162        s.advance().ok();
5163
5164        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
5165
5166        assert_eq!(
5167            s.pipe.server.local_error.as_ref().unwrap().error_code,
5168            Error::to_wire(Error::ExcessiveLoad)
5169        );
5170    }
5171
5172    #[test]
5173    /// Send a PRIORITY_UPDATE for request stream from the client.
5174    fn priority_update_single_stream_rearm() {
5175        let mut s = Session::new().unwrap();
5176        s.handshake().unwrap();
5177
5178        s.client
5179            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5180                urgency: 3,
5181                incremental: false,
5182            })
5183            .unwrap();
5184        s.advance().ok();
5185
5186        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5187        assert_eq!(s.poll_server(), Err(Error::Done));
5188
5189        s.client
5190            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5191                urgency: 5,
5192                incremental: false,
5193            })
5194            .unwrap();
5195        s.advance().ok();
5196
5197        assert_eq!(s.poll_server(), Err(Error::Done));
5198
5199        // There is only one PRIORITY_UPDATE frame to read. Once read, the event
5200        // will rearm ready for more.
5201        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=5".to_vec()));
5202        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5203
5204        s.client
5205            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5206                urgency: 7,
5207                incremental: false,
5208            })
5209            .unwrap();
5210        s.advance().ok();
5211
5212        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5213        assert_eq!(s.poll_server(), Err(Error::Done));
5214
5215        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=7".to_vec()));
5216        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5217    }
5218
5219    #[test]
5220    /// Send multiple PRIORITY_UPDATE frames for different streams from the
5221    /// client across multiple flights of exchange.
5222    fn priority_update_request_multiple_stream_arm_multiple_flights() {
5223        let mut s = Session::new().unwrap();
5224        s.handshake().unwrap();
5225
5226        s.client
5227            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5228                urgency: 3,
5229                incremental: false,
5230            })
5231            .unwrap();
5232        s.advance().ok();
5233
5234        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5235        assert_eq!(s.poll_server(), Err(Error::Done));
5236
5237        s.client
5238            .send_priority_update_for_request(&mut s.pipe.client, 4, &Priority {
5239                urgency: 1,
5240                incremental: false,
5241            })
5242            .unwrap();
5243        s.advance().ok();
5244
5245        assert_eq!(s.poll_server(), Ok((4, Event::PriorityUpdate)));
5246        assert_eq!(s.poll_server(), Err(Error::Done));
5247
5248        s.client
5249            .send_priority_update_for_request(&mut s.pipe.client, 8, &Priority {
5250                urgency: 2,
5251                incremental: false,
5252            })
5253            .unwrap();
5254        s.advance().ok();
5255
5256        assert_eq!(s.poll_server(), Ok((8, Event::PriorityUpdate)));
5257        assert_eq!(s.poll_server(), Err(Error::Done));
5258
5259        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5260        assert_eq!(s.server.take_last_priority_update(4), Ok(b"u=1".to_vec()));
5261        assert_eq!(s.server.take_last_priority_update(8), Ok(b"u=2".to_vec()));
5262        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5263    }
5264
5265    #[test]
5266    /// Send multiple PRIORITY_UPDATE frames for different streams from the
5267    /// client across a single flight.
5268    fn priority_update_request_multiple_stream_arm_single_flight() {
5269        let mut s = Session::new().unwrap();
5270        s.handshake().unwrap();
5271
5272        let mut d = [42; 65535];
5273
5274        let mut b = octets::OctetsMut::with_slice(&mut d);
5275
5276        let p1 = frame::Frame::PriorityUpdateRequest {
5277            prioritized_element_id: 0,
5278            priority_field_value: b"u=3".to_vec(),
5279        };
5280
5281        let p2 = frame::Frame::PriorityUpdateRequest {
5282            prioritized_element_id: 4,
5283            priority_field_value: b"u=3".to_vec(),
5284        };
5285
5286        let p3 = frame::Frame::PriorityUpdateRequest {
5287            prioritized_element_id: 8,
5288            priority_field_value: b"u=3".to_vec(),
5289        };
5290
5291        p1.to_bytes(&mut b).unwrap();
5292        p2.to_bytes(&mut b).unwrap();
5293        p3.to_bytes(&mut b).unwrap();
5294
5295        let off = b.off();
5296        s.pipe
5297            .client
5298            .stream_send(s.client.control_stream_id.unwrap(), &d[..off], false)
5299            .unwrap();
5300
5301        s.advance().ok();
5302
5303        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5304        assert_eq!(s.poll_server(), Ok((4, Event::PriorityUpdate)));
5305        assert_eq!(s.poll_server(), Ok((8, Event::PriorityUpdate)));
5306        assert_eq!(s.poll_server(), Err(Error::Done));
5307
5308        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5309        assert_eq!(s.server.take_last_priority_update(4), Ok(b"u=3".to_vec()));
5310        assert_eq!(s.server.take_last_priority_update(8), Ok(b"u=3".to_vec()));
5311
5312        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5313    }
5314
5315    #[test]
5316    /// Send a PRIORITY_UPDATE for a request stream, before and after the stream
5317    /// has been completed.
5318    fn priority_update_request_collected_completed() {
5319        let mut s = Session::new().unwrap();
5320        s.handshake().unwrap();
5321
5322        s.client
5323            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5324                urgency: 3,
5325                incremental: false,
5326            })
5327            .unwrap();
5328        s.advance().ok();
5329
5330        let (stream, req) = s.send_request(true).unwrap();
5331        let ev_headers = Event::Headers {
5332            list: req,
5333            more_frames: false,
5334        };
5335
5336        // Priority event is generated before request headers.
5337        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5338        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5339        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
5340        assert_eq!(s.poll_server(), Err(Error::Done));
5341
5342        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5343        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5344
5345        let resp = s.send_response(stream, true).unwrap();
5346
5347        let ev_headers = Event::Headers {
5348            list: resp,
5349            more_frames: false,
5350        };
5351
5352        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
5353        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
5354        assert_eq!(s.poll_client(), Err(Error::Done));
5355
5356        // Now send a PRIORITY_UPDATE for the completed request stream.
5357        s.client
5358            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5359                urgency: 3,
5360                incremental: false,
5361            })
5362            .unwrap();
5363        s.advance().ok();
5364
5365        // No event generated at server
5366        assert_eq!(s.poll_server(), Err(Error::Done));
5367    }
5368
5369    #[test]
5370    /// Send a PRIORITY_UPDATE for a request stream after H3 has collected it,
5371    /// but before the transport stream has been collected.
5372    fn priority_update_request_after_h3_collection() {
5373        let mut s = Session::new().unwrap();
5374        s.handshake().unwrap();
5375
5376        let init_streams_server = s.server.streams.len();
5377
5378        let (stream, req) = s.send_request(true).unwrap();
5379        let ev_headers = Event::Headers {
5380            list: req,
5381            more_frames: false,
5382        };
5383
5384        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5385        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
5386        assert_eq!(s.poll_server(), Err(Error::Done));
5387
5388        let resp = vec![
5389            Header::new(b":status", b"200"),
5390            Header::new(b"server", b"quiche-test"),
5391        ];
5392
5393        s.server
5394            .send_response(&mut s.pipe.server, stream, &resp, true)
5395            .unwrap();
5396
5397        // H3 no longer needs its stream state once it has sent the response
5398        // FIN and consumed the request FIN. The QUIC stream remains until the
5399        // response FIN is acknowledged by the peer.
5400        assert_eq!(s.server.streams.len(), init_streams_server);
5401        assert!(s.pipe.server.stream_finished(stream));
5402        assert!(s.pipe.server.stream_closed(stream));
5403
5404        let stream_state = s.pipe.server.streams.get(stream).unwrap();
5405        assert!(stream_state.recv.is_fin());
5406        assert!(stream_state.send.is_fin());
5407        assert!(!s.pipe.server.streams.is_collected(stream));
5408
5409        s.client
5410            .send_priority_update_for_request(
5411                &mut s.pipe.client,
5412                stream,
5413                &Priority {
5414                    urgency: 3,
5415                    incremental: false,
5416                },
5417            )
5418            .unwrap();
5419
5420        let flight = crate::test_utils::emit_flight(&mut s.pipe.client).unwrap();
5421        crate::test_utils::process_flight(&mut s.pipe.server, flight).unwrap();
5422
5423        assert_eq!(s.poll_server(), Err(Error::Done));
5424        assert_eq!(s.server.streams.len(), init_streams_server);
5425    }
5426
5427    #[test]
5428    /// Send a PRIORITY_UPDATE for a request stream, before and after the stream
5429    /// has been stopped.
5430    fn priority_update_request_collected_stopped() {
5431        let mut s = Session::new().unwrap();
5432        s.handshake().unwrap();
5433
5434        s.client
5435            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5436                urgency: 3,
5437                incremental: false,
5438            })
5439            .unwrap();
5440        s.advance().ok();
5441
5442        let (stream, req) = s.send_request(false).unwrap();
5443        let ev_headers = Event::Headers {
5444            list: req,
5445            more_frames: true,
5446        };
5447
5448        // Priority event is generated before request headers.
5449        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5450        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5451        assert_eq!(s.poll_server(), Err(Error::Done));
5452
5453        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5454        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5455
5456        s.pipe
5457            .client
5458            .stream_shutdown(stream, crate::Shutdown::Write, 0x100)
5459            .unwrap();
5460        s.pipe
5461            .client
5462            .stream_shutdown(stream, crate::Shutdown::Read, 0x100)
5463            .unwrap();
5464
5465        s.advance().ok();
5466
5467        assert_eq!(s.poll_server(), Ok((0, Event::Reset(0x100))));
5468        assert_eq!(s.poll_server(), Err(Error::Done));
5469
5470        // Now send a PRIORITY_UPDATE for the closed request stream.
5471        s.client
5472            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5473                urgency: 3,
5474                incremental: false,
5475            })
5476            .unwrap();
5477        s.advance().ok();
5478
5479        // No event generated at server
5480        assert_eq!(s.poll_server(), Err(Error::Done));
5481
5482        assert!(s.pipe.server.streams.is_collected(0));
5483        assert!(s.pipe.client.streams.is_collected(0));
5484    }
5485
5486    #[test]
5487    /// Send a PRIORITY_UPDATE for push stream from the client.
5488    fn priority_update_push() {
5489        let mut s = Session::new().unwrap();
5490        s.handshake().unwrap();
5491
5492        s.send_frame_client(
5493            frame::Frame::PriorityUpdatePush {
5494                prioritized_element_id: 3,
5495                priority_field_value: b"u=3".to_vec(),
5496            },
5497            s.client.control_stream_id.unwrap(),
5498            false,
5499        )
5500        .unwrap();
5501
5502        assert_eq!(s.poll_server(), Err(Error::Done));
5503    }
5504
5505    #[test]
5506    /// Send a PRIORITY_UPDATE for request stream from the client but for an
5507    /// incorrect stream type.
5508    fn priority_update_request_bad_stream() {
5509        let mut s = Session::new().unwrap();
5510        s.handshake().unwrap();
5511
5512        s.send_frame_client(
5513            frame::Frame::PriorityUpdateRequest {
5514                prioritized_element_id: 5,
5515                priority_field_value: b"u=3".to_vec(),
5516            },
5517            s.client.control_stream_id.unwrap(),
5518            false,
5519        )
5520        .unwrap();
5521
5522        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
5523    }
5524
5525    #[test]
5526    /// Send a PRIORITY_UPDATE for push stream from the client but for an
5527    /// incorrect stream type.
5528    fn priority_update_push_bad_stream() {
5529        let mut s = Session::new().unwrap();
5530        s.handshake().unwrap();
5531
5532        s.send_frame_client(
5533            frame::Frame::PriorityUpdatePush {
5534                prioritized_element_id: 5,
5535                priority_field_value: b"u=3".to_vec(),
5536            },
5537            s.client.control_stream_id.unwrap(),
5538            false,
5539        )
5540        .unwrap();
5541
5542        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
5543    }
5544
5545    #[test]
5546    /// Send a PRIORITY_UPDATE for request stream from the server.
5547    fn priority_update_request_from_server() {
5548        let mut s = Session::new().unwrap();
5549        s.handshake().unwrap();
5550
5551        s.send_frame_server(
5552            frame::Frame::PriorityUpdateRequest {
5553                prioritized_element_id: 0,
5554                priority_field_value: b"u=3".to_vec(),
5555            },
5556            s.server.control_stream_id.unwrap(),
5557            false,
5558        )
5559        .unwrap();
5560
5561        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
5562    }
5563
5564    #[test]
5565    /// Send a PRIORITY_UPDATE for request stream from the server.
5566    fn priority_update_push_from_server() {
5567        let mut s = Session::new().unwrap();
5568        s.handshake().unwrap();
5569
5570        s.send_frame_server(
5571            frame::Frame::PriorityUpdatePush {
5572                prioritized_element_id: 0,
5573                priority_field_value: b"u=3".to_vec(),
5574            },
5575            s.server.control_stream_id.unwrap(),
5576            false,
5577        )
5578        .unwrap();
5579
5580        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
5581    }
5582
5583    #[test]
5584    /// Ensure quiche allocates streams for client and server roles as expected.
5585    fn uni_stream_local_counting() {
5586        let config = Config::new().unwrap();
5587
5588        let h3_cln = Connection::new(&config, false, false).unwrap();
5589        assert_eq!(h3_cln.next_uni_stream_id, 2);
5590
5591        let h3_srv = Connection::new(&config, true, false).unwrap();
5592        assert_eq!(h3_srv.next_uni_stream_id, 3);
5593    }
5594
5595    #[test]
5596    /// Client opens multiple control streams, which is forbidden.
5597    fn open_multiple_control_streams() {
5598        let mut s = Session::new().unwrap();
5599        s.handshake().unwrap();
5600
5601        let stream_id = s.client.next_uni_stream_id;
5602
5603        let mut d = [42; 8];
5604        let mut b = octets::OctetsMut::with_slice(&mut d);
5605
5606        s.pipe
5607            .client
5608            .stream_send(
5609                stream_id,
5610                b.put_varint(stream::HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(),
5611                false,
5612            )
5613            .unwrap();
5614
5615        s.advance().ok();
5616
5617        assert_eq!(s.poll_server(), Err(Error::StreamCreationError));
5618    }
5619
5620    #[test]
5621    /// Client closes the control stream, which is forbidden.
5622    fn close_control_stream_after_type() {
5623        let mut s = Session::new().unwrap();
5624        s.handshake().unwrap();
5625
5626        s.pipe
5627            .client
5628            .stream_send(s.client.control_stream_id.unwrap(), &[], true)
5629            .unwrap();
5630
5631        s.advance().ok();
5632
5633        assert_eq!(
5634            Err(Error::ClosedCriticalStream),
5635            s.server.poll(&mut s.pipe.server)
5636        );
5637        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5638    }
5639
5640    #[test]
5641    /// Client closes the control stream after a frame is sent, which is
5642    /// forbidden.
5643    fn close_control_stream_after_frame() {
5644        let mut s = Session::new().unwrap();
5645        s.handshake().unwrap();
5646
5647        s.send_frame_client(
5648            frame::Frame::MaxPushId { push_id: 1 },
5649            s.client.control_stream_id.unwrap(),
5650            true,
5651        )
5652        .unwrap();
5653
5654        assert_eq!(
5655            Err(Error::ClosedCriticalStream),
5656            s.server.poll(&mut s.pipe.server)
5657        );
5658        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5659    }
5660
5661    #[test]
5662    /// Client resets the control stream, which is forbidden.
5663    fn reset_control_stream_after_type() {
5664        let mut s = Session::new().unwrap();
5665        s.handshake().unwrap();
5666
5667        s.pipe
5668            .client
5669            .stream_shutdown(
5670                s.client.control_stream_id.unwrap(),
5671                crate::Shutdown::Write,
5672                0,
5673            )
5674            .unwrap();
5675
5676        s.advance().ok();
5677
5678        assert_eq!(
5679            Err(Error::ClosedCriticalStream),
5680            s.server.poll(&mut s.pipe.server)
5681        );
5682        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5683    }
5684
5685    #[test]
5686    /// Client resets the control stream after a frame is sent, which is
5687    /// forbidden.
5688    fn reset_control_stream_after_frame() {
5689        let mut s = Session::new().unwrap();
5690        s.handshake().unwrap();
5691
5692        s.send_frame_client(
5693            frame::Frame::MaxPushId { push_id: 1 },
5694            s.client.control_stream_id.unwrap(),
5695            false,
5696        )
5697        .unwrap();
5698
5699        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5700
5701        s.pipe
5702            .client
5703            .stream_shutdown(
5704                s.client.control_stream_id.unwrap(),
5705                crate::Shutdown::Write,
5706                0,
5707            )
5708            .unwrap();
5709
5710        s.advance().ok();
5711
5712        assert_eq!(
5713            Err(Error::ClosedCriticalStream),
5714            s.server.poll(&mut s.pipe.server)
5715        );
5716        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5717    }
5718
5719    #[test]
5720    /// Client closes QPACK stream, which is forbidden.
5721    fn close_qpack_stream_after_type() {
5722        let mut s = Session::new().unwrap();
5723        s.handshake().unwrap();
5724
5725        s.pipe
5726            .client
5727            .stream_send(
5728                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
5729                &[],
5730                true,
5731            )
5732            .unwrap();
5733
5734        s.advance().ok();
5735
5736        assert_eq!(
5737            Err(Error::ClosedCriticalStream),
5738            s.server.poll(&mut s.pipe.server)
5739        );
5740        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5741    }
5742
5743    #[test]
5744    /// Client closes QPACK stream after sending some stuff, which is forbidden.
5745    fn close_qpack_stream_after_data() {
5746        let mut s = Session::new().unwrap();
5747        s.handshake().unwrap();
5748
5749        let stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5750        let d = [0; 1];
5751
5752        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5753        s.pipe.client.stream_send(stream_id, &d, true).unwrap();
5754
5755        s.advance().ok();
5756
5757        assert_eq!(
5758            Err(Error::ClosedCriticalStream),
5759            s.server.poll(&mut s.pipe.server)
5760        );
5761        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5762    }
5763
5764    #[test]
5765    /// Client resets QPACK stream, which is forbidden.
5766    fn reset_qpack_stream_after_type() {
5767        let mut s = Session::new().unwrap();
5768        s.handshake().unwrap();
5769
5770        s.pipe
5771            .client
5772            .stream_shutdown(
5773                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
5774                crate::Shutdown::Write,
5775                0,
5776            )
5777            .unwrap();
5778
5779        s.advance().ok();
5780
5781        assert_eq!(
5782            Err(Error::ClosedCriticalStream),
5783            s.server.poll(&mut s.pipe.server)
5784        );
5785        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5786    }
5787
5788    #[test]
5789    /// Client resets QPACK stream after sending some stuff, which is forbidden.
5790    fn reset_qpack_stream_after_data() {
5791        let mut s = Session::new().unwrap();
5792        s.handshake().unwrap();
5793
5794        let stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5795        let d = [0; 1];
5796
5797        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5798        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5799
5800        s.advance().ok();
5801
5802        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5803
5804        s.pipe
5805            .client
5806            .stream_shutdown(stream_id, crate::Shutdown::Write, 0)
5807            .unwrap();
5808
5809        s.advance().ok();
5810
5811        assert_eq!(
5812            Err(Error::ClosedCriticalStream),
5813            s.server.poll(&mut s.pipe.server)
5814        );
5815        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5816    }
5817
5818    #[test]
5819    /// Client sends QPACK data.
5820    fn qpack_data() {
5821        // TODO: QPACK instructions are ignored until dynamic table support is
5822        // added so we just test that the data is safely ignored.
5823        let mut s = Session::new().unwrap();
5824        s.handshake().unwrap();
5825
5826        let e_stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5827        let d_stream_id = s.client.local_qpack_streams.decoder_stream_id.unwrap();
5828        let d = [0; 20];
5829
5830        s.pipe.client.stream_send(e_stream_id, &d, false).unwrap();
5831        s.advance().ok();
5832
5833        s.pipe.client.stream_send(d_stream_id, &d, false).unwrap();
5834        s.advance().ok();
5835
5836        match s.server.poll(&mut s.pipe.server) {
5837            Ok(_) => panic!(),
5838
5839            Err(Error::Done) => {
5840                assert_eq!(s.server.peer_qpack_streams.encoder_stream_bytes, 20);
5841                assert_eq!(s.server.peer_qpack_streams.decoder_stream_bytes, 20);
5842            },
5843
5844            Err(_) => {
5845                panic!();
5846            },
5847        }
5848
5849        let stats = s.server.stats();
5850        assert_eq!(stats.qpack_encoder_stream_recv_bytes, 20);
5851        assert_eq!(stats.qpack_decoder_stream_recv_bytes, 20);
5852    }
5853
5854    #[test]
5855    /// Tests limits for the stream state buffer maximum size.
5856    fn max_state_buf_size() {
5857        let mut s = Session::new().unwrap();
5858        s.handshake().unwrap();
5859
5860        let req = vec![
5861            Header::new(b":method", b"GET"),
5862            Header::new(b":scheme", b"https"),
5863            Header::new(b":authority", b"quic.tech"),
5864            Header::new(b":path", b"/test"),
5865            Header::new(b"user-agent", b"quiche-test"),
5866        ];
5867
5868        assert_eq!(
5869            s.client.send_request(&mut s.pipe.client, &req, false),
5870            Ok(0)
5871        );
5872
5873        s.advance().ok();
5874
5875        let ev_headers = Event::Headers {
5876            list: req,
5877            more_frames: true,
5878        };
5879
5880        assert_eq!(s.server.poll(&mut s.pipe.server), Ok((0, ev_headers)));
5881
5882        // DATA frames don't consume the state buffer, so can be of any size.
5883        let mut d = [42; 128];
5884        let mut b = octets::OctetsMut::with_slice(&mut d);
5885
5886        let frame_type = b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
5887        s.pipe.client.stream_send(0, frame_type, false).unwrap();
5888
5889        let frame_len = b.put_varint(1 << 24).unwrap();
5890        s.pipe.client.stream_send(0, frame_len, false).unwrap();
5891
5892        s.pipe.client.stream_send(0, &d, false).unwrap();
5893
5894        s.advance().ok();
5895
5896        assert_eq!(s.server.poll(&mut s.pipe.server), Ok((0, Event::Data)));
5897
5898        // GREASE frames consume the state buffer, so need to be limited.
5899        let mut s = Session::new().unwrap();
5900        s.handshake().unwrap();
5901
5902        let mut d = [42; 128];
5903        let mut b = octets::OctetsMut::with_slice(&mut d);
5904
5905        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
5906        s.pipe.client.stream_send(0, frame_type, false).unwrap();
5907
5908        let frame_len = b.put_varint(1 << 24).unwrap();
5909        s.pipe.client.stream_send(0, frame_len, false).unwrap();
5910
5911        s.pipe.client.stream_send(0, &d, false).unwrap();
5912
5913        s.advance().ok();
5914
5915        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::ExcessiveLoad));
5916    }
5917
5918    #[test]
5919    /// Tests that DATA frames are properly truncated depending on the request
5920    /// stream's outgoing flow control capacity.
5921    fn stream_backpressure() {
5922        let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
5923
5924        let mut s = Session::new().unwrap();
5925        s.handshake().unwrap();
5926
5927        let (stream, req) = s.send_request(false).unwrap();
5928
5929        let total_data_frames = 6;
5930
5931        for _ in 0..total_data_frames {
5932            assert_eq!(
5933                s.client
5934                    .send_body(&mut s.pipe.client, stream, &bytes, false),
5935                Ok(bytes.len())
5936            );
5937
5938            s.advance().ok();
5939        }
5940
5941        assert_eq!(
5942            s.client.send_body(&mut s.pipe.client, stream, &bytes, true),
5943            Ok(bytes.len() - 2)
5944        );
5945
5946        s.advance().ok();
5947
5948        let mut recv_buf = vec![0; bytes.len()];
5949
5950        let ev_headers = Event::Headers {
5951            list: req,
5952            more_frames: true,
5953        };
5954
5955        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5956        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
5957        assert_eq!(s.poll_server(), Err(Error::Done));
5958
5959        for _ in 0..total_data_frames {
5960            assert_eq!(
5961                s.recv_body_server(stream, &mut recv_buf),
5962                Ok(bytes.len())
5963            );
5964        }
5965
5966        assert_eq!(
5967            s.recv_body_server(stream, &mut recv_buf),
5968            Ok(bytes.len() - 2)
5969        );
5970
5971        // Fin flag from last send_body() call was not sent as the buffer was
5972        // only partially written.
5973        assert_eq!(s.poll_server(), Err(Error::Done));
5974
5975        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
5976        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
5977        assert_eq!(s.pipe.server.data_blocked_recv_count, 0);
5978        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 1);
5979
5980        assert_eq!(s.pipe.client.data_blocked_sent_count, 0);
5981        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 1);
5982        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
5983        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
5984    }
5985
5986    #[test]
5987    /// Tests that the max header list size setting allows larger headers than
5988    /// default.
5989    fn request_max_header_size_limit_accepts_large_headers() {
5990        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5991        config
5992            .load_cert_chain_from_pem_file("examples/cert.crt")
5993            .unwrap();
5994        config
5995            .load_priv_key_from_pem_file("examples/cert.key")
5996            .unwrap();
5997        config.set_application_protos(&[b"h3"]).unwrap();
5998        config.set_initial_max_data(150000000);
5999        config.set_initial_max_stream_data_bidi_local(150000000);
6000        config.set_initial_max_stream_data_bidi_remote(150000000);
6001        config.set_initial_max_stream_data_uni(150000000);
6002        config.set_initial_max_streams_bidi(5);
6003        config.set_initial_max_streams_uni(5);
6004        config.verify_peer(false);
6005        config.set_initial_congestion_window_packets(100);
6006
6007        let mut h3_config = Config::new().unwrap();
6008        h3_config.set_max_field_section_size(256 * 1024);
6009
6010        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6011
6012        s.handshake().unwrap();
6013
6014        let mut req = vec![
6015            Header::new(b":method", b"GET"),
6016            Header::new(b":scheme", b"https"),
6017            Header::new(b":authority", b"quic.tech"),
6018            Header::new(b":path", b"/test"),
6019        ];
6020
6021        for _ in 1..5000 {
6022            req.push(Header::new(b"aaaaaaaaaa", b"aaaaaaaaa"));
6023        }
6024
6025        let ev_headers = Event::Headers {
6026            list: req.clone(),
6027            more_frames: false,
6028        };
6029
6030        let stream = s
6031            .client
6032            .send_request(&mut s.pipe.client, &req, true)
6033            .unwrap();
6034
6035        s.advance().ok();
6036
6037        assert_eq!(stream, 0);
6038
6039        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6040        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6041        assert_eq!(s.poll_server(), Err(Error::Done));
6042    }
6043
6044    #[test]
6045    /// Tests that the max header list size setting is enforced after decoding.
6046    fn request_max_header_size_limit_decoded_field_section() {
6047        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6048        config
6049            .load_cert_chain_from_pem_file("examples/cert.crt")
6050            .unwrap();
6051        config
6052            .load_priv_key_from_pem_file("examples/cert.key")
6053            .unwrap();
6054        config.set_application_protos(&[b"h3"]).unwrap();
6055        config.set_initial_max_data(1500);
6056        config.set_initial_max_stream_data_bidi_local(150);
6057        config.set_initial_max_stream_data_bidi_remote(150);
6058        config.set_initial_max_stream_data_uni(150);
6059        config.set_initial_max_streams_bidi(5);
6060        config.set_initial_max_streams_uni(5);
6061        config.verify_peer(false);
6062
6063        let mut h3_config = Config::new().unwrap();
6064        h3_config.set_max_field_section_size(65);
6065
6066        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6067
6068        s.handshake().unwrap();
6069
6070        let req = vec![
6071            Header::new(b":method", b"GET"),
6072            Header::new(b":scheme", b"https"),
6073            Header::new(b":authority", b"quic.tech"),
6074            Header::new(b":path", b"/test"),
6075            Header::new(b"aaaaaaa", b"aaaaaaaa"),
6076        ];
6077
6078        let stream = s
6079            .client
6080            .send_request(&mut s.pipe.client, &req, true)
6081            .unwrap();
6082
6083        s.advance().ok();
6084
6085        assert_eq!(stream, 0);
6086
6087        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
6088
6089        assert_eq!(
6090            s.pipe.server.local_error.as_ref().unwrap().error_code,
6091            Error::to_wire(Error::ExcessiveLoad)
6092        );
6093    }
6094
6095    #[test]
6096    /// Tests that the max header list size setting is enforced when observing
6097    /// frame size before decode.
6098    fn request_max_header_size_limit_default_abort_before_decode() {
6099        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6100        config
6101            .load_cert_chain_from_pem_file("examples/cert.crt")
6102            .unwrap();
6103        config
6104            .load_priv_key_from_pem_file("examples/cert.key")
6105            .unwrap();
6106        config.set_application_protos(&[b"h3"]).unwrap();
6107        config.set_initial_max_data(150000);
6108        config.set_initial_max_stream_data_bidi_local(150000);
6109        config.set_initial_max_stream_data_bidi_remote(150000);
6110        config.set_initial_max_stream_data_uni(150000);
6111        config.set_initial_max_streams_bidi(5);
6112        config.set_initial_max_streams_uni(5);
6113        config.verify_peer(false);
6114
6115        let h3_config = Config::new().unwrap();
6116
6117        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6118
6119        s.handshake().unwrap();
6120
6121        let mut d = vec![42; 200000];
6122        let mut b = octets::OctetsMut::with_slice(&mut d);
6123
6124        let hdrs = frame::Frame::Headers {
6125            header_block: vec![0; 65536],
6126        };
6127
6128        hdrs.to_bytes(&mut b).unwrap();
6129
6130        s.pipe.client.stream_send(0, &d, true).unwrap();
6131
6132        s.advance().ok();
6133
6134        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
6135
6136        assert_eq!(
6137            s.pipe.server.local_error.as_ref().unwrap().error_code,
6138            Error::to_wire(Error::ExcessiveLoad)
6139        );
6140    }
6141
6142    #[test]
6143    /// Tests that Error::TransportError contains a transport error.
6144    fn transport_error() {
6145        let mut s = Session::new().unwrap();
6146        s.handshake().unwrap();
6147
6148        let req = vec![
6149            Header::new(b":method", b"GET"),
6150            Header::new(b":scheme", b"https"),
6151            Header::new(b":authority", b"quic.tech"),
6152            Header::new(b":path", b"/test"),
6153            Header::new(b"user-agent", b"quiche-test"),
6154        ];
6155
6156        // We need to open all streams in the same flight, so we can't use the
6157        // Session::send_request() method because it also calls advance(),
6158        // otherwise the server would send a MAX_STREAMS frame and the client
6159        // wouldn't hit the streams limit.
6160        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6161        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(4));
6162        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(8));
6163        assert_eq!(
6164            s.client.send_request(&mut s.pipe.client, &req, true),
6165            Ok(12)
6166        );
6167        assert_eq!(
6168            s.client.send_request(&mut s.pipe.client, &req, true),
6169            Ok(16)
6170        );
6171
6172        assert_eq!(
6173            s.client.send_request(&mut s.pipe.client, &req, true),
6174            Err(Error::TransportError(crate::Error::StreamLimit))
6175        );
6176    }
6177
6178    #[test]
6179    /// Tests that sending DATA before HEADERS causes an error.
6180    fn data_before_headers() {
6181        let mut s = Session::new().unwrap();
6182        s.handshake().unwrap();
6183
6184        let mut d = [42; 128];
6185        let mut b = octets::OctetsMut::with_slice(&mut d);
6186
6187        let frame_type = b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
6188        s.pipe.client.stream_send(0, frame_type, false).unwrap();
6189
6190        let frame_len = b.put_varint(5).unwrap();
6191        s.pipe.client.stream_send(0, frame_len, false).unwrap();
6192
6193        s.pipe.client.stream_send(0, b"hello", false).unwrap();
6194
6195        s.advance().ok();
6196
6197        assert_eq!(
6198            s.server.poll(&mut s.pipe.server),
6199            Err(Error::FrameUnexpected)
6200        );
6201    }
6202
6203    #[test]
6204    /// Tests that calling poll() after an error occurred does nothing.
6205    fn poll_after_error() {
6206        let mut s = Session::new().unwrap();
6207        s.handshake().unwrap();
6208
6209        let mut d = [42; 128];
6210        let mut b = octets::OctetsMut::with_slice(&mut d);
6211
6212        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
6213        s.pipe.client.stream_send(0, frame_type, false).unwrap();
6214
6215        let frame_len = b.put_varint(1 << 24).unwrap();
6216        s.pipe.client.stream_send(0, frame_len, false).unwrap();
6217
6218        s.pipe.client.stream_send(0, &d, false).unwrap();
6219
6220        s.advance().ok();
6221
6222        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::ExcessiveLoad));
6223
6224        // Try to call poll() again after an error occurred.
6225        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
6226    }
6227
6228    #[test]
6229    /// Tests that we limit sending HEADERS based on the stream capacity.
6230    fn headers_blocked() {
6231        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6232        config
6233            .load_cert_chain_from_pem_file("examples/cert.crt")
6234            .unwrap();
6235        config
6236            .load_priv_key_from_pem_file("examples/cert.key")
6237            .unwrap();
6238        config.set_application_protos(&[b"h3"]).unwrap();
6239        config.set_initial_max_data(75);
6240        config.set_initial_max_stream_data_bidi_local(150);
6241        config.set_initial_max_stream_data_bidi_remote(150);
6242        config.set_initial_max_stream_data_uni(150);
6243        config.set_initial_max_streams_bidi(100);
6244        config.set_initial_max_streams_uni(5);
6245        config.verify_peer(false);
6246
6247        let h3_config = Config::new().unwrap();
6248
6249        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6250
6251        s.handshake().unwrap();
6252
6253        let req = vec![
6254            Header::new(b":method", b"GET"),
6255            Header::new(b":scheme", b"https"),
6256            Header::new(b":authority", b"quic.tech"),
6257            Header::new(b":path", b"/test"),
6258        ];
6259
6260        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6261
6262        assert_eq!(
6263            s.client.send_request(&mut s.pipe.client, &req, true),
6264            Err(Error::StreamBlocked)
6265        );
6266
6267        // Clear the writable stream queue.
6268        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6269        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6270        assert_eq!(s.pipe.client.stream_writable_next(), Some(10));
6271        assert_eq!(s.pipe.client.stream_writable_next(), None);
6272
6273        s.advance().ok();
6274
6275        // Once the server gives flow control credits back, we can send the
6276        // request.
6277        assert_eq!(s.pipe.client.stream_writable_next(), Some(4));
6278        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(4));
6279
6280        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
6281        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
6282        assert_eq!(s.pipe.server.data_blocked_recv_count, 1);
6283        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 0);
6284
6285        assert_eq!(s.pipe.client.data_blocked_sent_count, 1);
6286        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 0);
6287        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
6288        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
6289    }
6290
6291    #[test]
6292    /// Ensure StreamBlocked when connection flow control prevents headers.
6293    fn headers_blocked_on_conn() {
6294        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6295        config
6296            .load_cert_chain_from_pem_file("examples/cert.crt")
6297            .unwrap();
6298        config
6299            .load_priv_key_from_pem_file("examples/cert.key")
6300            .unwrap();
6301        config.set_application_protos(&[b"h3"]).unwrap();
6302        config.set_initial_max_data(75);
6303        config.set_initial_max_stream_data_bidi_local(150);
6304        config.set_initial_max_stream_data_bidi_remote(150);
6305        config.set_initial_max_stream_data_uni(150);
6306        config.set_initial_max_streams_bidi(100);
6307        config.set_initial_max_streams_uni(5);
6308        config.verify_peer(false);
6309
6310        let h3_config = Config::new().unwrap();
6311
6312        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6313
6314        s.handshake().unwrap();
6315
6316        // After the HTTP handshake, some bytes of connection flow control have
6317        // been consumed. Fill the connection with more grease data on the control
6318        // stream.
6319        let d = [42; 28];
6320        assert_eq!(s.pipe.client.stream_send(2, &d, false), Ok(23));
6321
6322        let req = vec![
6323            Header::new(b":method", b"GET"),
6324            Header::new(b":scheme", b"https"),
6325            Header::new(b":authority", b"quic.tech"),
6326            Header::new(b":path", b"/test"),
6327        ];
6328
6329        // There is 0 connection-level flow control, so sending a request is
6330        // blocked.
6331        assert_eq!(
6332            s.client.send_request(&mut s.pipe.client, &req, true),
6333            Err(Error::StreamBlocked)
6334        );
6335        assert_eq!(s.pipe.client.stream_writable_next(), None);
6336
6337        // Emit the control stream data and drain it at the server via poll() to
6338        // consumes it via poll() and gives back flow control.
6339        s.advance().ok();
6340        assert_eq!(s.poll_server(), Err(Error::Done));
6341        s.advance().ok();
6342
6343        // Now we can send the request.
6344        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6345        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6346        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6347
6348        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
6349        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
6350        assert_eq!(s.pipe.server.data_blocked_recv_count, 1);
6351        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 0);
6352
6353        assert_eq!(s.pipe.client.data_blocked_sent_count, 1);
6354        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 0);
6355        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
6356        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
6357    }
6358
6359    #[test]
6360    /// Ensure that the connection does not consume a stream ID when
6361    /// send_request fails with StreamBlocked due to hitting the
6362    /// MAX_DATA flow control limit when attempting to send headers.
6363    /// The headers are sent successfully after a MAX_DATA update.
6364    fn headers_blocked_by_max_data_success_on_retry() {
6365        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6366        config
6367            .load_cert_chain_from_pem_file("examples/cert.crt")
6368            .unwrap();
6369        config
6370            .load_priv_key_from_pem_file("examples/cert.key")
6371            .unwrap();
6372        config.set_application_protos(&[b"h3"]).unwrap();
6373        config.set_initial_max_data(70);
6374        config.set_initial_max_stream_data_bidi_local(150);
6375        config.set_initial_max_stream_data_bidi_remote(150);
6376        config.set_initial_max_stream_data_uni(150);
6377        config.set_initial_max_streams_bidi(100);
6378        config.set_initial_max_streams_uni(5);
6379        config.verify_peer(false);
6380
6381        let h3_config = Config::new().unwrap();
6382
6383        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6384
6385        s.handshake().unwrap();
6386
6387        let req = vec![
6388            Header::new(b":method", b"GET"),
6389            Header::new(b":scheme", b"https"),
6390            Header::new(b":authority", b"quic.tech"),
6391            Header::new(b":path", b"/test/with/long/url"),
6392        ];
6393
6394        // After the HTTP handshake, some bytes of connection flow
6395        // control have been consumed.  The serialized request does
6396        // not fit in the remaining connection-level flow control
6397        // limit.  send_request fails without creating a stream.
6398        assert_eq!(
6399            s.client.send_request(&mut s.pipe.client, &req, true),
6400            Err(Error::StreamBlocked)
6401        );
6402
6403        // Verify that stream 0 does not exist in the H3 stream map after the
6404        // failed attempt.
6405        assert!(!s.client.streams.contains_key(&0));
6406
6407        // Emit the control stream data and drain it at the server to give back
6408        // flow control.
6409        s.advance().ok();
6410        assert_eq!(s.poll_server(), Err(Error::Done));
6411        s.advance().ok();
6412
6413        // Now we can send the request. The stream ID should be 0 (not 4),
6414        // confirming that the blocked attempt did not consume the stream ID.
6415        let stream_id = s.client.send_request(&mut s.pipe.client, &req, true);
6416        assert_eq!(stream_id, Ok(0));
6417        assert!(s.client.streams.contains_key(&0));
6418        assert!(!s.client.streams.contains_key(&4));
6419
6420        // Subsequent request should use stream ID 4.
6421        let stream_id2 = s.client.send_request(&mut s.pipe.client, &req, true);
6422        assert_eq!(stream_id2, Ok(4));
6423        assert!(s.client.streams.contains_key(&0));
6424        assert!(s.client.streams.contains_key(&4));
6425
6426        s.advance().ok();
6427    }
6428
6429    #[test]
6430    /// Ensure STREAM_DATA_BLOCKED is not emitted multiple times with the same
6431    /// offset when trying to send large bodies.
6432    fn send_body_truncation_stream_blocked() {
6433        use crate::test_utils::decode_pkt;
6434
6435        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6436        config
6437            .load_cert_chain_from_pem_file("examples/cert.crt")
6438            .unwrap();
6439        config
6440            .load_priv_key_from_pem_file("examples/cert.key")
6441            .unwrap();
6442        config.set_application_protos(&[b"h3"]).unwrap();
6443        config.set_initial_max_data(10000); // large connection-level flow control
6444        config.set_initial_max_stream_data_bidi_local(80);
6445        config.set_initial_max_stream_data_bidi_remote(80);
6446        config.set_initial_max_stream_data_uni(150);
6447        config.set_initial_max_streams_bidi(100);
6448        config.set_initial_max_streams_uni(5);
6449        config.verify_peer(false);
6450
6451        let h3_config = Config::new().unwrap();
6452
6453        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6454
6455        s.handshake().unwrap();
6456
6457        let (stream, req) = s.send_request(true).unwrap();
6458
6459        let ev_headers = Event::Headers {
6460            list: req,
6461            more_frames: false,
6462        };
6463
6464        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6465        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6466
6467        let _ = s.send_response(stream, false).unwrap();
6468
6469        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6470
6471        // The body must be larger than the stream window would allow
6472        let d = [42; 500];
6473        let mut off = 0;
6474
6475        let sent = s
6476            .server
6477            .send_body(&mut s.pipe.server, stream, &d, true)
6478            .unwrap();
6479        assert_eq!(sent, 25);
6480        off += sent;
6481
6482        // send_body wrote as much as it could (sent < size of buff).
6483        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6484        assert_eq!(
6485            s.server
6486                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6487            Err(Error::Done)
6488        );
6489        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6490
6491        // Now read raw frames to see what the QUIC layer did
6492        let mut buf = [0; 65535];
6493        let (len, _) = s.pipe.server.send(&mut buf).unwrap();
6494
6495        let frames = decode_pkt(&mut s.pipe.client, &mut buf[..len]).unwrap();
6496
6497        let mut iter = frames.iter();
6498
6499        assert_eq!(
6500            iter.next(),
6501            Some(&crate::frame::Frame::StreamDataBlocked {
6502                stream_id: 0,
6503                limit: 80,
6504            })
6505        );
6506
6507        // At the server, after sending the STREAM_DATA_BLOCKED frame, we clear
6508        // the mark.
6509        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6510
6511        // Don't read any data from the client, so stream flow control is never
6512        // given back in the form of changing the stream's max offset.
6513        // Subsequent body send operations will still fail but no more
6514        // STREAM_DATA_BLOCKED frames should be submitted since the limit didn't
6515        // change. No frames means no packet to send.
6516        assert_eq!(
6517            s.server
6518                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6519            Err(Error::Done)
6520        );
6521        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6522        assert_eq!(s.pipe.server.send(&mut buf), Err(crate::Error::Done));
6523
6524        // Now update the client's max offset manually.
6525        let frames = [crate::frame::Frame::MaxStreamData {
6526            stream_id: 0,
6527            max: 100,
6528        }];
6529
6530        let pkt_type = crate::packet::Type::Short;
6531        assert_eq!(
6532            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
6533            Ok(39),
6534        );
6535
6536        let sent = s
6537            .server
6538            .send_body(&mut s.pipe.server, stream, &d[off..], true)
6539            .unwrap();
6540        assert_eq!(sent, 18);
6541
6542        // Same thing here...
6543        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6544        assert_eq!(
6545            s.server
6546                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6547            Err(Error::Done)
6548        );
6549        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6550
6551        let (len, _) = s.pipe.server.send(&mut buf).unwrap();
6552
6553        let frames = decode_pkt(&mut s.pipe.client, &mut buf[..len]).unwrap();
6554
6555        let mut iter = frames.iter();
6556
6557        assert_eq!(
6558            iter.next(),
6559            Some(&crate::frame::Frame::StreamDataBlocked {
6560                stream_id: 0,
6561                limit: 100,
6562            })
6563        );
6564    }
6565
6566    #[test]
6567    /// Ensure stream doesn't hang due to small cwnd.
6568    fn send_body_stream_blocked_by_small_cwnd() {
6569        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6570        config
6571            .load_cert_chain_from_pem_file("examples/cert.crt")
6572            .unwrap();
6573        config
6574            .load_priv_key_from_pem_file("examples/cert.key")
6575            .unwrap();
6576        config.set_application_protos(&[b"h3"]).unwrap();
6577        config.set_initial_max_data(100000); // large connection-level flow control
6578        config.set_initial_max_stream_data_bidi_local(100000);
6579        config.set_initial_max_stream_data_bidi_remote(50000);
6580        config.set_initial_max_stream_data_uni(150);
6581        config.set_initial_max_streams_bidi(100);
6582        config.set_initial_max_streams_uni(5);
6583        config.verify_peer(false);
6584
6585        let h3_config = Config::new().unwrap();
6586
6587        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6588
6589        s.handshake().unwrap();
6590
6591        let (stream, req) = s.send_request(true).unwrap();
6592
6593        let ev_headers = Event::Headers {
6594            list: req,
6595            more_frames: false,
6596        };
6597
6598        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6599        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6600
6601        let _ = s.send_response(stream, false).unwrap();
6602
6603        // Clear the writable stream queue.
6604        assert_eq!(s.pipe.server.stream_writable_next(), Some(3));
6605        assert_eq!(s.pipe.server.stream_writable_next(), Some(7));
6606        assert_eq!(s.pipe.server.stream_writable_next(), Some(11));
6607        assert_eq!(s.pipe.server.stream_writable_next(), Some(stream));
6608        assert_eq!(s.pipe.server.stream_writable_next(), None);
6609
6610        // The body must be larger than the cwnd would allow.
6611        let send_buf = [42; 80000];
6612
6613        let sent = s
6614            .server
6615            .send_body(&mut s.pipe.server, stream, &send_buf, true)
6616            .unwrap();
6617
6618        // send_body wrote as much as it could (sent < size of buff).
6619        assert_eq!(sent, 11995);
6620
6621        s.advance().ok();
6622
6623        // Client reads received headers and body.
6624        let mut recv_buf = [42; 80000];
6625        assert!(s.poll_client().is_ok());
6626        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6627        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(11995));
6628
6629        s.advance().ok();
6630
6631        // Server send cap is smaller than remaining body buffer.
6632        assert!(s.pipe.server.tx_cap < send_buf.len() - sent);
6633
6634        // Once the server cwnd opens up, we can send more body.
6635        assert_eq!(s.pipe.server.stream_writable_next(), Some(0));
6636    }
6637
6638    #[test]
6639    /// Ensure stream doesn't hang due to small cwnd.
6640    fn send_body_stream_blocked_zero_length() {
6641        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6642        config
6643            .load_cert_chain_from_pem_file("examples/cert.crt")
6644            .unwrap();
6645        config
6646            .load_priv_key_from_pem_file("examples/cert.key")
6647            .unwrap();
6648        config.set_application_protos(&[b"h3"]).unwrap();
6649        config.set_initial_max_data(100000); // large connection-level flow control
6650        config.set_initial_max_stream_data_bidi_local(100000);
6651        config.set_initial_max_stream_data_bidi_remote(50000);
6652        config.set_initial_max_stream_data_uni(150);
6653        config.set_initial_max_streams_bidi(100);
6654        config.set_initial_max_streams_uni(5);
6655        config.verify_peer(false);
6656
6657        let h3_config = Config::new().unwrap();
6658
6659        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6660
6661        s.handshake().unwrap();
6662
6663        let (stream, req) = s.send_request(true).unwrap();
6664
6665        let ev_headers = Event::Headers {
6666            list: req,
6667            more_frames: false,
6668        };
6669
6670        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6671        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6672
6673        let _ = s.send_response(stream, false).unwrap();
6674
6675        // Clear the writable stream queue.
6676        assert_eq!(s.pipe.server.stream_writable_next(), Some(3));
6677        assert_eq!(s.pipe.server.stream_writable_next(), Some(7));
6678        assert_eq!(s.pipe.server.stream_writable_next(), Some(11));
6679        assert_eq!(s.pipe.server.stream_writable_next(), Some(stream));
6680        assert_eq!(s.pipe.server.stream_writable_next(), None);
6681
6682        // The body is large enough to fill the cwnd, except for enough bytes
6683        // for another DATA frame header (but no payload).
6684        let send_buf = [42; 11994];
6685
6686        let sent = s
6687            .server
6688            .send_body(&mut s.pipe.server, stream, &send_buf, false)
6689            .unwrap();
6690
6691        assert_eq!(sent, 11994);
6692
6693        // There is only enough capacity left for the DATA frame header, but
6694        // no payload.
6695        assert_eq!(s.pipe.server.stream_capacity(stream).unwrap(), 3);
6696        assert_eq!(
6697            s.server
6698                .send_body(&mut s.pipe.server, stream, &send_buf, false),
6699            Err(Error::Done)
6700        );
6701
6702        s.advance().ok();
6703
6704        // Client reads received headers and body.
6705        let mut recv_buf = [42; 80000];
6706        assert!(s.poll_client().is_ok());
6707        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6708        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(11994));
6709
6710        s.advance().ok();
6711
6712        // Once the server cwnd opens up, we can send more body.
6713        assert_eq!(s.pipe.server.stream_writable_next(), Some(0));
6714    }
6715
6716    #[test]
6717    /// Test handling of 0-length DATA writes with and without fin.
6718    fn zero_length_data() {
6719        let mut s = Session::new().unwrap();
6720        s.handshake().unwrap();
6721
6722        let (stream, req) = s.send_request(false).unwrap();
6723
6724        assert_eq!(
6725            s.client.send_body(&mut s.pipe.client, 0, b"", false),
6726            Err(Error::Done)
6727        );
6728        assert_eq!(s.client.send_body(&mut s.pipe.client, 0, b"", true), Ok(0));
6729
6730        s.advance().ok();
6731
6732        let mut recv_buf = vec![0; 100];
6733
6734        let ev_headers = Event::Headers {
6735            list: req,
6736            more_frames: true,
6737        };
6738
6739        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6740
6741        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
6742        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Err(Error::Done));
6743
6744        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6745        assert_eq!(s.poll_server(), Err(Error::Done));
6746
6747        let resp = s.send_response(stream, false).unwrap();
6748
6749        assert_eq!(
6750            s.server.send_body(&mut s.pipe.server, 0, b"", false),
6751            Err(Error::Done)
6752        );
6753        assert_eq!(s.server.send_body(&mut s.pipe.server, 0, b"", true), Ok(0));
6754
6755        s.advance().ok();
6756
6757        let ev_headers = Event::Headers {
6758            list: resp,
6759            more_frames: true,
6760        };
6761
6762        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
6763
6764        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6765        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Err(Error::Done));
6766
6767        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
6768        assert_eq!(s.poll_client(), Err(Error::Done));
6769    }
6770
6771    #[test]
6772    /// Tests that blocked 0-length DATA writes are reported correctly.
6773    fn zero_length_data_blocked() {
6774        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6775        config
6776            .load_cert_chain_from_pem_file("examples/cert.crt")
6777            .unwrap();
6778        config
6779            .load_priv_key_from_pem_file("examples/cert.key")
6780            .unwrap();
6781        config.set_application_protos(&[b"h3"]).unwrap();
6782        config.set_initial_max_data(74);
6783        config.set_initial_max_stream_data_bidi_local(150);
6784        config.set_initial_max_stream_data_bidi_remote(150);
6785        config.set_initial_max_stream_data_uni(150);
6786        config.set_initial_max_streams_bidi(100);
6787        config.set_initial_max_streams_uni(5);
6788        config.verify_peer(false);
6789
6790        let h3_config = Config::new().unwrap();
6791
6792        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6793
6794        s.handshake().unwrap();
6795
6796        let req = vec![
6797            Header::new(b":method", b"GET"),
6798            Header::new(b":scheme", b"https"),
6799            Header::new(b":authority", b"quic.tech"),
6800            Header::new(b":path", b"/test"),
6801        ];
6802
6803        assert_eq!(
6804            s.client.send_request(&mut s.pipe.client, &req, false),
6805            Ok(0)
6806        );
6807
6808        assert_eq!(
6809            s.client.send_body(&mut s.pipe.client, 0, b"", true),
6810            Err(Error::Done)
6811        );
6812
6813        // Clear the writable stream queue.
6814        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6815        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6816        assert_eq!(s.pipe.client.stream_writable_next(), Some(10));
6817        assert_eq!(s.pipe.client.stream_writable_next(), None);
6818
6819        s.advance().ok();
6820
6821        // Once the server gives flow control credits back, we can send the body.
6822        assert_eq!(s.pipe.client.stream_writable_next(), Some(0));
6823        assert_eq!(s.client.send_body(&mut s.pipe.client, 0, b"", true), Ok(0));
6824    }
6825
6826    #[test]
6827    /// Tests that receiving an empty SETTINGS frame is handled and reported.
6828    fn empty_settings() {
6829        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6830        config
6831            .load_cert_chain_from_pem_file("examples/cert.crt")
6832            .unwrap();
6833        config
6834            .load_priv_key_from_pem_file("examples/cert.key")
6835            .unwrap();
6836        config.set_application_protos(&[b"h3"]).unwrap();
6837        config.set_initial_max_data(1500);
6838        config.set_initial_max_stream_data_bidi_local(150);
6839        config.set_initial_max_stream_data_bidi_remote(150);
6840        config.set_initial_max_stream_data_uni(150);
6841        config.set_initial_max_streams_bidi(5);
6842        config.set_initial_max_streams_uni(5);
6843        config.verify_peer(false);
6844        config.set_ack_delay_exponent(8);
6845        config.grease(false);
6846
6847        let h3_config = Config::new().unwrap();
6848        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6849
6850        s.handshake().unwrap();
6851
6852        assert!(s.client.peer_settings_raw().is_some());
6853        assert!(s.server.peer_settings_raw().is_some());
6854    }
6855
6856    #[test]
6857    /// Tests that receiving a H3_DATAGRAM setting is ok.
6858    fn dgram_setting() {
6859        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6860        config
6861            .load_cert_chain_from_pem_file("examples/cert.crt")
6862            .unwrap();
6863        config
6864            .load_priv_key_from_pem_file("examples/cert.key")
6865            .unwrap();
6866        config.set_application_protos(&[b"h3"]).unwrap();
6867        config.set_initial_max_data(70);
6868        config.set_initial_max_stream_data_bidi_local(150);
6869        config.set_initial_max_stream_data_bidi_remote(150);
6870        config.set_initial_max_stream_data_uni(150);
6871        config.set_initial_max_streams_bidi(100);
6872        config.set_initial_max_streams_uni(5);
6873        config.enable_dgram(true, 1000, 1000);
6874        config.verify_peer(false);
6875
6876        let h3_config = Config::new().unwrap();
6877
6878        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6879        assert_eq!(s.pipe.handshake(), Ok(()));
6880
6881        s.client.send_settings(&mut s.pipe.client).unwrap();
6882        assert_eq!(s.pipe.advance(), Ok(()));
6883
6884        // Before processing SETTINGS (via poll), HTTP/3 DATAGRAMS are not
6885        // enabled.
6886        assert!(!s.server.dgram_enabled_by_peer(&s.pipe.server));
6887
6888        // When everything is ok, poll returns Done and DATAGRAM is enabled.
6889        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
6890        assert!(s.server.dgram_enabled_by_peer(&s.pipe.server));
6891
6892        // Now detect things on the client
6893        s.server.send_settings(&mut s.pipe.server).unwrap();
6894        assert_eq!(s.pipe.advance(), Ok(()));
6895        assert!(!s.client.dgram_enabled_by_peer(&s.pipe.client));
6896        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::Done));
6897        assert!(s.client.dgram_enabled_by_peer(&s.pipe.client));
6898    }
6899
6900    #[test]
6901    /// Tests that receiving a H3_DATAGRAM setting when no TP is set generates
6902    /// an error.
6903    fn dgram_setting_no_tp() {
6904        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6905        config
6906            .load_cert_chain_from_pem_file("examples/cert.crt")
6907            .unwrap();
6908        config
6909            .load_priv_key_from_pem_file("examples/cert.key")
6910            .unwrap();
6911        config.set_application_protos(&[b"h3"]).unwrap();
6912        config.set_initial_max_data(70);
6913        config.set_initial_max_stream_data_bidi_local(150);
6914        config.set_initial_max_stream_data_bidi_remote(150);
6915        config.set_initial_max_stream_data_uni(150);
6916        config.set_initial_max_streams_bidi(100);
6917        config.set_initial_max_streams_uni(5);
6918        config.verify_peer(false);
6919
6920        let h3_config = Config::new().unwrap();
6921
6922        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6923        assert_eq!(s.pipe.handshake(), Ok(()));
6924
6925        s.client.control_stream_id = Some(
6926            s.client
6927                .open_uni_stream(
6928                    &mut s.pipe.client,
6929                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6930                )
6931                .unwrap(),
6932        );
6933
6934        let settings = frame::Frame::Settings {
6935            max_field_section_size: None,
6936            qpack_max_table_capacity: None,
6937            qpack_blocked_streams: None,
6938            connect_protocol_enabled: None,
6939            h3_datagram: Some(1),
6940            grease: None,
6941            additional_settings: Default::default(),
6942            raw: Default::default(),
6943        };
6944
6945        s.send_frame_client(settings, s.client.control_stream_id.unwrap(), false)
6946            .unwrap();
6947
6948        assert_eq!(s.pipe.advance(), Ok(()));
6949
6950        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::SettingsError));
6951    }
6952
6953    #[test]
6954    /// Tests that receiving SETTINGS with prohibited values generates an error.
6955    fn settings_h2_prohibited() {
6956        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6957        config
6958            .load_cert_chain_from_pem_file("examples/cert.crt")
6959            .unwrap();
6960        config
6961            .load_priv_key_from_pem_file("examples/cert.key")
6962            .unwrap();
6963        config.set_application_protos(&[b"h3"]).unwrap();
6964        config.set_initial_max_data(70);
6965        config.set_initial_max_stream_data_bidi_local(150);
6966        config.set_initial_max_stream_data_bidi_remote(150);
6967        config.set_initial_max_stream_data_uni(150);
6968        config.set_initial_max_streams_bidi(100);
6969        config.set_initial_max_streams_uni(5);
6970        config.verify_peer(false);
6971
6972        let h3_config = Config::new().unwrap();
6973
6974        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6975        assert_eq!(s.pipe.handshake(), Ok(()));
6976
6977        s.client.control_stream_id = Some(
6978            s.client
6979                .open_uni_stream(
6980                    &mut s.pipe.client,
6981                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6982                )
6983                .unwrap(),
6984        );
6985
6986        s.server.control_stream_id = Some(
6987            s.server
6988                .open_uni_stream(
6989                    &mut s.pipe.server,
6990                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6991                )
6992                .unwrap(),
6993        );
6994
6995        let frame_payload_len = 2u64;
6996        let settings = [
6997            frame::SETTINGS_FRAME_TYPE_ID as u8,
6998            frame_payload_len as u8,
6999            0x2, // 0x2 is a reserved setting type
7000            1,
7001        ];
7002
7003        s.send_arbitrary_stream_data_client(
7004            &settings,
7005            s.client.control_stream_id.unwrap(),
7006            false,
7007        )
7008        .unwrap();
7009
7010        s.send_arbitrary_stream_data_server(
7011            &settings,
7012            s.server.control_stream_id.unwrap(),
7013            false,
7014        )
7015        .unwrap();
7016
7017        assert_eq!(s.pipe.advance(), Ok(()));
7018
7019        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::SettingsError));
7020
7021        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::SettingsError));
7022    }
7023
7024    #[test]
7025    /// Tests that setting SETTINGS with prohibited values generates an error.
7026    fn set_prohibited_additional_settings() {
7027        let mut h3_config = Config::new().unwrap();
7028        assert_eq!(
7029            h3_config.set_additional_settings(vec![(
7030                frame::SETTINGS_QPACK_MAX_TABLE_CAPACITY,
7031                43
7032            )]),
7033            Err(Error::SettingsError)
7034        );
7035        assert_eq!(
7036            h3_config.set_additional_settings(vec![(
7037                frame::SETTINGS_MAX_FIELD_SECTION_SIZE,
7038                43
7039            )]),
7040            Err(Error::SettingsError)
7041        );
7042        assert_eq!(
7043            h3_config.set_additional_settings(vec![(
7044                frame::SETTINGS_QPACK_BLOCKED_STREAMS,
7045                43
7046            )]),
7047            Err(Error::SettingsError)
7048        );
7049        assert_eq!(
7050            h3_config.set_additional_settings(vec![(
7051                frame::SETTINGS_ENABLE_CONNECT_PROTOCOL,
7052                43
7053            )]),
7054            Err(Error::SettingsError)
7055        );
7056        assert_eq!(
7057            h3_config
7058                .set_additional_settings(vec![(frame::SETTINGS_H3_DATAGRAM, 43)]),
7059            Err(Error::SettingsError)
7060        );
7061    }
7062
7063    #[test]
7064    /// Tests that a client rejects a SETTINGS frame received on a request
7065    /// stream.
7066    fn settings_on_request_stream_client() {
7067        let mut s = Session::new().unwrap();
7068        s.handshake().unwrap();
7069
7070        let (stream, _req) = s.send_request(true).unwrap();
7071
7072        let settings = frame::Frame::Settings {
7073            max_field_section_size: None,
7074            qpack_max_table_capacity: None,
7075            qpack_blocked_streams: None,
7076            connect_protocol_enabled: None,
7077            h3_datagram: None,
7078            grease: None,
7079            additional_settings: Default::default(),
7080            raw: Default::default(),
7081        };
7082
7083        s.send_frame_server(settings, stream, false).unwrap();
7084
7085        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7086        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7087        assert_eq!(
7088            s.pipe.client.local_error(),
7089            Some(&crate::ConnectionError {
7090                is_app: true,
7091                error_code: WireErrorCode::FrameUnexpected as u64,
7092                reason: format!(
7093                    "Unexpected frame type {}",
7094                    frame::SETTINGS_FRAME_TYPE_ID
7095                )
7096                .into_bytes(),
7097            })
7098        );
7099    }
7100
7101    #[test]
7102    /// Tests that a client rejects a CANCEL_PUSH frame received on a request
7103    /// stream.
7104    fn cancel_push_on_request_stream_client() {
7105        let mut s = Session::new().unwrap();
7106        s.handshake().unwrap();
7107
7108        let (stream, _req) = s.send_request(true).unwrap();
7109        let cancel_push = frame::Frame::CancelPush { push_id: 0 };
7110        s.send_frame_server(cancel_push, stream, false).unwrap();
7111
7112        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7113        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7114        assert_eq!(
7115            s.pipe.client.local_error(),
7116            Some(&crate::ConnectionError {
7117                is_app: true,
7118                error_code: WireErrorCode::FrameUnexpected as u64,
7119                reason: format!(
7120                    "Unexpected frame type {}",
7121                    frame::CANCEL_PUSH_FRAME_TYPE_ID
7122                )
7123                .into_bytes(),
7124            })
7125        );
7126    }
7127
7128    #[test]
7129    /// Tests that a client rejects a GOAWAY frame received on a request
7130    /// stream.
7131    fn goaway_on_request_stream_client() {
7132        let mut s = Session::new().unwrap();
7133        s.handshake().unwrap();
7134
7135        let (stream, _req) = s.send_request(true).unwrap();
7136        let goaway = frame::Frame::GoAway { id: 0 };
7137
7138        s.send_frame_server(goaway, stream, false).unwrap();
7139
7140        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7141        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7142        assert_eq!(
7143            s.pipe.client.local_error(),
7144            Some(&crate::ConnectionError {
7145                is_app: true,
7146                error_code: WireErrorCode::FrameUnexpected as u64,
7147                reason: format!(
7148                    "Unexpected frame type {}",
7149                    frame::GOAWAY_FRAME_TYPE_ID
7150                )
7151                .into_bytes(),
7152            })
7153        );
7154    }
7155
7156    #[test]
7157    /// Tests that a client rejects a MAX_PUSH_ID frame received on a request
7158    /// stream.
7159    fn max_push_id_on_request_stream_client() {
7160        let mut s = Session::new().unwrap();
7161        s.handshake().unwrap();
7162
7163        let (stream, _req) = s.send_request(true).unwrap();
7164        let max_push_id = frame::Frame::MaxPushId { push_id: 0 };
7165
7166        s.send_frame_server(max_push_id, stream, false).unwrap();
7167
7168        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7169        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7170        assert_eq!(
7171            s.pipe.client.local_error(),
7172            Some(&crate::ConnectionError {
7173                is_app: true,
7174                error_code: WireErrorCode::FrameUnexpected as u64,
7175                reason: format!(
7176                    "Unexpected frame type {}",
7177                    frame::MAX_PUSH_FRAME_TYPE_ID
7178                )
7179                .into_bytes(),
7180            })
7181        );
7182    }
7183
7184    #[test]
7185    /// Tests additional settings are actually exchanged by the peers.
7186    fn set_additional_settings() {
7187        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7188        config
7189            .load_cert_chain_from_pem_file("examples/cert.crt")
7190            .unwrap();
7191        config
7192            .load_priv_key_from_pem_file("examples/cert.key")
7193            .unwrap();
7194        config.set_application_protos(&[b"h3"]).unwrap();
7195        config.set_initial_max_data(70);
7196        config.set_initial_max_stream_data_bidi_local(150);
7197        config.set_initial_max_stream_data_bidi_remote(150);
7198        config.set_initial_max_stream_data_uni(150);
7199        config.set_initial_max_streams_bidi(100);
7200        config.set_initial_max_streams_uni(5);
7201        config.verify_peer(false);
7202        config.grease(false);
7203
7204        let mut h3_config = Config::new().unwrap();
7205        h3_config
7206            .set_additional_settings(vec![(42, 43), (44, 45)])
7207            .unwrap();
7208
7209        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7210        assert_eq!(s.pipe.handshake(), Ok(()));
7211
7212        assert_eq!(s.pipe.advance(), Ok(()));
7213
7214        s.client.send_settings(&mut s.pipe.client).unwrap();
7215        assert_eq!(s.pipe.advance(), Ok(()));
7216        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
7217
7218        s.server.send_settings(&mut s.pipe.server).unwrap();
7219        assert_eq!(s.pipe.advance(), Ok(()));
7220        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::Done));
7221
7222        assert_eq!(
7223            s.server.peer_settings_raw(),
7224            Some(&[(6, 32_768), (42, 43), (44, 45)][..])
7225        );
7226        assert_eq!(
7227            s.client.peer_settings_raw(),
7228            Some(&[(6, 32_768), (42, 43), (44, 45)][..])
7229        );
7230    }
7231
7232    #[test]
7233    /// Send a single DATAGRAM.
7234    fn single_dgram() {
7235        let mut buf = [0; 65535];
7236        let mut s = Session::new().unwrap();
7237        s.handshake().unwrap();
7238
7239        // We'll send default data of 10 bytes on flow ID 0.
7240        let result = (11, 0, 1);
7241
7242        s.send_dgram_client(0).unwrap();
7243
7244        assert_eq!(s.poll_server(), Err(Error::Done));
7245        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7246
7247        s.send_dgram_server(0).unwrap();
7248        assert_eq!(s.poll_client(), Err(Error::Done));
7249        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7250    }
7251
7252    #[test]
7253    /// Send multiple DATAGRAMs.
7254    fn multiple_dgram() {
7255        let mut buf = [0; 65535];
7256        let mut s = Session::new().unwrap();
7257        s.handshake().unwrap();
7258
7259        // We'll send default data of 10 bytes on flow ID 0.
7260        let result = (11, 0, 1);
7261
7262        s.send_dgram_client(0).unwrap();
7263        s.send_dgram_client(0).unwrap();
7264        s.send_dgram_client(0).unwrap();
7265
7266        assert_eq!(s.poll_server(), Err(Error::Done));
7267        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7268        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7269        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7270        assert_eq!(s.recv_dgram_server(&mut buf), Err(Error::Done));
7271
7272        s.send_dgram_server(0).unwrap();
7273        s.send_dgram_server(0).unwrap();
7274        s.send_dgram_server(0).unwrap();
7275
7276        assert_eq!(s.poll_client(), Err(Error::Done));
7277        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7278        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7279        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7280        assert_eq!(s.recv_dgram_client(&mut buf), Err(Error::Done));
7281    }
7282
7283    #[test]
7284    /// Send more DATAGRAMs than the send queue allows.
7285    fn multiple_dgram_overflow() {
7286        let mut buf = [0; 65535];
7287        let mut s = Session::new().unwrap();
7288        s.handshake().unwrap();
7289
7290        // We'll send default data of 10 bytes on flow ID 0.
7291        let result = (11, 0, 1);
7292
7293        // Five DATAGRAMs
7294        s.send_dgram_client(0).unwrap();
7295        s.send_dgram_client(0).unwrap();
7296        s.send_dgram_client(0).unwrap();
7297        s.send_dgram_client(0).unwrap();
7298        s.send_dgram_client(0).unwrap();
7299
7300        // Only 3 independent DATAGRAMs to read events will fire.
7301        assert_eq!(s.poll_server(), Err(Error::Done));
7302        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7303        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7304        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7305        assert_eq!(s.recv_dgram_server(&mut buf), Err(Error::Done));
7306    }
7307
7308    #[test]
7309    /// Send a single DATAGRAM and request.
7310    fn poll_datagram_cycling_no_read() {
7311        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7312        config
7313            .load_cert_chain_from_pem_file("examples/cert.crt")
7314            .unwrap();
7315        config
7316            .load_priv_key_from_pem_file("examples/cert.key")
7317            .unwrap();
7318        config.set_application_protos(&[b"h3"]).unwrap();
7319        config.set_initial_max_data(1500);
7320        config.set_initial_max_stream_data_bidi_local(150);
7321        config.set_initial_max_stream_data_bidi_remote(150);
7322        config.set_initial_max_stream_data_uni(150);
7323        config.set_initial_max_streams_bidi(100);
7324        config.set_initial_max_streams_uni(5);
7325        config.verify_peer(false);
7326        config.enable_dgram(true, 100, 100);
7327
7328        let h3_config = Config::new().unwrap();
7329        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7330        s.handshake().unwrap();
7331
7332        // Send request followed by DATAGRAM on client side.
7333        let (stream, req) = s.send_request(false).unwrap();
7334
7335        s.send_body_client(stream, true).unwrap();
7336
7337        let ev_headers = Event::Headers {
7338            list: req,
7339            more_frames: true,
7340        };
7341
7342        s.send_dgram_client(0).unwrap();
7343
7344        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7345        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7346
7347        assert_eq!(s.poll_server(), Err(Error::Done));
7348    }
7349
7350    #[test]
7351    /// Send a single DATAGRAM and request.
7352    fn poll_datagram_single_read() {
7353        let mut buf = [0; 65535];
7354
7355        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7356        config
7357            .load_cert_chain_from_pem_file("examples/cert.crt")
7358            .unwrap();
7359        config
7360            .load_priv_key_from_pem_file("examples/cert.key")
7361            .unwrap();
7362        config.set_application_protos(&[b"h3"]).unwrap();
7363        config.set_initial_max_data(1500);
7364        config.set_initial_max_stream_data_bidi_local(150);
7365        config.set_initial_max_stream_data_bidi_remote(150);
7366        config.set_initial_max_stream_data_uni(150);
7367        config.set_initial_max_streams_bidi(100);
7368        config.set_initial_max_streams_uni(5);
7369        config.verify_peer(false);
7370        config.enable_dgram(true, 100, 100);
7371
7372        let h3_config = Config::new().unwrap();
7373        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7374        s.handshake().unwrap();
7375
7376        // We'll send default data of 10 bytes on flow ID 0.
7377        let result = (11, 0, 1);
7378
7379        // Send request followed by DATAGRAM on client side.
7380        let (stream, req) = s.send_request(false).unwrap();
7381
7382        let body = s.send_body_client(stream, true).unwrap();
7383
7384        let mut recv_buf = vec![0; body.len()];
7385
7386        let ev_headers = Event::Headers {
7387            list: req,
7388            more_frames: true,
7389        };
7390
7391        s.send_dgram_client(0).unwrap();
7392
7393        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7394        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7395
7396        assert_eq!(s.poll_server(), Err(Error::Done));
7397
7398        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7399
7400        assert_eq!(s.poll_server(), Err(Error::Done));
7401
7402        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7403        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7404        assert_eq!(s.poll_server(), Err(Error::Done));
7405
7406        // Send response followed by DATAGRAM on server side
7407        let resp = s.send_response(stream, false).unwrap();
7408
7409        let body = s.send_body_server(stream, true).unwrap();
7410
7411        let mut recv_buf = vec![0; body.len()];
7412
7413        let ev_headers = Event::Headers {
7414            list: resp,
7415            more_frames: true,
7416        };
7417
7418        s.send_dgram_server(0).unwrap();
7419
7420        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7421        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
7422
7423        assert_eq!(s.poll_client(), Err(Error::Done));
7424
7425        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7426
7427        assert_eq!(s.poll_client(), Err(Error::Done));
7428
7429        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
7430
7431        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7432        assert_eq!(s.poll_client(), Err(Error::Done));
7433    }
7434
7435    #[test]
7436    /// Send multiple DATAGRAMs and requests.
7437    fn poll_datagram_multi_read() {
7438        let mut buf = [0; 65535];
7439
7440        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7441        config
7442            .load_cert_chain_from_pem_file("examples/cert.crt")
7443            .unwrap();
7444        config
7445            .load_priv_key_from_pem_file("examples/cert.key")
7446            .unwrap();
7447        config.set_application_protos(&[b"h3"]).unwrap();
7448        config.set_initial_max_data(1500);
7449        config.set_initial_max_stream_data_bidi_local(150);
7450        config.set_initial_max_stream_data_bidi_remote(150);
7451        config.set_initial_max_stream_data_uni(150);
7452        config.set_initial_max_streams_bidi(100);
7453        config.set_initial_max_streams_uni(5);
7454        config.verify_peer(false);
7455        config.enable_dgram(true, 100, 100);
7456
7457        let h3_config = Config::new().unwrap();
7458        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7459        s.handshake().unwrap();
7460
7461        // 10 bytes on flow ID 0 and 2.
7462        let flow_0_result = (11, 0, 1);
7463        let flow_2_result = (11, 2, 1);
7464
7465        // Send requests followed by DATAGRAMs on client side.
7466        let (stream, req) = s.send_request(false).unwrap();
7467
7468        let body = s.send_body_client(stream, true).unwrap();
7469
7470        let mut recv_buf = vec![0; body.len()];
7471
7472        let ev_headers = Event::Headers {
7473            list: req,
7474            more_frames: true,
7475        };
7476
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(0).unwrap();
7481        s.send_dgram_client(0).unwrap();
7482        s.send_dgram_client(2).unwrap();
7483        s.send_dgram_client(2).unwrap();
7484        s.send_dgram_client(2).unwrap();
7485        s.send_dgram_client(2).unwrap();
7486        s.send_dgram_client(2).unwrap();
7487
7488        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7489        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7490
7491        assert_eq!(s.poll_server(), Err(Error::Done));
7492
7493        // Second cycle, start to read
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        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7499        assert_eq!(s.poll_server(), Err(Error::Done));
7500
7501        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7502        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7503
7504        assert_eq!(s.poll_server(), Err(Error::Done));
7505
7506        // Third cycle.
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_0_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        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7520        assert_eq!(s.poll_server(), Err(Error::Done));
7521
7522        // Send response followed by DATAGRAM on server side
7523        let resp = s.send_response(stream, false).unwrap();
7524
7525        let body = s.send_body_server(stream, true).unwrap();
7526
7527        let mut recv_buf = vec![0; body.len()];
7528
7529        let ev_headers = Event::Headers {
7530            list: resp,
7531            more_frames: true,
7532        };
7533
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(0).unwrap();
7538        s.send_dgram_server(0).unwrap();
7539        s.send_dgram_server(2).unwrap();
7540        s.send_dgram_server(2).unwrap();
7541        s.send_dgram_server(2).unwrap();
7542        s.send_dgram_server(2).unwrap();
7543        s.send_dgram_server(2).unwrap();
7544
7545        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7546        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
7547
7548        assert_eq!(s.poll_client(), Err(Error::Done));
7549
7550        // Second cycle, start to read
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        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7556        assert_eq!(s.poll_client(), Err(Error::Done));
7557
7558        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
7559        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7560
7561        assert_eq!(s.poll_client(), Err(Error::Done));
7562
7563        // Third cycle.
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_0_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        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7577        assert_eq!(s.poll_client(), Err(Error::Done));
7578    }
7579
7580    #[test]
7581    /// Tests that the Finished event is not issued for streams of unknown type
7582    /// (e.g. GREASE).
7583    fn finished_is_for_requests() {
7584        let mut s = Session::new().unwrap();
7585        s.handshake().unwrap();
7586
7587        assert_eq!(s.poll_client(), Err(Error::Done));
7588        assert_eq!(s.poll_server(), Err(Error::Done));
7589
7590        assert_eq!(s.client.open_grease_stream(&mut s.pipe.client), Ok(()));
7591        assert_eq!(s.pipe.advance(), Ok(()));
7592
7593        assert_eq!(s.poll_client(), Err(Error::Done));
7594        assert_eq!(s.poll_server(), Err(Error::Done));
7595    }
7596
7597    #[test]
7598    fn unknown_uni_stream_leaks_past_max_streams_uni() {
7599        let (mut config, h3_config) = Session::default_configs().unwrap();
7600        config.set_initial_max_data(100_000);
7601        config.set_initial_max_stream_data_uni(100_000);
7602        config.set_initial_max_streams_uni(5);
7603        config.grease(false);
7604
7605        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7606        s.handshake().unwrap();
7607
7608        let baseline = s.server.streams.len();
7609        let mut leaked_streams = Vec::new();
7610        let mut id = 14;
7611
7612        let n = 500;
7613        for i in 0..n {
7614            s.pipe
7615                .client
7616                .stream_send(id, &[0x21], true)
7617                .unwrap_or_else(|e| {
7618                    panic!(
7619                        "stream credit was not re-issued after {} streams: {:?}",
7620                        i, e
7621                    )
7622                });
7623
7624            s.pipe.advance().unwrap();
7625            assert_eq!(s.poll_server(), Err(Error::Done));
7626            assert!(s.pipe.server.streams.is_collected(id));
7627            s.pipe.advance().unwrap();
7628
7629            leaked_streams.push(id);
7630            id += 4;
7631        }
7632
7633        for id in leaked_streams {
7634            assert!(s.pipe.server.streams.is_collected(id));
7635        }
7636
7637        assert_eq!(
7638            s.server.streams.len(),
7639            baseline,
7640            "expected {} allocated streams in stream map after {} streams with unknown type",
7641            baseline,
7642            n,
7643        );
7644    }
7645
7646    #[test]
7647    fn empty_uni_stream_leaks_past_max_streams_uni() {
7648        let (mut config, h3_config) = Session::default_configs().unwrap();
7649        config.set_initial_max_data(100_000);
7650        config.set_initial_max_stream_data_uni(100_000);
7651        config.set_initial_max_streams_uni(5);
7652        config.grease(false);
7653
7654        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7655        s.handshake().unwrap();
7656
7657        let baseline = s.server.streams.len();
7658        let mut leaked_streams = Vec::new();
7659        let mut id = 14;
7660
7661        let n = 500;
7662        for i in 0..n {
7663            s.pipe
7664                .client
7665                .stream_send(id, &[], true)
7666                .unwrap_or_else(|e| {
7667                    panic!(
7668                        "stream credit was not re-issued after {} streams: {:?}",
7669                        i, e
7670                    )
7671                });
7672
7673            s.pipe.advance().unwrap();
7674            assert_eq!(s.poll_server(), Err(Error::Done));
7675            assert!(s.pipe.server.streams.is_collected(id));
7676            s.pipe.advance().unwrap();
7677
7678            leaked_streams.push(id);
7679            id += 4;
7680        }
7681
7682        for id in leaked_streams {
7683            assert!(s.pipe.server.streams.is_collected(id));
7684        }
7685
7686        assert_eq!(
7687            s.server.streams.len(),
7688            baseline,
7689            "expected {} allocated streams in stream map after {} streams without stream type",
7690            baseline,
7691            n,
7692        );
7693    }
7694
7695    #[test]
7696    /// Tests that streams are marked as finished only once.
7697    fn finished_once() {
7698        let mut s = Session::new().unwrap();
7699        s.handshake().unwrap();
7700
7701        let (stream, req) = s.send_request(false).unwrap();
7702        let body = s.send_body_client(stream, true).unwrap();
7703
7704        let mut recv_buf = vec![0; body.len()];
7705
7706        let ev_headers = Event::Headers {
7707            list: req,
7708            more_frames: true,
7709        };
7710
7711        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7712        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7713
7714        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7715        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7716
7717        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Err(Error::Done));
7718        assert_eq!(s.poll_server(), Err(Error::Done));
7719    }
7720
7721    #[test]
7722    /// Tests that the Data event is properly re-armed.
7723    fn data_event_rearm() {
7724        let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
7725
7726        let mut s = Session::new().unwrap();
7727        s.handshake().unwrap();
7728
7729        let (r1_id, r1_hdrs) = s.send_request(false).unwrap();
7730
7731        let mut recv_buf = vec![0; bytes.len()];
7732
7733        let r1_ev_headers = Event::Headers {
7734            list: r1_hdrs,
7735            more_frames: true,
7736        };
7737
7738        // Manually send an incomplete DATA frame (i.e. the frame size is longer
7739        // than the actual data sent).
7740        {
7741            let mut d = [42; 10];
7742            let mut b = octets::OctetsMut::with_slice(&mut d);
7743
7744            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7745            b.put_varint(bytes.len() as u64).unwrap();
7746            let off = b.off();
7747            s.pipe.client.stream_send(r1_id, &d[..off], false).unwrap();
7748
7749            assert_eq!(
7750                s.pipe.client.stream_send(r1_id, &bytes[..5], false),
7751                Ok(5)
7752            );
7753
7754            s.advance().ok();
7755        }
7756
7757        assert_eq!(s.poll_server(), Ok((r1_id, r1_ev_headers)));
7758        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7759        assert_eq!(s.poll_server(), Err(Error::Done));
7760
7761        // Read the available body data.
7762        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(5));
7763
7764        // Send the remaining DATA payload.
7765        assert_eq!(s.pipe.client.stream_send(r1_id, &bytes[5..], false), Ok(5));
7766        s.advance().ok();
7767
7768        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7769        assert_eq!(s.poll_server(), Err(Error::Done));
7770
7771        // Read the rest of the body data.
7772        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(5));
7773        assert_eq!(s.poll_server(), Err(Error::Done));
7774
7775        // Send more data.
7776        let r1_body = s.send_body_client(r1_id, false).unwrap();
7777
7778        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7779        assert_eq!(s.poll_server(), Err(Error::Done));
7780
7781        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(r1_body.len()));
7782
7783        // Send a new request to ensure cross-stream events don't break rearming.
7784        let (r2_id, r2_hdrs) = s.send_request(false).unwrap();
7785        let r2_ev_headers = Event::Headers {
7786            list: r2_hdrs,
7787            more_frames: true,
7788        };
7789        let r2_body = s.send_body_client(r2_id, false).unwrap();
7790
7791        s.advance().ok();
7792
7793        assert_eq!(s.poll_server(), Ok((r2_id, r2_ev_headers)));
7794        assert_eq!(s.poll_server(), Ok((r2_id, Event::Data)));
7795        assert_eq!(s.recv_body_server(r2_id, &mut recv_buf), Ok(r2_body.len()));
7796        assert_eq!(s.poll_server(), Err(Error::Done));
7797
7798        // Send more data on request 1, then trailing HEADERS.
7799        let r1_body = s.send_body_client(r1_id, false).unwrap();
7800
7801        let trailers = vec![Header::new(b"hello", b"world")];
7802
7803        s.client
7804            .send_headers(&mut s.pipe.client, r1_id, &trailers, true)
7805            .unwrap();
7806
7807        let r1_ev_trailers = Event::Headers {
7808            list: trailers.clone(),
7809            more_frames: false,
7810        };
7811
7812        s.advance().ok();
7813
7814        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7815        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(r1_body.len()));
7816
7817        assert_eq!(s.poll_server(), Ok((r1_id, r1_ev_trailers)));
7818        assert_eq!(s.poll_server(), Ok((r1_id, Event::Finished)));
7819        assert_eq!(s.poll_server(), Err(Error::Done));
7820
7821        // Send more data on request 2, then trailing HEADERS.
7822        let r2_body = s.send_body_client(r2_id, false).unwrap();
7823
7824        s.client
7825            .send_headers(&mut s.pipe.client, r2_id, &trailers, false)
7826            .unwrap();
7827
7828        let r2_ev_trailers = Event::Headers {
7829            list: trailers,
7830            more_frames: true,
7831        };
7832
7833        s.advance().ok();
7834
7835        assert_eq!(s.poll_server(), Ok((r2_id, Event::Data)));
7836        assert_eq!(s.recv_body_server(r2_id, &mut recv_buf), Ok(r2_body.len()));
7837        assert_eq!(s.poll_server(), Ok((r2_id, r2_ev_trailers)));
7838        assert_eq!(s.poll_server(), Err(Error::Done));
7839
7840        let (r3_id, r3_hdrs) = s.send_request(false).unwrap();
7841
7842        let r3_ev_headers = Event::Headers {
7843            list: r3_hdrs,
7844            more_frames: true,
7845        };
7846
7847        // Manually send an incomplete DATA frame (i.e. only the header is sent).
7848        {
7849            let mut d = [42; 10];
7850            let mut b = octets::OctetsMut::with_slice(&mut d);
7851
7852            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7853            b.put_varint(bytes.len() as u64).unwrap();
7854            let off = b.off();
7855            s.pipe.client.stream_send(r3_id, &d[..off], false).unwrap();
7856
7857            s.advance().ok();
7858        }
7859
7860        assert_eq!(s.poll_server(), Ok((r3_id, r3_ev_headers)));
7861        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7862        assert_eq!(s.poll_server(), Err(Error::Done));
7863
7864        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Err(Error::Done));
7865
7866        assert_eq!(s.pipe.client.stream_send(r3_id, &bytes[..5], false), Ok(5));
7867
7868        s.advance().ok();
7869
7870        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7871        assert_eq!(s.poll_server(), Err(Error::Done));
7872
7873        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(5));
7874
7875        assert_eq!(s.pipe.client.stream_send(r3_id, &bytes[5..], false), Ok(5));
7876        s.advance().ok();
7877
7878        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7879        assert_eq!(s.poll_server(), Err(Error::Done));
7880
7881        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(5));
7882
7883        // Buffer multiple data frames.
7884        let body = s.send_body_client(r3_id, false).unwrap();
7885        s.send_body_client(r3_id, false).unwrap();
7886        s.send_body_client(r3_id, false).unwrap();
7887
7888        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7889        assert_eq!(s.poll_server(), Err(Error::Done));
7890
7891        {
7892            let mut d = [42; 10];
7893            let mut b = octets::OctetsMut::with_slice(&mut d);
7894
7895            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7896            b.put_varint(0).unwrap();
7897            let off = b.off();
7898            s.pipe.client.stream_send(r3_id, &d[..off], true).unwrap();
7899
7900            s.advance().ok();
7901        }
7902
7903        let mut recv_buf = vec![0; bytes.len() * 3];
7904
7905        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(body.len() * 3));
7906    }
7907
7908    #[test]
7909    /// Tests that the Datagram event is properly re-armed.
7910    fn dgram_event_rearm() {
7911        let mut buf = [0; 65535];
7912
7913        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7914        config
7915            .load_cert_chain_from_pem_file("examples/cert.crt")
7916            .unwrap();
7917        config
7918            .load_priv_key_from_pem_file("examples/cert.key")
7919            .unwrap();
7920        config.set_application_protos(&[b"h3"]).unwrap();
7921        config.set_initial_max_data(1500);
7922        config.set_initial_max_stream_data_bidi_local(150);
7923        config.set_initial_max_stream_data_bidi_remote(150);
7924        config.set_initial_max_stream_data_uni(150);
7925        config.set_initial_max_streams_bidi(100);
7926        config.set_initial_max_streams_uni(5);
7927        config.verify_peer(false);
7928        config.enable_dgram(true, 100, 100);
7929
7930        let h3_config = Config::new().unwrap();
7931        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7932        s.handshake().unwrap();
7933
7934        // 10 bytes on flow ID 0 and 2.
7935        let flow_0_result = (11, 0, 1);
7936        let flow_2_result = (11, 2, 1);
7937
7938        // Send requests followed by DATAGRAMs on client side.
7939        let (stream, req) = s.send_request(false).unwrap();
7940
7941        let body = s.send_body_client(stream, true).unwrap();
7942
7943        let mut recv_buf = vec![0; body.len()];
7944
7945        let ev_headers = Event::Headers {
7946            list: req,
7947            more_frames: true,
7948        };
7949
7950        s.send_dgram_client(0).unwrap();
7951        s.send_dgram_client(0).unwrap();
7952        s.send_dgram_client(2).unwrap();
7953        s.send_dgram_client(2).unwrap();
7954
7955        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7956        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7957
7958        assert_eq!(s.poll_server(), Err(Error::Done));
7959        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7960
7961        assert_eq!(s.poll_server(), Err(Error::Done));
7962        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7963
7964        assert_eq!(s.poll_server(), Err(Error::Done));
7965        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7966
7967        assert_eq!(s.poll_server(), Err(Error::Done));
7968        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7969
7970        assert_eq!(s.poll_server(), Err(Error::Done));
7971
7972        s.send_dgram_client(0).unwrap();
7973        s.send_dgram_client(2).unwrap();
7974
7975        assert_eq!(s.poll_server(), Err(Error::Done));
7976
7977        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7978        assert_eq!(s.poll_server(), Err(Error::Done));
7979
7980        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7981        assert_eq!(s.poll_server(), Err(Error::Done));
7982
7983        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7984        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7985
7986        // Verify that dgram counts are incremented.
7987        assert_eq!(s.pipe.client.dgram_sent_count, 6);
7988        assert_eq!(s.pipe.client.dgram_recv_count, 0);
7989        assert_eq!(s.pipe.server.dgram_sent_count, 0);
7990        assert_eq!(s.pipe.server.dgram_recv_count, 6);
7991
7992        let server_path = s.pipe.server.paths.get_active().expect("no active");
7993        let client_path = s.pipe.client.paths.get_active().expect("no active");
7994        assert_eq!(client_path.dgram_sent_count, 6);
7995        assert_eq!(client_path.dgram_recv_count, 0);
7996        assert_eq!(server_path.dgram_sent_count, 0);
7997        assert_eq!(server_path.dgram_recv_count, 6);
7998    }
7999
8000    #[test]
8001    fn reset_stream() {
8002        let mut buf = [0; 65535];
8003
8004        let mut s = Session::new().unwrap();
8005        s.handshake().unwrap();
8006
8007        // Client sends request.
8008        let (stream, req) = s.send_request(false).unwrap();
8009
8010        let ev_headers = Event::Headers {
8011            list: req,
8012            more_frames: true,
8013        };
8014
8015        // Server sends response and closes stream.
8016        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8017        assert_eq!(s.poll_server(), Err(Error::Done));
8018
8019        let resp = s.send_response(stream, true).unwrap();
8020
8021        let ev_headers = Event::Headers {
8022            list: resp,
8023            more_frames: false,
8024        };
8025
8026        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8027        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8028        assert_eq!(s.poll_client(), Err(Error::Done));
8029
8030        // Client sends RESET_STREAM, closing stream.
8031        let frames = [crate::frame::Frame::ResetStream {
8032            stream_id: stream,
8033            error_code: 42,
8034            final_size: 68,
8035        }];
8036
8037        let pkt_type = crate::packet::Type::Short;
8038        assert_eq!(
8039            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
8040            Ok(39)
8041        );
8042
8043        // Server issues Reset event for the stream.
8044        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
8045        assert_eq!(s.poll_server(), Err(Error::Done));
8046
8047        // Sending RESET_STREAM again shouldn't trigger another Reset event.
8048        assert_eq!(
8049            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
8050            Ok(39)
8051        );
8052
8053        assert_eq!(s.poll_server(), Err(Error::Done));
8054    }
8055
8056    /// The client shuts down the stream's write direction, the server
8057    /// shuts down its side with fin
8058    #[test]
8059    fn client_shutdown_write_server_fin() {
8060        let mut buf = [0; 65535];
8061        let mut s = Session::new().unwrap();
8062        s.handshake().unwrap();
8063
8064        // Client sends request.
8065        let (stream, req) = s.send_request(false).unwrap();
8066
8067        let ev_headers = Event::Headers {
8068            list: req,
8069            more_frames: true,
8070        };
8071
8072        // Server sends response and closes stream.
8073        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8074        assert_eq!(s.poll_server(), Err(Error::Done));
8075
8076        let resp = s.send_response(stream, true).unwrap();
8077
8078        let ev_headers = Event::Headers {
8079            list: resp,
8080            more_frames: false,
8081        };
8082
8083        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8084        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8085        assert_eq!(s.poll_client(), Err(Error::Done));
8086
8087        // Client shuts down stream ==> sends RESET_STREAM
8088        assert_eq!(
8089            s.pipe
8090                .client
8091                .stream_shutdown(stream, crate::Shutdown::Write, 42),
8092            Ok(())
8093        );
8094        assert_eq!(s.advance(), Ok(()));
8095
8096        // Server sees the Reset event for the stream.
8097        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
8098        assert_eq!(s.poll_server(), Err(Error::Done));
8099
8100        // Streams have been collected by quiche
8101        assert!(s.pipe.server.streams.is_collected(stream));
8102        assert!(s.pipe.client.streams.is_collected(stream));
8103
8104        // Client sends another request, server sends response without fin
8105        //
8106        let (stream, req) = s.send_request(false).unwrap();
8107
8108        let ev_headers = Event::Headers {
8109            list: req,
8110            more_frames: true,
8111        };
8112
8113        // Check that server has received the request.
8114        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8115        assert_eq!(s.poll_server(), Err(Error::Done));
8116
8117        // Server sends reponse without closing the stream.
8118        let resp = s.send_response(stream, false).unwrap();
8119
8120        let ev_headers = Event::Headers {
8121            list: resp,
8122            more_frames: true,
8123        };
8124
8125        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8126        assert_eq!(s.poll_client(), Err(Error::Done));
8127
8128        // Client shuts down stream ==> sends RESET_STREAM
8129        assert_eq!(
8130            s.pipe
8131                .client
8132                .stream_shutdown(stream, crate::Shutdown::Write, 42),
8133            Ok(())
8134        );
8135        assert_eq!(s.advance(), Ok(()));
8136
8137        // Server sees the Reset event for the stream.
8138        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
8139        assert_eq!(s.poll_server(), Err(Error::Done));
8140
8141        // Server sends body and closes the stream.
8142        s.send_body_server(stream, true).unwrap();
8143
8144        // Stream has been collected on server by quiche
8145        assert!(s.pipe.server.streams.is_collected(stream));
8146        // Client stream has not been collected, the client needs to
8147        // read the fin from the stream first.
8148        assert!(!s.pipe.client.streams.is_collected(stream));
8149        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8150        s.recv_body_client(stream, &mut buf).unwrap();
8151        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8152        assert_eq!(s.poll_client(), Err(Error::Done));
8153        assert!(s.pipe.client.streams.is_collected(stream));
8154    }
8155
8156    #[test]
8157    fn client_shutdown_read() {
8158        let mut buf = [0; 65535];
8159        let mut s = Session::new().unwrap();
8160        s.handshake().unwrap();
8161
8162        // Client sends request and leaves stream open.
8163        let (stream, req) = s.send_request(false).unwrap();
8164
8165        let ev_headers = Event::Headers {
8166            list: req,
8167            more_frames: true,
8168        };
8169
8170        // Server sends response and leaves stream open.
8171        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8172        assert_eq!(s.poll_server(), Err(Error::Done));
8173
8174        let resp = s.send_response(stream, false).unwrap();
8175
8176        let ev_headers = Event::Headers {
8177            list: resp,
8178            more_frames: true,
8179        };
8180
8181        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8182        assert_eq!(s.poll_client(), Err(Error::Done));
8183        // Client shuts down read
8184        assert_eq!(
8185            s.pipe
8186                .client
8187                .stream_shutdown(stream, crate::Shutdown::Read, 42),
8188            Ok(())
8189        );
8190        assert_eq!(s.advance(), Ok(()));
8191
8192        // Stream is writable on server side, but returns StreamStopped
8193        assert_eq!(s.poll_server(), Err(Error::Done));
8194        let writables: Vec<u64> = s.pipe.server.writable().collect();
8195        assert!(writables.contains(&stream));
8196        assert_eq!(
8197            s.send_body_server(stream, false),
8198            Err(Error::TransportError(crate::Error::StreamStopped(42)))
8199        );
8200
8201        // Client needs to finish its side by sending a fin
8202        assert_eq!(
8203            s.client.send_body(&mut s.pipe.client, stream, &[], true),
8204            Ok(0)
8205        );
8206        assert_eq!(s.advance(), Ok(()));
8207        // Note, we get an Event::Data for an empty buffer today. But it
8208        // would also be fine to not get it.
8209        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8210        assert_eq!(s.recv_body_server(stream, &mut buf), Err(Error::Done));
8211        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8212        assert_eq!(s.poll_server(), Err(Error::Done));
8213
8214        // Since the client has already send a fin, the stream is collected
8215        // on both client and server
8216        assert!(s.pipe.client.streams.is_collected(stream));
8217        assert!(s.pipe.server.streams.is_collected(stream));
8218    }
8219
8220    #[test]
8221    fn reset_finished_at_server() {
8222        let mut s = Session::new().unwrap();
8223        s.handshake().unwrap();
8224
8225        // Client sends HEADERS and doesn't fin
8226        let (stream, _req) = s.send_request(false).unwrap();
8227
8228        // ..then Client sends RESET_STREAM
8229        assert_eq!(
8230            s.pipe.client.stream_shutdown(0, crate::Shutdown::Write, 0),
8231            Ok(())
8232        );
8233
8234        assert_eq!(s.pipe.advance(), Ok(()));
8235
8236        // Server receives just a reset
8237        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(0))));
8238        assert_eq!(s.poll_server(), Err(Error::Done));
8239
8240        // Client sends HEADERS and fin
8241        let (stream, req) = s.send_request(true).unwrap();
8242
8243        // ..then Client sends RESET_STREAM
8244        assert_eq!(
8245            s.pipe.client.stream_shutdown(4, crate::Shutdown::Write, 0),
8246            Ok(())
8247        );
8248
8249        let ev_headers = Event::Headers {
8250            list: req,
8251            more_frames: false,
8252        };
8253
8254        // Server receives headers and fin.
8255        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8256        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8257        assert_eq!(s.poll_server(), Err(Error::Done));
8258    }
8259
8260    #[test]
8261    fn reset_finished_at_server_with_data_pending() {
8262        let mut s = Session::new().unwrap();
8263        s.handshake().unwrap();
8264
8265        // Client sends HEADERS and doesn't fin.
8266        let (stream, req) = s.send_request(false).unwrap();
8267
8268        assert!(s.send_body_client(stream, false).is_ok());
8269
8270        assert_eq!(s.pipe.advance(), Ok(()));
8271
8272        let ev_headers = Event::Headers {
8273            list: req,
8274            more_frames: true,
8275        };
8276
8277        // Server receives headers and data...
8278        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8279        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8280
8281        // ..then Client sends RESET_STREAM.
8282        assert_eq!(
8283            s.pipe
8284                .client
8285                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8286            Ok(())
8287        );
8288
8289        assert_eq!(s.pipe.advance(), Ok(()));
8290
8291        // The server does *not* attempt to read from the stream,
8292        // but polls and receives the reset and there are no more
8293        // readable streams.
8294        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(0))));
8295        assert_eq!(s.poll_server(), Err(Error::Done));
8296        assert_eq!(s.pipe.server.readable().len(), 0);
8297    }
8298
8299    #[test]
8300    fn reset_finished_at_server_with_data_pending_2() {
8301        let mut s = Session::new().unwrap();
8302        s.handshake().unwrap();
8303
8304        // Client sends HEADERS and doesn't fin.
8305        let (stream, req) = s.send_request(false).unwrap();
8306
8307        assert!(s.send_body_client(stream, false).is_ok());
8308
8309        assert_eq!(s.pipe.advance(), Ok(()));
8310
8311        let ev_headers = Event::Headers {
8312            list: req,
8313            more_frames: true,
8314        };
8315
8316        // Server receives headers and data...
8317        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8318        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8319
8320        // ..then Client sends RESET_STREAM.
8321        assert_eq!(
8322            s.pipe
8323                .client
8324                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8325            Ok(())
8326        );
8327
8328        assert_eq!(s.pipe.advance(), Ok(()));
8329
8330        // Server reads from the stream and receives the reset while
8331        // attempting to read.
8332        assert_eq!(
8333            s.recv_body_server(stream, &mut [0; 100]),
8334            Err(Error::TransportError(crate::Error::StreamReset(0)))
8335        );
8336
8337        // No more events and there are no more readable streams.
8338        assert_eq!(s.poll_server(), Err(Error::Done));
8339        assert_eq!(s.pipe.server.readable().len(), 0);
8340    }
8341
8342    #[test]
8343    fn reset_finished_at_client() {
8344        let mut buf = [0; 65535];
8345        let mut s = Session::new().unwrap();
8346        s.handshake().unwrap();
8347
8348        // Client sends HEADERS and doesn't fin
8349        let (stream, req) = s.send_request(false).unwrap();
8350
8351        let ev_headers = Event::Headers {
8352            list: req,
8353            more_frames: true,
8354        };
8355
8356        // Server receives headers.
8357        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8358        assert_eq!(s.poll_server(), Err(Error::Done));
8359
8360        // Server sends response and doesn't fin
8361        s.send_response(stream, false).unwrap();
8362
8363        assert_eq!(s.pipe.advance(), Ok(()));
8364
8365        // .. then Server sends RESET_STREAM
8366        assert_eq!(
8367            s.pipe
8368                .server
8369                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8370            Ok(())
8371        );
8372
8373        assert_eq!(s.pipe.advance(), Ok(()));
8374
8375        // Client receives Reset only
8376        assert_eq!(s.poll_client(), Ok((stream, Event::Reset(0))));
8377        assert_eq!(s.poll_server(), Err(Error::Done));
8378
8379        // Client sends headers and fin.
8380        let (stream, req) = s.send_request(true).unwrap();
8381
8382        let ev_headers = Event::Headers {
8383            list: req,
8384            more_frames: false,
8385        };
8386
8387        // Server receives headers and fin.
8388        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8389        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8390        assert_eq!(s.poll_server(), Err(Error::Done));
8391
8392        // Server sends response and fin
8393        let resp = s.send_response(stream, true).unwrap();
8394
8395        assert_eq!(s.pipe.advance(), Ok(()));
8396
8397        // ..then Server sends RESET_STREAM
8398        let frames = [crate::frame::Frame::ResetStream {
8399            stream_id: stream,
8400            error_code: 42,
8401            final_size: 68,
8402        }];
8403
8404        let pkt_type = crate::packet::Type::Short;
8405        assert_eq!(
8406            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
8407            Ok(39)
8408        );
8409
8410        assert_eq!(s.pipe.advance(), Ok(()));
8411
8412        let ev_headers = Event::Headers {
8413            list: resp,
8414            more_frames: false,
8415        };
8416
8417        // Client receives headers and fin.
8418        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8419        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8420        assert_eq!(s.poll_client(), Err(Error::Done));
8421    }
8422
8423    #[test]
8424    fn collect_completed_streams() {
8425        let mut s = Session::new().unwrap();
8426        s.handshake().unwrap();
8427
8428        let init_streams_client = s.client.streams.len();
8429        let init_streams_server = s.server.streams.len();
8430
8431        // Client sends HEADERS and doesn't fin
8432        let (stream, req) = s.send_request(false).unwrap();
8433
8434        let ev_headers = Event::Headers {
8435            list: req,
8436            more_frames: true,
8437        };
8438
8439        // Server receives headers.
8440        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8441        assert_eq!(s.poll_server(), Err(Error::Done));
8442
8443        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8444        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8445
8446        // Client sends body and fin
8447        let body = s.send_body_client(stream, true).unwrap();
8448
8449        let mut recv_buf = vec![0; body.len()];
8450
8451        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8452        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
8453
8454        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8455
8456        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8457        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8458
8459        // Server sends response and finishes the stream
8460        let resp_headers = s.send_response(stream, false).unwrap();
8461        s.send_body_server(stream, true).unwrap();
8462
8463        let ev_headers = Event::Headers {
8464            list: resp_headers,
8465            more_frames: true,
8466        };
8467
8468        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8469        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8470        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
8471
8472        // The server stream should be gone now
8473        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8474        assert_eq!(s.server.streams.len(), init_streams_server);
8475
8476        // Polling again should clean up the client
8477        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8478        assert_eq!(s.poll_client(), Err(Error::Done));
8479
8480        assert_eq!(s.client.streams.len(), init_streams_client);
8481    }
8482
8483    #[test]
8484    fn collect_reset_streams() {
8485        let mut s = Session::new().unwrap();
8486        s.handshake().unwrap();
8487
8488        let init_streams_client = s.client.streams.len();
8489        let init_streams_server = s.server.streams.len();
8490
8491        // Client sends HEADERS and doesn't fin
8492        let (stream, req) = s.send_request(false).unwrap();
8493
8494        let ev_headers = Event::Headers {
8495            list: req,
8496            more_frames: true,
8497        };
8498
8499        // Server receives headers.
8500        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8501        assert_eq!(s.poll_server(), Err(Error::Done));
8502
8503        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8504        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8505
8506        // Client sends body and fin
8507        let body = s.send_body_client(stream, true).unwrap();
8508
8509        let mut recv_buf = vec![0; body.len()];
8510
8511        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8512        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
8513
8514        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8515
8516        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8517        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8518
8519        // Server sends response and resets the stream.
8520        s.send_response(stream, false).unwrap();
8521        s.pipe
8522            .server
8523            .stream_shutdown(stream, crate::Shutdown::Write, 0)
8524            .unwrap();
8525
8526        s.advance().ok();
8527
8528        // TODO: need to notify resets better.
8529        //
8530        // This will trigger the h3 layer to check for the stream resets,
8531        // otherwise it wouldn't know that a reset happened.
8532        //
8533        // We will need to figure out a way to do this automatically to avoid
8534        // requiring applications to do this manually. For now just keep this
8535        // for testing purposes.
8536        let _ = s.send_body_server(stream, true);
8537
8538        assert_eq!(s.poll_server(), Err(Error::Done));
8539
8540        assert_eq!(s.poll_client(), Ok((stream, Event::Reset(0))));
8541        assert_eq!(s.poll_client(), Err(Error::Done));
8542
8543        // The server stream should be gone now
8544        assert_eq!(s.client.streams.len(), init_streams_client);
8545        assert_eq!(s.server.streams.len(), init_streams_server);
8546    }
8547}
8548
8549#[cfg(feature = "ffi")]
8550mod ffi;
8551#[cfg(feature = "internal")]
8552#[doc(hidden)]
8553pub mod frame;
8554#[cfg(not(feature = "internal"))]
8555mod frame;
8556#[doc(hidden)]
8557pub mod qpack;
8558mod stream;