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
2958            _ => (),
2959        };
2960    }
2961
2962    fn finish_local_stream<F: BufFactory>(
2963        &mut self, conn: &super::Connection<F>, stream_id: u64,
2964        initialize_local: bool,
2965    ) {
2966        let hash_map::Entry::Occupied(mut stream) = self.streams.entry(stream_id)
2967        else {
2968            return;
2969        };
2970
2971        {
2972            let stream = stream.get_mut();
2973
2974            if initialize_local {
2975                stream.initialize_local();
2976            }
2977
2978            stream.finish_local();
2979        }
2980
2981        if conn.stream_finished(stream_id) {
2982            stream.remove();
2983        }
2984    }
2985
2986    fn remove_local_finished_stream(&mut self, stream_id: u64) {
2987        if let hash_map::Entry::Occupied(stream) = self.streams.entry(stream_id) {
2988            if stream.get().local_finished() {
2989                stream.remove();
2990            }
2991        }
2992    }
2993
2994    fn pop_finished_stream<F: BufFactory>(
2995        &mut self, conn: &mut super::Connection<F>,
2996    ) -> Option<(u64, Event)> {
2997        let finished = self.finished_streams.pop_front()?;
2998
2999        self.remove_local_finished_stream(finished);
3000
3001        if conn.stream_readable(finished) {
3002            // The stream is finished, but is still readable, it may indicate
3003            // that there is a pending error, such as reset.
3004            if let Err(crate::Error::StreamReset(e)) =
3005                conn.stream_recv(finished, &mut [])
3006            {
3007                return Some((finished, Event::Reset(e)));
3008            }
3009        }
3010
3011        Some((finished, Event::Finished))
3012    }
3013
3014    fn process_frame<F: BufFactory>(
3015        &mut self, conn: &mut super::Connection<F>, stream_id: u64,
3016        frame: frame::Frame, payload_len: u64,
3017    ) -> Result<(u64, Event)> {
3018        trace!(
3019            "{} rx frm {:?} stream={} payload_len={}",
3020            conn.trace_id(),
3021            frame,
3022            stream_id,
3023            payload_len
3024        );
3025
3026        qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
3027            // HEADERS frames are special case and will be logged below.
3028            if !matches!(frame, frame::Frame::Headers { .. }) {
3029                let frame = frame.to_qlog();
3030                let ev_data = EventData::Http3FrameParsed(FrameParsed {
3031                    stream_id,
3032                    length: Some(payload_len),
3033                    frame,
3034                    ..Default::default()
3035                });
3036
3037                q.add_event_data_now(ev_data).ok();
3038            }
3039        });
3040
3041        match frame {
3042            frame::Frame::Settings {
3043                max_field_section_size,
3044                qpack_max_table_capacity,
3045                qpack_blocked_streams,
3046                connect_protocol_enabled,
3047                h3_datagram,
3048                additional_settings,
3049                raw,
3050                ..
3051            } => {
3052                self.peer_settings = ConnectionSettings {
3053                    max_field_section_size,
3054                    qpack_max_table_capacity,
3055                    qpack_blocked_streams,
3056                    connect_protocol_enabled,
3057                    h3_datagram,
3058                    additional_settings,
3059                    raw,
3060                };
3061
3062                if let Some(1) = h3_datagram {
3063                    // The peer MUST have also enabled DATAGRAM with a TP
3064                    if conn.dgram_max_writable_len().is_none() {
3065                        conn.close(
3066                            true,
3067                            Error::SettingsError.to_wire(),
3068                            b"H3_DATAGRAM sent with value 1 but max_datagram_frame_size TP not set.",
3069                        )?;
3070
3071                        return Err(Error::SettingsError);
3072                    }
3073                }
3074            },
3075
3076            frame::Frame::Headers { header_block } => {
3077                // Servers reject too many HEADERS frames.
3078                if let Some(s) = self.streams.get_mut(&stream_id) {
3079                    if self.is_server && s.headers_received_count() == 2 {
3080                        conn.close(
3081                            true,
3082                            Error::FrameUnexpected.to_wire(),
3083                            b"Too many HEADERS frames",
3084                        )?;
3085                        return Err(Error::FrameUnexpected);
3086                    }
3087
3088                    s.increment_headers_received();
3089                }
3090
3091                // Use "infinite" as default value for max_field_section_size if
3092                // it is not configured by the application.
3093                let max_size = self
3094                    .local_settings
3095                    .max_field_section_size
3096                    .unwrap_or(u64::MAX);
3097
3098                let headers = match self
3099                    .qpack_decoder
3100                    .decode(&header_block[..], max_size)
3101                {
3102                    Ok(v) => v,
3103
3104                    Err(e) => {
3105                        let e = match e {
3106                            qpack::Error::HeaderListTooLarge =>
3107                                Error::ExcessiveLoad,
3108
3109                            _ => Error::QpackDecompressionFailed,
3110                        };
3111
3112                        conn.close(true, e.to_wire(), b"Error parsing headers.")?;
3113
3114                        return Err(e);
3115                    },
3116                };
3117
3118                qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
3119                    let qlog_headers = headers
3120                        .iter()
3121                        .map(|h| qlog::events::http3::HttpHeader {
3122                            name: Some(
3123                                String::from_utf8_lossy(h.name()).into_owned(),
3124                            ),
3125                            name_bytes: None,
3126                            value: Some(
3127                                String::from_utf8_lossy(h.value()).into_owned(),
3128                            ),
3129                            value_bytes: None,
3130                        })
3131                        .collect();
3132
3133                    let frame = Http3Frame::Headers {
3134                        headers: qlog_headers,
3135                        raw: None,
3136                    };
3137
3138                    let ev_data = EventData::Http3FrameParsed(FrameParsed {
3139                        stream_id,
3140                        length: Some(payload_len),
3141                        frame,
3142                        ..Default::default()
3143                    });
3144
3145                    q.add_event_data_now(ev_data).ok();
3146                });
3147
3148                let more_frames = !conn.stream_finished(stream_id);
3149
3150                return Ok((stream_id, Event::Headers {
3151                    list: headers,
3152                    more_frames,
3153                }));
3154            },
3155
3156            frame::Frame::Data { .. } => {
3157                // Do nothing. The Data event is returned separately.
3158            },
3159
3160            frame::Frame::GoAway { id } => {
3161                if !self.is_server && id % 4 != 0 {
3162                    conn.close(
3163                        true,
3164                        Error::FrameUnexpected.to_wire(),
3165                        b"GOAWAY received with ID of non-request stream",
3166                    )?;
3167
3168                    return Err(Error::IdError);
3169                }
3170
3171                if let Some(received_id) = self.peer_goaway_id {
3172                    if id > received_id {
3173                        conn.close(
3174                            true,
3175                            Error::IdError.to_wire(),
3176                            b"GOAWAY received with ID larger than previously received",
3177                        )?;
3178
3179                        return Err(Error::IdError);
3180                    }
3181                }
3182
3183                self.peer_goaway_id = Some(id);
3184
3185                return Ok((id, Event::GoAway));
3186            },
3187
3188            frame::Frame::MaxPushId { push_id } => {
3189                if !self.is_server {
3190                    conn.close(
3191                        true,
3192                        Error::FrameUnexpected.to_wire(),
3193                        b"MAX_PUSH_ID received by client",
3194                    )?;
3195
3196                    return Err(Error::FrameUnexpected);
3197                }
3198
3199                if push_id < self.max_push_id {
3200                    conn.close(
3201                        true,
3202                        Error::IdError.to_wire(),
3203                        b"MAX_PUSH_ID reduced limit",
3204                    )?;
3205
3206                    return Err(Error::IdError);
3207                }
3208
3209                self.max_push_id = push_id;
3210            },
3211
3212            frame::Frame::PushPromise { .. } => {
3213                if self.is_server {
3214                    conn.close(
3215                        true,
3216                        Error::FrameUnexpected.to_wire(),
3217                        b"PUSH_PROMISE received by server",
3218                    )?;
3219
3220                    return Err(Error::FrameUnexpected);
3221                }
3222
3223                if !stream_id.is_multiple_of(4) {
3224                    conn.close(
3225                        true,
3226                        Error::FrameUnexpected.to_wire(),
3227                        b"PUSH_PROMISE received on non-request stream",
3228                    )?;
3229
3230                    return Err(Error::FrameUnexpected);
3231                }
3232
3233                // TODO: implement more checks and PUSH_PROMISE event
3234            },
3235
3236            frame::Frame::CancelPush { .. } => {
3237                // TODO: implement CANCEL_PUSH frame
3238            },
3239
3240            frame::Frame::PriorityUpdateRequest {
3241                prioritized_element_id,
3242                priority_field_value,
3243            } => {
3244                if !self.is_server {
3245                    conn.close(
3246                        true,
3247                        Error::FrameUnexpected.to_wire(),
3248                        b"PRIORITY_UPDATE received by client",
3249                    )?;
3250
3251                    return Err(Error::FrameUnexpected);
3252                }
3253
3254                if prioritized_element_id % 4 != 0 {
3255                    conn.close(
3256                        true,
3257                        Error::FrameUnexpected.to_wire(),
3258                        b"PRIORITY_UPDATE for request stream type with wrong ID",
3259                    )?;
3260
3261                    return Err(Error::FrameUnexpected);
3262                }
3263
3264                if prioritized_element_id > conn.streams.max_streams_bidi() * 4 {
3265                    conn.close(
3266                        true,
3267                        Error::IdError.to_wire(),
3268                        b"PRIORITY_UPDATE for request stream beyond max streams limit",
3269                    )?;
3270
3271                    return Err(Error::IdError);
3272                }
3273
3274                // PRIORITY_UPDATE can arrive before the request stream exists,
3275                // so a missing transport stream is allowed. Ignore updates only
3276                // once the transport stream was collected or both transport
3277                // directions are finished.
3278                if conn.stream_closed(prioritized_element_id) {
3279                    return Err(Error::Done);
3280                }
3281
3282                // If the stream did not yet exist, create it and store.
3283                let stream = self
3284                    .streams
3285                    .entry(prioritized_element_id)
3286                    .or_insert_with(|| {
3287                        <stream::Stream>::new(
3288                            prioritized_element_id,
3289                            false,
3290                            self.local_settings.max_field_section_size.unwrap_or(
3291                                SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT,
3292                            ),
3293                            self.max_priority_update_size,
3294                        )
3295                    });
3296
3297                let had_priority_update = stream.has_last_priority_update();
3298                stream.set_last_priority_update(Some(priority_field_value));
3299
3300                // Only trigger the event when there wasn't already a stored
3301                // PRIORITY_UPDATE.
3302                if !had_priority_update {
3303                    return Ok((prioritized_element_id, Event::PriorityUpdate));
3304                } else {
3305                    return Err(Error::Done);
3306                }
3307            },
3308
3309            frame::Frame::PriorityUpdatePush {
3310                prioritized_element_id,
3311                ..
3312            } => {
3313                if !self.is_server {
3314                    conn.close(
3315                        true,
3316                        Error::FrameUnexpected.to_wire(),
3317                        b"PRIORITY_UPDATE received by client",
3318                    )?;
3319
3320                    return Err(Error::FrameUnexpected);
3321                }
3322
3323                if prioritized_element_id % 3 != 0 {
3324                    conn.close(
3325                        true,
3326                        Error::FrameUnexpected.to_wire(),
3327                        b"PRIORITY_UPDATE for push stream type with wrong ID",
3328                    )?;
3329
3330                    return Err(Error::FrameUnexpected);
3331                }
3332
3333                // TODO: we only implement this if we implement server push
3334            },
3335
3336            frame::Frame::Unknown { .. } => (),
3337        }
3338
3339        Err(Error::Done)
3340    }
3341
3342    /// Collects and returns statistics about the connection.
3343    #[inline]
3344    pub fn stats(&self) -> Stats {
3345        Stats {
3346            qpack_encoder_stream_recv_bytes: self
3347                .peer_qpack_streams
3348                .encoder_stream_bytes,
3349            qpack_decoder_stream_recv_bytes: self
3350                .peer_qpack_streams
3351                .decoder_stream_bytes,
3352        }
3353    }
3354}
3355
3356/// Generates an HTTP/3 GREASE variable length integer.
3357pub fn grease_value() -> u64 {
3358    let n = super::rand::rand_u64_uniform(148_764_065_110_560_899);
3359    31 * n + 33
3360}
3361
3362#[doc(hidden)]
3363#[cfg(any(test, feature = "internal"))]
3364pub mod testing {
3365    use super::*;
3366
3367    use crate::test_utils;
3368    use crate::DefaultBufFactory;
3369
3370    /// Session is an HTTP/3 test helper structure. It holds a client, server
3371    /// and pipe that allows them to communicate.
3372    ///
3373    /// `default()` creates a session with some sensible default
3374    /// configuration. `with_configs()` allows for providing a specific
3375    /// configuration.
3376    ///
3377    /// `handshake()` performs all the steps needed to establish an HTTP/3
3378    /// connection.
3379    ///
3380    /// Some utility functions are provided that make it less verbose to send
3381    /// request, responses and individual headers. The full quiche API remains
3382    /// available for any test that need to do unconventional things (such as
3383    /// bad behaviour that triggers errors).
3384    pub struct Session<F = DefaultBufFactory>
3385    where
3386        F: BufFactory,
3387    {
3388        pub pipe: test_utils::Pipe<F>,
3389        pub client: Connection,
3390        pub server: Connection,
3391    }
3392
3393    impl Session {
3394        pub fn new() -> Result<Session> {
3395            Session::<DefaultBufFactory>::new_with_buf()
3396        }
3397
3398        pub fn with_configs(
3399            config: &mut crate::Config, h3_config: &Config,
3400        ) -> Result<Session> {
3401            Session::<DefaultBufFactory>::with_configs_and_buf(config, h3_config)
3402        }
3403
3404        pub fn default_configs() -> Result<(crate::Config, Config)> {
3405            fn path_relative_to_manifest_dir(path: &str) -> String {
3406                std::fs::canonicalize(
3407                    std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(path),
3408                )
3409                .unwrap()
3410                .to_string_lossy()
3411                .into_owned()
3412            }
3413
3414            let mut config = crate::Config::new(crate::PROTOCOL_VERSION)?;
3415            config.load_cert_chain_from_pem_file(
3416                &path_relative_to_manifest_dir("examples/cert.crt"),
3417            )?;
3418            config.load_priv_key_from_pem_file(
3419                &path_relative_to_manifest_dir("examples/cert.key"),
3420            )?;
3421            config.set_application_protos(&[b"h3"])?;
3422            config.set_initial_max_data(1500);
3423            config.set_initial_max_stream_data_bidi_local(150);
3424            config.set_initial_max_stream_data_bidi_remote(150);
3425            config.set_initial_max_stream_data_uni(150);
3426            config.set_initial_max_streams_bidi(5);
3427            config.set_initial_max_streams_uni(5);
3428            config.verify_peer(false);
3429            config.enable_dgram(true, 3, 3);
3430            config.set_ack_delay_exponent(8);
3431
3432            let h3_config = Config::new()?;
3433            Ok((config, h3_config))
3434        }
3435    }
3436
3437    impl<F: BufFactory> Session<F> {
3438        pub fn new_with_buf() -> Result<Session<F>> {
3439            let (mut config, h3_config) = Session::default_configs()?;
3440            Session::with_configs_and_buf(&mut config, &h3_config)
3441        }
3442
3443        pub fn with_configs_and_buf(
3444            config: &mut crate::Config, h3_config: &Config,
3445        ) -> Result<Session<F>> {
3446            let pipe = test_utils::Pipe::with_config_and_buf(config)?;
3447            let client_dgram = pipe.client.dgram_enabled();
3448            let server_dgram = pipe.server.dgram_enabled();
3449            Ok(Session {
3450                pipe,
3451                client: Connection::new(h3_config, false, client_dgram)?,
3452                server: Connection::new(h3_config, true, server_dgram)?,
3453            })
3454        }
3455
3456        /// Do the HTTP/3 handshake so both ends are in sane initial state.
3457        pub fn handshake(&mut self) -> Result<()> {
3458            self.pipe.handshake()?;
3459
3460            // Client streams.
3461            self.client.send_settings(&mut self.pipe.client)?;
3462            self.pipe.advance().ok();
3463
3464            self.client
3465                .open_qpack_encoder_stream(&mut self.pipe.client)?;
3466            self.pipe.advance().ok();
3467
3468            self.client
3469                .open_qpack_decoder_stream(&mut self.pipe.client)?;
3470            self.pipe.advance().ok();
3471
3472            if self.pipe.client.grease {
3473                self.client.open_grease_stream(&mut self.pipe.client)?;
3474            }
3475
3476            self.pipe.advance().ok();
3477
3478            // Server streams.
3479            self.server.send_settings(&mut self.pipe.server)?;
3480            self.pipe.advance().ok();
3481
3482            self.server
3483                .open_qpack_encoder_stream(&mut self.pipe.server)?;
3484            self.pipe.advance().ok();
3485
3486            self.server
3487                .open_qpack_decoder_stream(&mut self.pipe.server)?;
3488            self.pipe.advance().ok();
3489
3490            if self.pipe.server.grease {
3491                self.server.open_grease_stream(&mut self.pipe.server)?;
3492            }
3493
3494            self.advance().ok();
3495
3496            while self.client.poll(&mut self.pipe.client).is_ok() {
3497                // Do nothing.
3498            }
3499
3500            while self.server.poll(&mut self.pipe.server).is_ok() {
3501                // Do nothing.
3502            }
3503
3504            Ok(())
3505        }
3506
3507        /// Advances the session pipe over the buffer.
3508        pub fn advance(&mut self) -> crate::Result<()> {
3509            self.pipe.advance()
3510        }
3511
3512        /// Polls the client for events.
3513        pub fn poll_client(&mut self) -> Result<(u64, Event)> {
3514            self.client.poll(&mut self.pipe.client)
3515        }
3516
3517        /// Polls the server for events.
3518        pub fn poll_server(&mut self) -> Result<(u64, Event)> {
3519            self.server.poll(&mut self.pipe.server)
3520        }
3521
3522        /// Sends a request from client with default headers.
3523        ///
3524        /// On success it returns the newly allocated stream and the headers.
3525        pub fn send_request(&mut self, fin: bool) -> Result<(u64, Vec<Header>)> {
3526            let req = vec![
3527                Header::new(b":method", b"GET"),
3528                Header::new(b":scheme", b"https"),
3529                Header::new(b":authority", b"quic.tech"),
3530                Header::new(b":path", b"/test"),
3531                Header::new(b"user-agent", b"quiche-test"),
3532            ];
3533
3534            let stream =
3535                self.client.send_request(&mut self.pipe.client, &req, fin)?;
3536
3537            self.advance().ok();
3538
3539            Ok((stream, req))
3540        }
3541
3542        /// Sends a response from server with default headers.
3543        ///
3544        /// On success it returns the headers.
3545        pub fn send_response(
3546            &mut self, stream: u64, fin: bool,
3547        ) -> Result<Vec<Header>> {
3548            let resp = vec![
3549                Header::new(b":status", b"200"),
3550                Header::new(b"server", b"quiche-test"),
3551            ];
3552
3553            self.server.send_response(
3554                &mut self.pipe.server,
3555                stream,
3556                &resp,
3557                fin,
3558            )?;
3559
3560            self.advance().ok();
3561
3562            Ok(resp)
3563        }
3564
3565        /// Sends some default payload from client.
3566        ///
3567        /// On success it returns the payload.
3568        pub fn send_body_client(
3569            &mut self, stream: u64, fin: bool,
3570        ) -> Result<Vec<u8>> {
3571            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3572
3573            self.client
3574                .send_body(&mut self.pipe.client, stream, &bytes, fin)?;
3575
3576            self.advance().ok();
3577
3578            Ok(bytes)
3579        }
3580
3581        /// Fetches DATA payload from the server.
3582        ///
3583        /// On success it returns the number of bytes received.
3584        pub fn recv_body_client(
3585            &mut self, stream: u64, buf: &mut [u8],
3586        ) -> Result<usize> {
3587            self.client.recv_body(&mut self.pipe.client, stream, buf)
3588        }
3589
3590        /// Fetches DATA payload from the server.
3591        ///
3592        /// On success it returns the number of bytes received.
3593        pub fn recv_body_buf_client<B: bytes::BufMut>(
3594            &mut self, stream: u64, buf: B,
3595        ) -> Result<usize> {
3596            self.client
3597                .recv_body_buf(&mut self.pipe.client, stream, buf)
3598        }
3599
3600        /// Sends some default payload from server.
3601        ///
3602        /// On success it returns the payload.
3603        pub fn send_body_server(
3604            &mut self, stream: u64, fin: bool,
3605        ) -> Result<Vec<u8>> {
3606            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3607
3608            self.server
3609                .send_body(&mut self.pipe.server, stream, &bytes, fin)?;
3610
3611            self.advance().ok();
3612
3613            Ok(bytes)
3614        }
3615
3616        /// Fetches DATA payload from the client.
3617        ///
3618        /// On success it returns the number of bytes received.
3619        pub fn recv_body_server(
3620            &mut self, stream: u64, buf: &mut [u8],
3621        ) -> Result<usize> {
3622            self.server.recv_body(&mut self.pipe.server, stream, buf)
3623        }
3624
3625        /// Fetches DATA payload from the client.
3626        ///
3627        /// On success it returns the number of bytes received.
3628        pub fn recv_body_buf_server<B: bytes::BufMut>(
3629            &mut self, stream: u64, buf: B,
3630        ) -> Result<usize> {
3631            self.server
3632                .recv_body_buf(&mut self.pipe.server, stream, buf)
3633        }
3634
3635        /// Sends a single HTTP/3 frame from the client.
3636        pub fn send_frame_client(
3637            &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3638        ) -> Result<()> {
3639            let mut d = [42; 65535];
3640
3641            let mut b = octets::OctetsMut::with_slice(&mut d);
3642
3643            frame.to_bytes(&mut b)?;
3644
3645            let off = b.off();
3646            self.pipe.client.stream_send(stream_id, &d[..off], fin)?;
3647
3648            self.advance().ok();
3649
3650            Ok(())
3651        }
3652
3653        /// Send an HTTP/3 DATAGRAM with default data from the client.
3654        ///
3655        /// On success it returns the data.
3656        pub fn send_dgram_client(&mut self, flow_id: u64) -> Result<Vec<u8>> {
3657            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3658            let len = octets::varint_len(flow_id) + bytes.len();
3659            let mut d = vec![0; len];
3660            let mut b = octets::OctetsMut::with_slice(&mut d);
3661
3662            b.put_varint(flow_id)?;
3663            b.put_bytes(&bytes)?;
3664
3665            self.pipe.client.dgram_send(&d)?;
3666
3667            self.advance().ok();
3668
3669            Ok(bytes)
3670        }
3671
3672        /// Receives an HTTP/3 DATAGRAM from the server.
3673        ///
3674        /// On success it returns the DATAGRAM length, flow ID and flow ID
3675        /// length.
3676        pub fn recv_dgram_client(
3677            &mut self, buf: &mut [u8],
3678        ) -> Result<(usize, u64, usize)> {
3679            let len = self.pipe.client.dgram_recv(buf)?;
3680            let mut b = octets::Octets::with_slice(buf);
3681            let flow_id = b.get_varint()?;
3682
3683            Ok((len, flow_id, b.off()))
3684        }
3685
3686        /// Send an HTTP/3 DATAGRAM with default data from the server
3687        ///
3688        /// On success it returns the data.
3689        pub fn send_dgram_server(&mut self, flow_id: u64) -> Result<Vec<u8>> {
3690            let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3691            let len = octets::varint_len(flow_id) + bytes.len();
3692            let mut d = vec![0; len];
3693            let mut b = octets::OctetsMut::with_slice(&mut d);
3694
3695            b.put_varint(flow_id)?;
3696            b.put_bytes(&bytes)?;
3697
3698            self.pipe.server.dgram_send(&d)?;
3699
3700            self.advance().ok();
3701
3702            Ok(bytes)
3703        }
3704
3705        /// Receives an HTTP/3 DATAGRAM from the client.
3706        ///
3707        /// On success it returns the DATAGRAM length, flow ID and flow ID
3708        /// length.
3709        pub fn recv_dgram_server(
3710            &mut self, buf: &mut [u8],
3711        ) -> Result<(usize, u64, usize)> {
3712            let len = self.pipe.server.dgram_recv(buf)?;
3713            let mut b = octets::Octets::with_slice(buf);
3714            let flow_id = b.get_varint()?;
3715
3716            Ok((len, flow_id, b.off()))
3717        }
3718
3719        /// Sends a single HTTP/3 frame from the server.
3720        pub fn send_frame_server(
3721            &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3722        ) -> Result<()> {
3723            let mut d = [42; 65535];
3724
3725            let mut b = octets::OctetsMut::with_slice(&mut d);
3726
3727            frame.to_bytes(&mut b)?;
3728
3729            let off = b.off();
3730            self.pipe.server.stream_send(stream_id, &d[..off], fin)?;
3731
3732            self.advance().ok();
3733
3734            Ok(())
3735        }
3736
3737        /// Sends an arbitrary buffer of HTTP/3 stream data from the client.
3738        pub fn send_arbitrary_stream_data_client(
3739            &mut self, data: &[u8], stream_id: u64, fin: bool,
3740        ) -> Result<()> {
3741            self.pipe.client.stream_send(stream_id, data, fin)?;
3742
3743            self.advance().ok();
3744
3745            Ok(())
3746        }
3747
3748        /// Sends an arbitrary buffer of HTTP/3 stream data from the server.
3749        pub fn send_arbitrary_stream_data_server(
3750            &mut self, data: &[u8], stream_id: u64, fin: bool,
3751        ) -> Result<()> {
3752            self.pipe.server.stream_send(stream_id, data, fin)?;
3753
3754            self.advance().ok();
3755
3756            Ok(())
3757        }
3758    }
3759}
3760
3761#[cfg(test)]
3762mod tests {
3763    use bytes::BufMut as _;
3764
3765    use super::*;
3766
3767    use super::testing::*;
3768
3769    #[test]
3770    /// Make sure that random GREASE values is within the specified limit.
3771    fn grease_value_in_varint_limit() {
3772        assert!(grease_value() < 2u64.pow(62) - 1);
3773    }
3774
3775    #[test]
3776    fn h3_handshake_0rtt() {
3777        let mut buf = [0; 65535];
3778
3779        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
3780        config
3781            .load_cert_chain_from_pem_file("examples/cert.crt")
3782            .unwrap();
3783        config
3784            .load_priv_key_from_pem_file("examples/cert.key")
3785            .unwrap();
3786        config
3787            .set_application_protos(&[b"proto1", b"proto2"])
3788            .unwrap();
3789        config.set_initial_max_data(30);
3790        config.set_initial_max_stream_data_bidi_local(15);
3791        config.set_initial_max_stream_data_bidi_remote(15);
3792        config.set_initial_max_stream_data_uni(15);
3793        config.set_initial_max_streams_bidi(3);
3794        config.set_initial_max_streams_uni(3);
3795        config.enable_early_data();
3796        config.verify_peer(false);
3797
3798        let h3_config = Config::new().unwrap();
3799
3800        // Perform initial handshake.
3801        let mut pipe = crate::test_utils::Pipe::with_config(&mut config).unwrap();
3802        assert_eq!(pipe.handshake(), Ok(()));
3803
3804        // Extract session,
3805        let session = pipe.client.session().unwrap();
3806
3807        // Configure session on new connection.
3808        let mut pipe = crate::test_utils::Pipe::with_config(&mut config).unwrap();
3809        assert_eq!(pipe.client.set_session(session), Ok(()));
3810
3811        // Can't create an H3 connection until the QUIC connection is determined
3812        // to have made sufficient early data progress.
3813        assert!(matches!(
3814            Connection::with_transport(&mut pipe.client, &h3_config),
3815            Err(Error::InternalError)
3816        ));
3817
3818        // Client sends initial flight.
3819        let (len, _) = pipe.client.send(&mut buf).unwrap();
3820
3821        // Now an H3 connection can be created.
3822        assert!(Connection::with_transport(&mut pipe.client, &h3_config).is_ok());
3823        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
3824
3825        // Client sends 0-RTT packet.
3826        let pkt_type = crate::packet::Type::ZeroRTT;
3827
3828        let frames = [crate::frame::Frame::Stream {
3829            stream_id: 6,
3830            data: <crate::range_buf::RangeBuf>::from(b"aaaaa", 0, true),
3831        }];
3832
3833        assert_eq!(
3834            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
3835            Ok(1200)
3836        );
3837
3838        assert_eq!(pipe.server.undecryptable_pkts.len(), 0);
3839
3840        // 0-RTT stream data is readable.
3841        let mut r = pipe.server.readable();
3842        assert_eq!(r.next(), Some(6));
3843        assert_eq!(r.next(), None);
3844
3845        let mut b = [0; 15];
3846        assert_eq!(pipe.server.stream_recv(6, &mut b), Ok((5, true)));
3847        assert_eq!(&b[..5], b"aaaaa");
3848    }
3849
3850    #[test]
3851    /// Send a request with no body, get a response with no body.
3852    fn request_no_body_response_no_body() {
3853        let mut s = Session::new().unwrap();
3854        s.handshake().unwrap();
3855
3856        let (stream, req) = s.send_request(true).unwrap();
3857
3858        assert_eq!(stream, 0);
3859
3860        let ev_headers = Event::Headers {
3861            list: req,
3862            more_frames: false,
3863        };
3864
3865        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3866        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3867
3868        let resp = s.send_response(stream, true).unwrap();
3869
3870        let ev_headers = Event::Headers {
3871            list: resp,
3872            more_frames: false,
3873        };
3874
3875        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3876        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3877        assert_eq!(s.poll_client(), Err(Error::Done));
3878    }
3879
3880    #[test]
3881    /// Send a request with no body, get a response with one DATA frame.
3882    fn request_no_body_response_one_chunk() {
3883        let mut s = Session::new().unwrap();
3884        s.handshake().unwrap();
3885
3886        let (stream, req) = s.send_request(true).unwrap();
3887        assert_eq!(stream, 0);
3888
3889        let ev_headers = Event::Headers {
3890            list: req,
3891            more_frames: false,
3892        };
3893
3894        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3895
3896        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3897
3898        let resp = s.send_response(stream, false).unwrap();
3899
3900        let body = s.send_body_server(stream, true).unwrap();
3901
3902        let mut recv_buf = vec![0; body.len()];
3903
3904        let ev_headers = Event::Headers {
3905            list: resp,
3906            more_frames: true,
3907        };
3908
3909        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3910
3911        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
3912        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
3913
3914        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3915        assert_eq!(s.poll_client(), Err(Error::Done));
3916    }
3917
3918    #[test]
3919    /// Send a request with no body, get a response with multiple DATA frames.
3920    fn request_no_body_response_many_chunks() {
3921        let mut s = Session::new().unwrap();
3922        s.handshake().unwrap();
3923
3924        let (stream, req) = s.send_request(true).unwrap();
3925
3926        let ev_headers = Event::Headers {
3927            list: req,
3928            more_frames: false,
3929        };
3930
3931        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3932        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3933
3934        let total_data_frames = 4;
3935
3936        let resp = s.send_response(stream, false).unwrap();
3937
3938        for _ in 0..total_data_frames - 1 {
3939            s.send_body_server(stream, false).unwrap();
3940        }
3941
3942        let body = s.send_body_server(stream, true).unwrap();
3943
3944        let mut recv_buf = vec![0; body.len()];
3945
3946        let ev_headers = Event::Headers {
3947            list: resp,
3948            more_frames: true,
3949        };
3950
3951        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
3952        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
3953        assert_eq!(s.poll_client(), Err(Error::Done));
3954
3955        for _ in 0..total_data_frames {
3956            assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
3957        }
3958
3959        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
3960        assert_eq!(s.poll_client(), Err(Error::Done));
3961    }
3962
3963    #[test]
3964    /// Send a request with no body, get a response with multiple DATA frames.
3965    fn request_no_body_response_many_chunks_with_buf() {
3966        let (mut config, h3_config) = Session::default_configs().unwrap();
3967        // we don't want to be limited by flow or cong. control
3968        config.set_initial_congestion_window_packets(100);
3969        config.set_initial_max_data(200_000);
3970        config.set_initial_max_stream_data_bidi_local(200_000);
3971        config.set_initial_max_stream_data_bidi_remote(200_000);
3972        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
3973        s.handshake().unwrap();
3974
3975        let (stream, req) = s.send_request(true).unwrap();
3976
3977        let ev_headers = Event::Headers {
3978            list: req,
3979            more_frames: false,
3980        };
3981
3982        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
3983        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
3984
3985        let total_data_frames = 4;
3986
3987        // Use a large body
3988        let data = vec![0xab_u8; 16 * 1024];
3989
3990        let resp = s.send_response(stream, false).unwrap();
3991
3992        for _ in 0..total_data_frames - 1 {
3993            assert_eq!(
3994                s.server.send_body(&mut s.pipe.server, stream, &data, false),
3995                Ok(data.len())
3996            );
3997            s.advance().ok();
3998        }
3999
4000        s.server
4001            .send_body(&mut s.pipe.server, stream, &data, true)
4002            .unwrap();
4003        s.advance().ok();
4004
4005        let ev_headers = Event::Headers {
4006            list: resp,
4007            more_frames: true,
4008        };
4009
4010        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4011        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
4012        assert_eq!(s.poll_client(), Err(Error::Done));
4013
4014        // We expect to be able to read multiple data frames in a single call and
4015        // reads don't have to end on frame boundaries. So let's try to read
4016        // 1.5 times the amount we sent in one frame.
4017        let how_much_to_read_per_call = data.len() * 2 / 3;
4018        let mut remaining_to_read = total_data_frames * data.len();
4019        let mut recv_buf = Vec::new().limit(how_much_to_read_per_call);
4020        assert_eq!(
4021            s.recv_body_buf_client(stream, &mut recv_buf),
4022            Ok(how_much_to_read_per_call)
4023        );
4024        remaining_to_read -= how_much_to_read_per_call;
4025        assert_eq!(recv_buf.get_ref().len(), how_much_to_read_per_call);
4026
4027        while remaining_to_read > 0 {
4028            // Set a different limit for the following reads.
4029            recv_buf.set_limit(data.len());
4030            // We should either read up to the limit we set above, or to
4031            // the end of buffered data.
4032            let expected = std::cmp::min(data.len(), remaining_to_read);
4033            assert_eq!(
4034                s.recv_body_buf_client(stream, &mut recv_buf),
4035                Ok(expected)
4036            );
4037            remaining_to_read -= expected;
4038        }
4039        // We've read everything now. Ensure the Vec reflects that
4040        assert_eq!(recv_buf.get_ref().len(), total_data_frames * data.len());
4041
4042        // No more data to read.
4043        assert_eq!(
4044            s.recv_body_buf_client(stream, &mut recv_buf),
4045            Err(Error::Done)
4046        );
4047
4048        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4049        assert_eq!(s.poll_client(), Err(Error::Done));
4050    }
4051
4052    #[test]
4053    /// Send a request with one DATA frame, get a response with no body.
4054    fn request_one_chunk_response_no_body() {
4055        let mut s = Session::new().unwrap();
4056        s.handshake().unwrap();
4057
4058        let (stream, req) = s.send_request(false).unwrap();
4059
4060        let body = s.send_body_client(stream, true).unwrap();
4061
4062        let mut recv_buf = vec![0; body.len()];
4063
4064        let ev_headers = Event::Headers {
4065            list: req,
4066            more_frames: true,
4067        };
4068
4069        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4070
4071        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4072        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4073
4074        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4075
4076        let resp = s.send_response(stream, true).unwrap();
4077
4078        let ev_headers = Event::Headers {
4079            list: resp,
4080            more_frames: false,
4081        };
4082
4083        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4084        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4085    }
4086
4087    #[test]
4088    /// Send a request with multiple DATA frames, get a response with no body.
4089    fn request_many_chunks_response_no_body() {
4090        let mut s = Session::new().unwrap();
4091        s.handshake().unwrap();
4092
4093        let (stream, req) = s.send_request(false).unwrap();
4094
4095        let total_data_frames = 4;
4096
4097        for _ in 0..total_data_frames - 1 {
4098            s.send_body_client(stream, false).unwrap();
4099        }
4100
4101        let body = s.send_body_client(stream, true).unwrap();
4102
4103        let mut recv_buf = vec![0; body.len()];
4104
4105        let ev_headers = Event::Headers {
4106            list: req,
4107            more_frames: true,
4108        };
4109
4110        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4111        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4112        assert_eq!(s.poll_server(), Err(Error::Done));
4113
4114        for _ in 0..total_data_frames {
4115            assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4116        }
4117
4118        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4119
4120        let resp = s.send_response(stream, true).unwrap();
4121
4122        let ev_headers = Event::Headers {
4123            list: resp,
4124            more_frames: false,
4125        };
4126
4127        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4128        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4129    }
4130
4131    #[test]
4132    /// Send a request with multiple DATA frames, get a response with one DATA
4133    /// frame.
4134    fn many_requests_many_chunks_response_one_chunk() {
4135        let mut s = Session::new().unwrap();
4136        s.handshake().unwrap();
4137
4138        let mut reqs = Vec::new();
4139
4140        let (stream1, req1) = s.send_request(false).unwrap();
4141        assert_eq!(stream1, 0);
4142        reqs.push(req1);
4143
4144        let (stream2, req2) = s.send_request(false).unwrap();
4145        assert_eq!(stream2, 4);
4146        reqs.push(req2);
4147
4148        let (stream3, req3) = s.send_request(false).unwrap();
4149        assert_eq!(stream3, 8);
4150        reqs.push(req3);
4151
4152        let body = s.send_body_client(stream1, false).unwrap();
4153        s.send_body_client(stream2, false).unwrap();
4154        s.send_body_client(stream3, false).unwrap();
4155
4156        let mut recv_buf = vec![0; body.len()];
4157
4158        // Reverse order of writes.
4159
4160        s.send_body_client(stream3, true).unwrap();
4161        s.send_body_client(stream2, true).unwrap();
4162        s.send_body_client(stream1, true).unwrap();
4163
4164        let (_, ev) = s.poll_server().unwrap();
4165        let ev_headers = Event::Headers {
4166            list: reqs[0].clone(),
4167            more_frames: true,
4168        };
4169        assert_eq!(ev, ev_headers);
4170
4171        let (_, ev) = s.poll_server().unwrap();
4172        let ev_headers = Event::Headers {
4173            list: reqs[1].clone(),
4174            more_frames: true,
4175        };
4176        assert_eq!(ev, ev_headers);
4177
4178        let (_, ev) = s.poll_server().unwrap();
4179        let ev_headers = Event::Headers {
4180            list: reqs[2].clone(),
4181            more_frames: true,
4182        };
4183        assert_eq!(ev, ev_headers);
4184
4185        assert_eq!(s.poll_server(), Ok((0, Event::Data)));
4186        assert_eq!(s.recv_body_server(0, &mut recv_buf), Ok(body.len()));
4187        assert_eq!(s.poll_client(), Err(Error::Done));
4188        assert_eq!(s.recv_body_server(0, &mut recv_buf), Ok(body.len()));
4189        assert_eq!(s.poll_server(), Ok((0, Event::Finished)));
4190
4191        assert_eq!(s.poll_server(), Ok((4, Event::Data)));
4192        assert_eq!(s.recv_body_server(4, &mut recv_buf), Ok(body.len()));
4193        assert_eq!(s.poll_client(), Err(Error::Done));
4194        assert_eq!(s.recv_body_server(4, &mut recv_buf), Ok(body.len()));
4195        assert_eq!(s.poll_server(), Ok((4, Event::Finished)));
4196
4197        assert_eq!(s.poll_server(), Ok((8, Event::Data)));
4198        assert_eq!(s.recv_body_server(8, &mut recv_buf), Ok(body.len()));
4199        assert_eq!(s.poll_client(), Err(Error::Done));
4200        assert_eq!(s.recv_body_server(8, &mut recv_buf), Ok(body.len()));
4201        assert_eq!(s.poll_server(), Ok((8, Event::Finished)));
4202
4203        assert_eq!(s.poll_server(), Err(Error::Done));
4204
4205        let mut resps = Vec::new();
4206
4207        let resp1 = s.send_response(stream1, true).unwrap();
4208        resps.push(resp1);
4209
4210        let resp2 = s.send_response(stream2, true).unwrap();
4211        resps.push(resp2);
4212
4213        let resp3 = s.send_response(stream3, true).unwrap();
4214        resps.push(resp3);
4215
4216        for _ in 0..resps.len() {
4217            let (stream, ev) = s.poll_client().unwrap();
4218            let ev_headers = Event::Headers {
4219                list: resps[(stream / 4) as usize].clone(),
4220                more_frames: false,
4221            };
4222            assert_eq!(ev, ev_headers);
4223            assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4224        }
4225
4226        assert_eq!(s.poll_client(), Err(Error::Done));
4227    }
4228
4229    #[test]
4230    /// Send a request with no body, get a response with one DATA frame and an
4231    /// empty FIN after reception from the client.
4232    fn request_no_body_response_one_chunk_empty_fin() {
4233        let mut s = Session::new().unwrap();
4234        s.handshake().unwrap();
4235
4236        let (stream, req) = s.send_request(true).unwrap();
4237
4238        let ev_headers = Event::Headers {
4239            list: req,
4240            more_frames: false,
4241        };
4242
4243        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4244        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4245
4246        let resp = s.send_response(stream, false).unwrap();
4247
4248        let body = s.send_body_server(stream, false).unwrap();
4249
4250        let mut recv_buf = vec![0; body.len()];
4251
4252        let ev_headers = Event::Headers {
4253            list: resp,
4254            more_frames: true,
4255        };
4256
4257        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4258
4259        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
4260        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
4261
4262        assert_eq!(s.pipe.server.stream_send(stream, &[], true), Ok(0));
4263        s.advance().ok();
4264
4265        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4266        assert_eq!(s.poll_client(), Err(Error::Done));
4267    }
4268
4269    #[test]
4270    /// Send a request with no body, get a response with no body followed by
4271    /// GREASE that is STREAM frame with a FIN.
4272    fn request_no_body_response_no_body_with_grease() {
4273        let mut s = Session::new().unwrap();
4274        s.handshake().unwrap();
4275
4276        let (stream, req) = s.send_request(true).unwrap();
4277
4278        assert_eq!(stream, 0);
4279
4280        let ev_headers = Event::Headers {
4281            list: req,
4282            more_frames: false,
4283        };
4284
4285        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4286        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4287
4288        let resp = s.send_response(stream, false).unwrap();
4289
4290        let ev_headers = Event::Headers {
4291            list: resp,
4292            more_frames: true,
4293        };
4294
4295        // Inject a GREASE frame.
4296        let mut d = [42; 10];
4297        let mut b = octets::OctetsMut::with_slice(&mut d);
4298
4299        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
4300        s.pipe.server.stream_send(0, frame_type, false).unwrap();
4301
4302        let frame_len = b.put_varint(10).unwrap();
4303        s.pipe.server.stream_send(0, frame_len, false).unwrap();
4304
4305        s.pipe.server.stream_send(0, &d, true).unwrap();
4306
4307        s.advance().ok();
4308
4309        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4310        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4311        assert_eq!(s.poll_client(), Err(Error::Done));
4312    }
4313
4314    #[test]
4315    /// Try to send DATA frames before HEADERS.
4316    fn body_response_before_headers() {
4317        let mut s = Session::new().unwrap();
4318        s.handshake().unwrap();
4319
4320        let (stream, req) = s.send_request(true).unwrap();
4321        assert_eq!(stream, 0);
4322
4323        let ev_headers = Event::Headers {
4324            list: req,
4325            more_frames: false,
4326        };
4327
4328        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4329
4330        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4331
4332        assert_eq!(
4333            s.send_body_server(stream, true),
4334            Err(Error::FrameUnexpected)
4335        );
4336
4337        assert_eq!(s.poll_client(), Err(Error::Done));
4338    }
4339
4340    #[test]
4341    /// Try to send DATA frames on wrong streams, ensure the API returns an
4342    /// error before anything hits the transport layer.
4343    fn send_body_invalid_client_stream() {
4344        let mut s = Session::new().unwrap();
4345        s.handshake().unwrap();
4346
4347        assert_eq!(s.send_body_client(0, true), Err(Error::FrameUnexpected));
4348
4349        assert_eq!(
4350            s.send_body_client(s.client.control_stream_id.unwrap(), true),
4351            Err(Error::FrameUnexpected)
4352        );
4353
4354        assert_eq!(
4355            s.send_body_client(
4356                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
4357                true
4358            ),
4359            Err(Error::FrameUnexpected)
4360        );
4361
4362        assert_eq!(
4363            s.send_body_client(
4364                s.client.local_qpack_streams.decoder_stream_id.unwrap(),
4365                true
4366            ),
4367            Err(Error::FrameUnexpected)
4368        );
4369
4370        assert_eq!(
4371            s.send_body_client(s.client.peer_control_stream_id.unwrap(), true),
4372            Err(Error::FrameUnexpected)
4373        );
4374
4375        assert_eq!(
4376            s.send_body_client(
4377                s.client.peer_qpack_streams.encoder_stream_id.unwrap(),
4378                true
4379            ),
4380            Err(Error::FrameUnexpected)
4381        );
4382
4383        assert_eq!(
4384            s.send_body_client(
4385                s.client.peer_qpack_streams.decoder_stream_id.unwrap(),
4386                true
4387            ),
4388            Err(Error::FrameUnexpected)
4389        );
4390    }
4391
4392    #[test]
4393    /// Try to send DATA frames on wrong streams, ensure the API returns an
4394    /// error before anything hits the transport layer.
4395    fn send_body_invalid_server_stream() {
4396        let mut s = Session::new().unwrap();
4397        s.handshake().unwrap();
4398
4399        assert_eq!(s.send_body_server(0, true), Err(Error::FrameUnexpected));
4400
4401        assert_eq!(
4402            s.send_body_server(s.server.control_stream_id.unwrap(), true),
4403            Err(Error::FrameUnexpected)
4404        );
4405
4406        assert_eq!(
4407            s.send_body_server(
4408                s.server.local_qpack_streams.encoder_stream_id.unwrap(),
4409                true
4410            ),
4411            Err(Error::FrameUnexpected)
4412        );
4413
4414        assert_eq!(
4415            s.send_body_server(
4416                s.server.local_qpack_streams.decoder_stream_id.unwrap(),
4417                true
4418            ),
4419            Err(Error::FrameUnexpected)
4420        );
4421
4422        assert_eq!(
4423            s.send_body_server(s.server.peer_control_stream_id.unwrap(), true),
4424            Err(Error::FrameUnexpected)
4425        );
4426
4427        assert_eq!(
4428            s.send_body_server(
4429                s.server.peer_qpack_streams.encoder_stream_id.unwrap(),
4430                true
4431            ),
4432            Err(Error::FrameUnexpected)
4433        );
4434
4435        assert_eq!(
4436            s.send_body_server(
4437                s.server.peer_qpack_streams.decoder_stream_id.unwrap(),
4438                true
4439            ),
4440            Err(Error::FrameUnexpected)
4441        );
4442    }
4443
4444    #[test]
4445    /// Client sends request with body and trailers.
4446    fn trailers() {
4447        let mut s = Session::new().unwrap();
4448        s.handshake().unwrap();
4449
4450        let (stream, req) = s.send_request(false).unwrap();
4451
4452        let body = s.send_body_client(stream, false).unwrap();
4453
4454        let mut recv_buf = vec![0; body.len()];
4455
4456        let req_trailers = vec![Header::new(b"foo", b"bar")];
4457
4458        s.client
4459            .send_additional_headers(
4460                &mut s.pipe.client,
4461                stream,
4462                &req_trailers,
4463                true,
4464                true,
4465            )
4466            .unwrap();
4467
4468        s.advance().ok();
4469
4470        let ev_headers = Event::Headers {
4471            list: req,
4472            more_frames: true,
4473        };
4474
4475        let ev_trailers = Event::Headers {
4476            list: req_trailers,
4477            more_frames: false,
4478        };
4479
4480        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4481
4482        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4483        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4484
4485        assert_eq!(s.poll_server(), Ok((stream, ev_trailers)));
4486        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4487    }
4488
4489    #[test]
4490    /// Server responds with a 103, then a 200 with no body.
4491    fn informational_response() {
4492        let mut s = Session::new().unwrap();
4493        s.handshake().unwrap();
4494
4495        let (stream, req) = s.send_request(true).unwrap();
4496
4497        assert_eq!(stream, 0);
4498
4499        let ev_headers = Event::Headers {
4500            list: req,
4501            more_frames: false,
4502        };
4503
4504        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4505        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4506
4507        let info_resp = vec![
4508            Header::new(b":status", b"103"),
4509            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4510        ];
4511
4512        let resp = vec![
4513            Header::new(b":status", b"200"),
4514            Header::new(b"server", b"quiche-test"),
4515        ];
4516
4517        s.server
4518            .send_response(&mut s.pipe.server, stream, &info_resp, false)
4519            .unwrap();
4520
4521        s.server
4522            .send_additional_headers(
4523                &mut s.pipe.server,
4524                stream,
4525                &resp,
4526                false,
4527                true,
4528            )
4529            .unwrap();
4530
4531        s.advance().ok();
4532
4533        let ev_info_headers = Event::Headers {
4534            list: info_resp,
4535            more_frames: true,
4536        };
4537
4538        let ev_headers = Event::Headers {
4539            list: resp,
4540            more_frames: false,
4541        };
4542
4543        assert_eq!(s.poll_client(), Ok((stream, ev_info_headers)));
4544        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
4545        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
4546        assert_eq!(s.poll_client(), Err(Error::Done));
4547    }
4548
4549    #[test]
4550    /// Server responds with a 103, then attempts to send a 200 using
4551    /// send_response again, which should fail.
4552    fn no_multiple_response() {
4553        let mut s = Session::new().unwrap();
4554        s.handshake().unwrap();
4555
4556        let (stream, req) = s.send_request(true).unwrap();
4557
4558        assert_eq!(stream, 0);
4559
4560        let ev_headers = Event::Headers {
4561            list: req,
4562            more_frames: false,
4563        };
4564
4565        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4566        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4567
4568        let info_resp = vec![
4569            Header::new(b":status", b"103"),
4570            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4571        ];
4572
4573        let resp = vec![
4574            Header::new(b":status", b"200"),
4575            Header::new(b"server", b"quiche-test"),
4576        ];
4577
4578        s.server
4579            .send_response(&mut s.pipe.server, stream, &info_resp, false)
4580            .unwrap();
4581
4582        assert_eq!(
4583            Err(Error::FrameUnexpected),
4584            s.server
4585                .send_response(&mut s.pipe.server, stream, &resp, true)
4586        );
4587
4588        s.advance().ok();
4589
4590        let ev_info_headers = Event::Headers {
4591            list: info_resp,
4592            more_frames: true,
4593        };
4594
4595        assert_eq!(s.poll_client(), Ok((stream, ev_info_headers)));
4596        assert_eq!(s.poll_client(), Err(Error::Done));
4597    }
4598
4599    #[test]
4600    /// Server attempts to use send_additional_headers before initial response.
4601    fn no_send_additional_before_initial_response() {
4602        let mut s = Session::new().unwrap();
4603        s.handshake().unwrap();
4604
4605        let (stream, req) = s.send_request(true).unwrap();
4606
4607        assert_eq!(stream, 0);
4608
4609        let ev_headers = Event::Headers {
4610            list: req,
4611            more_frames: false,
4612        };
4613
4614        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4615        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
4616
4617        let info_resp = vec![
4618            Header::new(b":status", b"103"),
4619            Header::new(b"link", b"<https://example.com>; rel=\"preconnect\""),
4620        ];
4621
4622        assert_eq!(
4623            Err(Error::FrameUnexpected),
4624            s.server.send_additional_headers(
4625                &mut s.pipe.server,
4626                stream,
4627                &info_resp,
4628                false,
4629                false
4630            )
4631        );
4632
4633        s.advance().ok();
4634
4635        assert_eq!(s.poll_client(), Err(Error::Done));
4636    }
4637
4638    #[test]
4639    /// Client sends multiple HEADERS before data.
4640    fn additional_headers_before_data_client() {
4641        let mut s = Session::new().unwrap();
4642        s.handshake().unwrap();
4643
4644        let (stream, req) = s.send_request(false).unwrap();
4645
4646        let req_trailer = vec![Header::new(b"goodbye", b"world")];
4647
4648        assert_eq!(
4649            s.client.send_additional_headers(
4650                &mut s.pipe.client,
4651                stream,
4652                &req_trailer,
4653                true,
4654                false
4655            ),
4656            Ok(())
4657        );
4658
4659        s.advance().ok();
4660
4661        let ev_initial_headers = Event::Headers {
4662            list: req,
4663            more_frames: true,
4664        };
4665
4666        let ev_trailing_headers = Event::Headers {
4667            list: req_trailer,
4668            more_frames: true,
4669        };
4670
4671        assert_eq!(s.poll_server(), Ok((stream, ev_initial_headers)));
4672        assert_eq!(s.poll_server(), Ok((stream, ev_trailing_headers)));
4673        assert_eq!(s.poll_server(), Err(Error::Done));
4674    }
4675
4676    #[test]
4677    /// Client sends multiple HEADERS before data.
4678    fn data_after_trailers_client() {
4679        let mut s = Session::new().unwrap();
4680        s.handshake().unwrap();
4681
4682        let (stream, req) = s.send_request(false).unwrap();
4683
4684        let body = s.send_body_client(stream, false).unwrap();
4685
4686        let mut recv_buf = vec![0; body.len()];
4687
4688        let req_trailers = vec![Header::new(b"foo", b"bar")];
4689
4690        s.client
4691            .send_additional_headers(
4692                &mut s.pipe.client,
4693                stream,
4694                &req_trailers,
4695                true,
4696                false,
4697            )
4698            .unwrap();
4699
4700        s.advance().ok();
4701
4702        s.send_frame_client(
4703            frame::Frame::Data {
4704                payload: vec![1, 2, 3, 4],
4705            },
4706            stream,
4707            true,
4708        )
4709        .unwrap();
4710
4711        let ev_headers = Event::Headers {
4712            list: req,
4713            more_frames: true,
4714        };
4715
4716        let ev_trailers = Event::Headers {
4717            list: req_trailers,
4718            more_frames: true,
4719        };
4720
4721        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4722        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
4723        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
4724        assert_eq!(s.poll_server(), Ok((stream, ev_trailers)));
4725        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4726    }
4727
4728    #[test]
4729    /// Send a MAX_PUSH_ID frame from the client on a valid stream.
4730    fn max_push_id_from_client_good() {
4731        let mut s = Session::new().unwrap();
4732        s.handshake().unwrap();
4733
4734        s.send_frame_client(
4735            frame::Frame::MaxPushId { push_id: 1 },
4736            s.client.control_stream_id.unwrap(),
4737            false,
4738        )
4739        .unwrap();
4740
4741        assert_eq!(s.poll_server(), Err(Error::Done));
4742    }
4743
4744    #[test]
4745    /// Send a MAX_PUSH_ID frame from the client on an invalid stream.
4746    fn max_push_id_from_client_bad_stream() {
4747        let mut s = Session::new().unwrap();
4748        s.handshake().unwrap();
4749
4750        let (stream, req) = s.send_request(false).unwrap();
4751
4752        s.send_frame_client(
4753            frame::Frame::MaxPushId { push_id: 2 },
4754            stream,
4755            false,
4756        )
4757        .unwrap();
4758
4759        let ev_headers = Event::Headers {
4760            list: req,
4761            more_frames: true,
4762        };
4763
4764        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4765        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4766    }
4767
4768    #[test]
4769    /// Send a sequence of MAX_PUSH_ID frames from the client that attempt to
4770    /// reduce the limit.
4771    fn max_push_id_from_client_limit_reduction() {
4772        let mut s = Session::new().unwrap();
4773        s.handshake().unwrap();
4774
4775        s.send_frame_client(
4776            frame::Frame::MaxPushId { push_id: 2 },
4777            s.client.control_stream_id.unwrap(),
4778            false,
4779        )
4780        .unwrap();
4781
4782        s.send_frame_client(
4783            frame::Frame::MaxPushId { push_id: 1 },
4784            s.client.control_stream_id.unwrap(),
4785            false,
4786        )
4787        .unwrap();
4788
4789        assert_eq!(s.poll_server(), Err(Error::IdError));
4790    }
4791
4792    #[test]
4793    /// Send a MAX_PUSH_ID frame from the server, which is forbidden.
4794    fn max_push_id_from_server() {
4795        let mut s = Session::new().unwrap();
4796        s.handshake().unwrap();
4797
4798        s.send_frame_server(
4799            frame::Frame::MaxPushId { push_id: 1 },
4800            s.server.control_stream_id.unwrap(),
4801            false,
4802        )
4803        .unwrap();
4804
4805        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
4806    }
4807
4808    #[test]
4809    /// Send a PUSH_PROMISE frame from the client, which is forbidden.
4810    fn push_promise_from_client() {
4811        let mut s = Session::new().unwrap();
4812        s.handshake().unwrap();
4813
4814        let (stream, req) = s.send_request(false).unwrap();
4815
4816        let header_block = s.client.encode_header_block(&req).unwrap();
4817
4818        s.send_frame_client(
4819            frame::Frame::PushPromise {
4820                push_id: 1,
4821                header_block,
4822            },
4823            stream,
4824            false,
4825        )
4826        .unwrap();
4827
4828        let ev_headers = Event::Headers {
4829            list: req,
4830            more_frames: true,
4831        };
4832
4833        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4834        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4835    }
4836
4837    #[test]
4838    /// Server push streams from client are not allowed by the protocol.
4839    fn push_stream_from_client() {
4840        let mut s = Session::new().unwrap();
4841        s.handshake().unwrap();
4842
4843        s.client
4844            .open_uni_stream(
4845                &mut s.pipe.client,
4846                stream::HTTP3_PUSH_STREAM_TYPE_ID,
4847            )
4848            .unwrap();
4849
4850        s.advance().ok();
4851
4852        assert_eq!(s.poll_server(), Err(Error::StreamCreationError));
4853    }
4854
4855    #[test]
4856    /// Server push streams from server are not allowed since the client does
4857    /// not advertise server push support by default.
4858    fn push_stream_from_server() {
4859        let mut s = Session::new().unwrap();
4860        s.handshake().unwrap();
4861
4862        s.server
4863            .open_uni_stream(
4864                &mut s.pipe.server,
4865                stream::HTTP3_PUSH_STREAM_TYPE_ID,
4866            )
4867            .unwrap();
4868
4869        s.advance().ok();
4870
4871        assert_eq!(s.poll_client(), Err(Error::StreamCreationError));
4872    }
4873
4874    #[test]
4875    /// Send a CANCEL_PUSH frame from the client.
4876    fn cancel_push_from_client() {
4877        let mut s = Session::new().unwrap();
4878        s.handshake().unwrap();
4879
4880        s.send_frame_client(
4881            frame::Frame::CancelPush { push_id: 1 },
4882            s.client.control_stream_id.unwrap(),
4883            false,
4884        )
4885        .unwrap();
4886
4887        assert_eq!(s.poll_server(), Err(Error::Done));
4888    }
4889
4890    #[test]
4891    /// Send a CANCEL_PUSH frame from the client on an invalid stream.
4892    fn cancel_push_from_client_bad_stream() {
4893        let mut s = Session::new().unwrap();
4894        s.handshake().unwrap();
4895
4896        let (stream, req) = s.send_request(false).unwrap();
4897
4898        s.send_frame_client(
4899            frame::Frame::CancelPush { push_id: 2 },
4900            stream,
4901            false,
4902        )
4903        .unwrap();
4904
4905        let ev_headers = Event::Headers {
4906            list: req,
4907            more_frames: true,
4908        };
4909
4910        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
4911        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
4912    }
4913
4914    #[test]
4915    /// Send a CANCEL_PUSH frame from the client.
4916    fn cancel_push_from_server() {
4917        let mut s = Session::new().unwrap();
4918        s.handshake().unwrap();
4919
4920        s.send_frame_server(
4921            frame::Frame::CancelPush { push_id: 1 },
4922            s.server.control_stream_id.unwrap(),
4923            false,
4924        )
4925        .unwrap();
4926
4927        assert_eq!(s.poll_client(), Err(Error::Done));
4928    }
4929
4930    #[test]
4931    /// Send a GOAWAY frame from the client.
4932    fn goaway_from_client_good() {
4933        let mut s = Session::new().unwrap();
4934        s.handshake().unwrap();
4935
4936        s.client.send_goaway(&mut s.pipe.client, 100).unwrap();
4937
4938        s.advance().ok();
4939
4940        // TODO: server push
4941        assert_eq!(s.poll_server(), Ok((0, Event::GoAway)));
4942    }
4943
4944    #[test]
4945    /// Send a GOAWAY frame from the server.
4946    fn goaway_from_server_good() {
4947        let mut s = Session::new().unwrap();
4948        s.handshake().unwrap();
4949
4950        s.server.send_goaway(&mut s.pipe.server, 4000).unwrap();
4951
4952        s.advance().ok();
4953
4954        assert_eq!(s.poll_client(), Ok((4000, Event::GoAway)));
4955    }
4956
4957    #[test]
4958    /// A client MUST NOT send a request after it receives GOAWAY.
4959    fn client_request_after_goaway() {
4960        let mut s = Session::new().unwrap();
4961        s.handshake().unwrap();
4962
4963        s.server.send_goaway(&mut s.pipe.server, 4000).unwrap();
4964
4965        s.advance().ok();
4966
4967        assert_eq!(s.poll_client(), Ok((4000, Event::GoAway)));
4968
4969        assert_eq!(s.send_request(true), Err(Error::FrameUnexpected));
4970    }
4971
4972    #[test]
4973    /// Send a GOAWAY frame from the server, using an invalid goaway ID.
4974    fn goaway_from_server_invalid_id() {
4975        let mut s = Session::new().unwrap();
4976        s.handshake().unwrap();
4977
4978        s.send_frame_server(
4979            frame::Frame::GoAway { id: 1 },
4980            s.server.control_stream_id.unwrap(),
4981            false,
4982        )
4983        .unwrap();
4984
4985        assert_eq!(s.poll_client(), Err(Error::IdError));
4986    }
4987
4988    #[test]
4989    /// Send multiple GOAWAY frames from the server, that increase the goaway
4990    /// ID.
4991    fn goaway_from_server_increase_id() {
4992        let mut s = Session::new().unwrap();
4993        s.handshake().unwrap();
4994
4995        s.send_frame_server(
4996            frame::Frame::GoAway { id: 0 },
4997            s.server.control_stream_id.unwrap(),
4998            false,
4999        )
5000        .unwrap();
5001
5002        s.send_frame_server(
5003            frame::Frame::GoAway { id: 4 },
5004            s.server.control_stream_id.unwrap(),
5005            false,
5006        )
5007        .unwrap();
5008
5009        assert_eq!(s.poll_client(), Ok((0, Event::GoAway)));
5010
5011        assert_eq!(s.poll_client(), Err(Error::IdError));
5012    }
5013
5014    #[test]
5015    #[cfg(feature = "sfv")]
5016    fn parse_priority_field_value() {
5017        // Legal dicts
5018        assert_eq!(
5019            Ok(Priority::new(0, false)),
5020            Priority::try_from(b"u=0".as_slice())
5021        );
5022        assert_eq!(
5023            Ok(Priority::new(3, false)),
5024            Priority::try_from(b"u=3".as_slice())
5025        );
5026        assert_eq!(
5027            Ok(Priority::new(7, false)),
5028            Priority::try_from(b"u=7".as_slice())
5029        );
5030
5031        assert_eq!(
5032            Ok(Priority::new(0, true)),
5033            Priority::try_from(b"u=0, i".as_slice())
5034        );
5035        assert_eq!(
5036            Ok(Priority::new(3, true)),
5037            Priority::try_from(b"u=3, i".as_slice())
5038        );
5039        assert_eq!(
5040            Ok(Priority::new(7, true)),
5041            Priority::try_from(b"u=7, i".as_slice())
5042        );
5043
5044        assert_eq!(
5045            Ok(Priority::new(0, true)),
5046            Priority::try_from(b"u=0, i=?1".as_slice())
5047        );
5048        assert_eq!(
5049            Ok(Priority::new(3, true)),
5050            Priority::try_from(b"u=3, i=?1".as_slice())
5051        );
5052        assert_eq!(
5053            Ok(Priority::new(7, true)),
5054            Priority::try_from(b"u=7, i=?1".as_slice())
5055        );
5056
5057        assert_eq!(
5058            Ok(Priority::new(3, false)),
5059            Priority::try_from(b"".as_slice())
5060        );
5061
5062        assert_eq!(
5063            Ok(Priority::new(0, true)),
5064            Priority::try_from(b"u=0;foo, i;bar".as_slice())
5065        );
5066        assert_eq!(
5067            Ok(Priority::new(3, true)),
5068            Priority::try_from(b"u=3;hello, i;world".as_slice())
5069        );
5070        assert_eq!(
5071            Ok(Priority::new(7, true)),
5072            Priority::try_from(b"u=7;croeso, i;gymru".as_slice())
5073        );
5074
5075        assert_eq!(
5076            Ok(Priority::new(0, true)),
5077            Priority::try_from(b"u=0, i, spinaltap=11".as_slice())
5078        );
5079
5080        // Illegal formats
5081        assert_eq!(Err(Error::Done), Priority::try_from(b"0".as_slice()));
5082        assert_eq!(
5083            Ok(Priority::new(7, false)),
5084            Priority::try_from(b"u=-1".as_slice())
5085        );
5086        assert_eq!(Err(Error::Done), Priority::try_from(b"u=0.2".as_slice()));
5087        assert_eq!(
5088            Ok(Priority::new(7, false)),
5089            Priority::try_from(b"u=100".as_slice())
5090        );
5091        assert_eq!(
5092            Err(Error::Done),
5093            Priority::try_from(b"u=3, i=true".as_slice())
5094        );
5095
5096        // Trailing comma in dict is malformed
5097        assert_eq!(Err(Error::Done), Priority::try_from(b"u=7, ".as_slice()));
5098    }
5099
5100    #[test]
5101    /// Send a PRIORITY_UPDATE for request stream from the client.
5102    fn priority_update_request() {
5103        let mut s = Session::new().unwrap();
5104        s.handshake().unwrap();
5105
5106        s.client
5107            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5108                urgency: 3,
5109                incremental: false,
5110            })
5111            .unwrap();
5112        s.advance().ok();
5113
5114        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5115        assert_eq!(s.poll_server(), Err(Error::Done));
5116    }
5117
5118    #[test]
5119    /// Send a PRIORITY_UPDATE for request stream from the client that is too
5120    /// large.
5121    fn priority_update_request_max_size_limit_default() {
5122        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5123        config
5124            .load_cert_chain_from_pem_file("examples/cert.crt")
5125            .unwrap();
5126        config
5127            .load_priv_key_from_pem_file("examples/cert.key")
5128            .unwrap();
5129        config.set_application_protos(&[b"h3"]).unwrap();
5130        config.set_initial_max_data(1500);
5131        config.set_initial_max_stream_data_bidi_local(1500);
5132        config.set_initial_max_stream_data_bidi_remote(1500);
5133        config.set_initial_max_stream_data_uni(1500);
5134        config.set_initial_max_streams_bidi(5);
5135        config.set_initial_max_streams_uni(5);
5136        config.verify_peer(false);
5137
5138        let h3_config = Config::new().unwrap();
5139
5140        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
5141
5142        s.handshake().unwrap();
5143
5144        let mut d = vec![42; 600];
5145        let mut b = octets::OctetsMut::with_slice(&mut d);
5146
5147        let pu = frame::Frame::PriorityUpdateRequest {
5148            prioritized_element_id: 0,
5149            priority_field_value: vec![0; 512],
5150        };
5151
5152        pu.to_bytes(&mut b).unwrap();
5153
5154        s.pipe.client.stream_send(2, &d, true).unwrap();
5155
5156        s.advance().ok();
5157
5158        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
5159
5160        assert_eq!(
5161            s.pipe.server.local_error.as_ref().unwrap().error_code,
5162            Error::to_wire(Error::ExcessiveLoad)
5163        );
5164    }
5165
5166    #[test]
5167    /// Send a PRIORITY_UPDATE for request stream from the client.
5168    fn priority_update_single_stream_rearm() {
5169        let mut s = Session::new().unwrap();
5170        s.handshake().unwrap();
5171
5172        s.client
5173            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5174                urgency: 3,
5175                incremental: false,
5176            })
5177            .unwrap();
5178        s.advance().ok();
5179
5180        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5181        assert_eq!(s.poll_server(), Err(Error::Done));
5182
5183        s.client
5184            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5185                urgency: 5,
5186                incremental: false,
5187            })
5188            .unwrap();
5189        s.advance().ok();
5190
5191        assert_eq!(s.poll_server(), Err(Error::Done));
5192
5193        // There is only one PRIORITY_UPDATE frame to read. Once read, the event
5194        // will rearm ready for more.
5195        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=5".to_vec()));
5196        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5197
5198        s.client
5199            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5200                urgency: 7,
5201                incremental: false,
5202            })
5203            .unwrap();
5204        s.advance().ok();
5205
5206        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5207        assert_eq!(s.poll_server(), Err(Error::Done));
5208
5209        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=7".to_vec()));
5210        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5211    }
5212
5213    #[test]
5214    /// Send multiple PRIORITY_UPDATE frames for different streams from the
5215    /// client across multiple flights of exchange.
5216    fn priority_update_request_multiple_stream_arm_multiple_flights() {
5217        let mut s = Session::new().unwrap();
5218        s.handshake().unwrap();
5219
5220        s.client
5221            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5222                urgency: 3,
5223                incremental: false,
5224            })
5225            .unwrap();
5226        s.advance().ok();
5227
5228        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5229        assert_eq!(s.poll_server(), Err(Error::Done));
5230
5231        s.client
5232            .send_priority_update_for_request(&mut s.pipe.client, 4, &Priority {
5233                urgency: 1,
5234                incremental: false,
5235            })
5236            .unwrap();
5237        s.advance().ok();
5238
5239        assert_eq!(s.poll_server(), Ok((4, Event::PriorityUpdate)));
5240        assert_eq!(s.poll_server(), Err(Error::Done));
5241
5242        s.client
5243            .send_priority_update_for_request(&mut s.pipe.client, 8, &Priority {
5244                urgency: 2,
5245                incremental: false,
5246            })
5247            .unwrap();
5248        s.advance().ok();
5249
5250        assert_eq!(s.poll_server(), Ok((8, Event::PriorityUpdate)));
5251        assert_eq!(s.poll_server(), Err(Error::Done));
5252
5253        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5254        assert_eq!(s.server.take_last_priority_update(4), Ok(b"u=1".to_vec()));
5255        assert_eq!(s.server.take_last_priority_update(8), Ok(b"u=2".to_vec()));
5256        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5257    }
5258
5259    #[test]
5260    /// Send multiple PRIORITY_UPDATE frames for different streams from the
5261    /// client across a single flight.
5262    fn priority_update_request_multiple_stream_arm_single_flight() {
5263        let mut s = Session::new().unwrap();
5264        s.handshake().unwrap();
5265
5266        let mut d = [42; 65535];
5267
5268        let mut b = octets::OctetsMut::with_slice(&mut d);
5269
5270        let p1 = frame::Frame::PriorityUpdateRequest {
5271            prioritized_element_id: 0,
5272            priority_field_value: b"u=3".to_vec(),
5273        };
5274
5275        let p2 = frame::Frame::PriorityUpdateRequest {
5276            prioritized_element_id: 4,
5277            priority_field_value: b"u=3".to_vec(),
5278        };
5279
5280        let p3 = frame::Frame::PriorityUpdateRequest {
5281            prioritized_element_id: 8,
5282            priority_field_value: b"u=3".to_vec(),
5283        };
5284
5285        p1.to_bytes(&mut b).unwrap();
5286        p2.to_bytes(&mut b).unwrap();
5287        p3.to_bytes(&mut b).unwrap();
5288
5289        let off = b.off();
5290        s.pipe
5291            .client
5292            .stream_send(s.client.control_stream_id.unwrap(), &d[..off], false)
5293            .unwrap();
5294
5295        s.advance().ok();
5296
5297        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5298        assert_eq!(s.poll_server(), Ok((4, Event::PriorityUpdate)));
5299        assert_eq!(s.poll_server(), Ok((8, Event::PriorityUpdate)));
5300        assert_eq!(s.poll_server(), Err(Error::Done));
5301
5302        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5303        assert_eq!(s.server.take_last_priority_update(4), Ok(b"u=3".to_vec()));
5304        assert_eq!(s.server.take_last_priority_update(8), Ok(b"u=3".to_vec()));
5305
5306        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5307    }
5308
5309    #[test]
5310    /// Send a PRIORITY_UPDATE for a request stream, before and after the stream
5311    /// has been completed.
5312    fn priority_update_request_collected_completed() {
5313        let mut s = Session::new().unwrap();
5314        s.handshake().unwrap();
5315
5316        s.client
5317            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5318                urgency: 3,
5319                incremental: false,
5320            })
5321            .unwrap();
5322        s.advance().ok();
5323
5324        let (stream, req) = s.send_request(true).unwrap();
5325        let ev_headers = Event::Headers {
5326            list: req,
5327            more_frames: false,
5328        };
5329
5330        // Priority event is generated before request headers.
5331        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5332        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5333        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
5334        assert_eq!(s.poll_server(), Err(Error::Done));
5335
5336        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5337        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5338
5339        let resp = s.send_response(stream, true).unwrap();
5340
5341        let ev_headers = Event::Headers {
5342            list: resp,
5343            more_frames: false,
5344        };
5345
5346        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
5347        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
5348        assert_eq!(s.poll_client(), Err(Error::Done));
5349
5350        // Now send a PRIORITY_UPDATE for the completed request stream.
5351        s.client
5352            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5353                urgency: 3,
5354                incremental: false,
5355            })
5356            .unwrap();
5357        s.advance().ok();
5358
5359        // No event generated at server
5360        assert_eq!(s.poll_server(), Err(Error::Done));
5361    }
5362
5363    #[test]
5364    /// Send a PRIORITY_UPDATE for a request stream after H3 has collected it,
5365    /// but before the transport stream has been collected.
5366    fn priority_update_request_after_h3_collection() {
5367        let mut s = Session::new().unwrap();
5368        s.handshake().unwrap();
5369
5370        let init_streams_server = s.server.streams.len();
5371
5372        let (stream, req) = s.send_request(true).unwrap();
5373        let ev_headers = Event::Headers {
5374            list: req,
5375            more_frames: false,
5376        };
5377
5378        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5379        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
5380        assert_eq!(s.poll_server(), Err(Error::Done));
5381
5382        let resp = vec![
5383            Header::new(b":status", b"200"),
5384            Header::new(b"server", b"quiche-test"),
5385        ];
5386
5387        s.server
5388            .send_response(&mut s.pipe.server, stream, &resp, true)
5389            .unwrap();
5390
5391        // H3 no longer needs its stream state once it has sent the response
5392        // FIN and consumed the request FIN. The QUIC stream remains until the
5393        // response FIN is acknowledged by the peer.
5394        assert_eq!(s.server.streams.len(), init_streams_server);
5395        assert!(s.pipe.server.stream_finished(stream));
5396        assert!(s.pipe.server.stream_closed(stream));
5397
5398        let stream_state = s.pipe.server.streams.get(stream).unwrap();
5399        assert!(stream_state.recv.is_fin());
5400        assert!(stream_state.send.is_fin());
5401        assert!(!s.pipe.server.streams.is_collected(stream));
5402
5403        s.client
5404            .send_priority_update_for_request(
5405                &mut s.pipe.client,
5406                stream,
5407                &Priority {
5408                    urgency: 3,
5409                    incremental: false,
5410                },
5411            )
5412            .unwrap();
5413
5414        let flight = crate::test_utils::emit_flight(&mut s.pipe.client).unwrap();
5415        crate::test_utils::process_flight(&mut s.pipe.server, flight).unwrap();
5416
5417        assert_eq!(s.poll_server(), Err(Error::Done));
5418        assert_eq!(s.server.streams.len(), init_streams_server);
5419    }
5420
5421    #[test]
5422    /// Send a PRIORITY_UPDATE for a request stream, before and after the stream
5423    /// has been stopped.
5424    fn priority_update_request_collected_stopped() {
5425        let mut s = Session::new().unwrap();
5426        s.handshake().unwrap();
5427
5428        s.client
5429            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5430                urgency: 3,
5431                incremental: false,
5432            })
5433            .unwrap();
5434        s.advance().ok();
5435
5436        let (stream, req) = s.send_request(false).unwrap();
5437        let ev_headers = Event::Headers {
5438            list: req,
5439            more_frames: true,
5440        };
5441
5442        // Priority event is generated before request headers.
5443        assert_eq!(s.poll_server(), Ok((0, Event::PriorityUpdate)));
5444        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5445        assert_eq!(s.poll_server(), Err(Error::Done));
5446
5447        assert_eq!(s.server.take_last_priority_update(0), Ok(b"u=3".to_vec()));
5448        assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5449
5450        s.pipe
5451            .client
5452            .stream_shutdown(stream, crate::Shutdown::Write, 0x100)
5453            .unwrap();
5454        s.pipe
5455            .client
5456            .stream_shutdown(stream, crate::Shutdown::Read, 0x100)
5457            .unwrap();
5458
5459        s.advance().ok();
5460
5461        assert_eq!(s.poll_server(), Ok((0, Event::Reset(0x100))));
5462        assert_eq!(s.poll_server(), Err(Error::Done));
5463
5464        // Now send a PRIORITY_UPDATE for the closed request stream.
5465        s.client
5466            .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5467                urgency: 3,
5468                incremental: false,
5469            })
5470            .unwrap();
5471        s.advance().ok();
5472
5473        // No event generated at server
5474        assert_eq!(s.poll_server(), Err(Error::Done));
5475
5476        assert!(s.pipe.server.streams.is_collected(0));
5477        assert!(s.pipe.client.streams.is_collected(0));
5478    }
5479
5480    #[test]
5481    /// Send a PRIORITY_UPDATE for push stream from the client.
5482    fn priority_update_push() {
5483        let mut s = Session::new().unwrap();
5484        s.handshake().unwrap();
5485
5486        s.send_frame_client(
5487            frame::Frame::PriorityUpdatePush {
5488                prioritized_element_id: 3,
5489                priority_field_value: b"u=3".to_vec(),
5490            },
5491            s.client.control_stream_id.unwrap(),
5492            false,
5493        )
5494        .unwrap();
5495
5496        assert_eq!(s.poll_server(), Err(Error::Done));
5497    }
5498
5499    #[test]
5500    /// Send a PRIORITY_UPDATE for request stream from the client but for an
5501    /// incorrect stream type.
5502    fn priority_update_request_bad_stream() {
5503        let mut s = Session::new().unwrap();
5504        s.handshake().unwrap();
5505
5506        s.send_frame_client(
5507            frame::Frame::PriorityUpdateRequest {
5508                prioritized_element_id: 5,
5509                priority_field_value: b"u=3".to_vec(),
5510            },
5511            s.client.control_stream_id.unwrap(),
5512            false,
5513        )
5514        .unwrap();
5515
5516        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
5517    }
5518
5519    #[test]
5520    /// Send a PRIORITY_UPDATE for push stream from the client but for an
5521    /// incorrect stream type.
5522    fn priority_update_push_bad_stream() {
5523        let mut s = Session::new().unwrap();
5524        s.handshake().unwrap();
5525
5526        s.send_frame_client(
5527            frame::Frame::PriorityUpdatePush {
5528                prioritized_element_id: 5,
5529                priority_field_value: b"u=3".to_vec(),
5530            },
5531            s.client.control_stream_id.unwrap(),
5532            false,
5533        )
5534        .unwrap();
5535
5536        assert_eq!(s.poll_server(), Err(Error::FrameUnexpected));
5537    }
5538
5539    #[test]
5540    /// Send a PRIORITY_UPDATE for request stream from the server.
5541    fn priority_update_request_from_server() {
5542        let mut s = Session::new().unwrap();
5543        s.handshake().unwrap();
5544
5545        s.send_frame_server(
5546            frame::Frame::PriorityUpdateRequest {
5547                prioritized_element_id: 0,
5548                priority_field_value: b"u=3".to_vec(),
5549            },
5550            s.server.control_stream_id.unwrap(),
5551            false,
5552        )
5553        .unwrap();
5554
5555        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
5556    }
5557
5558    #[test]
5559    /// Send a PRIORITY_UPDATE for request stream from the server.
5560    fn priority_update_push_from_server() {
5561        let mut s = Session::new().unwrap();
5562        s.handshake().unwrap();
5563
5564        s.send_frame_server(
5565            frame::Frame::PriorityUpdatePush {
5566                prioritized_element_id: 0,
5567                priority_field_value: b"u=3".to_vec(),
5568            },
5569            s.server.control_stream_id.unwrap(),
5570            false,
5571        )
5572        .unwrap();
5573
5574        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
5575    }
5576
5577    #[test]
5578    /// Ensure quiche allocates streams for client and server roles as expected.
5579    fn uni_stream_local_counting() {
5580        let config = Config::new().unwrap();
5581
5582        let h3_cln = Connection::new(&config, false, false).unwrap();
5583        assert_eq!(h3_cln.next_uni_stream_id, 2);
5584
5585        let h3_srv = Connection::new(&config, true, false).unwrap();
5586        assert_eq!(h3_srv.next_uni_stream_id, 3);
5587    }
5588
5589    #[test]
5590    /// Client opens multiple control streams, which is forbidden.
5591    fn open_multiple_control_streams() {
5592        let mut s = Session::new().unwrap();
5593        s.handshake().unwrap();
5594
5595        let stream_id = s.client.next_uni_stream_id;
5596
5597        let mut d = [42; 8];
5598        let mut b = octets::OctetsMut::with_slice(&mut d);
5599
5600        s.pipe
5601            .client
5602            .stream_send(
5603                stream_id,
5604                b.put_varint(stream::HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(),
5605                false,
5606            )
5607            .unwrap();
5608
5609        s.advance().ok();
5610
5611        assert_eq!(s.poll_server(), Err(Error::StreamCreationError));
5612    }
5613
5614    #[test]
5615    /// Client closes the control stream, which is forbidden.
5616    fn close_control_stream_after_type() {
5617        let mut s = Session::new().unwrap();
5618        s.handshake().unwrap();
5619
5620        s.pipe
5621            .client
5622            .stream_send(s.client.control_stream_id.unwrap(), &[], true)
5623            .unwrap();
5624
5625        s.advance().ok();
5626
5627        assert_eq!(
5628            Err(Error::ClosedCriticalStream),
5629            s.server.poll(&mut s.pipe.server)
5630        );
5631        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5632    }
5633
5634    #[test]
5635    /// Client closes the control stream after a frame is sent, which is
5636    /// forbidden.
5637    fn close_control_stream_after_frame() {
5638        let mut s = Session::new().unwrap();
5639        s.handshake().unwrap();
5640
5641        s.send_frame_client(
5642            frame::Frame::MaxPushId { push_id: 1 },
5643            s.client.control_stream_id.unwrap(),
5644            true,
5645        )
5646        .unwrap();
5647
5648        assert_eq!(
5649            Err(Error::ClosedCriticalStream),
5650            s.server.poll(&mut s.pipe.server)
5651        );
5652        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5653    }
5654
5655    #[test]
5656    /// Client resets the control stream, which is forbidden.
5657    fn reset_control_stream_after_type() {
5658        let mut s = Session::new().unwrap();
5659        s.handshake().unwrap();
5660
5661        s.pipe
5662            .client
5663            .stream_shutdown(
5664                s.client.control_stream_id.unwrap(),
5665                crate::Shutdown::Write,
5666                0,
5667            )
5668            .unwrap();
5669
5670        s.advance().ok();
5671
5672        assert_eq!(
5673            Err(Error::ClosedCriticalStream),
5674            s.server.poll(&mut s.pipe.server)
5675        );
5676        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5677    }
5678
5679    #[test]
5680    /// Client resets the control stream after a frame is sent, which is
5681    /// forbidden.
5682    fn reset_control_stream_after_frame() {
5683        let mut s = Session::new().unwrap();
5684        s.handshake().unwrap();
5685
5686        s.send_frame_client(
5687            frame::Frame::MaxPushId { push_id: 1 },
5688            s.client.control_stream_id.unwrap(),
5689            false,
5690        )
5691        .unwrap();
5692
5693        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5694
5695        s.pipe
5696            .client
5697            .stream_shutdown(
5698                s.client.control_stream_id.unwrap(),
5699                crate::Shutdown::Write,
5700                0,
5701            )
5702            .unwrap();
5703
5704        s.advance().ok();
5705
5706        assert_eq!(
5707            Err(Error::ClosedCriticalStream),
5708            s.server.poll(&mut s.pipe.server)
5709        );
5710        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5711    }
5712
5713    #[test]
5714    /// Client closes QPACK stream, which is forbidden.
5715    fn close_qpack_stream_after_type() {
5716        let mut s = Session::new().unwrap();
5717        s.handshake().unwrap();
5718
5719        s.pipe
5720            .client
5721            .stream_send(
5722                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
5723                &[],
5724                true,
5725            )
5726            .unwrap();
5727
5728        s.advance().ok();
5729
5730        assert_eq!(
5731            Err(Error::ClosedCriticalStream),
5732            s.server.poll(&mut s.pipe.server)
5733        );
5734        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5735    }
5736
5737    #[test]
5738    /// Client closes QPACK stream after sending some stuff, which is forbidden.
5739    fn close_qpack_stream_after_data() {
5740        let mut s = Session::new().unwrap();
5741        s.handshake().unwrap();
5742
5743        let stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5744        let d = [0; 1];
5745
5746        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5747        s.pipe.client.stream_send(stream_id, &d, true).unwrap();
5748
5749        s.advance().ok();
5750
5751        assert_eq!(
5752            Err(Error::ClosedCriticalStream),
5753            s.server.poll(&mut s.pipe.server)
5754        );
5755        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5756    }
5757
5758    #[test]
5759    /// Client resets QPACK stream, which is forbidden.
5760    fn reset_qpack_stream_after_type() {
5761        let mut s = Session::new().unwrap();
5762        s.handshake().unwrap();
5763
5764        s.pipe
5765            .client
5766            .stream_shutdown(
5767                s.client.local_qpack_streams.encoder_stream_id.unwrap(),
5768                crate::Shutdown::Write,
5769                0,
5770            )
5771            .unwrap();
5772
5773        s.advance().ok();
5774
5775        assert_eq!(
5776            Err(Error::ClosedCriticalStream),
5777            s.server.poll(&mut s.pipe.server)
5778        );
5779        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5780    }
5781
5782    #[test]
5783    /// Client resets QPACK stream after sending some stuff, which is forbidden.
5784    fn reset_qpack_stream_after_data() {
5785        let mut s = Session::new().unwrap();
5786        s.handshake().unwrap();
5787
5788        let stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5789        let d = [0; 1];
5790
5791        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5792        s.pipe.client.stream_send(stream_id, &d, false).unwrap();
5793
5794        s.advance().ok();
5795
5796        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5797
5798        s.pipe
5799            .client
5800            .stream_shutdown(stream_id, crate::Shutdown::Write, 0)
5801            .unwrap();
5802
5803        s.advance().ok();
5804
5805        assert_eq!(
5806            Err(Error::ClosedCriticalStream),
5807            s.server.poll(&mut s.pipe.server)
5808        );
5809        assert_eq!(Err(Error::Done), s.server.poll(&mut s.pipe.server));
5810    }
5811
5812    #[test]
5813    /// Client sends QPACK data.
5814    fn qpack_data() {
5815        // TODO: QPACK instructions are ignored until dynamic table support is
5816        // added so we just test that the data is safely ignored.
5817        let mut s = Session::new().unwrap();
5818        s.handshake().unwrap();
5819
5820        let e_stream_id = s.client.local_qpack_streams.encoder_stream_id.unwrap();
5821        let d_stream_id = s.client.local_qpack_streams.decoder_stream_id.unwrap();
5822        let d = [0; 20];
5823
5824        s.pipe.client.stream_send(e_stream_id, &d, false).unwrap();
5825        s.advance().ok();
5826
5827        s.pipe.client.stream_send(d_stream_id, &d, false).unwrap();
5828        s.advance().ok();
5829
5830        match s.server.poll(&mut s.pipe.server) {
5831            Ok(_) => panic!(),
5832
5833            Err(Error::Done) => {
5834                assert_eq!(s.server.peer_qpack_streams.encoder_stream_bytes, 20);
5835                assert_eq!(s.server.peer_qpack_streams.decoder_stream_bytes, 20);
5836            },
5837
5838            Err(_) => {
5839                panic!();
5840            },
5841        }
5842
5843        let stats = s.server.stats();
5844        assert_eq!(stats.qpack_encoder_stream_recv_bytes, 20);
5845        assert_eq!(stats.qpack_decoder_stream_recv_bytes, 20);
5846    }
5847
5848    #[test]
5849    /// Tests limits for the stream state buffer maximum size.
5850    fn max_state_buf_size() {
5851        let mut s = Session::new().unwrap();
5852        s.handshake().unwrap();
5853
5854        let req = vec![
5855            Header::new(b":method", b"GET"),
5856            Header::new(b":scheme", b"https"),
5857            Header::new(b":authority", b"quic.tech"),
5858            Header::new(b":path", b"/test"),
5859            Header::new(b"user-agent", b"quiche-test"),
5860        ];
5861
5862        assert_eq!(
5863            s.client.send_request(&mut s.pipe.client, &req, false),
5864            Ok(0)
5865        );
5866
5867        s.advance().ok();
5868
5869        let ev_headers = Event::Headers {
5870            list: req,
5871            more_frames: true,
5872        };
5873
5874        assert_eq!(s.server.poll(&mut s.pipe.server), Ok((0, ev_headers)));
5875
5876        // DATA frames don't consume the state buffer, so can be of any size.
5877        let mut d = [42; 128];
5878        let mut b = octets::OctetsMut::with_slice(&mut d);
5879
5880        let frame_type = b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
5881        s.pipe.client.stream_send(0, frame_type, false).unwrap();
5882
5883        let frame_len = b.put_varint(1 << 24).unwrap();
5884        s.pipe.client.stream_send(0, frame_len, false).unwrap();
5885
5886        s.pipe.client.stream_send(0, &d, false).unwrap();
5887
5888        s.advance().ok();
5889
5890        assert_eq!(s.server.poll(&mut s.pipe.server), Ok((0, Event::Data)));
5891
5892        // GREASE frames consume the state buffer, so need to be limited.
5893        let mut s = Session::new().unwrap();
5894        s.handshake().unwrap();
5895
5896        let mut d = [42; 128];
5897        let mut b = octets::OctetsMut::with_slice(&mut d);
5898
5899        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
5900        s.pipe.client.stream_send(0, frame_type, false).unwrap();
5901
5902        let frame_len = b.put_varint(1 << 24).unwrap();
5903        s.pipe.client.stream_send(0, frame_len, false).unwrap();
5904
5905        s.pipe.client.stream_send(0, &d, false).unwrap();
5906
5907        s.advance().ok();
5908
5909        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::ExcessiveLoad));
5910    }
5911
5912    #[test]
5913    /// Tests that DATA frames are properly truncated depending on the request
5914    /// stream's outgoing flow control capacity.
5915    fn stream_backpressure() {
5916        let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
5917
5918        let mut s = Session::new().unwrap();
5919        s.handshake().unwrap();
5920
5921        let (stream, req) = s.send_request(false).unwrap();
5922
5923        let total_data_frames = 6;
5924
5925        for _ in 0..total_data_frames {
5926            assert_eq!(
5927                s.client
5928                    .send_body(&mut s.pipe.client, stream, &bytes, false),
5929                Ok(bytes.len())
5930            );
5931
5932            s.advance().ok();
5933        }
5934
5935        assert_eq!(
5936            s.client.send_body(&mut s.pipe.client, stream, &bytes, true),
5937            Ok(bytes.len() - 2)
5938        );
5939
5940        s.advance().ok();
5941
5942        let mut recv_buf = vec![0; bytes.len()];
5943
5944        let ev_headers = Event::Headers {
5945            list: req,
5946            more_frames: true,
5947        };
5948
5949        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
5950        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
5951        assert_eq!(s.poll_server(), Err(Error::Done));
5952
5953        for _ in 0..total_data_frames {
5954            assert_eq!(
5955                s.recv_body_server(stream, &mut recv_buf),
5956                Ok(bytes.len())
5957            );
5958        }
5959
5960        assert_eq!(
5961            s.recv_body_server(stream, &mut recv_buf),
5962            Ok(bytes.len() - 2)
5963        );
5964
5965        // Fin flag from last send_body() call was not sent as the buffer was
5966        // only partially written.
5967        assert_eq!(s.poll_server(), Err(Error::Done));
5968
5969        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
5970        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
5971        assert_eq!(s.pipe.server.data_blocked_recv_count, 0);
5972        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 1);
5973
5974        assert_eq!(s.pipe.client.data_blocked_sent_count, 0);
5975        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 1);
5976        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
5977        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
5978    }
5979
5980    #[test]
5981    /// Tests that the max header list size setting allows larger headers than
5982    /// default.
5983    fn request_max_header_size_limit_accepts_large_headers() {
5984        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5985        config
5986            .load_cert_chain_from_pem_file("examples/cert.crt")
5987            .unwrap();
5988        config
5989            .load_priv_key_from_pem_file("examples/cert.key")
5990            .unwrap();
5991        config.set_application_protos(&[b"h3"]).unwrap();
5992        config.set_initial_max_data(150000000);
5993        config.set_initial_max_stream_data_bidi_local(150000000);
5994        config.set_initial_max_stream_data_bidi_remote(150000000);
5995        config.set_initial_max_stream_data_uni(150000000);
5996        config.set_initial_max_streams_bidi(5);
5997        config.set_initial_max_streams_uni(5);
5998        config.verify_peer(false);
5999        config.set_initial_congestion_window_packets(100);
6000
6001        let mut h3_config = Config::new().unwrap();
6002        h3_config.set_max_field_section_size(256 * 1024);
6003
6004        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6005
6006        s.handshake().unwrap();
6007
6008        let mut req = vec![
6009            Header::new(b":method", b"GET"),
6010            Header::new(b":scheme", b"https"),
6011            Header::new(b":authority", b"quic.tech"),
6012            Header::new(b":path", b"/test"),
6013        ];
6014
6015        for _ in 1..5000 {
6016            req.push(Header::new(b"aaaaaaaaaa", b"aaaaaaaaa"));
6017        }
6018
6019        let ev_headers = Event::Headers {
6020            list: req.clone(),
6021            more_frames: false,
6022        };
6023
6024        let stream = s
6025            .client
6026            .send_request(&mut s.pipe.client, &req, true)
6027            .unwrap();
6028
6029        s.advance().ok();
6030
6031        assert_eq!(stream, 0);
6032
6033        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6034        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6035        assert_eq!(s.poll_server(), Err(Error::Done));
6036    }
6037
6038    #[test]
6039    /// Tests that the max header list size setting is enforced after decoding.
6040    fn request_max_header_size_limit_decoded_field_section() {
6041        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6042        config
6043            .load_cert_chain_from_pem_file("examples/cert.crt")
6044            .unwrap();
6045        config
6046            .load_priv_key_from_pem_file("examples/cert.key")
6047            .unwrap();
6048        config.set_application_protos(&[b"h3"]).unwrap();
6049        config.set_initial_max_data(1500);
6050        config.set_initial_max_stream_data_bidi_local(150);
6051        config.set_initial_max_stream_data_bidi_remote(150);
6052        config.set_initial_max_stream_data_uni(150);
6053        config.set_initial_max_streams_bidi(5);
6054        config.set_initial_max_streams_uni(5);
6055        config.verify_peer(false);
6056
6057        let mut h3_config = Config::new().unwrap();
6058        h3_config.set_max_field_section_size(65);
6059
6060        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6061
6062        s.handshake().unwrap();
6063
6064        let req = vec![
6065            Header::new(b":method", b"GET"),
6066            Header::new(b":scheme", b"https"),
6067            Header::new(b":authority", b"quic.tech"),
6068            Header::new(b":path", b"/test"),
6069            Header::new(b"aaaaaaa", b"aaaaaaaa"),
6070        ];
6071
6072        let stream = s
6073            .client
6074            .send_request(&mut s.pipe.client, &req, true)
6075            .unwrap();
6076
6077        s.advance().ok();
6078
6079        assert_eq!(stream, 0);
6080
6081        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
6082
6083        assert_eq!(
6084            s.pipe.server.local_error.as_ref().unwrap().error_code,
6085            Error::to_wire(Error::ExcessiveLoad)
6086        );
6087    }
6088
6089    #[test]
6090    /// Tests that the max header list size setting is enforced when observing
6091    /// frame size before decode.
6092    fn request_max_header_size_limit_default_abort_before_decode() {
6093        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6094        config
6095            .load_cert_chain_from_pem_file("examples/cert.crt")
6096            .unwrap();
6097        config
6098            .load_priv_key_from_pem_file("examples/cert.key")
6099            .unwrap();
6100        config.set_application_protos(&[b"h3"]).unwrap();
6101        config.set_initial_max_data(150000);
6102        config.set_initial_max_stream_data_bidi_local(150000);
6103        config.set_initial_max_stream_data_bidi_remote(150000);
6104        config.set_initial_max_stream_data_uni(150000);
6105        config.set_initial_max_streams_bidi(5);
6106        config.set_initial_max_streams_uni(5);
6107        config.verify_peer(false);
6108
6109        let h3_config = Config::new().unwrap();
6110
6111        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6112
6113        s.handshake().unwrap();
6114
6115        let mut d = vec![42; 200000];
6116        let mut b = octets::OctetsMut::with_slice(&mut d);
6117
6118        let hdrs = frame::Frame::Headers {
6119            header_block: vec![0; 65536],
6120        };
6121
6122        hdrs.to_bytes(&mut b).unwrap();
6123
6124        s.pipe.client.stream_send(0, &d, true).unwrap();
6125
6126        s.advance().ok();
6127
6128        assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
6129
6130        assert_eq!(
6131            s.pipe.server.local_error.as_ref().unwrap().error_code,
6132            Error::to_wire(Error::ExcessiveLoad)
6133        );
6134    }
6135
6136    #[test]
6137    /// Tests that Error::TransportError contains a transport error.
6138    fn transport_error() {
6139        let mut s = Session::new().unwrap();
6140        s.handshake().unwrap();
6141
6142        let req = vec![
6143            Header::new(b":method", b"GET"),
6144            Header::new(b":scheme", b"https"),
6145            Header::new(b":authority", b"quic.tech"),
6146            Header::new(b":path", b"/test"),
6147            Header::new(b"user-agent", b"quiche-test"),
6148        ];
6149
6150        // We need to open all streams in the same flight, so we can't use the
6151        // Session::send_request() method because it also calls advance(),
6152        // otherwise the server would send a MAX_STREAMS frame and the client
6153        // wouldn't hit the streams limit.
6154        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6155        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(4));
6156        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(8));
6157        assert_eq!(
6158            s.client.send_request(&mut s.pipe.client, &req, true),
6159            Ok(12)
6160        );
6161        assert_eq!(
6162            s.client.send_request(&mut s.pipe.client, &req, true),
6163            Ok(16)
6164        );
6165
6166        assert_eq!(
6167            s.client.send_request(&mut s.pipe.client, &req, true),
6168            Err(Error::TransportError(crate::Error::StreamLimit))
6169        );
6170    }
6171
6172    #[test]
6173    /// Tests that sending DATA before HEADERS causes an error.
6174    fn data_before_headers() {
6175        let mut s = Session::new().unwrap();
6176        s.handshake().unwrap();
6177
6178        let mut d = [42; 128];
6179        let mut b = octets::OctetsMut::with_slice(&mut d);
6180
6181        let frame_type = b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
6182        s.pipe.client.stream_send(0, frame_type, false).unwrap();
6183
6184        let frame_len = b.put_varint(5).unwrap();
6185        s.pipe.client.stream_send(0, frame_len, false).unwrap();
6186
6187        s.pipe.client.stream_send(0, b"hello", false).unwrap();
6188
6189        s.advance().ok();
6190
6191        assert_eq!(
6192            s.server.poll(&mut s.pipe.server),
6193            Err(Error::FrameUnexpected)
6194        );
6195    }
6196
6197    #[test]
6198    /// Tests that calling poll() after an error occurred does nothing.
6199    fn poll_after_error() {
6200        let mut s = Session::new().unwrap();
6201        s.handshake().unwrap();
6202
6203        let mut d = [42; 128];
6204        let mut b = octets::OctetsMut::with_slice(&mut d);
6205
6206        let frame_type = b.put_varint(148_764_065_110_560_899).unwrap();
6207        s.pipe.client.stream_send(0, frame_type, false).unwrap();
6208
6209        let frame_len = b.put_varint(1 << 24).unwrap();
6210        s.pipe.client.stream_send(0, frame_len, false).unwrap();
6211
6212        s.pipe.client.stream_send(0, &d, false).unwrap();
6213
6214        s.advance().ok();
6215
6216        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::ExcessiveLoad));
6217
6218        // Try to call poll() again after an error occurred.
6219        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
6220    }
6221
6222    #[test]
6223    /// Tests that we limit sending HEADERS based on the stream capacity.
6224    fn headers_blocked() {
6225        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6226        config
6227            .load_cert_chain_from_pem_file("examples/cert.crt")
6228            .unwrap();
6229        config
6230            .load_priv_key_from_pem_file("examples/cert.key")
6231            .unwrap();
6232        config.set_application_protos(&[b"h3"]).unwrap();
6233        config.set_initial_max_data(75);
6234        config.set_initial_max_stream_data_bidi_local(150);
6235        config.set_initial_max_stream_data_bidi_remote(150);
6236        config.set_initial_max_stream_data_uni(150);
6237        config.set_initial_max_streams_bidi(100);
6238        config.set_initial_max_streams_uni(5);
6239        config.verify_peer(false);
6240
6241        let h3_config = Config::new().unwrap();
6242
6243        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6244
6245        s.handshake().unwrap();
6246
6247        let req = vec![
6248            Header::new(b":method", b"GET"),
6249            Header::new(b":scheme", b"https"),
6250            Header::new(b":authority", b"quic.tech"),
6251            Header::new(b":path", b"/test"),
6252        ];
6253
6254        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6255
6256        assert_eq!(
6257            s.client.send_request(&mut s.pipe.client, &req, true),
6258            Err(Error::StreamBlocked)
6259        );
6260
6261        // Clear the writable stream queue.
6262        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6263        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6264        assert_eq!(s.pipe.client.stream_writable_next(), Some(10));
6265        assert_eq!(s.pipe.client.stream_writable_next(), None);
6266
6267        s.advance().ok();
6268
6269        // Once the server gives flow control credits back, we can send the
6270        // request.
6271        assert_eq!(s.pipe.client.stream_writable_next(), Some(4));
6272        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(4));
6273
6274        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
6275        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
6276        assert_eq!(s.pipe.server.data_blocked_recv_count, 1);
6277        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 0);
6278
6279        assert_eq!(s.pipe.client.data_blocked_sent_count, 1);
6280        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 0);
6281        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
6282        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
6283    }
6284
6285    #[test]
6286    /// Ensure StreamBlocked when connection flow control prevents headers.
6287    fn headers_blocked_on_conn() {
6288        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6289        config
6290            .load_cert_chain_from_pem_file("examples/cert.crt")
6291            .unwrap();
6292        config
6293            .load_priv_key_from_pem_file("examples/cert.key")
6294            .unwrap();
6295        config.set_application_protos(&[b"h3"]).unwrap();
6296        config.set_initial_max_data(75);
6297        config.set_initial_max_stream_data_bidi_local(150);
6298        config.set_initial_max_stream_data_bidi_remote(150);
6299        config.set_initial_max_stream_data_uni(150);
6300        config.set_initial_max_streams_bidi(100);
6301        config.set_initial_max_streams_uni(5);
6302        config.verify_peer(false);
6303
6304        let h3_config = Config::new().unwrap();
6305
6306        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6307
6308        s.handshake().unwrap();
6309
6310        // After the HTTP handshake, some bytes of connection flow control have
6311        // been consumed. Fill the connection with more grease data on the control
6312        // stream.
6313        let d = [42; 28];
6314        assert_eq!(s.pipe.client.stream_send(2, &d, false), Ok(23));
6315
6316        let req = vec![
6317            Header::new(b":method", b"GET"),
6318            Header::new(b":scheme", b"https"),
6319            Header::new(b":authority", b"quic.tech"),
6320            Header::new(b":path", b"/test"),
6321        ];
6322
6323        // There is 0 connection-level flow control, so sending a request is
6324        // blocked.
6325        assert_eq!(
6326            s.client.send_request(&mut s.pipe.client, &req, true),
6327            Err(Error::StreamBlocked)
6328        );
6329        assert_eq!(s.pipe.client.stream_writable_next(), None);
6330
6331        // Emit the control stream data and drain it at the server via poll() to
6332        // consumes it via poll() and gives back flow control.
6333        s.advance().ok();
6334        assert_eq!(s.poll_server(), Err(Error::Done));
6335        s.advance().ok();
6336
6337        // Now we can send the request.
6338        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6339        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6340        assert_eq!(s.client.send_request(&mut s.pipe.client, &req, true), Ok(0));
6341
6342        assert_eq!(s.pipe.server.data_blocked_sent_count, 0);
6343        assert_eq!(s.pipe.server.stream_data_blocked_sent_count, 0);
6344        assert_eq!(s.pipe.server.data_blocked_recv_count, 1);
6345        assert_eq!(s.pipe.server.stream_data_blocked_recv_count, 0);
6346
6347        assert_eq!(s.pipe.client.data_blocked_sent_count, 1);
6348        assert_eq!(s.pipe.client.stream_data_blocked_sent_count, 0);
6349        assert_eq!(s.pipe.client.data_blocked_recv_count, 0);
6350        assert_eq!(s.pipe.client.stream_data_blocked_recv_count, 0);
6351    }
6352
6353    #[test]
6354    /// Ensure that the connection does not consume a stream ID when
6355    /// send_request fails with StreamBlocked due to hitting the
6356    /// MAX_DATA flow control limit when attempting to send headers.
6357    /// The headers are sent successfully after a MAX_DATA update.
6358    fn headers_blocked_by_max_data_success_on_retry() {
6359        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6360        config
6361            .load_cert_chain_from_pem_file("examples/cert.crt")
6362            .unwrap();
6363        config
6364            .load_priv_key_from_pem_file("examples/cert.key")
6365            .unwrap();
6366        config.set_application_protos(&[b"h3"]).unwrap();
6367        config.set_initial_max_data(70);
6368        config.set_initial_max_stream_data_bidi_local(150);
6369        config.set_initial_max_stream_data_bidi_remote(150);
6370        config.set_initial_max_stream_data_uni(150);
6371        config.set_initial_max_streams_bidi(100);
6372        config.set_initial_max_streams_uni(5);
6373        config.verify_peer(false);
6374
6375        let h3_config = Config::new().unwrap();
6376
6377        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6378
6379        s.handshake().unwrap();
6380
6381        let req = vec![
6382            Header::new(b":method", b"GET"),
6383            Header::new(b":scheme", b"https"),
6384            Header::new(b":authority", b"quic.tech"),
6385            Header::new(b":path", b"/test/with/long/url"),
6386        ];
6387
6388        // After the HTTP handshake, some bytes of connection flow
6389        // control have been consumed.  The serialized request does
6390        // not fit in the remaining connection-level flow control
6391        // limit.  send_request fails without creating a stream.
6392        assert_eq!(
6393            s.client.send_request(&mut s.pipe.client, &req, true),
6394            Err(Error::StreamBlocked)
6395        );
6396
6397        // Verify that stream 0 does not exist in the H3 stream map after the
6398        // failed attempt.
6399        assert!(!s.client.streams.contains_key(&0));
6400
6401        // Emit the control stream data and drain it at the server to give back
6402        // flow control.
6403        s.advance().ok();
6404        assert_eq!(s.poll_server(), Err(Error::Done));
6405        s.advance().ok();
6406
6407        // Now we can send the request. The stream ID should be 0 (not 4),
6408        // confirming that the blocked attempt did not consume the stream ID.
6409        let stream_id = s.client.send_request(&mut s.pipe.client, &req, true);
6410        assert_eq!(stream_id, Ok(0));
6411        assert!(s.client.streams.contains_key(&0));
6412        assert!(!s.client.streams.contains_key(&4));
6413
6414        // Subsequent request should use stream ID 4.
6415        let stream_id2 = s.client.send_request(&mut s.pipe.client, &req, true);
6416        assert_eq!(stream_id2, Ok(4));
6417        assert!(s.client.streams.contains_key(&0));
6418        assert!(s.client.streams.contains_key(&4));
6419
6420        s.advance().ok();
6421    }
6422
6423    #[test]
6424    /// Ensure STREAM_DATA_BLOCKED is not emitted multiple times with the same
6425    /// offset when trying to send large bodies.
6426    fn send_body_truncation_stream_blocked() {
6427        use crate::test_utils::decode_pkt;
6428
6429        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6430        config
6431            .load_cert_chain_from_pem_file("examples/cert.crt")
6432            .unwrap();
6433        config
6434            .load_priv_key_from_pem_file("examples/cert.key")
6435            .unwrap();
6436        config.set_application_protos(&[b"h3"]).unwrap();
6437        config.set_initial_max_data(10000); // large connection-level flow control
6438        config.set_initial_max_stream_data_bidi_local(80);
6439        config.set_initial_max_stream_data_bidi_remote(80);
6440        config.set_initial_max_stream_data_uni(150);
6441        config.set_initial_max_streams_bidi(100);
6442        config.set_initial_max_streams_uni(5);
6443        config.verify_peer(false);
6444
6445        let h3_config = Config::new().unwrap();
6446
6447        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6448
6449        s.handshake().unwrap();
6450
6451        let (stream, req) = s.send_request(true).unwrap();
6452
6453        let ev_headers = Event::Headers {
6454            list: req,
6455            more_frames: false,
6456        };
6457
6458        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6459        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6460
6461        let _ = s.send_response(stream, false).unwrap();
6462
6463        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6464
6465        // The body must be larger than the stream window would allow
6466        let d = [42; 500];
6467        let mut off = 0;
6468
6469        let sent = s
6470            .server
6471            .send_body(&mut s.pipe.server, stream, &d, true)
6472            .unwrap();
6473        assert_eq!(sent, 25);
6474        off += sent;
6475
6476        // send_body wrote as much as it could (sent < size of buff).
6477        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6478        assert_eq!(
6479            s.server
6480                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6481            Err(Error::Done)
6482        );
6483        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6484
6485        // Now read raw frames to see what the QUIC layer did
6486        let mut buf = [0; 65535];
6487        let (len, _) = s.pipe.server.send(&mut buf).unwrap();
6488
6489        let frames = decode_pkt(&mut s.pipe.client, &mut buf[..len]).unwrap();
6490
6491        let mut iter = frames.iter();
6492
6493        assert_eq!(
6494            iter.next(),
6495            Some(&crate::frame::Frame::StreamDataBlocked {
6496                stream_id: 0,
6497                limit: 80,
6498            })
6499        );
6500
6501        // At the server, after sending the STREAM_DATA_BLOCKED frame, we clear
6502        // the mark.
6503        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6504
6505        // Don't read any data from the client, so stream flow control is never
6506        // given back in the form of changing the stream's max offset.
6507        // Subsequent body send operations will still fail but no more
6508        // STREAM_DATA_BLOCKED frames should be submitted since the limit didn't
6509        // change. No frames means no packet to send.
6510        assert_eq!(
6511            s.server
6512                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6513            Err(Error::Done)
6514        );
6515        assert_eq!(s.pipe.server.streams.blocked().len(), 0);
6516        assert_eq!(s.pipe.server.send(&mut buf), Err(crate::Error::Done));
6517
6518        // Now update the client's max offset manually.
6519        let frames = [crate::frame::Frame::MaxStreamData {
6520            stream_id: 0,
6521            max: 100,
6522        }];
6523
6524        let pkt_type = crate::packet::Type::Short;
6525        assert_eq!(
6526            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
6527            Ok(39),
6528        );
6529
6530        let sent = s
6531            .server
6532            .send_body(&mut s.pipe.server, stream, &d[off..], true)
6533            .unwrap();
6534        assert_eq!(sent, 18);
6535
6536        // Same thing here...
6537        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6538        assert_eq!(
6539            s.server
6540                .send_body(&mut s.pipe.server, stream, &d[off..], true),
6541            Err(Error::Done)
6542        );
6543        assert_eq!(s.pipe.server.streams.blocked().len(), 1);
6544
6545        let (len, _) = s.pipe.server.send(&mut buf).unwrap();
6546
6547        let frames = decode_pkt(&mut s.pipe.client, &mut buf[..len]).unwrap();
6548
6549        let mut iter = frames.iter();
6550
6551        assert_eq!(
6552            iter.next(),
6553            Some(&crate::frame::Frame::StreamDataBlocked {
6554                stream_id: 0,
6555                limit: 100,
6556            })
6557        );
6558    }
6559
6560    #[test]
6561    /// Ensure stream doesn't hang due to small cwnd.
6562    fn send_body_stream_blocked_by_small_cwnd() {
6563        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6564        config
6565            .load_cert_chain_from_pem_file("examples/cert.crt")
6566            .unwrap();
6567        config
6568            .load_priv_key_from_pem_file("examples/cert.key")
6569            .unwrap();
6570        config.set_application_protos(&[b"h3"]).unwrap();
6571        config.set_initial_max_data(100000); // large connection-level flow control
6572        config.set_initial_max_stream_data_bidi_local(100000);
6573        config.set_initial_max_stream_data_bidi_remote(50000);
6574        config.set_initial_max_stream_data_uni(150);
6575        config.set_initial_max_streams_bidi(100);
6576        config.set_initial_max_streams_uni(5);
6577        config.verify_peer(false);
6578
6579        let h3_config = Config::new().unwrap();
6580
6581        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6582
6583        s.handshake().unwrap();
6584
6585        let (stream, req) = s.send_request(true).unwrap();
6586
6587        let ev_headers = Event::Headers {
6588            list: req,
6589            more_frames: false,
6590        };
6591
6592        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6593        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6594
6595        let _ = s.send_response(stream, false).unwrap();
6596
6597        // Clear the writable stream queue.
6598        assert_eq!(s.pipe.server.stream_writable_next(), Some(3));
6599        assert_eq!(s.pipe.server.stream_writable_next(), Some(7));
6600        assert_eq!(s.pipe.server.stream_writable_next(), Some(11));
6601        assert_eq!(s.pipe.server.stream_writable_next(), Some(stream));
6602        assert_eq!(s.pipe.server.stream_writable_next(), None);
6603
6604        // The body must be larger than the cwnd would allow.
6605        let send_buf = [42; 80000];
6606
6607        let sent = s
6608            .server
6609            .send_body(&mut s.pipe.server, stream, &send_buf, true)
6610            .unwrap();
6611
6612        // send_body wrote as much as it could (sent < size of buff).
6613        assert_eq!(sent, 11995);
6614
6615        s.advance().ok();
6616
6617        // Client reads received headers and body.
6618        let mut recv_buf = [42; 80000];
6619        assert!(s.poll_client().is_ok());
6620        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6621        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(11995));
6622
6623        s.advance().ok();
6624
6625        // Server send cap is smaller than remaining body buffer.
6626        assert!(s.pipe.server.tx_cap < send_buf.len() - sent);
6627
6628        // Once the server cwnd opens up, we can send more body.
6629        assert_eq!(s.pipe.server.stream_writable_next(), Some(0));
6630    }
6631
6632    #[test]
6633    /// Ensure stream doesn't hang due to small cwnd.
6634    fn send_body_stream_blocked_zero_length() {
6635        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6636        config
6637            .load_cert_chain_from_pem_file("examples/cert.crt")
6638            .unwrap();
6639        config
6640            .load_priv_key_from_pem_file("examples/cert.key")
6641            .unwrap();
6642        config.set_application_protos(&[b"h3"]).unwrap();
6643        config.set_initial_max_data(100000); // large connection-level flow control
6644        config.set_initial_max_stream_data_bidi_local(100000);
6645        config.set_initial_max_stream_data_bidi_remote(50000);
6646        config.set_initial_max_stream_data_uni(150);
6647        config.set_initial_max_streams_bidi(100);
6648        config.set_initial_max_streams_uni(5);
6649        config.verify_peer(false);
6650
6651        let h3_config = Config::new().unwrap();
6652
6653        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6654
6655        s.handshake().unwrap();
6656
6657        let (stream, req) = s.send_request(true).unwrap();
6658
6659        let ev_headers = Event::Headers {
6660            list: req,
6661            more_frames: false,
6662        };
6663
6664        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6665        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6666
6667        let _ = s.send_response(stream, false).unwrap();
6668
6669        // Clear the writable stream queue.
6670        assert_eq!(s.pipe.server.stream_writable_next(), Some(3));
6671        assert_eq!(s.pipe.server.stream_writable_next(), Some(7));
6672        assert_eq!(s.pipe.server.stream_writable_next(), Some(11));
6673        assert_eq!(s.pipe.server.stream_writable_next(), Some(stream));
6674        assert_eq!(s.pipe.server.stream_writable_next(), None);
6675
6676        // The body is large enough to fill the cwnd, except for enough bytes
6677        // for another DATA frame header (but no payload).
6678        let send_buf = [42; 11994];
6679
6680        let sent = s
6681            .server
6682            .send_body(&mut s.pipe.server, stream, &send_buf, false)
6683            .unwrap();
6684
6685        assert_eq!(sent, 11994);
6686
6687        // There is only enough capacity left for the DATA frame header, but
6688        // no payload.
6689        assert_eq!(s.pipe.server.stream_capacity(stream).unwrap(), 3);
6690        assert_eq!(
6691            s.server
6692                .send_body(&mut s.pipe.server, stream, &send_buf, false),
6693            Err(Error::Done)
6694        );
6695
6696        s.advance().ok();
6697
6698        // Client reads received headers and body.
6699        let mut recv_buf = [42; 80000];
6700        assert!(s.poll_client().is_ok());
6701        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6702        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(11994));
6703
6704        s.advance().ok();
6705
6706        // Once the server cwnd opens up, we can send more body.
6707        assert_eq!(s.pipe.server.stream_writable_next(), Some(0));
6708    }
6709
6710    #[test]
6711    /// Test handling of 0-length DATA writes with and without fin.
6712    fn zero_length_data() {
6713        let mut s = Session::new().unwrap();
6714        s.handshake().unwrap();
6715
6716        let (stream, req) = s.send_request(false).unwrap();
6717
6718        assert_eq!(
6719            s.client.send_body(&mut s.pipe.client, 0, b"", false),
6720            Err(Error::Done)
6721        );
6722        assert_eq!(s.client.send_body(&mut s.pipe.client, 0, b"", true), Ok(0));
6723
6724        s.advance().ok();
6725
6726        let mut recv_buf = vec![0; 100];
6727
6728        let ev_headers = Event::Headers {
6729            list: req,
6730            more_frames: true,
6731        };
6732
6733        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6734
6735        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
6736        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Err(Error::Done));
6737
6738        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6739        assert_eq!(s.poll_server(), Err(Error::Done));
6740
6741        let resp = s.send_response(stream, false).unwrap();
6742
6743        assert_eq!(
6744            s.server.send_body(&mut s.pipe.server, 0, b"", false),
6745            Err(Error::Done)
6746        );
6747        assert_eq!(s.server.send_body(&mut s.pipe.server, 0, b"", true), Ok(0));
6748
6749        s.advance().ok();
6750
6751        let ev_headers = Event::Headers {
6752            list: resp,
6753            more_frames: true,
6754        };
6755
6756        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
6757
6758        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
6759        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Err(Error::Done));
6760
6761        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
6762        assert_eq!(s.poll_client(), Err(Error::Done));
6763    }
6764
6765    #[test]
6766    /// Tests that blocked 0-length DATA writes are reported correctly.
6767    fn zero_length_data_blocked() {
6768        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6769        config
6770            .load_cert_chain_from_pem_file("examples/cert.crt")
6771            .unwrap();
6772        config
6773            .load_priv_key_from_pem_file("examples/cert.key")
6774            .unwrap();
6775        config.set_application_protos(&[b"h3"]).unwrap();
6776        config.set_initial_max_data(74);
6777        config.set_initial_max_stream_data_bidi_local(150);
6778        config.set_initial_max_stream_data_bidi_remote(150);
6779        config.set_initial_max_stream_data_uni(150);
6780        config.set_initial_max_streams_bidi(100);
6781        config.set_initial_max_streams_uni(5);
6782        config.verify_peer(false);
6783
6784        let h3_config = Config::new().unwrap();
6785
6786        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6787
6788        s.handshake().unwrap();
6789
6790        let req = vec![
6791            Header::new(b":method", b"GET"),
6792            Header::new(b":scheme", b"https"),
6793            Header::new(b":authority", b"quic.tech"),
6794            Header::new(b":path", b"/test"),
6795        ];
6796
6797        assert_eq!(
6798            s.client.send_request(&mut s.pipe.client, &req, false),
6799            Ok(0)
6800        );
6801
6802        assert_eq!(
6803            s.client.send_body(&mut s.pipe.client, 0, b"", true),
6804            Err(Error::Done)
6805        );
6806
6807        // Clear the writable stream queue.
6808        assert_eq!(s.pipe.client.stream_writable_next(), Some(2));
6809        assert_eq!(s.pipe.client.stream_writable_next(), Some(6));
6810        assert_eq!(s.pipe.client.stream_writable_next(), Some(10));
6811        assert_eq!(s.pipe.client.stream_writable_next(), None);
6812
6813        s.advance().ok();
6814
6815        // Once the server gives flow control credits back, we can send the body.
6816        assert_eq!(s.pipe.client.stream_writable_next(), Some(0));
6817        assert_eq!(s.client.send_body(&mut s.pipe.client, 0, b"", true), Ok(0));
6818    }
6819
6820    #[test]
6821    /// Tests that receiving an empty SETTINGS frame is handled and reported.
6822    fn empty_settings() {
6823        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6824        config
6825            .load_cert_chain_from_pem_file("examples/cert.crt")
6826            .unwrap();
6827        config
6828            .load_priv_key_from_pem_file("examples/cert.key")
6829            .unwrap();
6830        config.set_application_protos(&[b"h3"]).unwrap();
6831        config.set_initial_max_data(1500);
6832        config.set_initial_max_stream_data_bidi_local(150);
6833        config.set_initial_max_stream_data_bidi_remote(150);
6834        config.set_initial_max_stream_data_uni(150);
6835        config.set_initial_max_streams_bidi(5);
6836        config.set_initial_max_streams_uni(5);
6837        config.verify_peer(false);
6838        config.set_ack_delay_exponent(8);
6839        config.grease(false);
6840
6841        let h3_config = Config::new().unwrap();
6842        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6843
6844        s.handshake().unwrap();
6845
6846        assert!(s.client.peer_settings_raw().is_some());
6847        assert!(s.server.peer_settings_raw().is_some());
6848    }
6849
6850    #[test]
6851    /// Tests that receiving a H3_DATAGRAM setting is ok.
6852    fn dgram_setting() {
6853        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6854        config
6855            .load_cert_chain_from_pem_file("examples/cert.crt")
6856            .unwrap();
6857        config
6858            .load_priv_key_from_pem_file("examples/cert.key")
6859            .unwrap();
6860        config.set_application_protos(&[b"h3"]).unwrap();
6861        config.set_initial_max_data(70);
6862        config.set_initial_max_stream_data_bidi_local(150);
6863        config.set_initial_max_stream_data_bidi_remote(150);
6864        config.set_initial_max_stream_data_uni(150);
6865        config.set_initial_max_streams_bidi(100);
6866        config.set_initial_max_streams_uni(5);
6867        config.enable_dgram(true, 1000, 1000);
6868        config.verify_peer(false);
6869
6870        let h3_config = Config::new().unwrap();
6871
6872        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6873        assert_eq!(s.pipe.handshake(), Ok(()));
6874
6875        s.client.send_settings(&mut s.pipe.client).unwrap();
6876        assert_eq!(s.pipe.advance(), Ok(()));
6877
6878        // Before processing SETTINGS (via poll), HTTP/3 DATAGRAMS are not
6879        // enabled.
6880        assert!(!s.server.dgram_enabled_by_peer(&s.pipe.server));
6881
6882        // When everything is ok, poll returns Done and DATAGRAM is enabled.
6883        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
6884        assert!(s.server.dgram_enabled_by_peer(&s.pipe.server));
6885
6886        // Now detect things on the client
6887        s.server.send_settings(&mut s.pipe.server).unwrap();
6888        assert_eq!(s.pipe.advance(), Ok(()));
6889        assert!(!s.client.dgram_enabled_by_peer(&s.pipe.client));
6890        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::Done));
6891        assert!(s.client.dgram_enabled_by_peer(&s.pipe.client));
6892    }
6893
6894    #[test]
6895    /// Tests that receiving a H3_DATAGRAM setting when no TP is set generates
6896    /// an error.
6897    fn dgram_setting_no_tp() {
6898        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6899        config
6900            .load_cert_chain_from_pem_file("examples/cert.crt")
6901            .unwrap();
6902        config
6903            .load_priv_key_from_pem_file("examples/cert.key")
6904            .unwrap();
6905        config.set_application_protos(&[b"h3"]).unwrap();
6906        config.set_initial_max_data(70);
6907        config.set_initial_max_stream_data_bidi_local(150);
6908        config.set_initial_max_stream_data_bidi_remote(150);
6909        config.set_initial_max_stream_data_uni(150);
6910        config.set_initial_max_streams_bidi(100);
6911        config.set_initial_max_streams_uni(5);
6912        config.verify_peer(false);
6913
6914        let h3_config = Config::new().unwrap();
6915
6916        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6917        assert_eq!(s.pipe.handshake(), Ok(()));
6918
6919        s.client.control_stream_id = Some(
6920            s.client
6921                .open_uni_stream(
6922                    &mut s.pipe.client,
6923                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6924                )
6925                .unwrap(),
6926        );
6927
6928        let settings = frame::Frame::Settings {
6929            max_field_section_size: None,
6930            qpack_max_table_capacity: None,
6931            qpack_blocked_streams: None,
6932            connect_protocol_enabled: None,
6933            h3_datagram: Some(1),
6934            grease: None,
6935            additional_settings: Default::default(),
6936            raw: Default::default(),
6937        };
6938
6939        s.send_frame_client(settings, s.client.control_stream_id.unwrap(), false)
6940            .unwrap();
6941
6942        assert_eq!(s.pipe.advance(), Ok(()));
6943
6944        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::SettingsError));
6945    }
6946
6947    #[test]
6948    /// Tests that receiving SETTINGS with prohibited values generates an error.
6949    fn settings_h2_prohibited() {
6950        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6951        config
6952            .load_cert_chain_from_pem_file("examples/cert.crt")
6953            .unwrap();
6954        config
6955            .load_priv_key_from_pem_file("examples/cert.key")
6956            .unwrap();
6957        config.set_application_protos(&[b"h3"]).unwrap();
6958        config.set_initial_max_data(70);
6959        config.set_initial_max_stream_data_bidi_local(150);
6960        config.set_initial_max_stream_data_bidi_remote(150);
6961        config.set_initial_max_stream_data_uni(150);
6962        config.set_initial_max_streams_bidi(100);
6963        config.set_initial_max_streams_uni(5);
6964        config.verify_peer(false);
6965
6966        let h3_config = Config::new().unwrap();
6967
6968        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6969        assert_eq!(s.pipe.handshake(), Ok(()));
6970
6971        s.client.control_stream_id = Some(
6972            s.client
6973                .open_uni_stream(
6974                    &mut s.pipe.client,
6975                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6976                )
6977                .unwrap(),
6978        );
6979
6980        s.server.control_stream_id = Some(
6981            s.server
6982                .open_uni_stream(
6983                    &mut s.pipe.server,
6984                    stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6985                )
6986                .unwrap(),
6987        );
6988
6989        let frame_payload_len = 2u64;
6990        let settings = [
6991            frame::SETTINGS_FRAME_TYPE_ID as u8,
6992            frame_payload_len as u8,
6993            0x2, // 0x2 is a reserved setting type
6994            1,
6995        ];
6996
6997        s.send_arbitrary_stream_data_client(
6998            &settings,
6999            s.client.control_stream_id.unwrap(),
7000            false,
7001        )
7002        .unwrap();
7003
7004        s.send_arbitrary_stream_data_server(
7005            &settings,
7006            s.server.control_stream_id.unwrap(),
7007            false,
7008        )
7009        .unwrap();
7010
7011        assert_eq!(s.pipe.advance(), Ok(()));
7012
7013        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::SettingsError));
7014
7015        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::SettingsError));
7016    }
7017
7018    #[test]
7019    /// Tests that setting SETTINGS with prohibited values generates an error.
7020    fn set_prohibited_additional_settings() {
7021        let mut h3_config = Config::new().unwrap();
7022        assert_eq!(
7023            h3_config.set_additional_settings(vec![(
7024                frame::SETTINGS_QPACK_MAX_TABLE_CAPACITY,
7025                43
7026            )]),
7027            Err(Error::SettingsError)
7028        );
7029        assert_eq!(
7030            h3_config.set_additional_settings(vec![(
7031                frame::SETTINGS_MAX_FIELD_SECTION_SIZE,
7032                43
7033            )]),
7034            Err(Error::SettingsError)
7035        );
7036        assert_eq!(
7037            h3_config.set_additional_settings(vec![(
7038                frame::SETTINGS_QPACK_BLOCKED_STREAMS,
7039                43
7040            )]),
7041            Err(Error::SettingsError)
7042        );
7043        assert_eq!(
7044            h3_config.set_additional_settings(vec![(
7045                frame::SETTINGS_ENABLE_CONNECT_PROTOCOL,
7046                43
7047            )]),
7048            Err(Error::SettingsError)
7049        );
7050        assert_eq!(
7051            h3_config
7052                .set_additional_settings(vec![(frame::SETTINGS_H3_DATAGRAM, 43)]),
7053            Err(Error::SettingsError)
7054        );
7055    }
7056
7057    #[test]
7058    /// Tests that a client rejects a SETTINGS frame received on a request
7059    /// stream.
7060    fn settings_on_request_stream_client() {
7061        let mut s = Session::new().unwrap();
7062        s.handshake().unwrap();
7063
7064        let (stream, _req) = s.send_request(true).unwrap();
7065
7066        let settings = frame::Frame::Settings {
7067            max_field_section_size: None,
7068            qpack_max_table_capacity: None,
7069            qpack_blocked_streams: None,
7070            connect_protocol_enabled: None,
7071            h3_datagram: None,
7072            grease: None,
7073            additional_settings: Default::default(),
7074            raw: Default::default(),
7075        };
7076
7077        s.send_frame_server(settings, stream, false).unwrap();
7078
7079        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7080        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7081        assert_eq!(
7082            s.pipe.client.local_error(),
7083            Some(&crate::ConnectionError {
7084                is_app: true,
7085                error_code: WireErrorCode::FrameUnexpected as u64,
7086                reason: format!(
7087                    "Unexpected frame type {}",
7088                    frame::SETTINGS_FRAME_TYPE_ID
7089                )
7090                .into_bytes(),
7091            })
7092        );
7093    }
7094
7095    #[test]
7096    /// Tests that a client rejects a CANCEL_PUSH frame received on a request
7097    /// stream.
7098    fn cancel_push_on_request_stream_client() {
7099        let mut s = Session::new().unwrap();
7100        s.handshake().unwrap();
7101
7102        let (stream, _req) = s.send_request(true).unwrap();
7103        let cancel_push = frame::Frame::CancelPush { push_id: 0 };
7104        s.send_frame_server(cancel_push, stream, false).unwrap();
7105
7106        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7107        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7108        assert_eq!(
7109            s.pipe.client.local_error(),
7110            Some(&crate::ConnectionError {
7111                is_app: true,
7112                error_code: WireErrorCode::FrameUnexpected as u64,
7113                reason: format!(
7114                    "Unexpected frame type {}",
7115                    frame::CANCEL_PUSH_FRAME_TYPE_ID
7116                )
7117                .into_bytes(),
7118            })
7119        );
7120    }
7121
7122    #[test]
7123    /// Tests that a client rejects a GOAWAY frame received on a request
7124    /// stream.
7125    fn goaway_on_request_stream_client() {
7126        let mut s = Session::new().unwrap();
7127        s.handshake().unwrap();
7128
7129        let (stream, _req) = s.send_request(true).unwrap();
7130        let goaway = frame::Frame::GoAway { id: 0 };
7131
7132        s.send_frame_server(goaway, stream, false).unwrap();
7133
7134        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7135        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7136        assert_eq!(
7137            s.pipe.client.local_error(),
7138            Some(&crate::ConnectionError {
7139                is_app: true,
7140                error_code: WireErrorCode::FrameUnexpected as u64,
7141                reason: format!(
7142                    "Unexpected frame type {}",
7143                    frame::GOAWAY_FRAME_TYPE_ID
7144                )
7145                .into_bytes(),
7146            })
7147        );
7148    }
7149
7150    #[test]
7151    /// Tests that a client rejects a MAX_PUSH_ID frame received on a request
7152    /// stream.
7153    fn max_push_id_on_request_stream_client() {
7154        let mut s = Session::new().unwrap();
7155        s.handshake().unwrap();
7156
7157        let (stream, _req) = s.send_request(true).unwrap();
7158        let max_push_id = frame::Frame::MaxPushId { push_id: 0 };
7159
7160        s.send_frame_server(max_push_id, stream, false).unwrap();
7161
7162        // The client MUST treat this as H3_FRAME_UNEXPECTED.
7163        assert_eq!(s.poll_client(), Err(Error::FrameUnexpected));
7164        assert_eq!(
7165            s.pipe.client.local_error(),
7166            Some(&crate::ConnectionError {
7167                is_app: true,
7168                error_code: WireErrorCode::FrameUnexpected as u64,
7169                reason: format!(
7170                    "Unexpected frame type {}",
7171                    frame::MAX_PUSH_FRAME_TYPE_ID
7172                )
7173                .into_bytes(),
7174            })
7175        );
7176    }
7177
7178    #[test]
7179    /// Tests additional settings are actually exchanged by the peers.
7180    fn set_additional_settings() {
7181        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7182        config
7183            .load_cert_chain_from_pem_file("examples/cert.crt")
7184            .unwrap();
7185        config
7186            .load_priv_key_from_pem_file("examples/cert.key")
7187            .unwrap();
7188        config.set_application_protos(&[b"h3"]).unwrap();
7189        config.set_initial_max_data(70);
7190        config.set_initial_max_stream_data_bidi_local(150);
7191        config.set_initial_max_stream_data_bidi_remote(150);
7192        config.set_initial_max_stream_data_uni(150);
7193        config.set_initial_max_streams_bidi(100);
7194        config.set_initial_max_streams_uni(5);
7195        config.verify_peer(false);
7196        config.grease(false);
7197
7198        let mut h3_config = Config::new().unwrap();
7199        h3_config
7200            .set_additional_settings(vec![(42, 43), (44, 45)])
7201            .unwrap();
7202
7203        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7204        assert_eq!(s.pipe.handshake(), Ok(()));
7205
7206        assert_eq!(s.pipe.advance(), Ok(()));
7207
7208        s.client.send_settings(&mut s.pipe.client).unwrap();
7209        assert_eq!(s.pipe.advance(), Ok(()));
7210        assert_eq!(s.server.poll(&mut s.pipe.server), Err(Error::Done));
7211
7212        s.server.send_settings(&mut s.pipe.server).unwrap();
7213        assert_eq!(s.pipe.advance(), Ok(()));
7214        assert_eq!(s.client.poll(&mut s.pipe.client), Err(Error::Done));
7215
7216        assert_eq!(
7217            s.server.peer_settings_raw(),
7218            Some(&[(6, 32_768), (42, 43), (44, 45)][..])
7219        );
7220        assert_eq!(
7221            s.client.peer_settings_raw(),
7222            Some(&[(6, 32_768), (42, 43), (44, 45)][..])
7223        );
7224    }
7225
7226    #[test]
7227    /// Send a single DATAGRAM.
7228    fn single_dgram() {
7229        let mut buf = [0; 65535];
7230        let mut s = Session::new().unwrap();
7231        s.handshake().unwrap();
7232
7233        // We'll send default data of 10 bytes on flow ID 0.
7234        let result = (11, 0, 1);
7235
7236        s.send_dgram_client(0).unwrap();
7237
7238        assert_eq!(s.poll_server(), Err(Error::Done));
7239        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7240
7241        s.send_dgram_server(0).unwrap();
7242        assert_eq!(s.poll_client(), Err(Error::Done));
7243        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7244    }
7245
7246    #[test]
7247    /// Send multiple DATAGRAMs.
7248    fn multiple_dgram() {
7249        let mut buf = [0; 65535];
7250        let mut s = Session::new().unwrap();
7251        s.handshake().unwrap();
7252
7253        // We'll send default data of 10 bytes on flow ID 0.
7254        let result = (11, 0, 1);
7255
7256        s.send_dgram_client(0).unwrap();
7257        s.send_dgram_client(0).unwrap();
7258        s.send_dgram_client(0).unwrap();
7259
7260        assert_eq!(s.poll_server(), Err(Error::Done));
7261        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7262        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7263        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7264        assert_eq!(s.recv_dgram_server(&mut buf), Err(Error::Done));
7265
7266        s.send_dgram_server(0).unwrap();
7267        s.send_dgram_server(0).unwrap();
7268        s.send_dgram_server(0).unwrap();
7269
7270        assert_eq!(s.poll_client(), Err(Error::Done));
7271        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7272        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7273        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7274        assert_eq!(s.recv_dgram_client(&mut buf), Err(Error::Done));
7275    }
7276
7277    #[test]
7278    /// Send more DATAGRAMs than the send queue allows.
7279    fn multiple_dgram_overflow() {
7280        let mut buf = [0; 65535];
7281        let mut s = Session::new().unwrap();
7282        s.handshake().unwrap();
7283
7284        // We'll send default data of 10 bytes on flow ID 0.
7285        let result = (11, 0, 1);
7286
7287        // Five DATAGRAMs
7288        s.send_dgram_client(0).unwrap();
7289        s.send_dgram_client(0).unwrap();
7290        s.send_dgram_client(0).unwrap();
7291        s.send_dgram_client(0).unwrap();
7292        s.send_dgram_client(0).unwrap();
7293
7294        // Only 3 independent DATAGRAMs to read events will fire.
7295        assert_eq!(s.poll_server(), Err(Error::Done));
7296        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7297        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7298        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7299        assert_eq!(s.recv_dgram_server(&mut buf), Err(Error::Done));
7300    }
7301
7302    #[test]
7303    /// Send a single DATAGRAM and request.
7304    fn poll_datagram_cycling_no_read() {
7305        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7306        config
7307            .load_cert_chain_from_pem_file("examples/cert.crt")
7308            .unwrap();
7309        config
7310            .load_priv_key_from_pem_file("examples/cert.key")
7311            .unwrap();
7312        config.set_application_protos(&[b"h3"]).unwrap();
7313        config.set_initial_max_data(1500);
7314        config.set_initial_max_stream_data_bidi_local(150);
7315        config.set_initial_max_stream_data_bidi_remote(150);
7316        config.set_initial_max_stream_data_uni(150);
7317        config.set_initial_max_streams_bidi(100);
7318        config.set_initial_max_streams_uni(5);
7319        config.verify_peer(false);
7320        config.enable_dgram(true, 100, 100);
7321
7322        let h3_config = Config::new().unwrap();
7323        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7324        s.handshake().unwrap();
7325
7326        // Send request followed by DATAGRAM on client side.
7327        let (stream, req) = s.send_request(false).unwrap();
7328
7329        s.send_body_client(stream, true).unwrap();
7330
7331        let ev_headers = Event::Headers {
7332            list: req,
7333            more_frames: true,
7334        };
7335
7336        s.send_dgram_client(0).unwrap();
7337
7338        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7339        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7340
7341        assert_eq!(s.poll_server(), Err(Error::Done));
7342    }
7343
7344    #[test]
7345    /// Send a single DATAGRAM and request.
7346    fn poll_datagram_single_read() {
7347        let mut buf = [0; 65535];
7348
7349        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7350        config
7351            .load_cert_chain_from_pem_file("examples/cert.crt")
7352            .unwrap();
7353        config
7354            .load_priv_key_from_pem_file("examples/cert.key")
7355            .unwrap();
7356        config.set_application_protos(&[b"h3"]).unwrap();
7357        config.set_initial_max_data(1500);
7358        config.set_initial_max_stream_data_bidi_local(150);
7359        config.set_initial_max_stream_data_bidi_remote(150);
7360        config.set_initial_max_stream_data_uni(150);
7361        config.set_initial_max_streams_bidi(100);
7362        config.set_initial_max_streams_uni(5);
7363        config.verify_peer(false);
7364        config.enable_dgram(true, 100, 100);
7365
7366        let h3_config = Config::new().unwrap();
7367        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7368        s.handshake().unwrap();
7369
7370        // We'll send default data of 10 bytes on flow ID 0.
7371        let result = (11, 0, 1);
7372
7373        // Send request followed by DATAGRAM on client side.
7374        let (stream, req) = s.send_request(false).unwrap();
7375
7376        let body = s.send_body_client(stream, true).unwrap();
7377
7378        let mut recv_buf = vec![0; body.len()];
7379
7380        let ev_headers = Event::Headers {
7381            list: req,
7382            more_frames: true,
7383        };
7384
7385        s.send_dgram_client(0).unwrap();
7386
7387        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7388        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7389
7390        assert_eq!(s.poll_server(), Err(Error::Done));
7391
7392        assert_eq!(s.recv_dgram_server(&mut buf), Ok(result));
7393
7394        assert_eq!(s.poll_server(), Err(Error::Done));
7395
7396        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7397        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7398        assert_eq!(s.poll_server(), Err(Error::Done));
7399
7400        // Send response followed by DATAGRAM on server side
7401        let resp = s.send_response(stream, false).unwrap();
7402
7403        let body = s.send_body_server(stream, true).unwrap();
7404
7405        let mut recv_buf = vec![0; body.len()];
7406
7407        let ev_headers = Event::Headers {
7408            list: resp,
7409            more_frames: true,
7410        };
7411
7412        s.send_dgram_server(0).unwrap();
7413
7414        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7415        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
7416
7417        assert_eq!(s.poll_client(), Err(Error::Done));
7418
7419        assert_eq!(s.recv_dgram_client(&mut buf), Ok(result));
7420
7421        assert_eq!(s.poll_client(), Err(Error::Done));
7422
7423        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
7424
7425        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7426        assert_eq!(s.poll_client(), Err(Error::Done));
7427    }
7428
7429    #[test]
7430    /// Send multiple DATAGRAMs and requests.
7431    fn poll_datagram_multi_read() {
7432        let mut buf = [0; 65535];
7433
7434        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7435        config
7436            .load_cert_chain_from_pem_file("examples/cert.crt")
7437            .unwrap();
7438        config
7439            .load_priv_key_from_pem_file("examples/cert.key")
7440            .unwrap();
7441        config.set_application_protos(&[b"h3"]).unwrap();
7442        config.set_initial_max_data(1500);
7443        config.set_initial_max_stream_data_bidi_local(150);
7444        config.set_initial_max_stream_data_bidi_remote(150);
7445        config.set_initial_max_stream_data_uni(150);
7446        config.set_initial_max_streams_bidi(100);
7447        config.set_initial_max_streams_uni(5);
7448        config.verify_peer(false);
7449        config.enable_dgram(true, 100, 100);
7450
7451        let h3_config = Config::new().unwrap();
7452        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7453        s.handshake().unwrap();
7454
7455        // 10 bytes on flow ID 0 and 2.
7456        let flow_0_result = (11, 0, 1);
7457        let flow_2_result = (11, 2, 1);
7458
7459        // Send requests followed by DATAGRAMs on client side.
7460        let (stream, req) = s.send_request(false).unwrap();
7461
7462        let body = s.send_body_client(stream, true).unwrap();
7463
7464        let mut recv_buf = vec![0; body.len()];
7465
7466        let ev_headers = Event::Headers {
7467            list: req,
7468            more_frames: true,
7469        };
7470
7471        s.send_dgram_client(0).unwrap();
7472        s.send_dgram_client(0).unwrap();
7473        s.send_dgram_client(0).unwrap();
7474        s.send_dgram_client(0).unwrap();
7475        s.send_dgram_client(0).unwrap();
7476        s.send_dgram_client(2).unwrap();
7477        s.send_dgram_client(2).unwrap();
7478        s.send_dgram_client(2).unwrap();
7479        s.send_dgram_client(2).unwrap();
7480        s.send_dgram_client(2).unwrap();
7481
7482        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7483        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7484
7485        assert_eq!(s.poll_server(), Err(Error::Done));
7486
7487        // Second cycle, start to read
7488        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7489        assert_eq!(s.poll_server(), Err(Error::Done));
7490        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7491        assert_eq!(s.poll_server(), Err(Error::Done));
7492        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7493        assert_eq!(s.poll_server(), Err(Error::Done));
7494
7495        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7496        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7497
7498        assert_eq!(s.poll_server(), Err(Error::Done));
7499
7500        // Third cycle.
7501        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7502        assert_eq!(s.poll_server(), Err(Error::Done));
7503        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7504        assert_eq!(s.poll_server(), Err(Error::Done));
7505        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7506        assert_eq!(s.poll_server(), Err(Error::Done));
7507        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7508        assert_eq!(s.poll_server(), Err(Error::Done));
7509        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7510        assert_eq!(s.poll_server(), Err(Error::Done));
7511        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7512        assert_eq!(s.poll_server(), Err(Error::Done));
7513        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7514        assert_eq!(s.poll_server(), Err(Error::Done));
7515
7516        // Send response followed by DATAGRAM on server side
7517        let resp = s.send_response(stream, false).unwrap();
7518
7519        let body = s.send_body_server(stream, true).unwrap();
7520
7521        let mut recv_buf = vec![0; body.len()];
7522
7523        let ev_headers = Event::Headers {
7524            list: resp,
7525            more_frames: true,
7526        };
7527
7528        s.send_dgram_server(0).unwrap();
7529        s.send_dgram_server(0).unwrap();
7530        s.send_dgram_server(0).unwrap();
7531        s.send_dgram_server(0).unwrap();
7532        s.send_dgram_server(0).unwrap();
7533        s.send_dgram_server(2).unwrap();
7534        s.send_dgram_server(2).unwrap();
7535        s.send_dgram_server(2).unwrap();
7536        s.send_dgram_server(2).unwrap();
7537        s.send_dgram_server(2).unwrap();
7538
7539        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7540        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
7541
7542        assert_eq!(s.poll_client(), Err(Error::Done));
7543
7544        // Second cycle, start to read
7545        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7546        assert_eq!(s.poll_client(), Err(Error::Done));
7547        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7548        assert_eq!(s.poll_client(), Err(Error::Done));
7549        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7550        assert_eq!(s.poll_client(), Err(Error::Done));
7551
7552        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
7553        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7554
7555        assert_eq!(s.poll_client(), Err(Error::Done));
7556
7557        // Third cycle.
7558        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7559        assert_eq!(s.poll_client(), Err(Error::Done));
7560        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_0_result));
7561        assert_eq!(s.poll_client(), Err(Error::Done));
7562        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7563        assert_eq!(s.poll_client(), Err(Error::Done));
7564        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7565        assert_eq!(s.poll_client(), Err(Error::Done));
7566        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7567        assert_eq!(s.poll_client(), Err(Error::Done));
7568        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7569        assert_eq!(s.poll_client(), Err(Error::Done));
7570        assert_eq!(s.recv_dgram_client(&mut buf), Ok(flow_2_result));
7571        assert_eq!(s.poll_client(), Err(Error::Done));
7572    }
7573
7574    #[test]
7575    /// Tests that the Finished event is not issued for streams of unknown type
7576    /// (e.g. GREASE).
7577    fn finished_is_for_requests() {
7578        let mut s = Session::new().unwrap();
7579        s.handshake().unwrap();
7580
7581        assert_eq!(s.poll_client(), Err(Error::Done));
7582        assert_eq!(s.poll_server(), Err(Error::Done));
7583
7584        assert_eq!(s.client.open_grease_stream(&mut s.pipe.client), Ok(()));
7585        assert_eq!(s.pipe.advance(), Ok(()));
7586
7587        assert_eq!(s.poll_client(), Err(Error::Done));
7588        assert_eq!(s.poll_server(), Err(Error::Done));
7589    }
7590
7591    #[test]
7592    /// Tests that streams are marked as finished only once.
7593    fn finished_once() {
7594        let mut s = Session::new().unwrap();
7595        s.handshake().unwrap();
7596
7597        let (stream, req) = s.send_request(false).unwrap();
7598        let body = s.send_body_client(stream, true).unwrap();
7599
7600        let mut recv_buf = vec![0; body.len()];
7601
7602        let ev_headers = Event::Headers {
7603            list: req,
7604            more_frames: true,
7605        };
7606
7607        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7608        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7609
7610        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7611        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7612
7613        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Err(Error::Done));
7614        assert_eq!(s.poll_server(), Err(Error::Done));
7615    }
7616
7617    #[test]
7618    /// Tests that the Data event is properly re-armed.
7619    fn data_event_rearm() {
7620        let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
7621
7622        let mut s = Session::new().unwrap();
7623        s.handshake().unwrap();
7624
7625        let (r1_id, r1_hdrs) = s.send_request(false).unwrap();
7626
7627        let mut recv_buf = vec![0; bytes.len()];
7628
7629        let r1_ev_headers = Event::Headers {
7630            list: r1_hdrs,
7631            more_frames: true,
7632        };
7633
7634        // Manually send an incomplete DATA frame (i.e. the frame size is longer
7635        // than the actual data sent).
7636        {
7637            let mut d = [42; 10];
7638            let mut b = octets::OctetsMut::with_slice(&mut d);
7639
7640            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7641            b.put_varint(bytes.len() as u64).unwrap();
7642            let off = b.off();
7643            s.pipe.client.stream_send(r1_id, &d[..off], false).unwrap();
7644
7645            assert_eq!(
7646                s.pipe.client.stream_send(r1_id, &bytes[..5], false),
7647                Ok(5)
7648            );
7649
7650            s.advance().ok();
7651        }
7652
7653        assert_eq!(s.poll_server(), Ok((r1_id, r1_ev_headers)));
7654        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7655        assert_eq!(s.poll_server(), Err(Error::Done));
7656
7657        // Read the available body data.
7658        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(5));
7659
7660        // Send the remaining DATA payload.
7661        assert_eq!(s.pipe.client.stream_send(r1_id, &bytes[5..], false), Ok(5));
7662        s.advance().ok();
7663
7664        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7665        assert_eq!(s.poll_server(), Err(Error::Done));
7666
7667        // Read the rest of the body data.
7668        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(5));
7669        assert_eq!(s.poll_server(), Err(Error::Done));
7670
7671        // Send more data.
7672        let r1_body = s.send_body_client(r1_id, false).unwrap();
7673
7674        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7675        assert_eq!(s.poll_server(), Err(Error::Done));
7676
7677        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(r1_body.len()));
7678
7679        // Send a new request to ensure cross-stream events don't break rearming.
7680        let (r2_id, r2_hdrs) = s.send_request(false).unwrap();
7681        let r2_ev_headers = Event::Headers {
7682            list: r2_hdrs,
7683            more_frames: true,
7684        };
7685        let r2_body = s.send_body_client(r2_id, false).unwrap();
7686
7687        s.advance().ok();
7688
7689        assert_eq!(s.poll_server(), Ok((r2_id, r2_ev_headers)));
7690        assert_eq!(s.poll_server(), Ok((r2_id, Event::Data)));
7691        assert_eq!(s.recv_body_server(r2_id, &mut recv_buf), Ok(r2_body.len()));
7692        assert_eq!(s.poll_server(), Err(Error::Done));
7693
7694        // Send more data on request 1, then trailing HEADERS.
7695        let r1_body = s.send_body_client(r1_id, false).unwrap();
7696
7697        let trailers = vec![Header::new(b"hello", b"world")];
7698
7699        s.client
7700            .send_headers(&mut s.pipe.client, r1_id, &trailers, true)
7701            .unwrap();
7702
7703        let r1_ev_trailers = Event::Headers {
7704            list: trailers.clone(),
7705            more_frames: false,
7706        };
7707
7708        s.advance().ok();
7709
7710        assert_eq!(s.poll_server(), Ok((r1_id, Event::Data)));
7711        assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(r1_body.len()));
7712
7713        assert_eq!(s.poll_server(), Ok((r1_id, r1_ev_trailers)));
7714        assert_eq!(s.poll_server(), Ok((r1_id, Event::Finished)));
7715        assert_eq!(s.poll_server(), Err(Error::Done));
7716
7717        // Send more data on request 2, then trailing HEADERS.
7718        let r2_body = s.send_body_client(r2_id, false).unwrap();
7719
7720        s.client
7721            .send_headers(&mut s.pipe.client, r2_id, &trailers, false)
7722            .unwrap();
7723
7724        let r2_ev_trailers = Event::Headers {
7725            list: trailers,
7726            more_frames: true,
7727        };
7728
7729        s.advance().ok();
7730
7731        assert_eq!(s.poll_server(), Ok((r2_id, Event::Data)));
7732        assert_eq!(s.recv_body_server(r2_id, &mut recv_buf), Ok(r2_body.len()));
7733        assert_eq!(s.poll_server(), Ok((r2_id, r2_ev_trailers)));
7734        assert_eq!(s.poll_server(), Err(Error::Done));
7735
7736        let (r3_id, r3_hdrs) = s.send_request(false).unwrap();
7737
7738        let r3_ev_headers = Event::Headers {
7739            list: r3_hdrs,
7740            more_frames: true,
7741        };
7742
7743        // Manually send an incomplete DATA frame (i.e. only the header is sent).
7744        {
7745            let mut d = [42; 10];
7746            let mut b = octets::OctetsMut::with_slice(&mut d);
7747
7748            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7749            b.put_varint(bytes.len() as u64).unwrap();
7750            let off = b.off();
7751            s.pipe.client.stream_send(r3_id, &d[..off], false).unwrap();
7752
7753            s.advance().ok();
7754        }
7755
7756        assert_eq!(s.poll_server(), Ok((r3_id, r3_ev_headers)));
7757        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7758        assert_eq!(s.poll_server(), Err(Error::Done));
7759
7760        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Err(Error::Done));
7761
7762        assert_eq!(s.pipe.client.stream_send(r3_id, &bytes[..5], false), Ok(5));
7763
7764        s.advance().ok();
7765
7766        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7767        assert_eq!(s.poll_server(), Err(Error::Done));
7768
7769        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(5));
7770
7771        assert_eq!(s.pipe.client.stream_send(r3_id, &bytes[5..], false), Ok(5));
7772        s.advance().ok();
7773
7774        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7775        assert_eq!(s.poll_server(), Err(Error::Done));
7776
7777        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(5));
7778
7779        // Buffer multiple data frames.
7780        let body = s.send_body_client(r3_id, false).unwrap();
7781        s.send_body_client(r3_id, false).unwrap();
7782        s.send_body_client(r3_id, false).unwrap();
7783
7784        assert_eq!(s.poll_server(), Ok((r3_id, Event::Data)));
7785        assert_eq!(s.poll_server(), Err(Error::Done));
7786
7787        {
7788            let mut d = [42; 10];
7789            let mut b = octets::OctetsMut::with_slice(&mut d);
7790
7791            b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
7792            b.put_varint(0).unwrap();
7793            let off = b.off();
7794            s.pipe.client.stream_send(r3_id, &d[..off], true).unwrap();
7795
7796            s.advance().ok();
7797        }
7798
7799        let mut recv_buf = vec![0; bytes.len() * 3];
7800
7801        assert_eq!(s.recv_body_server(r3_id, &mut recv_buf), Ok(body.len() * 3));
7802    }
7803
7804    #[test]
7805    /// Tests that the Datagram event is properly re-armed.
7806    fn dgram_event_rearm() {
7807        let mut buf = [0; 65535];
7808
7809        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
7810        config
7811            .load_cert_chain_from_pem_file("examples/cert.crt")
7812            .unwrap();
7813        config
7814            .load_priv_key_from_pem_file("examples/cert.key")
7815            .unwrap();
7816        config.set_application_protos(&[b"h3"]).unwrap();
7817        config.set_initial_max_data(1500);
7818        config.set_initial_max_stream_data_bidi_local(150);
7819        config.set_initial_max_stream_data_bidi_remote(150);
7820        config.set_initial_max_stream_data_uni(150);
7821        config.set_initial_max_streams_bidi(100);
7822        config.set_initial_max_streams_uni(5);
7823        config.verify_peer(false);
7824        config.enable_dgram(true, 100, 100);
7825
7826        let h3_config = Config::new().unwrap();
7827        let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7828        s.handshake().unwrap();
7829
7830        // 10 bytes on flow ID 0 and 2.
7831        let flow_0_result = (11, 0, 1);
7832        let flow_2_result = (11, 2, 1);
7833
7834        // Send requests followed by DATAGRAMs on client side.
7835        let (stream, req) = s.send_request(false).unwrap();
7836
7837        let body = s.send_body_client(stream, true).unwrap();
7838
7839        let mut recv_buf = vec![0; body.len()];
7840
7841        let ev_headers = Event::Headers {
7842            list: req,
7843            more_frames: true,
7844        };
7845
7846        s.send_dgram_client(0).unwrap();
7847        s.send_dgram_client(0).unwrap();
7848        s.send_dgram_client(2).unwrap();
7849        s.send_dgram_client(2).unwrap();
7850
7851        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7852        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7853
7854        assert_eq!(s.poll_server(), Err(Error::Done));
7855        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7856
7857        assert_eq!(s.poll_server(), Err(Error::Done));
7858        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7859
7860        assert_eq!(s.poll_server(), Err(Error::Done));
7861        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7862
7863        assert_eq!(s.poll_server(), Err(Error::Done));
7864        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7865
7866        assert_eq!(s.poll_server(), Err(Error::Done));
7867
7868        s.send_dgram_client(0).unwrap();
7869        s.send_dgram_client(2).unwrap();
7870
7871        assert_eq!(s.poll_server(), Err(Error::Done));
7872
7873        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_0_result));
7874        assert_eq!(s.poll_server(), Err(Error::Done));
7875
7876        assert_eq!(s.recv_dgram_server(&mut buf), Ok(flow_2_result));
7877        assert_eq!(s.poll_server(), Err(Error::Done));
7878
7879        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
7880        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
7881
7882        // Verify that dgram counts are incremented.
7883        assert_eq!(s.pipe.client.dgram_sent_count, 6);
7884        assert_eq!(s.pipe.client.dgram_recv_count, 0);
7885        assert_eq!(s.pipe.server.dgram_sent_count, 0);
7886        assert_eq!(s.pipe.server.dgram_recv_count, 6);
7887
7888        let server_path = s.pipe.server.paths.get_active().expect("no active");
7889        let client_path = s.pipe.client.paths.get_active().expect("no active");
7890        assert_eq!(client_path.dgram_sent_count, 6);
7891        assert_eq!(client_path.dgram_recv_count, 0);
7892        assert_eq!(server_path.dgram_sent_count, 0);
7893        assert_eq!(server_path.dgram_recv_count, 6);
7894    }
7895
7896    #[test]
7897    fn reset_stream() {
7898        let mut buf = [0; 65535];
7899
7900        let mut s = Session::new().unwrap();
7901        s.handshake().unwrap();
7902
7903        // Client sends request.
7904        let (stream, req) = s.send_request(false).unwrap();
7905
7906        let ev_headers = Event::Headers {
7907            list: req,
7908            more_frames: true,
7909        };
7910
7911        // Server sends response and closes stream.
7912        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7913        assert_eq!(s.poll_server(), Err(Error::Done));
7914
7915        let resp = s.send_response(stream, true).unwrap();
7916
7917        let ev_headers = Event::Headers {
7918            list: resp,
7919            more_frames: false,
7920        };
7921
7922        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7923        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7924        assert_eq!(s.poll_client(), Err(Error::Done));
7925
7926        // Client sends RESET_STREAM, closing stream.
7927        let frames = [crate::frame::Frame::ResetStream {
7928            stream_id: stream,
7929            error_code: 42,
7930            final_size: 68,
7931        }];
7932
7933        let pkt_type = crate::packet::Type::Short;
7934        assert_eq!(
7935            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
7936            Ok(39)
7937        );
7938
7939        // Server issues Reset event for the stream.
7940        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
7941        assert_eq!(s.poll_server(), Err(Error::Done));
7942
7943        // Sending RESET_STREAM again shouldn't trigger another Reset event.
7944        assert_eq!(
7945            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
7946            Ok(39)
7947        );
7948
7949        assert_eq!(s.poll_server(), Err(Error::Done));
7950    }
7951
7952    /// The client shuts down the stream's write direction, the server
7953    /// shuts down its side with fin
7954    #[test]
7955    fn client_shutdown_write_server_fin() {
7956        let mut buf = [0; 65535];
7957        let mut s = Session::new().unwrap();
7958        s.handshake().unwrap();
7959
7960        // Client sends request.
7961        let (stream, req) = s.send_request(false).unwrap();
7962
7963        let ev_headers = Event::Headers {
7964            list: req,
7965            more_frames: true,
7966        };
7967
7968        // Server sends response and closes stream.
7969        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7970        assert_eq!(s.poll_server(), Err(Error::Done));
7971
7972        let resp = s.send_response(stream, true).unwrap();
7973
7974        let ev_headers = Event::Headers {
7975            list: resp,
7976            more_frames: false,
7977        };
7978
7979        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
7980        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
7981        assert_eq!(s.poll_client(), Err(Error::Done));
7982
7983        // Client shuts down stream ==> sends RESET_STREAM
7984        assert_eq!(
7985            s.pipe
7986                .client
7987                .stream_shutdown(stream, crate::Shutdown::Write, 42),
7988            Ok(())
7989        );
7990        assert_eq!(s.advance(), Ok(()));
7991
7992        // Server sees the Reset event for the stream.
7993        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
7994        assert_eq!(s.poll_server(), Err(Error::Done));
7995
7996        // Streams have been collected by quiche
7997        assert!(s.pipe.server.streams.is_collected(stream));
7998        assert!(s.pipe.client.streams.is_collected(stream));
7999
8000        // Client sends another request, server sends response without fin
8001        //
8002        let (stream, req) = s.send_request(false).unwrap();
8003
8004        let ev_headers = Event::Headers {
8005            list: req,
8006            more_frames: true,
8007        };
8008
8009        // Check that server has received the request.
8010        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8011        assert_eq!(s.poll_server(), Err(Error::Done));
8012
8013        // Server sends reponse without closing the stream.
8014        let resp = s.send_response(stream, false).unwrap();
8015
8016        let ev_headers = Event::Headers {
8017            list: resp,
8018            more_frames: true,
8019        };
8020
8021        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8022        assert_eq!(s.poll_client(), Err(Error::Done));
8023
8024        // Client shuts down stream ==> sends RESET_STREAM
8025        assert_eq!(
8026            s.pipe
8027                .client
8028                .stream_shutdown(stream, crate::Shutdown::Write, 42),
8029            Ok(())
8030        );
8031        assert_eq!(s.advance(), Ok(()));
8032
8033        // Server sees the Reset event for the stream.
8034        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(42))));
8035        assert_eq!(s.poll_server(), Err(Error::Done));
8036
8037        // Server sends body and closes the stream.
8038        s.send_body_server(stream, true).unwrap();
8039
8040        // Stream has been collected on server by quiche
8041        assert!(s.pipe.server.streams.is_collected(stream));
8042        // Client stream has not been collected, the client needs to
8043        // read the fin from the stream first.
8044        assert!(!s.pipe.client.streams.is_collected(stream));
8045        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8046        s.recv_body_client(stream, &mut buf).unwrap();
8047        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8048        assert_eq!(s.poll_client(), Err(Error::Done));
8049        assert!(s.pipe.client.streams.is_collected(stream));
8050    }
8051
8052    #[test]
8053    fn client_shutdown_read() {
8054        let mut buf = [0; 65535];
8055        let mut s = Session::new().unwrap();
8056        s.handshake().unwrap();
8057
8058        // Client sends request and leaves stream open.
8059        let (stream, req) = s.send_request(false).unwrap();
8060
8061        let ev_headers = Event::Headers {
8062            list: req,
8063            more_frames: true,
8064        };
8065
8066        // Server sends response and leaves stream open.
8067        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8068        assert_eq!(s.poll_server(), Err(Error::Done));
8069
8070        let resp = s.send_response(stream, false).unwrap();
8071
8072        let ev_headers = Event::Headers {
8073            list: resp,
8074            more_frames: true,
8075        };
8076
8077        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8078        assert_eq!(s.poll_client(), Err(Error::Done));
8079        // Client shuts down read
8080        assert_eq!(
8081            s.pipe
8082                .client
8083                .stream_shutdown(stream, crate::Shutdown::Read, 42),
8084            Ok(())
8085        );
8086        assert_eq!(s.advance(), Ok(()));
8087
8088        // Stream is writable on server side, but returns StreamStopped
8089        assert_eq!(s.poll_server(), Err(Error::Done));
8090        let writables: Vec<u64> = s.pipe.server.writable().collect();
8091        assert!(writables.contains(&stream));
8092        assert_eq!(
8093            s.send_body_server(stream, false),
8094            Err(Error::TransportError(crate::Error::StreamStopped(42)))
8095        );
8096
8097        // Client needs to finish its side by sending a fin
8098        assert_eq!(
8099            s.client.send_body(&mut s.pipe.client, stream, &[], true),
8100            Ok(0)
8101        );
8102        assert_eq!(s.advance(), Ok(()));
8103        // Note, we get an Event::Data for an empty buffer today. But it
8104        // would also be fine to not get it.
8105        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8106        assert_eq!(s.recv_body_server(stream, &mut buf), Err(Error::Done));
8107        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8108        assert_eq!(s.poll_server(), Err(Error::Done));
8109
8110        // Since the client has already send a fin, the stream is collected
8111        // on both client and server
8112        assert!(s.pipe.client.streams.is_collected(stream));
8113        assert!(s.pipe.server.streams.is_collected(stream));
8114    }
8115
8116    #[test]
8117    fn reset_finished_at_server() {
8118        let mut s = Session::new().unwrap();
8119        s.handshake().unwrap();
8120
8121        // Client sends HEADERS and doesn't fin
8122        let (stream, _req) = s.send_request(false).unwrap();
8123
8124        // ..then Client sends RESET_STREAM
8125        assert_eq!(
8126            s.pipe.client.stream_shutdown(0, crate::Shutdown::Write, 0),
8127            Ok(())
8128        );
8129
8130        assert_eq!(s.pipe.advance(), Ok(()));
8131
8132        // Server receives just a reset
8133        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(0))));
8134        assert_eq!(s.poll_server(), Err(Error::Done));
8135
8136        // Client sends HEADERS and fin
8137        let (stream, req) = s.send_request(true).unwrap();
8138
8139        // ..then Client sends RESET_STREAM
8140        assert_eq!(
8141            s.pipe.client.stream_shutdown(4, crate::Shutdown::Write, 0),
8142            Ok(())
8143        );
8144
8145        let ev_headers = Event::Headers {
8146            list: req,
8147            more_frames: false,
8148        };
8149
8150        // Server receives headers and fin.
8151        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8152        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8153        assert_eq!(s.poll_server(), Err(Error::Done));
8154    }
8155
8156    #[test]
8157    fn reset_finished_at_server_with_data_pending() {
8158        let mut s = Session::new().unwrap();
8159        s.handshake().unwrap();
8160
8161        // Client sends HEADERS and doesn't fin.
8162        let (stream, req) = s.send_request(false).unwrap();
8163
8164        assert!(s.send_body_client(stream, false).is_ok());
8165
8166        assert_eq!(s.pipe.advance(), Ok(()));
8167
8168        let ev_headers = Event::Headers {
8169            list: req,
8170            more_frames: true,
8171        };
8172
8173        // Server receives headers and data...
8174        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8175        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8176
8177        // ..then Client sends RESET_STREAM.
8178        assert_eq!(
8179            s.pipe
8180                .client
8181                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8182            Ok(())
8183        );
8184
8185        assert_eq!(s.pipe.advance(), Ok(()));
8186
8187        // The server does *not* attempt to read from the stream,
8188        // but polls and receives the reset and there are no more
8189        // readable streams.
8190        assert_eq!(s.poll_server(), Ok((stream, Event::Reset(0))));
8191        assert_eq!(s.poll_server(), Err(Error::Done));
8192        assert_eq!(s.pipe.server.readable().len(), 0);
8193    }
8194
8195    #[test]
8196    fn reset_finished_at_server_with_data_pending_2() {
8197        let mut s = Session::new().unwrap();
8198        s.handshake().unwrap();
8199
8200        // Client sends HEADERS and doesn't fin.
8201        let (stream, req) = s.send_request(false).unwrap();
8202
8203        assert!(s.send_body_client(stream, false).is_ok());
8204
8205        assert_eq!(s.pipe.advance(), Ok(()));
8206
8207        let ev_headers = Event::Headers {
8208            list: req,
8209            more_frames: true,
8210        };
8211
8212        // Server receives headers and data...
8213        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8214        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8215
8216        // ..then Client sends RESET_STREAM.
8217        assert_eq!(
8218            s.pipe
8219                .client
8220                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8221            Ok(())
8222        );
8223
8224        assert_eq!(s.pipe.advance(), Ok(()));
8225
8226        // Server reads from the stream and receives the reset while
8227        // attempting to read.
8228        assert_eq!(
8229            s.recv_body_server(stream, &mut [0; 100]),
8230            Err(Error::TransportError(crate::Error::StreamReset(0)))
8231        );
8232
8233        // No more events and there are no more readable streams.
8234        assert_eq!(s.poll_server(), Err(Error::Done));
8235        assert_eq!(s.pipe.server.readable().len(), 0);
8236    }
8237
8238    #[test]
8239    fn reset_finished_at_client() {
8240        let mut buf = [0; 65535];
8241        let mut s = Session::new().unwrap();
8242        s.handshake().unwrap();
8243
8244        // Client sends HEADERS and doesn't fin
8245        let (stream, req) = s.send_request(false).unwrap();
8246
8247        let ev_headers = Event::Headers {
8248            list: req,
8249            more_frames: true,
8250        };
8251
8252        // Server receives headers.
8253        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8254        assert_eq!(s.poll_server(), Err(Error::Done));
8255
8256        // Server sends response and doesn't fin
8257        s.send_response(stream, false).unwrap();
8258
8259        assert_eq!(s.pipe.advance(), Ok(()));
8260
8261        // .. then Server sends RESET_STREAM
8262        assert_eq!(
8263            s.pipe
8264                .server
8265                .stream_shutdown(stream, crate::Shutdown::Write, 0),
8266            Ok(())
8267        );
8268
8269        assert_eq!(s.pipe.advance(), Ok(()));
8270
8271        // Client receives Reset only
8272        assert_eq!(s.poll_client(), Ok((stream, Event::Reset(0))));
8273        assert_eq!(s.poll_server(), Err(Error::Done));
8274
8275        // Client sends headers and fin.
8276        let (stream, req) = s.send_request(true).unwrap();
8277
8278        let ev_headers = Event::Headers {
8279            list: req,
8280            more_frames: false,
8281        };
8282
8283        // Server receives headers and fin.
8284        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8285        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8286        assert_eq!(s.poll_server(), Err(Error::Done));
8287
8288        // Server sends response and fin
8289        let resp = s.send_response(stream, true).unwrap();
8290
8291        assert_eq!(s.pipe.advance(), Ok(()));
8292
8293        // ..then Server sends RESET_STREAM
8294        let frames = [crate::frame::Frame::ResetStream {
8295            stream_id: stream,
8296            error_code: 42,
8297            final_size: 68,
8298        }];
8299
8300        let pkt_type = crate::packet::Type::Short;
8301        assert_eq!(
8302            s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
8303            Ok(39)
8304        );
8305
8306        assert_eq!(s.pipe.advance(), Ok(()));
8307
8308        let ev_headers = Event::Headers {
8309            list: resp,
8310            more_frames: false,
8311        };
8312
8313        // Client receives headers and fin.
8314        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8315        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8316        assert_eq!(s.poll_client(), Err(Error::Done));
8317    }
8318
8319    #[test]
8320    fn collect_completed_streams() {
8321        let mut s = Session::new().unwrap();
8322        s.handshake().unwrap();
8323
8324        let init_streams_client = s.client.streams.len();
8325        let init_streams_server = s.server.streams.len();
8326
8327        // Client sends HEADERS and doesn't fin
8328        let (stream, req) = s.send_request(false).unwrap();
8329
8330        let ev_headers = Event::Headers {
8331            list: req,
8332            more_frames: true,
8333        };
8334
8335        // Server receives headers.
8336        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8337        assert_eq!(s.poll_server(), Err(Error::Done));
8338
8339        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8340        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8341
8342        // Client sends body and fin
8343        let body = s.send_body_client(stream, true).unwrap();
8344
8345        let mut recv_buf = vec![0; body.len()];
8346
8347        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8348        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
8349
8350        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8351
8352        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8353        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8354
8355        // Server sends response and finishes the stream
8356        let resp_headers = s.send_response(stream, false).unwrap();
8357        s.send_body_server(stream, true).unwrap();
8358
8359        let ev_headers = Event::Headers {
8360            list: resp_headers,
8361            more_frames: true,
8362        };
8363
8364        assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8365        assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8366        assert_eq!(s.recv_body_client(stream, &mut recv_buf), Ok(body.len()));
8367
8368        // The server stream should be gone now
8369        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8370        assert_eq!(s.server.streams.len(), init_streams_server);
8371
8372        // Polling again should clean up the client
8373        assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8374        assert_eq!(s.poll_client(), Err(Error::Done));
8375
8376        assert_eq!(s.client.streams.len(), init_streams_client);
8377    }
8378
8379    #[test]
8380    fn collect_reset_streams() {
8381        let mut s = Session::new().unwrap();
8382        s.handshake().unwrap();
8383
8384        let init_streams_client = s.client.streams.len();
8385        let init_streams_server = s.server.streams.len();
8386
8387        // Client sends HEADERS and doesn't fin
8388        let (stream, req) = s.send_request(false).unwrap();
8389
8390        let ev_headers = Event::Headers {
8391            list: req,
8392            more_frames: true,
8393        };
8394
8395        // Server receives headers.
8396        assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8397        assert_eq!(s.poll_server(), Err(Error::Done));
8398
8399        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8400        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8401
8402        // Client sends body and fin
8403        let body = s.send_body_client(stream, true).unwrap();
8404
8405        let mut recv_buf = vec![0; body.len()];
8406
8407        assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
8408        assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(body.len()));
8409
8410        assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
8411
8412        assert_eq!(s.client.streams.len(), init_streams_client + 1);
8413        assert_eq!(s.server.streams.len(), init_streams_server + 1);
8414
8415        // Server sends response and resets the stream.
8416        s.send_response(stream, false).unwrap();
8417        s.pipe
8418            .server
8419            .stream_shutdown(stream, crate::Shutdown::Write, 0)
8420            .unwrap();
8421
8422        s.advance().ok();
8423
8424        // TODO: need to notify resets better.
8425        //
8426        // This will trigger the h3 layer to check for the stream resets,
8427        // otherwise it wouldn't know that a reset happened.
8428        //
8429        // We will need to figure out a way to do this automatically to avoid
8430        // requiring applications to do this manually. For now just keep this
8431        // for testing purposes.
8432        let _ = s.send_body_server(stream, true);
8433
8434        assert_eq!(s.poll_server(), Err(Error::Done));
8435
8436        assert_eq!(s.poll_client(), Ok((stream, Event::Reset(0))));
8437        assert_eq!(s.poll_client(), Err(Error::Done));
8438
8439        // The server stream should be gone now
8440        assert_eq!(s.client.streams.len(), init_streams_client);
8441        assert_eq!(s.server.streams.len(), init_streams_server);
8442    }
8443}
8444
8445#[cfg(feature = "ffi")]
8446mod ffi;
8447#[cfg(feature = "internal")]
8448#[doc(hidden)]
8449pub mod frame;
8450#[cfg(not(feature = "internal"))]
8451mod frame;
8452#[doc(hidden)]
8453pub mod qpack;
8454mod stream;