rwebtransport 0.2.2 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/Cargo.lock +523 -140
  2. package/Cargo.toml +5 -5
  3. package/README.md +8 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +2 -0
  6. package/crates/native/src/config.rs +69 -8
  7. package/crates/native/src/driver.rs +253 -90
  8. package/crates/native/src/lib.rs +80 -57
  9. package/crates/native/src/parser_memory.rs +203 -0
  10. package/crates/native/src/server.rs +215 -183
  11. package/crates/native/src/session.rs +1341 -243
  12. package/crates/native/src/test_support.rs +119 -0
  13. package/crates/wtcore/Cargo.toml +1 -0
  14. package/crates/wtcore/src/h3.rs +250 -46
  15. package/dist/index.js +218 -75
  16. package/dist/index.js.map +7 -1
  17. package/dist/index.mjs +214 -69
  18. package/dist/index.mjs.map +7 -1
  19. package/dist/native.d.ts +10 -4
  20. package/dist/server.d.ts +1 -0
  21. package/package.json +84 -78
  22. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  24. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  25. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  26. package/scripts/build-linux-prebuilt.sh +46 -0
  27. package/scripts/build-ts.js +41 -0
  28. package/scripts/build.js +10 -3
  29. package/vendor/quiche/AGENTS.md +10 -0
  30. package/vendor/quiche/Cargo.lock +239 -248
  31. package/vendor/quiche/Cargo.toml +23 -10
  32. package/vendor/quiche/examples/Makefile +4 -4
  33. package/vendor/quiche/examples/client.rs +10 -28
  34. package/vendor/quiche/examples/http3-client.rs +20 -34
  35. package/vendor/quiche/examples/http3-server.rs +47 -78
  36. package/vendor/quiche/examples/server.rs +19 -49
  37. package/vendor/quiche/include/quiche.h +35 -4
  38. package/vendor/quiche/src/build.rs +17 -0
  39. package/vendor/quiche/src/cid.rs +129 -61
  40. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  41. package/vendor/quiche/src/crypto/mod.rs +64 -77
  42. package/vendor/quiche/src/dgram.rs +2 -3
  43. package/vendor/quiche/src/error.rs +22 -20
  44. package/vendor/quiche/src/ffi.rs +558 -280
  45. package/vendor/quiche/src/flowcontrol.rs +0 -7
  46. package/vendor/quiche/src/frame.rs +134 -135
  47. package/vendor/quiche/src/h3/ffi.rs +73 -60
  48. package/vendor/quiche/src/h3/frame.rs +70 -90
  49. package/vendor/quiche/src/h3/mod.rs +1024 -593
  50. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  51. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  52. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  53. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  54. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  55. package/vendor/quiche/src/h3/stream.rs +1009 -194
  56. package/vendor/quiche/src/lib.rs +1361 -1195
  57. package/vendor/quiche/src/minmax.rs +2 -6
  58. package/vendor/quiche/src/packet.rs +420 -552
  59. package/vendor/quiche/src/path.rs +202 -72
  60. package/vendor/quiche/src/pmtud.rs +18 -16
  61. package/vendor/quiche/src/ranges.rs +184 -82
  62. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  63. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  64. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  65. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  66. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  67. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  68. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  69. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  70. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  71. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  72. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  73. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  74. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  84. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  85. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  86. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  87. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  88. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  89. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  90. package/vendor/quiche/src/recovery/mod.rs +414 -173
  91. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  92. package/vendor/quiche/src/stream/mod.rs +864 -133
  93. package/vendor/quiche/src/stream/recv_buf.rs +476 -172
  94. package/vendor/quiche/src/stream/send_buf.rs +343 -81
  95. package/vendor/quiche/src/test_utils.rs +78 -64
  96. package/vendor/quiche/src/tests.rs +2559 -1335
  97. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  98. package/vendor/quiche/src/tls/mod.rs +146 -193
  99. package/vendor/quiche/src/transport_params.rs +87 -103
@@ -335,15 +335,23 @@ const PRIORITY_URGENCY_UPPER_BOUND: u8 = 7;
335
335
  const PRIORITY_URGENCY_DEFAULT: u8 = 3;
336
336
  const PRIORITY_INCREMENTAL_DEFAULT: bool = false;
337
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>
342
+ pub 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>.
347
+ pub const SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT: u64 = 32_768;
348
+
338
349
  #[cfg(feature = "qlog")]
339
- const QLOG_FRAME_CREATED: EventType =
340
- EventType::Http3EventType(Http3EventType::FrameCreated);
350
+ const QLOG_FRAME_CREATED: EventType = EventType::Http3EventType(Http3EventType::FrameCreated);
341
351
  #[cfg(feature = "qlog")]
342
- const QLOG_FRAME_PARSED: EventType =
343
- EventType::Http3EventType(Http3EventType::FrameParsed);
352
+ const QLOG_FRAME_PARSED: EventType = EventType::Http3EventType(Http3EventType::FrameParsed);
344
353
  #[cfg(feature = "qlog")]
345
- const QLOG_STREAM_TYPE_SET: EventType =
346
- EventType::Http3EventType(Http3EventType::StreamTypeSet);
354
+ const QLOG_STREAM_TYPE_SET: EventType = EventType::Http3EventType(Http3EventType::StreamTypeSet);
347
355
 
348
356
  /// A specialized [`Result`] type for quiche HTTP/3 operations.
349
357
  ///
@@ -427,55 +435,63 @@ pub enum Error {
427
435
 
428
436
  /// HTTP/3 error codes sent on the wire.
429
437
  ///
430
- /// As defined in [RFC9114](https://www.rfc-editor.org/rfc/rfc9114.html#http-error-codes).
438
+ /// These are from the IANA registry at
439
+ /// https://www.iana.org/assignments/http3-parameters/http3-parameters.xhtml
440
+ /// (Section: "HTTP/3 Error Codes")
441
+ ///
442
+ /// Most of these are defined in
443
+ /// [RFC9114](https://www.rfc-editor.org/rfc/rfc9114.html#http-error-codes).
431
444
  #[derive(Copy, Clone, Debug, Eq, PartialEq)]
432
445
  pub enum WireErrorCode {
446
+ /// Datagram or Capsule Protocol error per
447
+ /// [RFC9297](https://datatracker.ietf.org/doc/html/rfc9297#name-http-3-error-code)
448
+ DatagramError = 0x33,
433
449
  /// No error. This is used when the connection or stream needs to be closed,
434
450
  /// but there is no error to signal.
435
- NoError = 0x100,
451
+ NoError = 0x100,
436
452
  /// Peer violated protocol requirements in a way that does not match a more
437
453
  /// specific error code or endpoint declines to use the more specific
438
454
  /// error code.
439
455
  GeneralProtocolError = 0x101,
440
456
  /// An internal error has occurred in the HTTP stack.
441
- InternalError = 0x102,
457
+ InternalError = 0x102,
442
458
  /// The endpoint detected that its peer created a stream that it will not
443
459
  /// accept.
444
- StreamCreationError = 0x103,
460
+ StreamCreationError = 0x103,
445
461
  /// A stream required by the HTTP/3 connection was closed or reset.
446
462
  ClosedCriticalStream = 0x104,
447
463
  /// A frame was received that was not permitted in the current state or on
448
464
  /// the current stream.
449
- FrameUnexpected = 0x105,
465
+ FrameUnexpected = 0x105,
450
466
  /// A frame that fails to satisfy layout requirements or with an invalid
451
467
  /// size was received.
452
- FrameError = 0x106,
468
+ FrameError = 0x106,
453
469
  /// The endpoint detected that its peer is exhibiting a behavior that might
454
470
  /// be generating excessive load.
455
- ExcessiveLoad = 0x107,
471
+ ExcessiveLoad = 0x107,
456
472
  /// A stream ID or push ID was used incorrectly, such as exceeding a limit,
457
473
  /// reducing a limit, or being reused.
458
- IdError = 0x108,
474
+ IdError = 0x108,
459
475
  /// An endpoint detected an error in the payload of a SETTINGS frame.
460
- SettingsError = 0x109,
476
+ SettingsError = 0x109,
461
477
  /// No SETTINGS frame was received at the beginning of the control stream.
462
- MissingSettings = 0x10a,
478
+ MissingSettings = 0x10a,
463
479
  /// A server rejected a request without performing any application
464
480
  /// processing.
465
- RequestRejected = 0x10b,
481
+ RequestRejected = 0x10b,
466
482
  /// The request or its response (including pushed response) is cancelled.
467
- RequestCancelled = 0x10c,
483
+ RequestCancelled = 0x10c,
468
484
  /// The client's stream terminated without containing a fully formed
469
485
  /// request.
470
- RequestIncomplete = 0x10d,
486
+ RequestIncomplete = 0x10d,
471
487
  /// An HTTP message was malformed and cannot be processed.
472
- MessageError = 0x10e,
488
+ MessageError = 0x10e,
473
489
  /// The TCP connection established in response to a CONNECT request was
474
490
  /// reset or abnormally closed.
475
- ConnectError = 0x10f,
491
+ ConnectError = 0x10f,
476
492
  /// The requested operation cannot be served over HTTP/3. The peer should
477
493
  /// retry over HTTP/1.1.
478
- VersionFallback = 0x110,
494
+ VersionFallback = 0x110,
479
495
  }
480
496
 
481
497
  impl Error {
@@ -483,10 +499,8 @@ impl Error {
483
499
  match self {
484
500
  Error::Done => WireErrorCode::NoError as u64,
485
501
  Error::InternalError => WireErrorCode::InternalError as u64,
486
- Error::StreamCreationError =>
487
- WireErrorCode::StreamCreationError as u64,
488
- Error::ClosedCriticalStream =>
489
- WireErrorCode::ClosedCriticalStream as u64,
502
+ Error::StreamCreationError => WireErrorCode::StreamCreationError as u64,
503
+ Error::ClosedCriticalStream => WireErrorCode::ClosedCriticalStream as u64,
490
504
  Error::FrameUnexpected => WireErrorCode::FrameUnexpected as u64,
491
505
  Error::FrameError => WireErrorCode::FrameError as u64,
492
506
  Error::ExcessiveLoad => WireErrorCode::ExcessiveLoad as u64,
@@ -571,26 +585,36 @@ pub struct Config {
571
585
  /// additional settings are settings that are not part of the H3
572
586
  /// settings explicitly handled above
573
587
  additional_settings: Option<Vec<(u64, u64)>>,
588
+
589
+ max_priority_update_size: u64,
574
590
  }
575
591
 
576
592
  impl Config {
577
593
  /// Creates a new configuration object with default settings.
578
594
  pub const fn new() -> Result<Config> {
579
595
  Ok(Config {
580
- max_field_section_size: None,
596
+ max_field_section_size: Some(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
581
597
  qpack_max_table_capacity: None,
582
598
  qpack_blocked_streams: None,
583
599
  connect_protocol_enabled: None,
584
600
  additional_settings: None,
601
+ max_priority_update_size: PRIORITY_UPDATE_FRAME_PAYLOAD_MAX_SIZE_DEFAULT,
585
602
  })
586
603
  }
587
604
 
588
605
  /// Sets the `SETTINGS_MAX_FIELD_SECTION_SIZE` setting.
589
606
  ///
590
- /// By default no limit is enforced. When a request whose headers exceed
591
- /// the limit set by the application is received, the call to the [`poll()`]
592
- /// method will return the [`Error::ExcessiveLoad`] error, and the
593
- /// connection will be closed.
607
+ /// The default is [`SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT`]. The value is
608
+ /// measured in units of bytes, it is used as a limit when parsing frames
609
+ /// that contain encoded HTTP headers when [`poll()`] is called. A first
610
+ /// check is applied when handling HEADERS and PUSH_PROMISE frames
611
+ /// themselves, with some margin allowed on top of the provided size. A
612
+ /// second check is applied when decoding QPACK, implementing the rules
613
+ /// described in <https://datatracker.ietf.org/doc/html/rfc9114#section-4.2.2>.
614
+ ///
615
+ /// When headers exceed the limit set by the application, the call to the
616
+ /// [`poll()`] method will return the [`Error::ExcessiveLoad`] error, and
617
+ /// the connection will be closed.
594
618
  ///
595
619
  /// [`poll()`]: struct.Connection.html#method.poll
596
620
  /// [`Error::ExcessiveLoad`]: enum.Error.html#variant.ExcessiveLoad
@@ -642,9 +666,7 @@ impl Config {
642
666
  /// the method will return the [`Error::SettingsError`] error.
643
667
  ///
644
668
  /// [`Error::SettingsError`]: enum.Error.html#variant.SettingsError
645
- pub fn set_additional_settings(
646
- &mut self, additional_settings: Vec<(u64, u64)>,
647
- ) -> Result<()> {
669
+ pub fn set_additional_settings(&mut self, additional_settings: Vec<(u64, u64)>) -> Result<()> {
648
670
  let explicit_quiche_settings = HashSet::from([
649
671
  frame::SETTINGS_QPACK_MAX_TABLE_CAPACITY,
650
672
  frame::SETTINGS_MAX_FIELD_SECTION_SIZE,
@@ -657,14 +679,29 @@ impl Config {
657
679
  let dedup_settings: HashSet<u64> =
658
680
  additional_settings.iter().map(|(key, _)| *key).collect();
659
681
 
660
- if dedup_settings.len() != additional_settings.len() ||
661
- !explicit_quiche_settings.is_disjoint(&dedup_settings)
682
+ if dedup_settings.len() != additional_settings.len()
683
+ || !explicit_quiche_settings.is_disjoint(&dedup_settings)
662
684
  {
663
685
  return Err(Error::SettingsError);
664
686
  }
665
687
  self.additional_settings = Some(additional_settings);
666
688
  Ok(())
667
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
+ }
668
705
  }
669
706
 
670
707
  /// A trait for types with associated string name and value.
@@ -857,7 +894,7 @@ impl TryFrom<&[u8]> for Priority {
857
894
  ///
858
895
  /// [Extensible Priorities]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
859
896
  fn try_from(value: &[u8]) -> std::result::Result<Self, Self::Error> {
860
- let dict = match sfv::Parser::parse_dictionary(value) {
897
+ let dict = match sfv::Parser::new(value).parse::<sfv::Dictionary>() {
861
898
  Ok(v) => v,
862
899
 
863
900
  Err(_) => return Err(Error::Done),
@@ -869,17 +906,16 @@ impl TryFrom<&[u8]> for Priority {
869
906
  // (0 through 7), that's an error so set it to the upper
870
907
  // bound (lowest priority) to avoid interference with
871
908
  // other streams.
872
- Some(sfv::ListEntry::Item(item)) => match item.bare_item.as_int() {
909
+ Some(sfv::ListEntry::Item(item)) => match item.bare_item.as_integer().map(i64::from) {
873
910
  Some(v) => {
874
- if !(PRIORITY_URGENCY_LOWER_BOUND as i64..=
875
- PRIORITY_URGENCY_UPPER_BOUND as i64)
911
+ if !(PRIORITY_URGENCY_LOWER_BOUND as i64..=PRIORITY_URGENCY_UPPER_BOUND as i64)
876
912
  .contains(&v)
877
913
  {
878
914
  PRIORITY_URGENCY_UPPER_BOUND
879
915
  } else {
880
916
  v as u8
881
917
  }
882
- },
918
+ }
883
919
 
884
920
  None => return Err(Error::Done),
885
921
  },
@@ -891,8 +927,7 @@ impl TryFrom<&[u8]> for Priority {
891
927
  };
892
928
 
893
929
  let incremental = match dict.get("i") {
894
- Some(sfv::ListEntry::Item(item)) =>
895
- item.bare_item.as_bool().ok_or(Error::Done)?,
930
+ Some(sfv::ListEntry::Item(item)) => item.bare_item.as_boolean().ok_or(Error::Done)?,
896
931
 
897
932
  // Omitted so use default value.
898
933
  _ => false,
@@ -934,9 +969,7 @@ pub struct Stats {
934
969
  pub qpack_decoder_stream_recv_bytes: u64,
935
970
  }
936
971
 
937
- fn close_conn_critical_stream<F: BufFactory>(
938
- conn: &mut super::Connection<F>,
939
- ) -> Result<()> {
972
+ fn close_conn_critical_stream<F: BufFactory>(conn: &mut super::Connection<F>) -> Result<()> {
940
973
  conn.close(
941
974
  true,
942
975
  Error::ClosedCriticalStream.to_wire(),
@@ -947,7 +980,8 @@ fn close_conn_critical_stream<F: BufFactory>(
947
980
  }
948
981
 
949
982
  fn close_conn_if_critical_stream_finished<F: BufFactory>(
950
- conn: &mut super::Connection<F>, stream_id: u64,
983
+ conn: &mut super::Connection<F>,
984
+ stream_id: u64,
951
985
  ) -> Result<()> {
952
986
  if conn.stream_finished(stream_id) {
953
987
  close_conn_critical_stream(conn)?;
@@ -989,12 +1023,12 @@ pub struct Connection {
989
1023
 
990
1024
  local_goaway_id: Option<u64>,
991
1025
  peer_goaway_id: Option<u64>,
1026
+
1027
+ max_priority_update_size: u64,
992
1028
  }
993
1029
 
994
1030
  impl Connection {
995
- fn new(
996
- config: &Config, is_server: bool, enable_dgram: bool,
997
- ) -> Result<Connection> {
1031
+ fn new(config: &Config, is_server: bool, enable_dgram: bool) -> Result<Connection> {
998
1032
  let initial_uni_stream_id = if is_server { 0x3 } else { 0x2 };
999
1033
  let h3_datagram = if enable_dgram { Some(1) } else { None };
1000
1034
 
@@ -1044,6 +1078,8 @@ impl Connection {
1044
1078
 
1045
1079
  local_goaway_id: None,
1046
1080
  peer_goaway_id: None,
1081
+
1082
+ max_priority_update_size: config.max_priority_update_size,
1047
1083
  })
1048
1084
  }
1049
1085
 
@@ -1064,7 +1100,8 @@ impl Connection {
1064
1100
  /// [`StreamLimit`]: ../enum.Error.html#variant.StreamLimit
1065
1101
  /// [`InternalError`]: ../enum.Error.html#variant.InternalError
1066
1102
  pub fn with_transport<F: BufFactory>(
1067
- conn: &mut super::Connection<F>, config: &Config,
1103
+ conn: &mut super::Connection<F>,
1104
+ config: &Config,
1068
1105
  ) -> Result<Connection> {
1069
1106
  let is_client = !conn.is_server;
1070
1107
  if is_client && !(conn.is_established() || conn.is_in_early_data()) {
@@ -1072,8 +1109,7 @@ impl Connection {
1072
1109
  return Err(Error::InternalError);
1073
1110
  }
1074
1111
 
1075
- let mut http3_conn =
1076
- Connection::new(config, conn.is_server, conn.dgram_enabled())?;
1112
+ let mut http3_conn = Connection::new(config, conn.is_server, conn.dgram_enabled())?;
1077
1113
 
1078
1114
  match http3_conn.send_settings(conn) {
1079
1115
  Ok(_) => (),
@@ -1081,7 +1117,7 @@ impl Connection {
1081
1117
  Err(e) => {
1082
1118
  conn.close(true, e.to_wire(), b"Error opening control stream")?;
1083
1119
  return Err(e);
1084
- },
1120
+ }
1085
1121
  };
1086
1122
 
1087
1123
  // Try opening QPACK streams, but ignore errors if it fails since we
@@ -1117,7 +1153,10 @@ impl Connection {
1117
1153
  /// [`send_body()`]: struct.Connection.html#method.send_body
1118
1154
  /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1119
1155
  pub fn send_request<T: NameValue, F: BufFactory>(
1120
- &mut self, conn: &mut super::Connection<F>, headers: &[T], fin: bool,
1156
+ &mut self,
1157
+ conn: &mut super::Connection<F>,
1158
+ headers: &[T],
1159
+ fin: bool,
1121
1160
  ) -> Result<u64> {
1122
1161
  // If we received a GOAWAY from the peer, MUST NOT initiate new
1123
1162
  // requests.
@@ -1127,8 +1166,17 @@ impl Connection {
1127
1166
 
1128
1167
  let stream_id = self.next_request_stream_id;
1129
1168
 
1130
- self.streams
1131
- .insert(stream_id, <stream::Stream>::new(stream_id, true));
1169
+ self.streams.insert(
1170
+ stream_id,
1171
+ <stream::Stream>::new(
1172
+ stream_id,
1173
+ true,
1174
+ self.local_settings
1175
+ .max_field_section_size
1176
+ .unwrap_or(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
1177
+ self.max_priority_update_size,
1178
+ ),
1179
+ );
1132
1180
 
1133
1181
  // The underlying QUIC stream does not exist yet, so calls to e.g.
1134
1182
  // stream_capacity() will fail. By writing a 0-length buffer, we force
@@ -1205,14 +1253,15 @@ impl Connection {
1205
1253
  /// [`FrameUnexpected`]: enum.Error.html#variant.FrameUnexpected
1206
1254
  /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1207
1255
  pub fn send_response<T: NameValue, F: BufFactory>(
1208
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1209
- headers: &[T], fin: bool,
1256
+ &mut self,
1257
+ conn: &mut super::Connection<F>,
1258
+ stream_id: u64,
1259
+ headers: &[T],
1260
+ fin: bool,
1210
1261
  ) -> Result<()> {
1211
1262
  let priority = Default::default();
1212
1263
 
1213
- self.send_response_with_priority(
1214
- conn, stream_id, headers, &priority, fin,
1215
- )?;
1264
+ self.send_response_with_priority(conn, stream_id, headers, &priority, fin)?;
1216
1265
 
1217
1266
  Ok(())
1218
1267
  }
@@ -1261,8 +1310,12 @@ impl Connection {
1261
1310
  /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1262
1311
  /// [Extensible Priority]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
1263
1312
  pub fn send_response_with_priority<T: NameValue, F: BufFactory>(
1264
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1265
- headers: &[T], priority: &Priority, fin: bool,
1313
+ &mut self,
1314
+ conn: &mut super::Connection<F>,
1315
+ stream_id: u64,
1316
+ headers: &[T],
1317
+ priority: &Priority,
1318
+ fin: bool,
1266
1319
  ) -> Result<()> {
1267
1320
  match self.streams.get(&stream_id) {
1268
1321
  Some(s) => {
@@ -1272,7 +1325,7 @@ impl Connection {
1272
1325
  }
1273
1326
 
1274
1327
  s
1275
- },
1328
+ }
1276
1329
 
1277
1330
  None => return Err(Error::FrameUnexpected),
1278
1331
  };
@@ -1282,8 +1335,8 @@ impl Connection {
1282
1335
  // Clamp and shift urgency into quiche-priority space
1283
1336
  let urgency = priority
1284
1337
  .urgency
1285
- .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND) +
1286
- PRIORITY_URGENCY_OFFSET;
1338
+ .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND)
1339
+ + PRIORITY_URGENCY_OFFSET;
1287
1340
 
1288
1341
  conn.stream_priority(stream_id, urgency, priority.incremental)?;
1289
1342
 
@@ -1317,8 +1370,12 @@ impl Connection {
1317
1370
  /// [Section 4.1 of RFC 9114]:
1318
1371
  /// https://www.rfc-editor.org/rfc/rfc9114.html#section-4.1.
1319
1372
  pub fn send_additional_headers<T: NameValue, F: BufFactory>(
1320
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1321
- headers: &[T], is_trailer_section: bool, fin: bool,
1373
+ &mut self,
1374
+ conn: &mut super::Connection<F>,
1375
+ stream_id: u64,
1376
+ headers: &[T],
1377
+ is_trailer_section: bool,
1378
+ fin: bool,
1322
1379
  ) -> Result<()> {
1323
1380
  // Clients can only send trailer headers.
1324
1381
  if !self.is_server && !is_trailer_section {
@@ -1338,7 +1395,7 @@ impl Connection {
1338
1395
  }
1339
1396
 
1340
1397
  s
1341
- },
1398
+ }
1342
1399
 
1343
1400
  None => return Err(Error::FrameUnexpected),
1344
1401
  };
@@ -1388,31 +1445,28 @@ impl Connection {
1388
1445
  /// https://www.rfc-editor.org/rfc/rfc9114.html#section-4.1.
1389
1446
  /// [Extensible Priority]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
1390
1447
  pub fn send_additional_headers_with_priority<T: NameValue, F: BufFactory>(
1391
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1392
- headers: &[T], priority: &Priority, is_trailer_section: bool, fin: bool,
1448
+ &mut self,
1449
+ conn: &mut super::Connection<F>,
1450
+ stream_id: u64,
1451
+ headers: &[T],
1452
+ priority: &Priority,
1453
+ is_trailer_section: bool,
1454
+ fin: bool,
1393
1455
  ) -> Result<()> {
1394
- self.send_additional_headers(
1395
- conn,
1396
- stream_id,
1397
- headers,
1398
- is_trailer_section,
1399
- fin,
1400
- )?;
1456
+ self.send_additional_headers(conn, stream_id, headers, is_trailer_section, fin)?;
1401
1457
 
1402
1458
  // Clamp and shift urgency into quiche-priority space
1403
1459
  let urgency = priority
1404
1460
  .urgency
1405
- .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND) +
1406
- PRIORITY_URGENCY_OFFSET;
1461
+ .clamp(PRIORITY_URGENCY_LOWER_BOUND, PRIORITY_URGENCY_UPPER_BOUND)
1462
+ + PRIORITY_URGENCY_OFFSET;
1407
1463
 
1408
1464
  conn.stream_priority(stream_id, urgency, priority.incremental)?;
1409
1465
 
1410
1466
  Ok(())
1411
1467
  }
1412
1468
 
1413
- fn encode_header_block<T: NameValue>(
1414
- &mut self, headers: &[T],
1415
- ) -> Result<Vec<u8>> {
1469
+ fn encode_header_block<T: NameValue>(&mut self, headers: &[T]) -> Result<Vec<u8>> {
1416
1470
  let headers_len = headers
1417
1471
  .iter()
1418
1472
  .fold(0, |acc, h| acc + h.value().len() + h.name().len() + 32);
@@ -1429,8 +1483,11 @@ impl Connection {
1429
1483
  }
1430
1484
 
1431
1485
  fn send_headers<T: NameValue, F: BufFactory>(
1432
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1433
- headers: &[T], fin: bool,
1486
+ &mut self,
1487
+ conn: &mut super::Connection<F>,
1488
+ stream_id: u64,
1489
+ headers: &[T],
1490
+ fin: bool,
1434
1491
  ) -> Result<()> {
1435
1492
  let mut d = [42; 10];
1436
1493
  let mut b = octets::OctetsMut::with_slice(&mut d);
@@ -1442,8 +1499,8 @@ impl Connection {
1442
1499
 
1443
1500
  let header_block = self.encode_header_block(headers)?;
1444
1501
 
1445
- let overhead = octets::varint_len(frame::HEADERS_FRAME_TYPE_ID) +
1446
- octets::varint_len(header_block.len() as u64);
1502
+ let overhead = octets::varint_len(frame::HEADERS_FRAME_TYPE_ID)
1503
+ + octets::varint_len(header_block.len() as u64);
1447
1504
 
1448
1505
  // Headers need to be sent atomically, so make sure the stream has
1449
1506
  // enough capacity.
@@ -1458,7 +1515,7 @@ impl Connection {
1458
1515
  }
1459
1516
 
1460
1517
  return Err(e.into());
1461
- },
1518
+ }
1462
1519
  };
1463
1520
 
1464
1521
  b.put_varint(frame::HEADERS_FRAME_TYPE_ID)?;
@@ -1526,7 +1583,10 @@ impl Connection {
1526
1583
  ///
1527
1584
  /// [`Done`]: enum.Error.html#variant.Done
1528
1585
  pub fn send_body<F: BufFactory>(
1529
- &mut self, conn: &mut super::Connection<F>, stream_id: u64, body: &[u8],
1586
+ &mut self,
1587
+ conn: &mut super::Connection<F>,
1588
+ stream_id: u64,
1589
+ body: &[u8],
1530
1590
  fin: bool,
1531
1591
  ) -> Result<usize> {
1532
1592
  self.do_send_body(
@@ -1567,8 +1627,11 @@ impl Connection {
1567
1627
  ///
1568
1628
  /// [`Done`]: enum.Error.html#variant.Done
1569
1629
  pub fn send_body_zc<F>(
1570
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1571
- body: &mut F::Buf, fin: bool,
1630
+ &mut self,
1631
+ conn: &mut super::Connection<F>,
1632
+ stream_id: u64,
1633
+ body: &mut F::Buf,
1634
+ fin: bool,
1572
1635
  ) -> Result<usize>
1573
1636
  where
1574
1637
  F: BufFactory,
@@ -1597,13 +1660,10 @@ impl Connection {
1597
1660
  // buffer
1598
1661
  debug_assert_eq!(body.as_ref().len(), body_len);
1599
1662
 
1600
- let (mut n, rem) =
1601
- conn.stream_send_zc(stream_id, body.clone(), fin)?;
1663
+ let (mut n, rem) = conn.stream_send_zc(stream_id, body.clone(), fin)?;
1602
1664
  if rem.as_ref().is_some_and(|v| !v.as_ref().is_empty()) {
1603
- // `rem` should always be None or empty.
1604
- // `do_send_body()` should have checked the capacity and
1605
- // ensured that there is enough capacity to write the header +
1606
- // fully body.
1665
+ // `do_send_body()` checked capacity before this write, so
1666
+ // `rem` should always be `None` or empty.
1607
1667
  debug_assert!(false);
1608
1668
  return Err(Error::InternalError);
1609
1669
  }
@@ -1622,20 +1682,17 @@ impl Connection {
1622
1682
  }
1623
1683
 
1624
1684
  fn do_send_body<F, B, R, SND>(
1625
- &mut self, conn: &mut super::Connection<F>, stream_id: u64, body: B,
1626
- fin: bool, write_fn: SND,
1685
+ &mut self,
1686
+ conn: &mut super::Connection<F>,
1687
+ stream_id: u64,
1688
+ body: B,
1689
+ fin: bool,
1690
+ write_fn: SND,
1627
1691
  ) -> Result<R>
1628
1692
  where
1629
1693
  F: BufFactory,
1630
1694
  B: AsRef<[u8]>,
1631
- SND: FnOnce(
1632
- &mut super::Connection<F>,
1633
- &[u8],
1634
- u64,
1635
- B,
1636
- usize,
1637
- bool,
1638
- ) -> Result<(usize, R)>,
1695
+ SND: FnOnce(&mut super::Connection<F>, &[u8], u64, B, usize, bool) -> Result<(usize, R)>,
1639
1696
  {
1640
1697
  let mut d = [42; 10];
1641
1698
  let mut b = octets::OctetsMut::with_slice(&mut d);
@@ -1656,11 +1713,11 @@ impl Connection {
1656
1713
  if s.trailers_sent() {
1657
1714
  return Err(Error::FrameUnexpected);
1658
1715
  }
1659
- },
1716
+ }
1660
1717
 
1661
1718
  None => {
1662
1719
  return Err(Error::FrameUnexpected);
1663
- },
1720
+ }
1664
1721
  };
1665
1722
 
1666
1723
  // Avoid sending 0-length DATA frames when the fin flag is false.
@@ -1668,8 +1725,8 @@ impl Connection {
1668
1725
  return Err(Error::Done);
1669
1726
  }
1670
1727
 
1671
- let overhead = octets::varint_len(frame::DATA_FRAME_TYPE_ID) +
1672
- octets::varint_len(len as u64);
1728
+ let overhead =
1729
+ octets::varint_len(frame::DATA_FRAME_TYPE_ID) + octets::varint_len(len as u64);
1673
1730
 
1674
1731
  let stream_cap = match conn.stream_capacity(stream_id) {
1675
1732
  Ok(v) => v,
@@ -1680,7 +1737,7 @@ impl Connection {
1680
1737
  }
1681
1738
 
1682
1739
  return Err(e.into());
1683
- },
1740
+ }
1684
1741
  };
1685
1742
 
1686
1743
  // Make sure there is enough capacity to send the DATA frame header.
@@ -1708,8 +1765,7 @@ impl Connection {
1708
1765
 
1709
1766
  // Return how many bytes were written, excluding the frame header.
1710
1767
  // Sending body separately avoids unnecessary copy.
1711
- let (written, ret) =
1712
- write_fn(conn, &d[..off], stream_id, body, body_len, fin)?;
1768
+ let (written, ret) = write_fn(conn, &d[..off], stream_id, body, body_len, fin)?;
1713
1769
  if written != body_len {
1714
1770
  // This should never happen. If it does, it means we wrote an
1715
1771
  // incorrect frame length and thus we can't really continue.
@@ -1761,11 +1817,8 @@ impl Connection {
1761
1817
  /// method.
1762
1818
  ///
1763
1819
  /// [`poll()`]: struct.Connection.html#method.poll
1764
- pub fn dgram_enabled_by_peer<F: BufFactory>(
1765
- &self, conn: &super::Connection<F>,
1766
- ) -> bool {
1767
- self.peer_settings.h3_datagram == Some(1) &&
1768
- conn.dgram_max_writable_len().is_some()
1820
+ pub fn dgram_enabled_by_peer<F: BufFactory>(&self, conn: &super::Connection<F>) -> bool {
1821
+ self.peer_settings.h3_datagram == Some(1) && conn.dgram_max_writable_len().is_some()
1769
1822
  }
1770
1823
 
1771
1824
  /// Returns whether the peer enabled extended CONNECT support.
@@ -1791,7 +1844,9 @@ impl Connection {
1791
1844
  /// [`Data`]: enum.Event.html#variant.Data
1792
1845
  /// [`Done`]: enum.Error.html#variant.Done
1793
1846
  pub fn recv_body<F: BufFactory>(
1794
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1847
+ &mut self,
1848
+ conn: &mut super::Connection<F>,
1849
+ stream_id: u64,
1795
1850
  out: &mut [u8],
1796
1851
  ) -> Result<usize> {
1797
1852
  self.recv_body_buf(conn, stream_id, out)
@@ -1834,7 +1889,10 @@ impl Connection {
1834
1889
  /// }
1835
1890
  /// ```
1836
1891
  pub fn recv_body_buf<F: BufFactory, OUT: bytes::BufMut>(
1837
- &mut self, conn: &mut super::Connection<F>, stream_id: u64, mut out: OUT,
1892
+ &mut self,
1893
+ conn: &mut super::Connection<F>,
1894
+ stream_id: u64,
1895
+ mut out: OUT,
1838
1896
  ) -> Result<usize> {
1839
1897
  let mut total = 0;
1840
1898
 
@@ -1855,6 +1913,14 @@ impl Connection {
1855
1913
 
1856
1914
  Err(Error::Done) => break,
1857
1915
 
1916
+ // The reset is consumed here, so the stream will not become
1917
+ // readable again for poll() to handle it.
1918
+ Err(e @ Error::TransportError(crate::Error::StreamReset(_))) => {
1919
+ self.process_reset_stream(stream_id);
1920
+
1921
+ return Err(e);
1922
+ }
1923
+
1858
1924
  Err(e) => return Err(e),
1859
1925
  };
1860
1926
 
@@ -1910,7 +1976,9 @@ impl Connection {
1910
1976
  /// [`StreamBlocked`]: enum.Error.html#variant.StreamBlocked
1911
1977
  /// [Extensible Priority]: https://www.rfc-editor.org/rfc/rfc9218.html#section-4.
1912
1978
  pub fn send_priority_update_for_request<F: BufFactory>(
1913
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
1979
+ &mut self,
1980
+ conn: &mut super::Connection<F>,
1981
+ stream_id: u64,
1914
1982
  priority: &Priority,
1915
1983
  ) -> Result<()> {
1916
1984
  let mut d = [42; 20];
@@ -1925,8 +1993,7 @@ impl Connection {
1925
1993
  return Err(Error::FrameUnexpected);
1926
1994
  }
1927
1995
 
1928
- let control_stream_id =
1929
- self.control_stream_id.ok_or(Error::FrameUnexpected)?;
1996
+ let control_stream_id = self.control_stream_id.ok_or(Error::FrameUnexpected)?;
1930
1997
 
1931
1998
  let urgency = priority
1932
1999
  .urgency
@@ -1939,26 +2006,21 @@ impl Connection {
1939
2006
  }
1940
2007
 
1941
2008
  let priority_field_value = field_value.as_bytes();
1942
- let frame_payload_len =
1943
- octets::varint_len(stream_id) + priority_field_value.len();
2009
+ let frame_payload_len = octets::varint_len(stream_id) + priority_field_value.len();
1944
2010
 
1945
- let overhead =
1946
- octets::varint_len(frame::PRIORITY_UPDATE_FRAME_REQUEST_TYPE_ID) +
1947
- octets::varint_len(stream_id) +
1948
- octets::varint_len(frame_payload_len as u64);
2011
+ let overhead = octets::varint_len(frame::PRIORITY_UPDATE_FRAME_REQUEST_TYPE_ID)
2012
+ + octets::varint_len(stream_id)
2013
+ + octets::varint_len(frame_payload_len as u64);
1949
2014
 
1950
2015
  // Make sure the control stream has enough capacity.
1951
- match conn.stream_writable(
1952
- control_stream_id,
1953
- overhead + priority_field_value.len(),
1954
- ) {
2016
+ match conn.stream_writable(control_stream_id, overhead + priority_field_value.len()) {
1955
2017
  Ok(true) => (),
1956
2018
 
1957
2019
  Ok(false) => return Err(Error::StreamBlocked),
1958
2020
 
1959
2021
  Err(e) => {
1960
2022
  return Err(e.into());
1961
- },
2023
+ }
1962
2024
  }
1963
2025
 
1964
2026
  b.put_varint(frame::PRIORITY_UPDATE_FRAME_REQUEST_TYPE_ID)?;
@@ -2012,9 +2074,7 @@ impl Connection {
2012
2074
  /// [`poll()`]: struct.Connection.html#method.poll
2013
2075
  /// [`PriorityUpdate`]: enum.Event.html#variant.PriorityUpdate
2014
2076
  /// [`Done`]: enum.Error.html#variant.Done
2015
- pub fn take_last_priority_update(
2016
- &mut self, prioritized_element_id: u64,
2017
- ) -> Result<Vec<u8>> {
2077
+ pub fn take_last_priority_update(&mut self, prioritized_element_id: u64) -> Result<Vec<u8>> {
2018
2078
  if let Some(stream) = self.streams.get_mut(&prioritized_element_id) {
2019
2079
  return stream.take_last_priority_update().ok_or(Error::Done);
2020
2080
  }
@@ -2059,9 +2119,7 @@ impl Connection {
2059
2119
  /// [`recv_dgram()`]: struct.Connection.html#method.recv_dgram
2060
2120
  /// [`take_last_priority_update()`]: struct.Connection.html#method.take_last_priority_update
2061
2121
  /// [`close()`]: ../struct.Connection.html#method.close
2062
- pub fn poll<F: BufFactory>(
2063
- &mut self, conn: &mut super::Connection<F>,
2064
- ) -> Result<(u64, Event)> {
2122
+ pub fn poll<F: BufFactory>(&mut self, conn: &mut super::Connection<F>) -> Result<(u64, Event)> {
2065
2123
  // When connection close is initiated by the local application (e.g. due
2066
2124
  // to a protocol error), the connection itself might be in a broken
2067
2125
  // state, so return early.
@@ -2117,10 +2175,10 @@ impl Connection {
2117
2175
  // Return early if the stream was reset, to avoid returning
2118
2176
  // a Finished event later as well.
2119
2177
  Err(Error::TransportError(crate::Error::StreamReset(e))) => {
2120
- self.remove_local_finished_stream(s);
2178
+ self.process_reset_stream(s);
2121
2179
 
2122
2180
  return Ok((s, Event::Reset(e)));
2123
- },
2181
+ }
2124
2182
 
2125
2183
  Err(e) => return Err(e),
2126
2184
  };
@@ -2157,7 +2215,9 @@ impl Connection {
2157
2215
  ///
2158
2216
  /// [`close()`]: ../struct.Connection.html#method.close
2159
2217
  pub fn send_goaway<F: BufFactory>(
2160
- &mut self, conn: &mut super::Connection<F>, id: u64,
2218
+ &mut self,
2219
+ conn: &mut super::Connection<F>,
2220
+ id: u64,
2161
2221
  ) -> Result<()> {
2162
2222
  let mut id = id;
2163
2223
 
@@ -2221,7 +2281,9 @@ impl Connection {
2221
2281
  }
2222
2282
 
2223
2283
  fn open_uni_stream<F: BufFactory>(
2224
- &mut self, conn: &mut super::Connection<F>, ty: u64,
2284
+ &mut self,
2285
+ conn: &mut super::Connection<F>,
2286
+ ty: u64,
2225
2287
  ) -> Result<u64> {
2226
2288
  let stream_id = self.next_uni_stream_id;
2227
2289
 
@@ -2230,11 +2292,11 @@ impl Connection {
2230
2292
 
2231
2293
  match ty {
2232
2294
  // Control and QPACK streams are the most important to schedule.
2233
- stream::HTTP3_CONTROL_STREAM_TYPE_ID |
2234
- stream::QPACK_ENCODER_STREAM_TYPE_ID |
2235
- stream::QPACK_DECODER_STREAM_TYPE_ID => {
2295
+ stream::HTTP3_CONTROL_STREAM_TYPE_ID
2296
+ | stream::QPACK_ENCODER_STREAM_TYPE_ID
2297
+ | stream::QPACK_DECODER_STREAM_TYPE_ID => {
2236
2298
  conn.stream_priority(stream_id, 0, false)?;
2237
- },
2299
+ }
2238
2300
 
2239
2301
  // TODO: Server push
2240
2302
  stream::HTTP3_PUSH_STREAM_TYPE_ID => (),
@@ -2242,7 +2304,7 @@ impl Connection {
2242
2304
  // Anything else is a GREASE stream, so make it the least important.
2243
2305
  _ => {
2244
2306
  conn.stream_priority(stream_id, 255, false)?;
2245
- },
2307
+ }
2246
2308
  }
2247
2309
 
2248
2310
  conn.stream_send(stream_id, b.put_varint(ty)?, false)?;
@@ -2258,10 +2320,10 @@ impl Connection {
2258
2320
  }
2259
2321
 
2260
2322
  fn open_qpack_encoder_stream<F: BufFactory>(
2261
- &mut self, conn: &mut super::Connection<F>,
2323
+ &mut self,
2324
+ conn: &mut super::Connection<F>,
2262
2325
  ) -> Result<()> {
2263
- let stream_id =
2264
- self.open_uni_stream(conn, stream::QPACK_ENCODER_STREAM_TYPE_ID)?;
2326
+ let stream_id = self.open_uni_stream(conn, stream::QPACK_ENCODER_STREAM_TYPE_ID)?;
2265
2327
 
2266
2328
  self.local_qpack_streams.encoder_stream_id = Some(stream_id);
2267
2329
 
@@ -2280,10 +2342,10 @@ impl Connection {
2280
2342
  }
2281
2343
 
2282
2344
  fn open_qpack_decoder_stream<F: BufFactory>(
2283
- &mut self, conn: &mut super::Connection<F>,
2345
+ &mut self,
2346
+ conn: &mut super::Connection<F>,
2284
2347
  ) -> Result<()> {
2285
- let stream_id =
2286
- self.open_uni_stream(conn, stream::QPACK_DECODER_STREAM_TYPE_ID)?;
2348
+ let stream_id = self.open_uni_stream(conn, stream::QPACK_DECODER_STREAM_TYPE_ID)?;
2287
2349
 
2288
2350
  self.local_qpack_streams.decoder_stream_id = Some(stream_id);
2289
2351
 
@@ -2303,7 +2365,9 @@ impl Connection {
2303
2365
 
2304
2366
  /// Send GREASE frames on the provided stream ID.
2305
2367
  fn send_grease_frames<F: BufFactory>(
2306
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
2368
+ &mut self,
2369
+ conn: &mut super::Connection<F>,
2370
+ stream_id: u64,
2307
2371
  ) -> Result<()> {
2308
2372
  let mut d = [0; 8];
2309
2373
 
@@ -2316,7 +2380,7 @@ impl Connection {
2316
2380
  }
2317
2381
 
2318
2382
  return Err(e.into());
2319
- },
2383
+ }
2320
2384
  };
2321
2385
 
2322
2386
  let grease_frame1 = grease_value();
@@ -2399,9 +2463,7 @@ impl Connection {
2399
2463
 
2400
2464
  /// Opens a new unidirectional stream with a GREASE type and sends some
2401
2465
  /// unframed payload.
2402
- fn open_grease_stream<F: BufFactory>(
2403
- &mut self, conn: &mut super::Connection<F>,
2404
- ) -> Result<()> {
2466
+ fn open_grease_stream<F: BufFactory>(&mut self, conn: &mut super::Connection<F>) -> Result<()> {
2405
2467
  let ty = grease_value();
2406
2468
  match self.open_uni_stream(conn, ty) {
2407
2469
  Ok(stream_id) => {
@@ -2420,13 +2482,13 @@ impl Connection {
2420
2482
 
2421
2483
  q.add_event_data_now(ev_data).ok();
2422
2484
  });
2423
- },
2485
+ }
2424
2486
 
2425
2487
  Err(Error::IdError) => {
2426
2488
  trace!("{} GREASE stream blocked", conn.trace_id(),);
2427
2489
 
2428
2490
  return Ok(());
2429
- },
2491
+ }
2430
2492
 
2431
2493
  Err(e) => return Err(e),
2432
2494
  };
@@ -2435,12 +2497,8 @@ impl Connection {
2435
2497
  }
2436
2498
 
2437
2499
  /// Sends SETTINGS frame based on HTTP/3 configuration.
2438
- fn send_settings<F: BufFactory>(
2439
- &mut self, conn: &mut super::Connection<F>,
2440
- ) -> Result<()> {
2441
- let stream_id = match self
2442
- .open_uni_stream(conn, stream::HTTP3_CONTROL_STREAM_TYPE_ID)
2443
- {
2500
+ fn send_settings<F: BufFactory>(&mut self, conn: &mut super::Connection<F>) -> Result<()> {
2501
+ let stream_id = match self.open_uni_stream(conn, stream::HTTP3_CONTROL_STREAM_TYPE_ID) {
2444
2502
  Ok(v) => v,
2445
2503
 
2446
2504
  Err(e) => {
@@ -2451,7 +2509,7 @@ impl Connection {
2451
2509
  }
2452
2510
 
2453
2511
  return Err(e);
2454
- },
2512
+ }
2455
2513
  };
2456
2514
 
2457
2515
  self.control_stream_id = Some(stream_id);
@@ -2475,13 +2533,9 @@ impl Connection {
2475
2533
 
2476
2534
  let frame = frame::Frame::Settings {
2477
2535
  max_field_section_size: self.local_settings.max_field_section_size,
2478
- qpack_max_table_capacity: self
2479
- .local_settings
2480
- .qpack_max_table_capacity,
2536
+ qpack_max_table_capacity: self.local_settings.qpack_max_table_capacity,
2481
2537
  qpack_blocked_streams: self.local_settings.qpack_blocked_streams,
2482
- connect_protocol_enabled: self
2483
- .local_settings
2484
- .connect_protocol_enabled,
2538
+ connect_protocol_enabled: self.local_settings.connect_protocol_enabled,
2485
2539
  h3_datagram: self.local_settings.h3_datagram,
2486
2540
  grease,
2487
2541
  additional_settings: self.local_settings.additional_settings.clone(),
@@ -2522,7 +2576,9 @@ impl Connection {
2522
2576
  }
2523
2577
 
2524
2578
  fn process_control_stream<F: BufFactory>(
2525
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
2579
+ &mut self,
2580
+ conn: &mut super::Connection<F>,
2581
+ stream_id: u64,
2526
2582
  ) -> Result<(u64, Event)> {
2527
2583
  close_conn_if_critical_stream_finished(conn, stream_id)?;
2528
2584
 
@@ -2544,11 +2600,21 @@ impl Connection {
2544
2600
  }
2545
2601
 
2546
2602
  fn process_readable_stream<F: BufFactory>(
2547
- &mut self, conn: &mut super::Connection<F>, stream_id: u64, polling: bool,
2603
+ &mut self,
2604
+ conn: &mut super::Connection<F>,
2605
+ stream_id: u64,
2606
+ polling: bool,
2548
2607
  ) -> Result<(u64, Event)> {
2549
- self.streams
2550
- .entry(stream_id)
2551
- .or_insert_with(|| <stream::Stream>::new(stream_id, false));
2608
+ self.streams.entry(stream_id).or_insert_with(|| {
2609
+ <stream::Stream>::new(
2610
+ stream_id,
2611
+ false,
2612
+ self.local_settings
2613
+ .max_field_section_size
2614
+ .unwrap_or(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
2615
+ self.max_priority_update_size,
2616
+ )
2617
+ });
2552
2618
 
2553
2619
  // We need to get a fresh reference to the stream for each
2554
2620
  // iteration, to avoid borrowing `self` for the entire duration
@@ -2579,14 +2645,13 @@ impl Connection {
2579
2645
  None
2580
2646
  };
2581
2647
 
2582
- let ev_data =
2583
- EventData::Http3StreamTypeSet(StreamTypeSet {
2584
- stream_id,
2585
- initiator: Some(Initiator::Remote),
2586
- stream_type: ty.to_qlog(),
2587
- stream_type_bytes: ty_val,
2588
- ..Default::default()
2589
- });
2648
+ let ev_data = EventData::Http3StreamTypeSet(StreamTypeSet {
2649
+ stream_id,
2650
+ initiator: Some(Initiator::Remote),
2651
+ stream_type: ty.to_qlog(),
2652
+ stream_type_bytes: ty_val,
2653
+ ..Default::default()
2654
+ });
2590
2655
 
2591
2656
  q.add_event_data_now(ev_data).ok();
2592
2657
  });
@@ -2610,12 +2675,10 @@ impl Connection {
2610
2675
  stream_id
2611
2676
  );
2612
2677
 
2613
- close_conn_if_critical_stream_finished(
2614
- conn, stream_id,
2615
- )?;
2678
+ close_conn_if_critical_stream_finished(conn, stream_id)?;
2616
2679
 
2617
2680
  self.peer_control_stream_id = Some(stream_id);
2618
- },
2681
+ }
2619
2682
 
2620
2683
  stream::Type::Push => {
2621
2684
  // Server push is not supported, so push streams at
@@ -2628,12 +2691,11 @@ impl Connection {
2628
2691
  )?;
2629
2692
 
2630
2693
  return Err(Error::StreamCreationError);
2631
- },
2694
+ }
2632
2695
 
2633
2696
  stream::Type::QpackEncoder => {
2634
2697
  // Only one qpack encoder stream allowed.
2635
- if self.peer_qpack_streams.encoder_stream_id.is_some()
2636
- {
2698
+ if self.peer_qpack_streams.encoder_stream_id.is_some() {
2637
2699
  conn.close(
2638
2700
  true,
2639
2701
  Error::StreamCreationError.to_wire(),
@@ -2643,18 +2705,14 @@ impl Connection {
2643
2705
  return Err(Error::StreamCreationError);
2644
2706
  }
2645
2707
 
2646
- close_conn_if_critical_stream_finished(
2647
- conn, stream_id,
2648
- )?;
2708
+ close_conn_if_critical_stream_finished(conn, stream_id)?;
2649
2709
 
2650
- self.peer_qpack_streams.encoder_stream_id =
2651
- Some(stream_id);
2652
- },
2710
+ self.peer_qpack_streams.encoder_stream_id = Some(stream_id);
2711
+ }
2653
2712
 
2654
2713
  stream::Type::QpackDecoder => {
2655
2714
  // Only one qpack decoder allowed.
2656
- if self.peer_qpack_streams.decoder_stream_id.is_some()
2657
- {
2715
+ if self.peer_qpack_streams.decoder_stream_id.is_some() {
2658
2716
  conn.close(
2659
2717
  true,
2660
2718
  Error::StreamCreationError.to_wire(),
@@ -2664,22 +2722,19 @@ impl Connection {
2664
2722
  return Err(Error::StreamCreationError);
2665
2723
  }
2666
2724
 
2667
- close_conn_if_critical_stream_finished(
2668
- conn, stream_id,
2669
- )?;
2725
+ close_conn_if_critical_stream_finished(conn, stream_id)?;
2670
2726
 
2671
- self.peer_qpack_streams.decoder_stream_id =
2672
- Some(stream_id);
2673
- },
2727
+ self.peer_qpack_streams.decoder_stream_id = Some(stream_id);
2728
+ }
2674
2729
 
2675
2730
  stream::Type::Unknown => {
2676
2731
  // Unknown stream types are ignored.
2677
2732
  // TODO: we MAY send STOP_SENDING
2678
- },
2733
+ }
2679
2734
 
2680
2735
  stream::Type::Request => unreachable!(),
2681
2736
  }
2682
- },
2737
+ }
2683
2738
 
2684
2739
  stream::State::PushId => {
2685
2740
  stream.try_fill_buffer(conn)?;
@@ -2694,7 +2749,7 @@ impl Connection {
2694
2749
  conn.close(true, e.to_wire(), b"")?;
2695
2750
  return Err(e);
2696
2751
  }
2697
- },
2752
+ }
2698
2753
 
2699
2754
  stream::State::FrameType => {
2700
2755
  stream.try_fill_buffer(conn)?;
@@ -2709,28 +2764,20 @@ impl Connection {
2709
2764
  Err(Error::FrameUnexpected) => {
2710
2765
  let msg = format!("Unexpected frame type {varint}");
2711
2766
 
2712
- conn.close(
2713
- true,
2714
- Error::FrameUnexpected.to_wire(),
2715
- msg.as_bytes(),
2716
- )?;
2767
+ conn.close(true, Error::FrameUnexpected.to_wire(), msg.as_bytes())?;
2717
2768
 
2718
2769
  return Err(Error::FrameUnexpected);
2719
- },
2770
+ }
2720
2771
 
2721
2772
  Err(e) => {
2722
- conn.close(
2723
- true,
2724
- e.to_wire(),
2725
- b"Error handling frame.",
2726
- )?;
2773
+ conn.close(true, e.to_wire(), b"Error handling frame.")?;
2727
2774
 
2728
2775
  return Err(e);
2729
- },
2776
+ }
2730
2777
 
2731
2778
  _ => (),
2732
2779
  }
2733
- },
2780
+ }
2734
2781
 
2735
2782
  stream::State::FramePayloadLen => {
2736
2783
  stream.try_fill_buffer(conn)?;
@@ -2741,8 +2788,8 @@ impl Connection {
2741
2788
  Err(_) => continue,
2742
2789
  };
2743
2790
 
2744
- // DATA frames are handled uniquely. After this point we lose
2745
- // visibility of DATA framing, so just log here.
2791
+ // DATA frames are handled uniquely. Log them here because
2792
+ // DATA framing is no longer visible after this point.
2746
2793
  if Some(frame::DATA_FRAME_TYPE_ID) == stream.frame_type() {
2747
2794
  trace!(
2748
2795
  "{} rx frm DATA stream={} wire_payload_len={}",
@@ -2754,23 +2801,24 @@ impl Connection {
2754
2801
  qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
2755
2802
  let frame = Http3Frame::Data { raw: None };
2756
2803
 
2757
- let ev_data =
2758
- EventData::Http3FrameParsed(FrameParsed {
2759
- stream_id,
2760
- length: Some(payload_len),
2761
- frame,
2762
- ..Default::default()
2763
- });
2804
+ let ev_data = EventData::Http3FrameParsed(FrameParsed {
2805
+ stream_id,
2806
+ length: Some(payload_len),
2807
+ frame,
2808
+ ..Default::default()
2809
+ });
2764
2810
 
2765
2811
  q.add_event_data_now(ev_data).ok();
2766
2812
  });
2767
2813
  }
2768
2814
 
2769
- if let Err(e) = stream.set_frame_payload_len(payload_len) {
2815
+ let res = stream.set_frame_payload_len(payload_len);
2816
+
2817
+ if let Err(e) = res {
2770
2818
  conn.close(true, e.to_wire(), b"")?;
2771
2819
  return Err(e);
2772
2820
  }
2773
- },
2821
+ }
2774
2822
 
2775
2823
  stream::State::FramePayload => {
2776
2824
  // Do not emit events when not polling.
@@ -2786,33 +2834,26 @@ impl Connection {
2786
2834
  Err(Error::Done) => return Err(Error::Done),
2787
2835
 
2788
2836
  Err(e) => {
2789
- conn.close(
2790
- true,
2791
- e.to_wire(),
2792
- b"Error handling frame.",
2793
- )?;
2837
+ conn.close(true, e.to_wire(), b"Error handling frame.")?;
2794
2838
 
2795
2839
  return Err(e);
2796
- },
2840
+ }
2797
2841
  };
2798
2842
 
2799
- match self.process_frame(conn, stream_id, frame, payload_len)
2800
- {
2843
+ match self.process_frame(conn, stream_id, frame, payload_len) {
2801
2844
  Ok(ev) => return Ok(ev),
2802
2845
 
2803
2846
  Err(Error::Done) => {
2804
- // This might be a frame that is processed internally
2805
- // without needing to bubble up to the user as an
2806
- // event. Check whether the frame has FIN'd by QUIC
2807
- // to prevent trying to read again on a closed stream.
2847
+ // Internal frames do not produce events. Avoid
2848
+ // reading again if QUIC marked the stream finished.
2808
2849
  if conn.stream_finished(stream_id) {
2809
2850
  break;
2810
2851
  }
2811
- },
2852
+ }
2812
2853
 
2813
2854
  Err(e) => return Err(e),
2814
2855
  };
2815
- },
2856
+ }
2816
2857
 
2817
2858
  stream::State::Data => {
2818
2859
  // Do not emit events when not polling.
@@ -2820,27 +2861,45 @@ impl Connection {
2820
2861
  break;
2821
2862
  }
2822
2863
 
2864
+ // A reset received while waiting for the rest of a DATA
2865
+ // frame makes the stream readable, with no payload left to
2866
+ // read. Use a dummy read to return the reset directly,
2867
+ // instead of an Event::Data after which `recv_body()`
2868
+ // returns `Done` and the stream looks finished, as if the
2869
+ // body were complete. The dummy read has side effects, such
2870
+ // as qlog events, so only do it for finished streams,
2871
+ // which include all reset streams.
2872
+ if conn.stream_finished(stream_id) {
2873
+ if let Err(crate::Error::StreamReset(e)) =
2874
+ conn.stream_recv(stream_id, &mut [])
2875
+ {
2876
+ return Err(Error::TransportError(crate::Error::StreamReset(e)));
2877
+ }
2878
+ }
2879
+
2823
2880
  if !stream.try_trigger_data_event() {
2824
2881
  break;
2825
2882
  }
2826
2883
 
2827
2884
  return Ok((stream_id, Event::Data));
2828
- },
2885
+ }
2829
2886
 
2830
2887
  stream::State::QpackInstruction => {
2831
2888
  let mut d = [0; 4096];
2832
2889
 
2833
2890
  // Read data from the stream and discard immediately.
2891
+ //
2892
+ // TODO: pass encoder stream data to the QPACK decoder.
2834
2893
  loop {
2835
2894
  let (recv, fin) = conn.stream_recv(stream_id, &mut d)?;
2836
2895
 
2837
2896
  match stream.ty() {
2838
- Some(stream::Type::QpackEncoder) =>
2839
- self.peer_qpack_streams.encoder_stream_bytes +=
2840
- recv as u64,
2841
- Some(stream::Type::QpackDecoder) =>
2842
- self.peer_qpack_streams.decoder_stream_bytes +=
2843
- recv as u64,
2897
+ Some(stream::Type::QpackEncoder) => {
2898
+ self.peer_qpack_streams.encoder_stream_bytes += recv as u64
2899
+ }
2900
+ Some(stream::Type::QpackDecoder) => {
2901
+ self.peer_qpack_streams.decoder_stream_bytes += recv as u64
2902
+ }
2844
2903
  _ => unreachable!(),
2845
2904
  };
2846
2905
 
@@ -2848,18 +2907,24 @@ impl Connection {
2848
2907
  close_conn_critical_stream(conn)?;
2849
2908
  }
2850
2909
  }
2851
- },
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
+ }
2852
2921
 
2853
2922
  stream::State::Drain => {
2854
2923
  // Discard incoming data on the stream.
2855
- conn.stream_shutdown(
2856
- stream_id,
2857
- crate::Shutdown::Read,
2858
- 0x100,
2859
- )?;
2924
+ conn.stream_shutdown(stream_id, crate::Shutdown::Read, 0x100)?;
2860
2925
 
2861
2926
  break;
2862
- },
2927
+ }
2863
2928
 
2864
2929
  stream::State::Finished => break,
2865
2930
  }
@@ -2884,18 +2949,25 @@ impl Connection {
2884
2949
  stream.finished();
2885
2950
 
2886
2951
  self.finished_streams.push_back(stream_id);
2887
- },
2888
-
2889
- _ => (),
2952
+ }
2953
+ Some(stream::Type::Unknown) | None => {
2954
+ self.streams.remove(&stream_id);
2955
+ }
2956
+ // Closing any of the critical streams leads to connection close,
2957
+ // so there is no need for any cleanup actions here.
2958
+ Some(stream::Type::Control)
2959
+ | Some(stream::Type::QpackEncoder)
2960
+ | Some(stream::Type::QpackDecoder) => (),
2890
2961
  };
2891
2962
  }
2892
2963
 
2893
2964
  fn finish_local_stream<F: BufFactory>(
2894
- &mut self, conn: &super::Connection<F>, stream_id: u64,
2965
+ &mut self,
2966
+ conn: &super::Connection<F>,
2967
+ stream_id: u64,
2895
2968
  initialize_local: bool,
2896
2969
  ) {
2897
- let hash_map::Entry::Occupied(mut stream) = self.streams.entry(stream_id)
2898
- else {
2970
+ let hash_map::Entry::Occupied(mut stream) = self.streams.entry(stream_id) else {
2899
2971
  return;
2900
2972
  };
2901
2973
 
@@ -2922,8 +2994,20 @@ impl Connection {
2922
2994
  }
2923
2995
  }
2924
2996
 
2997
+ // Marks a reset stream as finished, so that neither poll() nor a later
2998
+ // recv_body() call queues a Finished event for it, and removes the stream
2999
+ // if the local side is also finished.
3000
+ fn process_reset_stream(&mut self, stream_id: u64) {
3001
+ if let Some(stream) = self.streams.get_mut(&stream_id) {
3002
+ stream.finished();
3003
+ }
3004
+
3005
+ self.remove_local_finished_stream(stream_id);
3006
+ }
3007
+
2925
3008
  fn pop_finished_stream<F: BufFactory>(
2926
- &mut self, conn: &mut super::Connection<F>,
3009
+ &mut self,
3010
+ conn: &mut super::Connection<F>,
2927
3011
  ) -> Option<(u64, Event)> {
2928
3012
  let finished = self.finished_streams.pop_front()?;
2929
3013
 
@@ -2932,9 +3016,7 @@ impl Connection {
2932
3016
  if conn.stream_readable(finished) {
2933
3017
  // The stream is finished, but is still readable, it may indicate
2934
3018
  // that there is a pending error, such as reset.
2935
- if let Err(crate::Error::StreamReset(e)) =
2936
- conn.stream_recv(finished, &mut [])
2937
- {
3019
+ if let Err(crate::Error::StreamReset(e)) = conn.stream_recv(finished, &mut []) {
2938
3020
  return Some((finished, Event::Reset(e)));
2939
3021
  }
2940
3022
  }
@@ -2943,8 +3025,11 @@ impl Connection {
2943
3025
  }
2944
3026
 
2945
3027
  fn process_frame<F: BufFactory>(
2946
- &mut self, conn: &mut super::Connection<F>, stream_id: u64,
2947
- frame: frame::Frame, payload_len: u64,
3028
+ &mut self,
3029
+ conn: &mut super::Connection<F>,
3030
+ stream_id: u64,
3031
+ frame: frame::Frame,
3032
+ payload_len: u64,
2948
3033
  ) -> Result<(u64, Event)> {
2949
3034
  trace!(
2950
3035
  "{} rx frm {:?} stream={} payload_len={}",
@@ -3002,7 +3087,7 @@ impl Connection {
3002
3087
  return Err(Error::SettingsError);
3003
3088
  }
3004
3089
  }
3005
- },
3090
+ }
3006
3091
 
3007
3092
  frame::Frame::Headers { header_block } => {
3008
3093
  // Servers reject too many HEADERS frames.
@@ -3026,16 +3111,12 @@ impl Connection {
3026
3111
  .max_field_section_size
3027
3112
  .unwrap_or(u64::MAX);
3028
3113
 
3029
- let headers = match self
3030
- .qpack_decoder
3031
- .decode(&header_block[..], max_size)
3032
- {
3114
+ let headers = match self.qpack_decoder.decode(&header_block[..], max_size) {
3033
3115
  Ok(v) => v,
3034
3116
 
3035
3117
  Err(e) => {
3036
3118
  let e = match e {
3037
- qpack::Error::HeaderListTooLarge =>
3038
- Error::ExcessiveLoad,
3119
+ qpack::Error::HeaderListTooLarge => Error::ExcessiveLoad,
3039
3120
 
3040
3121
  _ => Error::QpackDecompressionFailed,
3041
3122
  };
@@ -3043,20 +3124,16 @@ impl Connection {
3043
3124
  conn.close(true, e.to_wire(), b"Error parsing headers.")?;
3044
3125
 
3045
3126
  return Err(e);
3046
- },
3127
+ }
3047
3128
  };
3048
3129
 
3049
3130
  qlog_with_type!(QLOG_FRAME_PARSED, conn.qlog, q, {
3050
3131
  let qlog_headers = headers
3051
3132
  .iter()
3052
3133
  .map(|h| qlog::events::http3::HttpHeader {
3053
- name: Some(
3054
- String::from_utf8_lossy(h.name()).into_owned(),
3055
- ),
3134
+ name: Some(String::from_utf8_lossy(h.name()).into_owned()),
3056
3135
  name_bytes: None,
3057
- value: Some(
3058
- String::from_utf8_lossy(h.value()).into_owned(),
3059
- ),
3136
+ value: Some(String::from_utf8_lossy(h.value()).into_owned()),
3060
3137
  value_bytes: None,
3061
3138
  })
3062
3139
  .collect();
@@ -3078,15 +3155,18 @@ impl Connection {
3078
3155
 
3079
3156
  let more_frames = !conn.stream_finished(stream_id);
3080
3157
 
3081
- return Ok((stream_id, Event::Headers {
3082
- list: headers,
3083
- more_frames,
3084
- }));
3085
- },
3158
+ return Ok((
3159
+ stream_id,
3160
+ Event::Headers {
3161
+ list: headers,
3162
+ more_frames,
3163
+ },
3164
+ ));
3165
+ }
3086
3166
 
3087
3167
  frame::Frame::Data { .. } => {
3088
3168
  // Do nothing. The Data event is returned separately.
3089
- },
3169
+ }
3090
3170
 
3091
3171
  frame::Frame::GoAway { id } => {
3092
3172
  if !self.is_server && id % 4 != 0 {
@@ -3114,7 +3194,7 @@ impl Connection {
3114
3194
  self.peer_goaway_id = Some(id);
3115
3195
 
3116
3196
  return Ok((id, Event::GoAway));
3117
- },
3197
+ }
3118
3198
 
3119
3199
  frame::Frame::MaxPushId { push_id } => {
3120
3200
  if !self.is_server {
@@ -3128,17 +3208,13 @@ impl Connection {
3128
3208
  }
3129
3209
 
3130
3210
  if push_id < self.max_push_id {
3131
- conn.close(
3132
- true,
3133
- Error::IdError.to_wire(),
3134
- b"MAX_PUSH_ID reduced limit",
3135
- )?;
3211
+ conn.close(true, Error::IdError.to_wire(), b"MAX_PUSH_ID reduced limit")?;
3136
3212
 
3137
3213
  return Err(Error::IdError);
3138
3214
  }
3139
3215
 
3140
3216
  self.max_push_id = push_id;
3141
- },
3217
+ }
3142
3218
 
3143
3219
  frame::Frame::PushPromise { .. } => {
3144
3220
  if self.is_server {
@@ -3162,11 +3238,11 @@ impl Connection {
3162
3238
  }
3163
3239
 
3164
3240
  // TODO: implement more checks and PUSH_PROMISE event
3165
- },
3241
+ }
3166
3242
 
3167
3243
  frame::Frame::CancelPush { .. } => {
3168
3244
  // TODO: implement CANCEL_PUSH frame
3169
- },
3245
+ }
3170
3246
 
3171
3247
  frame::Frame::PriorityUpdateRequest {
3172
3248
  prioritized_element_id,
@@ -3211,10 +3287,19 @@ impl Connection {
3211
3287
  }
3212
3288
 
3213
3289
  // If the stream did not yet exist, create it and store.
3214
- let stream =
3215
- self.streams.entry(prioritized_element_id).or_insert_with(
3216
- || <stream::Stream>::new(prioritized_element_id, false),
3217
- );
3290
+ let stream = self
3291
+ .streams
3292
+ .entry(prioritized_element_id)
3293
+ .or_insert_with(|| {
3294
+ <stream::Stream>::new(
3295
+ prioritized_element_id,
3296
+ false,
3297
+ self.local_settings
3298
+ .max_field_section_size
3299
+ .unwrap_or(SETTINGS_MAX_FIELD_SECTION_SIZE_DEFAULT),
3300
+ self.max_priority_update_size,
3301
+ )
3302
+ });
3218
3303
 
3219
3304
  let had_priority_update = stream.has_last_priority_update();
3220
3305
  stream.set_last_priority_update(Some(priority_field_value));
@@ -3226,7 +3311,7 @@ impl Connection {
3226
3311
  } else {
3227
3312
  return Err(Error::Done);
3228
3313
  }
3229
- },
3314
+ }
3230
3315
 
3231
3316
  frame::Frame::PriorityUpdatePush {
3232
3317
  prioritized_element_id,
@@ -3253,7 +3338,7 @@ impl Connection {
3253
3338
  }
3254
3339
 
3255
3340
  // TODO: we only implement this if we implement server push
3256
- },
3341
+ }
3257
3342
 
3258
3343
  frame::Frame::Unknown { .. } => (),
3259
3344
  }
@@ -3265,12 +3350,8 @@ impl Connection {
3265
3350
  #[inline]
3266
3351
  pub fn stats(&self) -> Stats {
3267
3352
  Stats {
3268
- qpack_encoder_stream_recv_bytes: self
3269
- .peer_qpack_streams
3270
- .encoder_stream_bytes,
3271
- qpack_decoder_stream_recv_bytes: self
3272
- .peer_qpack_streams
3273
- .decoder_stream_bytes,
3353
+ qpack_encoder_stream_recv_bytes: self.peer_qpack_streams.encoder_stream_bytes,
3354
+ qpack_decoder_stream_recv_bytes: self.peer_qpack_streams.decoder_stream_bytes,
3274
3355
  }
3275
3356
  }
3276
3357
  }
@@ -3317,29 +3398,24 @@ pub mod testing {
3317
3398
  Session::<DefaultBufFactory>::new_with_buf()
3318
3399
  }
3319
3400
 
3320
- pub fn with_configs(
3321
- config: &mut crate::Config, h3_config: &Config,
3322
- ) -> Result<Session> {
3401
+ pub fn with_configs(config: &mut crate::Config, h3_config: &Config) -> Result<Session> {
3323
3402
  Session::<DefaultBufFactory>::with_configs_and_buf(config, h3_config)
3324
3403
  }
3325
3404
 
3326
3405
  pub fn default_configs() -> Result<(crate::Config, Config)> {
3327
3406
  fn path_relative_to_manifest_dir(path: &str) -> String {
3328
- std::fs::canonicalize(
3329
- std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(path),
3330
- )
3331
- .unwrap()
3332
- .to_string_lossy()
3333
- .into_owned()
3407
+ std::fs::canonicalize(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(path))
3408
+ .unwrap()
3409
+ .to_string_lossy()
3410
+ .into_owned()
3334
3411
  }
3335
3412
 
3336
3413
  let mut config = crate::Config::new(crate::PROTOCOL_VERSION)?;
3337
- config.load_cert_chain_from_pem_file(
3338
- &path_relative_to_manifest_dir("examples/cert.crt"),
3339
- )?;
3340
- config.load_priv_key_from_pem_file(
3341
- &path_relative_to_manifest_dir("examples/cert.key"),
3342
- )?;
3414
+ config.load_cert_chain_from_pem_file(&path_relative_to_manifest_dir(
3415
+ "examples/cert.crt",
3416
+ ))?;
3417
+ config
3418
+ .load_priv_key_from_pem_file(&path_relative_to_manifest_dir("examples/cert.key"))?;
3343
3419
  config.set_application_protos(&[b"h3"])?;
3344
3420
  config.set_initial_max_data(1500);
3345
3421
  config.set_initial_max_stream_data_bidi_local(150);
@@ -3363,7 +3439,8 @@ pub mod testing {
3363
3439
  }
3364
3440
 
3365
3441
  pub fn with_configs_and_buf(
3366
- config: &mut crate::Config, h3_config: &Config,
3442
+ config: &mut crate::Config,
3443
+ h3_config: &Config,
3367
3444
  ) -> Result<Session<F>> {
3368
3445
  let pipe = test_utils::Pipe::with_config_and_buf(config)?;
3369
3446
  let client_dgram = pipe.client.dgram_enabled();
@@ -3453,8 +3530,7 @@ pub mod testing {
3453
3530
  Header::new(b"user-agent", b"quiche-test"),
3454
3531
  ];
3455
3532
 
3456
- let stream =
3457
- self.client.send_request(&mut self.pipe.client, &req, fin)?;
3533
+ let stream = self.client.send_request(&mut self.pipe.client, &req, fin)?;
3458
3534
 
3459
3535
  self.advance().ok();
3460
3536
 
@@ -3464,20 +3540,14 @@ pub mod testing {
3464
3540
  /// Sends a response from server with default headers.
3465
3541
  ///
3466
3542
  /// On success it returns the headers.
3467
- pub fn send_response(
3468
- &mut self, stream: u64, fin: bool,
3469
- ) -> Result<Vec<Header>> {
3543
+ pub fn send_response(&mut self, stream: u64, fin: bool) -> Result<Vec<Header>> {
3470
3544
  let resp = vec![
3471
3545
  Header::new(b":status", b"200"),
3472
3546
  Header::new(b"server", b"quiche-test"),
3473
3547
  ];
3474
3548
 
3475
- self.server.send_response(
3476
- &mut self.pipe.server,
3477
- stream,
3478
- &resp,
3479
- fin,
3480
- )?;
3549
+ self.server
3550
+ .send_response(&mut self.pipe.server, stream, &resp, fin)?;
3481
3551
 
3482
3552
  self.advance().ok();
3483
3553
 
@@ -3487,9 +3557,7 @@ pub mod testing {
3487
3557
  /// Sends some default payload from client.
3488
3558
  ///
3489
3559
  /// On success it returns the payload.
3490
- pub fn send_body_client(
3491
- &mut self, stream: u64, fin: bool,
3492
- ) -> Result<Vec<u8>> {
3560
+ pub fn send_body_client(&mut self, stream: u64, fin: bool) -> Result<Vec<u8>> {
3493
3561
  let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3494
3562
 
3495
3563
  self.client
@@ -3503,9 +3571,7 @@ pub mod testing {
3503
3571
  /// Fetches DATA payload from the server.
3504
3572
  ///
3505
3573
  /// On success it returns the number of bytes received.
3506
- pub fn recv_body_client(
3507
- &mut self, stream: u64, buf: &mut [u8],
3508
- ) -> Result<usize> {
3574
+ pub fn recv_body_client(&mut self, stream: u64, buf: &mut [u8]) -> Result<usize> {
3509
3575
  self.client.recv_body(&mut self.pipe.client, stream, buf)
3510
3576
  }
3511
3577
 
@@ -3513,7 +3579,9 @@ pub mod testing {
3513
3579
  ///
3514
3580
  /// On success it returns the number of bytes received.
3515
3581
  pub fn recv_body_buf_client<B: bytes::BufMut>(
3516
- &mut self, stream: u64, buf: B,
3582
+ &mut self,
3583
+ stream: u64,
3584
+ buf: B,
3517
3585
  ) -> Result<usize> {
3518
3586
  self.client
3519
3587
  .recv_body_buf(&mut self.pipe.client, stream, buf)
@@ -3522,9 +3590,7 @@ pub mod testing {
3522
3590
  /// Sends some default payload from server.
3523
3591
  ///
3524
3592
  /// On success it returns the payload.
3525
- pub fn send_body_server(
3526
- &mut self, stream: u64, fin: bool,
3527
- ) -> Result<Vec<u8>> {
3593
+ pub fn send_body_server(&mut self, stream: u64, fin: bool) -> Result<Vec<u8>> {
3528
3594
  let bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3529
3595
 
3530
3596
  self.server
@@ -3538,9 +3604,7 @@ pub mod testing {
3538
3604
  /// Fetches DATA payload from the client.
3539
3605
  ///
3540
3606
  /// On success it returns the number of bytes received.
3541
- pub fn recv_body_server(
3542
- &mut self, stream: u64, buf: &mut [u8],
3543
- ) -> Result<usize> {
3607
+ pub fn recv_body_server(&mut self, stream: u64, buf: &mut [u8]) -> Result<usize> {
3544
3608
  self.server.recv_body(&mut self.pipe.server, stream, buf)
3545
3609
  }
3546
3610
 
@@ -3548,7 +3612,9 @@ pub mod testing {
3548
3612
  ///
3549
3613
  /// On success it returns the number of bytes received.
3550
3614
  pub fn recv_body_buf_server<B: bytes::BufMut>(
3551
- &mut self, stream: u64, buf: B,
3615
+ &mut self,
3616
+ stream: u64,
3617
+ buf: B,
3552
3618
  ) -> Result<usize> {
3553
3619
  self.server
3554
3620
  .recv_body_buf(&mut self.pipe.server, stream, buf)
@@ -3556,7 +3622,10 @@ pub mod testing {
3556
3622
 
3557
3623
  /// Sends a single HTTP/3 frame from the client.
3558
3624
  pub fn send_frame_client(
3559
- &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3625
+ &mut self,
3626
+ frame: frame::Frame,
3627
+ stream_id: u64,
3628
+ fin: bool,
3560
3629
  ) -> Result<()> {
3561
3630
  let mut d = [42; 65535];
3562
3631
 
@@ -3595,9 +3664,7 @@ pub mod testing {
3595
3664
  ///
3596
3665
  /// On success it returns the DATAGRAM length, flow ID and flow ID
3597
3666
  /// length.
3598
- pub fn recv_dgram_client(
3599
- &mut self, buf: &mut [u8],
3600
- ) -> Result<(usize, u64, usize)> {
3667
+ pub fn recv_dgram_client(&mut self, buf: &mut [u8]) -> Result<(usize, u64, usize)> {
3601
3668
  let len = self.pipe.client.dgram_recv(buf)?;
3602
3669
  let mut b = octets::Octets::with_slice(buf);
3603
3670
  let flow_id = b.get_varint()?;
@@ -3628,9 +3695,7 @@ pub mod testing {
3628
3695
  ///
3629
3696
  /// On success it returns the DATAGRAM length, flow ID and flow ID
3630
3697
  /// length.
3631
- pub fn recv_dgram_server(
3632
- &mut self, buf: &mut [u8],
3633
- ) -> Result<(usize, u64, usize)> {
3698
+ pub fn recv_dgram_server(&mut self, buf: &mut [u8]) -> Result<(usize, u64, usize)> {
3634
3699
  let len = self.pipe.server.dgram_recv(buf)?;
3635
3700
  let mut b = octets::Octets::with_slice(buf);
3636
3701
  let flow_id = b.get_varint()?;
@@ -3640,7 +3705,10 @@ pub mod testing {
3640
3705
 
3641
3706
  /// Sends a single HTTP/3 frame from the server.
3642
3707
  pub fn send_frame_server(
3643
- &mut self, frame: frame::Frame, stream_id: u64, fin: bool,
3708
+ &mut self,
3709
+ frame: frame::Frame,
3710
+ stream_id: u64,
3711
+ fin: bool,
3644
3712
  ) -> Result<()> {
3645
3713
  let mut d = [42; 65535];
3646
3714
 
@@ -3658,7 +3726,10 @@ pub mod testing {
3658
3726
 
3659
3727
  /// Sends an arbitrary buffer of HTTP/3 stream data from the client.
3660
3728
  pub fn send_arbitrary_stream_data_client(
3661
- &mut self, data: &[u8], stream_id: u64, fin: bool,
3729
+ &mut self,
3730
+ data: &[u8],
3731
+ stream_id: u64,
3732
+ fin: bool,
3662
3733
  ) -> Result<()> {
3663
3734
  self.pipe.client.stream_send(stream_id, data, fin)?;
3664
3735
 
@@ -3669,7 +3740,10 @@ pub mod testing {
3669
3740
 
3670
3741
  /// Sends an arbitrary buffer of HTTP/3 stream data from the server.
3671
3742
  pub fn send_arbitrary_stream_data_server(
3672
- &mut self, data: &[u8], stream_id: u64, fin: bool,
3743
+ &mut self,
3744
+ data: &[u8],
3745
+ stream_id: u64,
3746
+ fin: bool,
3673
3747
  ) -> Result<()> {
3674
3748
  self.pipe.server.stream_send(stream_id, data, fin)?;
3675
3749
 
@@ -3698,7 +3772,9 @@ mod tests {
3698
3772
  fn h3_handshake_0rtt() {
3699
3773
  let mut buf = [0; 65535];
3700
3774
 
3701
- let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
3775
+ // Test drives the handshake one Initial at a time; requires a
3776
+ // single-Initial ClientHello.
3777
+ let mut config = crate::test_utils::config_no_pq(crate::PROTOCOL_VERSION).unwrap();
3702
3778
  config
3703
3779
  .load_cert_chain_from_pem_file("examples/cert.crt")
3704
3780
  .unwrap();
@@ -3933,9 +4009,8 @@ mod tests {
3933
4009
  assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
3934
4010
  assert_eq!(s.poll_client(), Err(Error::Done));
3935
4011
 
3936
- // We expect to be able to read multiple data frames in a single call and
3937
- // reads don't have to end on frame boundaries. So let's try to read
3938
- // 1.5 times the amount we sent in one frame.
4012
+ // Reads may span multiple DATA frames and need not end at frame
4013
+ // boundaries. Read one and a half times the payload of one frame.
3939
4014
  let how_much_to_read_per_call = data.len() * 2 / 3;
3940
4015
  let mut remaining_to_read = total_data_frames * data.len();
3941
4016
  let mut recv_buf = Vec::new().limit(how_much_to_read_per_call);
@@ -3952,10 +4027,7 @@ mod tests {
3952
4027
  // We should either read up to the limit we set above, or to
3953
4028
  // the end of buffered data.
3954
4029
  let expected = std::cmp::min(data.len(), remaining_to_read);
3955
- assert_eq!(
3956
- s.recv_body_buf_client(stream, &mut recv_buf),
3957
- Ok(expected)
3958
- );
4030
+ assert_eq!(s.recv_body_buf_client(stream, &mut recv_buf), Ok(expected));
3959
4031
  remaining_to_read -= expected;
3960
4032
  }
3961
4033
  // We've read everything now. Ensure the Vec reflects that
@@ -4214,7 +4286,7 @@ mod tests {
4214
4286
  more_frames: true,
4215
4287
  };
4216
4288
 
4217
- // Inject a GREASE frame
4289
+ // Inject a GREASE frame.
4218
4290
  let mut d = [42; 10];
4219
4291
  let mut b = octets::OctetsMut::with_slice(&mut d);
4220
4292
 
@@ -4295,18 +4367,12 @@ mod tests {
4295
4367
  );
4296
4368
 
4297
4369
  assert_eq!(
4298
- s.send_body_client(
4299
- s.client.peer_qpack_streams.encoder_stream_id.unwrap(),
4300
- true
4301
- ),
4370
+ s.send_body_client(s.client.peer_qpack_streams.encoder_stream_id.unwrap(), true),
4302
4371
  Err(Error::FrameUnexpected)
4303
4372
  );
4304
4373
 
4305
4374
  assert_eq!(
4306
- s.send_body_client(
4307
- s.client.peer_qpack_streams.decoder_stream_id.unwrap(),
4308
- true
4309
- ),
4375
+ s.send_body_client(s.client.peer_qpack_streams.decoder_stream_id.unwrap(), true),
4310
4376
  Err(Error::FrameUnexpected)
4311
4377
  );
4312
4378
  }
@@ -4347,18 +4413,12 @@ mod tests {
4347
4413
  );
4348
4414
 
4349
4415
  assert_eq!(
4350
- s.send_body_server(
4351
- s.server.peer_qpack_streams.encoder_stream_id.unwrap(),
4352
- true
4353
- ),
4416
+ s.send_body_server(s.server.peer_qpack_streams.encoder_stream_id.unwrap(), true),
4354
4417
  Err(Error::FrameUnexpected)
4355
4418
  );
4356
4419
 
4357
4420
  assert_eq!(
4358
- s.send_body_server(
4359
- s.server.peer_qpack_streams.decoder_stream_id.unwrap(),
4360
- true
4361
- ),
4421
+ s.send_body_server(s.server.peer_qpack_streams.decoder_stream_id.unwrap(), true),
4362
4422
  Err(Error::FrameUnexpected)
4363
4423
  );
4364
4424
  }
@@ -4378,13 +4438,7 @@ mod tests {
4378
4438
  let req_trailers = vec![Header::new(b"foo", b"bar")];
4379
4439
 
4380
4440
  s.client
4381
- .send_additional_headers(
4382
- &mut s.pipe.client,
4383
- stream,
4384
- &req_trailers,
4385
- true,
4386
- true,
4387
- )
4441
+ .send_additional_headers(&mut s.pipe.client, stream, &req_trailers, true, true)
4388
4442
  .unwrap();
4389
4443
 
4390
4444
  s.advance().ok();
@@ -4441,13 +4495,7 @@ mod tests {
4441
4495
  .unwrap();
4442
4496
 
4443
4497
  s.server
4444
- .send_additional_headers(
4445
- &mut s.pipe.server,
4446
- stream,
4447
- &resp,
4448
- false,
4449
- true,
4450
- )
4498
+ .send_additional_headers(&mut s.pipe.server, stream, &resp, false, true)
4451
4499
  .unwrap();
4452
4500
 
4453
4501
  s.advance().ok();
@@ -4543,13 +4591,8 @@ mod tests {
4543
4591
 
4544
4592
  assert_eq!(
4545
4593
  Err(Error::FrameUnexpected),
4546
- s.server.send_additional_headers(
4547
- &mut s.pipe.server,
4548
- stream,
4549
- &info_resp,
4550
- false,
4551
- false
4552
- )
4594
+ s.server
4595
+ .send_additional_headers(&mut s.pipe.server, stream, &info_resp, false, false)
4553
4596
  );
4554
4597
 
4555
4598
  s.advance().ok();
@@ -4568,13 +4611,8 @@ mod tests {
4568
4611
  let req_trailer = vec![Header::new(b"goodbye", b"world")];
4569
4612
 
4570
4613
  assert_eq!(
4571
- s.client.send_additional_headers(
4572
- &mut s.pipe.client,
4573
- stream,
4574
- &req_trailer,
4575
- true,
4576
- false
4577
- ),
4614
+ s.client
4615
+ .send_additional_headers(&mut s.pipe.client, stream, &req_trailer, true, false),
4578
4616
  Ok(())
4579
4617
  );
4580
4618
 
@@ -4610,13 +4648,7 @@ mod tests {
4610
4648
  let req_trailers = vec![Header::new(b"foo", b"bar")];
4611
4649
 
4612
4650
  s.client
4613
- .send_additional_headers(
4614
- &mut s.pipe.client,
4615
- stream,
4616
- &req_trailers,
4617
- true,
4618
- false,
4619
- )
4651
+ .send_additional_headers(&mut s.pipe.client, stream, &req_trailers, true, false)
4620
4652
  .unwrap();
4621
4653
 
4622
4654
  s.advance().ok();
@@ -4671,12 +4703,8 @@ mod tests {
4671
4703
 
4672
4704
  let (stream, req) = s.send_request(false).unwrap();
4673
4705
 
4674
- s.send_frame_client(
4675
- frame::Frame::MaxPushId { push_id: 2 },
4676
- stream,
4677
- false,
4678
- )
4679
- .unwrap();
4706
+ s.send_frame_client(frame::Frame::MaxPushId { push_id: 2 }, stream, false)
4707
+ .unwrap();
4680
4708
 
4681
4709
  let ev_headers = Event::Headers {
4682
4710
  list: req,
@@ -4763,10 +4791,7 @@ mod tests {
4763
4791
  s.handshake().unwrap();
4764
4792
 
4765
4793
  s.client
4766
- .open_uni_stream(
4767
- &mut s.pipe.client,
4768
- stream::HTTP3_PUSH_STREAM_TYPE_ID,
4769
- )
4794
+ .open_uni_stream(&mut s.pipe.client, stream::HTTP3_PUSH_STREAM_TYPE_ID)
4770
4795
  .unwrap();
4771
4796
 
4772
4797
  s.advance().ok();
@@ -4782,10 +4807,7 @@ mod tests {
4782
4807
  s.handshake().unwrap();
4783
4808
 
4784
4809
  s.server
4785
- .open_uni_stream(
4786
- &mut s.pipe.server,
4787
- stream::HTTP3_PUSH_STREAM_TYPE_ID,
4788
- )
4810
+ .open_uni_stream(&mut s.pipe.server, stream::HTTP3_PUSH_STREAM_TYPE_ID)
4789
4811
  .unwrap();
4790
4812
 
4791
4813
  s.advance().ok();
@@ -4817,12 +4839,8 @@ mod tests {
4817
4839
 
4818
4840
  let (stream, req) = s.send_request(false).unwrap();
4819
4841
 
4820
- s.send_frame_client(
4821
- frame::Frame::CancelPush { push_id: 2 },
4822
- stream,
4823
- false,
4824
- )
4825
- .unwrap();
4842
+ s.send_frame_client(frame::Frame::CancelPush { push_id: 2 }, stream, false)
4843
+ .unwrap();
4826
4844
 
4827
4845
  let ev_headers = Event::Headers {
4828
4846
  list: req,
@@ -5026,10 +5044,14 @@ mod tests {
5026
5044
  s.handshake().unwrap();
5027
5045
 
5028
5046
  s.client
5029
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5030
- urgency: 3,
5031
- incremental: false,
5032
- })
5047
+ .send_priority_update_for_request(
5048
+ &mut s.pipe.client,
5049
+ 0,
5050
+ &Priority {
5051
+ urgency: 3,
5052
+ incremental: false,
5053
+ },
5054
+ )
5033
5055
  .unwrap();
5034
5056
  s.advance().ok();
5035
5057
 
@@ -5038,16 +5060,68 @@ mod tests {
5038
5060
  }
5039
5061
 
5040
5062
  #[test]
5041
- /// Send a PRIORITY_UPDATE for request stream from the client.
5042
- fn priority_update_single_stream_rearm() {
5043
- let mut s = Session::new().unwrap();
5044
- s.handshake().unwrap();
5045
-
5046
- s.client
5047
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5048
- urgency: 3,
5049
- incremental: false,
5050
- })
5063
+ /// Send a PRIORITY_UPDATE for request stream from the client that is too
5064
+ /// large.
5065
+ fn priority_update_request_max_size_limit_default() {
5066
+ let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5067
+ config
5068
+ .load_cert_chain_from_pem_file("examples/cert.crt")
5069
+ .unwrap();
5070
+ config
5071
+ .load_priv_key_from_pem_file("examples/cert.key")
5072
+ .unwrap();
5073
+ config.set_application_protos(&[b"h3"]).unwrap();
5074
+ config.set_initial_max_data(1500);
5075
+ config.set_initial_max_stream_data_bidi_local(1500);
5076
+ config.set_initial_max_stream_data_bidi_remote(1500);
5077
+ config.set_initial_max_stream_data_uni(1500);
5078
+ config.set_initial_max_streams_bidi(5);
5079
+ config.set_initial_max_streams_uni(5);
5080
+ config.verify_peer(false);
5081
+
5082
+ let h3_config = Config::new().unwrap();
5083
+
5084
+ let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
5085
+
5086
+ s.handshake().unwrap();
5087
+
5088
+ let mut d = vec![42; 600];
5089
+ let mut b = octets::OctetsMut::with_slice(&mut d);
5090
+
5091
+ let pu = frame::Frame::PriorityUpdateRequest {
5092
+ prioritized_element_id: 0,
5093
+ priority_field_value: vec![0; 512],
5094
+ };
5095
+
5096
+ pu.to_bytes(&mut b).unwrap();
5097
+
5098
+ s.pipe.client.stream_send(2, &d, true).unwrap();
5099
+
5100
+ s.advance().ok();
5101
+
5102
+ assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
5103
+
5104
+ assert_eq!(
5105
+ s.pipe.server.local_error.as_ref().unwrap().error_code,
5106
+ Error::to_wire(Error::ExcessiveLoad)
5107
+ );
5108
+ }
5109
+
5110
+ #[test]
5111
+ /// Send a PRIORITY_UPDATE for request stream from the client.
5112
+ fn priority_update_single_stream_rearm() {
5113
+ let mut s = Session::new().unwrap();
5114
+ s.handshake().unwrap();
5115
+
5116
+ s.client
5117
+ .send_priority_update_for_request(
5118
+ &mut s.pipe.client,
5119
+ 0,
5120
+ &Priority {
5121
+ urgency: 3,
5122
+ incremental: false,
5123
+ },
5124
+ )
5051
5125
  .unwrap();
5052
5126
  s.advance().ok();
5053
5127
 
@@ -5055,10 +5129,14 @@ mod tests {
5055
5129
  assert_eq!(s.poll_server(), Err(Error::Done));
5056
5130
 
5057
5131
  s.client
5058
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5059
- urgency: 5,
5060
- incremental: false,
5061
- })
5132
+ .send_priority_update_for_request(
5133
+ &mut s.pipe.client,
5134
+ 0,
5135
+ &Priority {
5136
+ urgency: 5,
5137
+ incremental: false,
5138
+ },
5139
+ )
5062
5140
  .unwrap();
5063
5141
  s.advance().ok();
5064
5142
 
@@ -5070,10 +5148,14 @@ mod tests {
5070
5148
  assert_eq!(s.server.take_last_priority_update(0), Err(Error::Done));
5071
5149
 
5072
5150
  s.client
5073
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5074
- urgency: 7,
5075
- incremental: false,
5076
- })
5151
+ .send_priority_update_for_request(
5152
+ &mut s.pipe.client,
5153
+ 0,
5154
+ &Priority {
5155
+ urgency: 7,
5156
+ incremental: false,
5157
+ },
5158
+ )
5077
5159
  .unwrap();
5078
5160
  s.advance().ok();
5079
5161
 
@@ -5092,10 +5174,14 @@ mod tests {
5092
5174
  s.handshake().unwrap();
5093
5175
 
5094
5176
  s.client
5095
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5096
- urgency: 3,
5097
- incremental: false,
5098
- })
5177
+ .send_priority_update_for_request(
5178
+ &mut s.pipe.client,
5179
+ 0,
5180
+ &Priority {
5181
+ urgency: 3,
5182
+ incremental: false,
5183
+ },
5184
+ )
5099
5185
  .unwrap();
5100
5186
  s.advance().ok();
5101
5187
 
@@ -5103,10 +5189,14 @@ mod tests {
5103
5189
  assert_eq!(s.poll_server(), Err(Error::Done));
5104
5190
 
5105
5191
  s.client
5106
- .send_priority_update_for_request(&mut s.pipe.client, 4, &Priority {
5107
- urgency: 1,
5108
- incremental: false,
5109
- })
5192
+ .send_priority_update_for_request(
5193
+ &mut s.pipe.client,
5194
+ 4,
5195
+ &Priority {
5196
+ urgency: 1,
5197
+ incremental: false,
5198
+ },
5199
+ )
5110
5200
  .unwrap();
5111
5201
  s.advance().ok();
5112
5202
 
@@ -5114,10 +5204,14 @@ mod tests {
5114
5204
  assert_eq!(s.poll_server(), Err(Error::Done));
5115
5205
 
5116
5206
  s.client
5117
- .send_priority_update_for_request(&mut s.pipe.client, 8, &Priority {
5118
- urgency: 2,
5119
- incremental: false,
5120
- })
5207
+ .send_priority_update_for_request(
5208
+ &mut s.pipe.client,
5209
+ 8,
5210
+ &Priority {
5211
+ urgency: 2,
5212
+ incremental: false,
5213
+ },
5214
+ )
5121
5215
  .unwrap();
5122
5216
  s.advance().ok();
5123
5217
 
@@ -5188,10 +5282,14 @@ mod tests {
5188
5282
  s.handshake().unwrap();
5189
5283
 
5190
5284
  s.client
5191
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5192
- urgency: 3,
5193
- incremental: false,
5194
- })
5285
+ .send_priority_update_for_request(
5286
+ &mut s.pipe.client,
5287
+ 0,
5288
+ &Priority {
5289
+ urgency: 3,
5290
+ incremental: false,
5291
+ },
5292
+ )
5195
5293
  .unwrap();
5196
5294
  s.advance().ok();
5197
5295
 
@@ -5223,10 +5321,14 @@ mod tests {
5223
5321
 
5224
5322
  // Now send a PRIORITY_UPDATE for the completed request stream.
5225
5323
  s.client
5226
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5227
- urgency: 3,
5228
- incremental: false,
5229
- })
5324
+ .send_priority_update_for_request(
5325
+ &mut s.pipe.client,
5326
+ 0,
5327
+ &Priority {
5328
+ urgency: 3,
5329
+ incremental: false,
5330
+ },
5331
+ )
5230
5332
  .unwrap();
5231
5333
  s.advance().ok();
5232
5334
 
@@ -5300,10 +5402,14 @@ mod tests {
5300
5402
  s.handshake().unwrap();
5301
5403
 
5302
5404
  s.client
5303
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5304
- urgency: 3,
5305
- incremental: false,
5306
- })
5405
+ .send_priority_update_for_request(
5406
+ &mut s.pipe.client,
5407
+ 0,
5408
+ &Priority {
5409
+ urgency: 3,
5410
+ incremental: false,
5411
+ },
5412
+ )
5307
5413
  .unwrap();
5308
5414
  s.advance().ok();
5309
5415
 
@@ -5337,16 +5443,26 @@ mod tests {
5337
5443
 
5338
5444
  // Now send a PRIORITY_UPDATE for the closed request stream.
5339
5445
  s.client
5340
- .send_priority_update_for_request(&mut s.pipe.client, 0, &Priority {
5341
- urgency: 3,
5342
- incremental: false,
5343
- })
5446
+ .send_priority_update_for_request(
5447
+ &mut s.pipe.client,
5448
+ 0,
5449
+ &Priority {
5450
+ urgency: 3,
5451
+ incremental: false,
5452
+ },
5453
+ )
5344
5454
  .unwrap();
5345
5455
  s.advance().ok();
5346
5456
 
5347
5457
  // No event generated at server
5348
5458
  assert_eq!(s.poll_server(), Err(Error::Done));
5349
5459
 
5460
+ // The receive-side reset does not consume the STOP error.
5461
+ assert!(!s.pipe.server.streams.is_collected(0));
5462
+ assert_eq!(
5463
+ s.pipe.server.stream_capacity(0),
5464
+ Err(crate::Error::StreamStopped(0x100))
5465
+ );
5350
5466
  assert!(s.pipe.server.streams.is_collected(0));
5351
5467
  assert!(s.pipe.client.streams.is_collected(0));
5352
5468
  }
@@ -5707,11 +5823,11 @@ mod tests {
5707
5823
  Err(Error::Done) => {
5708
5824
  assert_eq!(s.server.peer_qpack_streams.encoder_stream_bytes, 20);
5709
5825
  assert_eq!(s.server.peer_qpack_streams.decoder_stream_bytes, 20);
5710
- },
5826
+ }
5711
5827
 
5712
5828
  Err(_) => {
5713
5829
  panic!();
5714
- },
5830
+ }
5715
5831
  }
5716
5832
 
5717
5833
  let stats = s.server.stats();
@@ -5825,10 +5941,7 @@ mod tests {
5825
5941
  assert_eq!(s.poll_server(), Err(Error::Done));
5826
5942
 
5827
5943
  for _ in 0..total_data_frames {
5828
- assert_eq!(
5829
- s.recv_body_server(stream, &mut recv_buf),
5830
- Ok(bytes.len())
5831
- );
5944
+ assert_eq!(s.recv_body_server(stream, &mut recv_buf), Ok(bytes.len()));
5832
5945
  }
5833
5946
 
5834
5947
  assert_eq!(
@@ -5852,8 +5965,66 @@ mod tests {
5852
5965
  }
5853
5966
 
5854
5967
  #[test]
5855
- /// Tests that the max header list size setting is enforced.
5856
- fn request_max_header_size_limit() {
5968
+ /// Tests that the max header list size setting allows larger headers than
5969
+ /// default.
5970
+ fn request_max_header_size_limit_accepts_large_headers() {
5971
+ let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5972
+ config
5973
+ .load_cert_chain_from_pem_file("examples/cert.crt")
5974
+ .unwrap();
5975
+ config
5976
+ .load_priv_key_from_pem_file("examples/cert.key")
5977
+ .unwrap();
5978
+ config.set_application_protos(&[b"h3"]).unwrap();
5979
+ config.set_initial_max_data(150000000);
5980
+ config.set_initial_max_stream_data_bidi_local(150000000);
5981
+ config.set_initial_max_stream_data_bidi_remote(150000000);
5982
+ config.set_initial_max_stream_data_uni(150000000);
5983
+ config.set_initial_max_streams_bidi(5);
5984
+ config.set_initial_max_streams_uni(5);
5985
+ config.verify_peer(false);
5986
+ config.set_initial_congestion_window_packets(100);
5987
+
5988
+ let mut h3_config = Config::new().unwrap();
5989
+ h3_config.set_max_field_section_size(256 * 1024);
5990
+
5991
+ let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
5992
+
5993
+ s.handshake().unwrap();
5994
+
5995
+ let mut req = vec![
5996
+ Header::new(b":method", b"GET"),
5997
+ Header::new(b":scheme", b"https"),
5998
+ Header::new(b":authority", b"quic.tech"),
5999
+ Header::new(b":path", b"/test"),
6000
+ ];
6001
+
6002
+ for _ in 1..5000 {
6003
+ req.push(Header::new(b"aaaaaaaaaa", b"aaaaaaaaa"));
6004
+ }
6005
+
6006
+ let ev_headers = Event::Headers {
6007
+ list: req.clone(),
6008
+ more_frames: false,
6009
+ };
6010
+
6011
+ let stream = s
6012
+ .client
6013
+ .send_request(&mut s.pipe.client, &req, true)
6014
+ .unwrap();
6015
+
6016
+ s.advance().ok();
6017
+
6018
+ assert_eq!(stream, 0);
6019
+
6020
+ assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
6021
+ assert_eq!(s.poll_server(), Ok((stream, Event::Finished)));
6022
+ assert_eq!(s.poll_server(), Err(Error::Done));
6023
+ }
6024
+
6025
+ #[test]
6026
+ /// Tests that the max header list size setting is enforced after decoding.
6027
+ fn request_max_header_size_limit_decoded_field_section() {
5857
6028
  let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
5858
6029
  config
5859
6030
  .load_cert_chain_from_pem_file("examples/cert.crt")
@@ -5902,6 +6073,53 @@ mod tests {
5902
6073
  );
5903
6074
  }
5904
6075
 
6076
+ #[test]
6077
+ /// Tests that the max header list size setting is enforced when observing
6078
+ /// frame size before decode.
6079
+ fn request_max_header_size_limit_default_abort_before_decode() {
6080
+ let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
6081
+ config
6082
+ .load_cert_chain_from_pem_file("examples/cert.crt")
6083
+ .unwrap();
6084
+ config
6085
+ .load_priv_key_from_pem_file("examples/cert.key")
6086
+ .unwrap();
6087
+ config.set_application_protos(&[b"h3"]).unwrap();
6088
+ config.set_initial_max_data(150000);
6089
+ config.set_initial_max_stream_data_bidi_local(150000);
6090
+ config.set_initial_max_stream_data_bidi_remote(150000);
6091
+ config.set_initial_max_stream_data_uni(150000);
6092
+ config.set_initial_max_streams_bidi(5);
6093
+ config.set_initial_max_streams_uni(5);
6094
+ config.verify_peer(false);
6095
+
6096
+ let h3_config = Config::new().unwrap();
6097
+
6098
+ let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
6099
+
6100
+ s.handshake().unwrap();
6101
+
6102
+ let mut d = vec![42; 200000];
6103
+ let mut b = octets::OctetsMut::with_slice(&mut d);
6104
+
6105
+ let hdrs = frame::Frame::Headers {
6106
+ header_block: vec![0; 65536],
6107
+ };
6108
+
6109
+ hdrs.to_bytes(&mut b).unwrap();
6110
+
6111
+ s.pipe.client.stream_send(0, &d, true).unwrap();
6112
+
6113
+ s.advance().ok();
6114
+
6115
+ assert_eq!(s.poll_server(), Err(Error::ExcessiveLoad));
6116
+
6117
+ assert_eq!(
6118
+ s.pipe.server.local_error.as_ref().unwrap().error_code,
6119
+ Error::to_wire(Error::ExcessiveLoad)
6120
+ );
6121
+ }
6122
+
5905
6123
  #[test]
5906
6124
  /// Tests that Error::TransportError contains a transport error.
5907
6125
  fn transport_error() {
@@ -5999,7 +6217,7 @@ mod tests {
5999
6217
  .load_priv_key_from_pem_file("examples/cert.key")
6000
6218
  .unwrap();
6001
6219
  config.set_application_protos(&[b"h3"]).unwrap();
6002
- config.set_initial_max_data(70);
6220
+ config.set_initial_max_data(75);
6003
6221
  config.set_initial_max_stream_data_bidi_local(150);
6004
6222
  config.set_initial_max_stream_data_bidi_remote(150);
6005
6223
  config.set_initial_max_stream_data_uni(150);
@@ -6062,7 +6280,7 @@ mod tests {
6062
6280
  .load_priv_key_from_pem_file("examples/cert.key")
6063
6281
  .unwrap();
6064
6282
  config.set_application_protos(&[b"h3"]).unwrap();
6065
- config.set_initial_max_data(70);
6283
+ config.set_initial_max_data(75);
6066
6284
  config.set_initial_max_stream_data_bidi_local(150);
6067
6285
  config.set_initial_max_stream_data_bidi_remote(150);
6068
6286
  config.set_initial_max_stream_data_uni(150);
@@ -6077,8 +6295,8 @@ mod tests {
6077
6295
  s.handshake().unwrap();
6078
6296
 
6079
6297
  // After the HTTP handshake, some bytes of connection flow control have
6080
- // been consumed. Fill the connection with more grease data on the control
6081
- // stream.
6298
+ // been consumed. Fill the connection with more grease data on the
6299
+ // control stream.
6082
6300
  let d = [42; 28];
6083
6301
  assert_eq!(s.pipe.client.stream_send(2, &d, false), Ok(23));
6084
6302
 
@@ -6203,7 +6421,8 @@ mod tests {
6203
6421
  .load_priv_key_from_pem_file("examples/cert.key")
6204
6422
  .unwrap();
6205
6423
  config.set_application_protos(&[b"h3"]).unwrap();
6206
- config.set_initial_max_data(10000); // large connection-level flow control
6424
+ // Use generous connection-level flow control.
6425
+ config.set_initial_max_data(10000);
6207
6426
  config.set_initial_max_stream_data_bidi_local(80);
6208
6427
  config.set_initial_max_stream_data_bidi_remote(80);
6209
6428
  config.set_initial_max_stream_data_uni(150);
@@ -6291,10 +6510,9 @@ mod tests {
6291
6510
  }];
6292
6511
 
6293
6512
  let pkt_type = crate::packet::Type::Short;
6294
- assert_eq!(
6295
- s.pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
6296
- Ok(39),
6297
- );
6513
+ s.pipe
6514
+ .send_pkt_to_server(pkt_type, &frames, &mut buf)
6515
+ .unwrap();
6298
6516
 
6299
6517
  let sent = s
6300
6518
  .server
@@ -6337,7 +6555,8 @@ mod tests {
6337
6555
  .load_priv_key_from_pem_file("examples/cert.key")
6338
6556
  .unwrap();
6339
6557
  config.set_application_protos(&[b"h3"]).unwrap();
6340
- config.set_initial_max_data(100000); // large connection-level flow control
6558
+ // Use generous connection-level flow control.
6559
+ config.set_initial_max_data(100000);
6341
6560
  config.set_initial_max_stream_data_bidi_local(100000);
6342
6561
  config.set_initial_max_stream_data_bidi_remote(50000);
6343
6562
  config.set_initial_max_stream_data_uni(150);
@@ -6409,7 +6628,8 @@ mod tests {
6409
6628
  .load_priv_key_from_pem_file("examples/cert.key")
6410
6629
  .unwrap();
6411
6630
  config.set_application_protos(&[b"h3"]).unwrap();
6412
- config.set_initial_max_data(100000); // large connection-level flow control
6631
+ // Use generous connection-level flow control.
6632
+ config.set_initial_max_data(100000);
6413
6633
  config.set_initial_max_stream_data_bidi_local(100000);
6414
6634
  config.set_initial_max_stream_data_bidi_remote(50000);
6415
6635
  config.set_initial_max_stream_data_uni(150);
@@ -6542,7 +6762,7 @@ mod tests {
6542
6762
  .load_priv_key_from_pem_file("examples/cert.key")
6543
6763
  .unwrap();
6544
6764
  config.set_application_protos(&[b"h3"]).unwrap();
6545
- config.set_initial_max_data(69);
6765
+ config.set_initial_max_data(74);
6546
6766
  config.set_initial_max_stream_data_bidi_local(150);
6547
6767
  config.set_initial_max_stream_data_bidi_remote(150);
6548
6768
  config.set_initial_max_stream_data_uni(150);
@@ -6581,7 +6801,7 @@ mod tests {
6581
6801
 
6582
6802
  s.advance().ok();
6583
6803
 
6584
- // Once the server gives flow control credits back, we can send the body.
6804
+ // Flow-control credit from the server allows the body to be sent.
6585
6805
  assert_eq!(s.pipe.client.stream_writable_next(), Some(0));
6586
6806
  assert_eq!(s.client.send_body(&mut s.pipe.client, 0, b"", true), Ok(0));
6587
6807
  }
@@ -6687,10 +6907,7 @@ mod tests {
6687
6907
 
6688
6908
  s.client.control_stream_id = Some(
6689
6909
  s.client
6690
- .open_uni_stream(
6691
- &mut s.pipe.client,
6692
- stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6693
- )
6910
+ .open_uni_stream(&mut s.pipe.client, stream::HTTP3_CONTROL_STREAM_TYPE_ID)
6694
6911
  .unwrap(),
6695
6912
  );
6696
6913
 
@@ -6739,19 +6956,13 @@ mod tests {
6739
6956
 
6740
6957
  s.client.control_stream_id = Some(
6741
6958
  s.client
6742
- .open_uni_stream(
6743
- &mut s.pipe.client,
6744
- stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6745
- )
6959
+ .open_uni_stream(&mut s.pipe.client, stream::HTTP3_CONTROL_STREAM_TYPE_ID)
6746
6960
  .unwrap(),
6747
6961
  );
6748
6962
 
6749
6963
  s.server.control_stream_id = Some(
6750
6964
  s.server
6751
- .open_uni_stream(
6752
- &mut s.pipe.server,
6753
- stream::HTTP3_CONTROL_STREAM_TYPE_ID,
6754
- )
6965
+ .open_uni_stream(&mut s.pipe.server, stream::HTTP3_CONTROL_STREAM_TYPE_ID)
6755
6966
  .unwrap(),
6756
6967
  );
6757
6968
 
@@ -6763,19 +6974,11 @@ mod tests {
6763
6974
  1,
6764
6975
  ];
6765
6976
 
6766
- s.send_arbitrary_stream_data_client(
6767
- &settings,
6768
- s.client.control_stream_id.unwrap(),
6769
- false,
6770
- )
6771
- .unwrap();
6977
+ s.send_arbitrary_stream_data_client(&settings, s.client.control_stream_id.unwrap(), false)
6978
+ .unwrap();
6772
6979
 
6773
- s.send_arbitrary_stream_data_server(
6774
- &settings,
6775
- s.server.control_stream_id.unwrap(),
6776
- false,
6777
- )
6778
- .unwrap();
6980
+ s.send_arbitrary_stream_data_server(&settings, s.server.control_stream_id.unwrap(), false)
6981
+ .unwrap();
6779
6982
 
6780
6983
  assert_eq!(s.pipe.advance(), Ok(()));
6781
6984
 
@@ -6789,36 +6992,23 @@ mod tests {
6789
6992
  fn set_prohibited_additional_settings() {
6790
6993
  let mut h3_config = Config::new().unwrap();
6791
6994
  assert_eq!(
6792
- h3_config.set_additional_settings(vec![(
6793
- frame::SETTINGS_QPACK_MAX_TABLE_CAPACITY,
6794
- 43
6795
- )]),
6995
+ h3_config.set_additional_settings(vec![(frame::SETTINGS_QPACK_MAX_TABLE_CAPACITY, 43)]),
6796
6996
  Err(Error::SettingsError)
6797
6997
  );
6798
6998
  assert_eq!(
6799
- h3_config.set_additional_settings(vec![(
6800
- frame::SETTINGS_MAX_FIELD_SECTION_SIZE,
6801
- 43
6802
- )]),
6999
+ h3_config.set_additional_settings(vec![(frame::SETTINGS_MAX_FIELD_SECTION_SIZE, 43)]),
6803
7000
  Err(Error::SettingsError)
6804
7001
  );
6805
7002
  assert_eq!(
6806
- h3_config.set_additional_settings(vec![(
6807
- frame::SETTINGS_QPACK_BLOCKED_STREAMS,
6808
- 43
6809
- )]),
7003
+ h3_config.set_additional_settings(vec![(frame::SETTINGS_QPACK_BLOCKED_STREAMS, 43)]),
6810
7004
  Err(Error::SettingsError)
6811
7005
  );
6812
7006
  assert_eq!(
6813
- h3_config.set_additional_settings(vec![(
6814
- frame::SETTINGS_ENABLE_CONNECT_PROTOCOL,
6815
- 43
6816
- )]),
7007
+ h3_config.set_additional_settings(vec![(frame::SETTINGS_ENABLE_CONNECT_PROTOCOL, 43)]),
6817
7008
  Err(Error::SettingsError)
6818
7009
  );
6819
7010
  assert_eq!(
6820
- h3_config
6821
- .set_additional_settings(vec![(frame::SETTINGS_H3_DATAGRAM, 43)]),
7011
+ h3_config.set_additional_settings(vec![(frame::SETTINGS_H3_DATAGRAM, 43)]),
6822
7012
  Err(Error::SettingsError)
6823
7013
  );
6824
7014
  }
@@ -6852,11 +7042,8 @@ mod tests {
6852
7042
  Some(&crate::ConnectionError {
6853
7043
  is_app: true,
6854
7044
  error_code: WireErrorCode::FrameUnexpected as u64,
6855
- reason: format!(
6856
- "Unexpected frame type {}",
6857
- frame::SETTINGS_FRAME_TYPE_ID
6858
- )
6859
- .into_bytes(),
7045
+ reason: format!("Unexpected frame type {}", frame::SETTINGS_FRAME_TYPE_ID)
7046
+ .into_bytes(),
6860
7047
  })
6861
7048
  );
6862
7049
  }
@@ -6879,11 +7066,8 @@ mod tests {
6879
7066
  Some(&crate::ConnectionError {
6880
7067
  is_app: true,
6881
7068
  error_code: WireErrorCode::FrameUnexpected as u64,
6882
- reason: format!(
6883
- "Unexpected frame type {}",
6884
- frame::CANCEL_PUSH_FRAME_TYPE_ID
6885
- )
6886
- .into_bytes(),
7069
+ reason: format!("Unexpected frame type {}", frame::CANCEL_PUSH_FRAME_TYPE_ID)
7070
+ .into_bytes(),
6887
7071
  })
6888
7072
  );
6889
7073
  }
@@ -6907,11 +7091,8 @@ mod tests {
6907
7091
  Some(&crate::ConnectionError {
6908
7092
  is_app: true,
6909
7093
  error_code: WireErrorCode::FrameUnexpected as u64,
6910
- reason: format!(
6911
- "Unexpected frame type {}",
6912
- frame::GOAWAY_FRAME_TYPE_ID
6913
- )
6914
- .into_bytes(),
7094
+ reason: format!("Unexpected frame type {}", frame::GOAWAY_FRAME_TYPE_ID)
7095
+ .into_bytes(),
6915
7096
  })
6916
7097
  );
6917
7098
  }
@@ -6935,11 +7116,8 @@ mod tests {
6935
7116
  Some(&crate::ConnectionError {
6936
7117
  is_app: true,
6937
7118
  error_code: WireErrorCode::FrameUnexpected as u64,
6938
- reason: format!(
6939
- "Unexpected frame type {}",
6940
- frame::MAX_PUSH_FRAME_TYPE_ID
6941
- )
6942
- .into_bytes(),
7119
+ reason: format!("Unexpected frame type {}", frame::MAX_PUSH_FRAME_TYPE_ID)
7120
+ .into_bytes(),
6943
7121
  })
6944
7122
  );
6945
7123
  }
@@ -6984,11 +7162,11 @@ mod tests {
6984
7162
 
6985
7163
  assert_eq!(
6986
7164
  s.server.peer_settings_raw(),
6987
- Some(&[(42, 43), (44, 45)][..])
7165
+ Some(&[(6, 32_768), (42, 43), (44, 45)][..])
6988
7166
  );
6989
7167
  assert_eq!(
6990
7168
  s.client.peer_settings_raw(),
6991
- Some(&[(42, 43), (44, 45)][..])
7169
+ Some(&[(6, 32_768), (42, 43), (44, 45)][..])
6992
7170
  );
6993
7171
  }
6994
7172
 
@@ -7357,6 +7535,104 @@ mod tests {
7357
7535
  assert_eq!(s.poll_server(), Err(Error::Done));
7358
7536
  }
7359
7537
 
7538
+ #[test]
7539
+ fn unknown_uni_stream_leaks_past_max_streams_uni() {
7540
+ let (mut config, h3_config) = Session::default_configs().unwrap();
7541
+ config.set_initial_max_data(100_000);
7542
+ config.set_initial_max_stream_data_uni(100_000);
7543
+ config.set_initial_max_streams_uni(5);
7544
+ config.grease(false);
7545
+
7546
+ let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7547
+ s.handshake().unwrap();
7548
+
7549
+ let baseline = s.server.streams.len();
7550
+ let mut leaked_streams = Vec::new();
7551
+ let mut id = 14;
7552
+
7553
+ let n = 500;
7554
+ for i in 0..n {
7555
+ s.pipe
7556
+ .client
7557
+ .stream_send(id, &[0x21], true)
7558
+ .unwrap_or_else(|e| {
7559
+ panic!(
7560
+ "stream credit was not re-issued after {} streams: {:?}",
7561
+ i, e
7562
+ )
7563
+ });
7564
+
7565
+ s.pipe.advance().unwrap();
7566
+ assert_eq!(s.poll_server(), Err(Error::Done));
7567
+ assert!(s.pipe.server.streams.is_collected(id));
7568
+ s.pipe.advance().unwrap();
7569
+
7570
+ leaked_streams.push(id);
7571
+ id += 4;
7572
+ }
7573
+
7574
+ for id in leaked_streams {
7575
+ assert!(s.pipe.server.streams.is_collected(id));
7576
+ }
7577
+
7578
+ assert_eq!(
7579
+ s.server.streams.len(),
7580
+ baseline,
7581
+ "expected {} allocated streams in stream map after {} streams with unknown type",
7582
+ baseline,
7583
+ n,
7584
+ );
7585
+ }
7586
+
7587
+ #[test]
7588
+ fn empty_uni_stream_leaks_past_max_streams_uni() {
7589
+ let (mut config, h3_config) = Session::default_configs().unwrap();
7590
+ config.set_initial_max_data(100_000);
7591
+ config.set_initial_max_stream_data_uni(100_000);
7592
+ config.set_initial_max_streams_uni(5);
7593
+ config.grease(false);
7594
+
7595
+ let mut s = Session::with_configs(&mut config, &h3_config).unwrap();
7596
+ s.handshake().unwrap();
7597
+
7598
+ let baseline = s.server.streams.len();
7599
+ let mut leaked_streams = Vec::new();
7600
+ let mut id = 14;
7601
+
7602
+ let n = 500;
7603
+ for i in 0..n {
7604
+ s.pipe
7605
+ .client
7606
+ .stream_send(id, &[], true)
7607
+ .unwrap_or_else(|e| {
7608
+ panic!(
7609
+ "stream credit was not re-issued after {} streams: {:?}",
7610
+ i, e
7611
+ )
7612
+ });
7613
+
7614
+ s.pipe.advance().unwrap();
7615
+ assert_eq!(s.poll_server(), Err(Error::Done));
7616
+ assert!(s.pipe.server.streams.is_collected(id));
7617
+ s.pipe.advance().unwrap();
7618
+
7619
+ leaked_streams.push(id);
7620
+ id += 4;
7621
+ }
7622
+
7623
+ for id in leaked_streams {
7624
+ assert!(s.pipe.server.streams.is_collected(id));
7625
+ }
7626
+
7627
+ assert_eq!(
7628
+ s.server.streams.len(),
7629
+ baseline,
7630
+ "expected {} allocated streams in stream map after {} streams without stream type",
7631
+ baseline,
7632
+ n,
7633
+ );
7634
+ }
7635
+
7360
7636
  #[test]
7361
7637
  /// Tests that streams are marked as finished only once.
7362
7638
  fn finished_once() {
@@ -7411,10 +7687,7 @@ mod tests {
7411
7687
  let off = b.off();
7412
7688
  s.pipe.client.stream_send(r1_id, &d[..off], false).unwrap();
7413
7689
 
7414
- assert_eq!(
7415
- s.pipe.client.stream_send(r1_id, &bytes[..5], false),
7416
- Ok(5)
7417
- );
7690
+ assert_eq!(s.pipe.client.stream_send(r1_id, &bytes[..5], false), Ok(5));
7418
7691
 
7419
7692
  s.advance().ok();
7420
7693
  }
@@ -7445,7 +7718,7 @@ mod tests {
7445
7718
 
7446
7719
  assert_eq!(s.recv_body_server(r1_id, &mut recv_buf), Ok(r1_body.len()));
7447
7720
 
7448
- // Send a new request to ensure cross-stream events don't break rearming.
7721
+ // Send a new request to test cross-stream event rearming.
7449
7722
  let (r2_id, r2_hdrs) = s.send_request(false).unwrap();
7450
7723
  let r2_ev_headers = Event::Headers {
7451
7724
  list: r2_hdrs,
@@ -7509,7 +7782,7 @@ mod tests {
7509
7782
  more_frames: true,
7510
7783
  };
7511
7784
 
7512
- // Manually send an incomplete DATA frame (i.e. only the header is sent).
7785
+ // Manually send an incomplete DATA frame containing only its header.
7513
7786
  {
7514
7787
  let mut d = [42; 10];
7515
7788
  let mut b = octets::OctetsMut::with_slice(&mut d);
@@ -7697,6 +7970,7 @@ mod tests {
7697
7970
  stream_id: stream,
7698
7971
  error_code: 42,
7699
7972
  final_size: 68,
7973
+ reliable_size: None,
7700
7974
  }];
7701
7975
 
7702
7976
  let pkt_type = crate::packet::Type::Short;
@@ -7718,6 +7992,146 @@ mod tests {
7718
7992
  assert_eq!(s.poll_server(), Err(Error::Done));
7719
7993
  }
7720
7994
 
7995
+ #[test]
7996
+ fn reset_mid_data_frame() {
7997
+ let mut s = Session::new().unwrap();
7998
+ s.handshake().unwrap();
7999
+
8000
+ // Client sends request.
8001
+ let (stream, req) = s.send_request(false).unwrap();
8002
+
8003
+ let ev_headers = Event::Headers {
8004
+ list: req,
8005
+ more_frames: true,
8006
+ };
8007
+
8008
+ // Server sends response
8009
+ assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8010
+ assert_eq!(s.poll_server(), Err(Error::Done));
8011
+
8012
+ let resp = s.send_response(stream, false).unwrap();
8013
+
8014
+ let ev_headers = Event::Headers {
8015
+ list: resp,
8016
+ more_frames: true,
8017
+ };
8018
+
8019
+ assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8020
+ assert_eq!(s.poll_client(), Err(Error::Done));
8021
+
8022
+ // We want to indicate a larger DATA frame than we will send, so
8023
+ // sidestep quiche's h3 API and write H3 frame bytes directly to QUIC
8024
+ // stream. Imagine a response body of 200 bytes, that would be sent
8025
+ // across two DATA frames of 100 bytes, but quiche's stream write is
8026
+ // limited to only emitting 95 bytes of the DATA frame before the stream
8027
+ // is reset.
8028
+ let mut d = [42; 10];
8029
+ let mut b = octets::OctetsMut::with_slice(&mut d);
8030
+ b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
8031
+ b.put_varint(100).unwrap();
8032
+ let off = b.off();
8033
+ s.pipe.server.stream_send(0, &d[..off], false).unwrap();
8034
+ let body = [42; 200];
8035
+ let written = s.pipe.server.stream_send(0, &body[..200], false).unwrap();
8036
+
8037
+ assert_eq!(written, 95);
8038
+
8039
+ s.advance().ok();
8040
+
8041
+ assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8042
+ assert_eq!(s.poll_client(), Err(Error::Done));
8043
+
8044
+ let mut read_buf = [0; 65535];
8045
+ let read = s
8046
+ .client
8047
+ .recv_body(&mut s.pipe.client, 0, &mut read_buf)
8048
+ .unwrap();
8049
+ assert_eq!(read, 95);
8050
+
8051
+ s.pipe
8052
+ .server
8053
+ .stream_shutdown(0, crate::Shutdown::Write, 42)
8054
+ .unwrap();
8055
+
8056
+ s.advance().ok();
8057
+
8058
+ assert_eq!(s.poll_client(), Ok((stream, Event::Reset(42))));
8059
+
8060
+ // Reading the body after the reset does not queue a Finished event.
8061
+ assert_eq!(
8062
+ s.client.recv_body(&mut s.pipe.client, 0, &mut read_buf),
8063
+ Err(Error::Done)
8064
+ );
8065
+ assert_eq!(s.poll_client(), Err(Error::Done));
8066
+ }
8067
+
8068
+ #[test]
8069
+ fn fin_mid_data_frame() {
8070
+ let mut s = Session::new().unwrap();
8071
+ s.handshake().unwrap();
8072
+
8073
+ // Client sends request.
8074
+ let (stream, req) = s.send_request(false).unwrap();
8075
+
8076
+ let ev_headers = Event::Headers {
8077
+ list: req,
8078
+ more_frames: true,
8079
+ };
8080
+
8081
+ // Server sends response
8082
+ assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
8083
+ assert_eq!(s.poll_server(), Err(Error::Done));
8084
+
8085
+ let resp = s.send_response(stream, false).unwrap();
8086
+
8087
+ let ev_headers = Event::Headers {
8088
+ list: resp,
8089
+ more_frames: true,
8090
+ };
8091
+
8092
+ assert_eq!(s.poll_client(), Ok((stream, ev_headers)));
8093
+ assert_eq!(s.poll_client(), Err(Error::Done));
8094
+
8095
+ // We want to indicate a larger DATA frame than we will send, so
8096
+ // sidestep quiche's h3 API and write H3 frame bytes directly to QUIC
8097
+ // stream. Imagine a response body of 200 bytes, that would be sent
8098
+ // across two DATA frames of 100 bytes, but quiche's stream write is
8099
+ // limited to only emitting 95 bytes of the DATA frame before the stream
8100
+ // is Fin'd.
8101
+ let mut d = [42; 10];
8102
+ let mut b = octets::OctetsMut::with_slice(&mut d);
8103
+ b.put_varint(frame::DATA_FRAME_TYPE_ID).unwrap();
8104
+ b.put_varint(100).unwrap();
8105
+ let off = b.off();
8106
+ s.pipe.server.stream_send(0, &d[..off], false).unwrap();
8107
+ let body = [42; 200];
8108
+ let written = s.pipe.server.stream_send(0, &body[..200], false).unwrap();
8109
+
8110
+ assert_eq!(written, 95);
8111
+
8112
+ s.advance().ok();
8113
+
8114
+ assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8115
+ assert_eq!(s.poll_client(), Err(Error::Done));
8116
+
8117
+ let mut read_buf = [0; 65535];
8118
+ let read = s
8119
+ .client
8120
+ .recv_body(&mut s.pipe.client, 0, &mut read_buf)
8121
+ .unwrap();
8122
+ assert_eq!(read, 95);
8123
+
8124
+ let written = s.pipe.server.stream_send(0, &[], true).unwrap();
8125
+
8126
+ assert_eq!(written, 0);
8127
+
8128
+ s.advance().ok();
8129
+
8130
+ assert_eq!(s.poll_client(), Ok((stream, Event::Data)));
8131
+ assert_eq!(s.poll_client(), Ok((stream, Event::Finished)));
8132
+ assert_eq!(s.poll_client(), Err(Error::Done));
8133
+ }
8134
+
7721
8135
  /// The client shuts down the stream's write direction, the server
7722
8136
  /// shuts down its side with fin
7723
8137
  #[test]
@@ -7961,11 +8375,13 @@ mod tests {
7961
8375
  assert_eq!(s.pipe.server.readable().len(), 0);
7962
8376
  }
7963
8377
 
7964
- #[test]
7965
- fn reset_finished_at_server_with_data_pending_2() {
8378
+ #[rstest::rstest]
8379
+ fn reset_finished_at_server_with_data_pending_2(#[values(true, false)] local_fin: bool) {
7966
8380
  let mut s = Session::new().unwrap();
7967
8381
  s.handshake().unwrap();
7968
8382
 
8383
+ let init_streams_server = s.server.streams.len();
8384
+
7969
8385
  // Client sends HEADERS and doesn't fin.
7970
8386
  let (stream, req) = s.send_request(false).unwrap();
7971
8387
 
@@ -7982,6 +8398,8 @@ mod tests {
7982
8398
  assert_eq!(s.poll_server(), Ok((stream, ev_headers)));
7983
8399
  assert_eq!(s.poll_server(), Ok((stream, Event::Data)));
7984
8400
 
8401
+ s.send_response(stream, local_fin).unwrap();
8402
+
7985
8403
  // ..then Client sends RESET_STREAM.
7986
8404
  assert_eq!(
7987
8405
  s.pipe
@@ -8002,6 +8420,18 @@ mod tests {
8002
8420
  // No more events and there are no more readable streams.
8003
8421
  assert_eq!(s.poll_server(), Err(Error::Done));
8004
8422
  assert_eq!(s.pipe.server.readable().len(), 0);
8423
+
8424
+ // Reading the body again does not queue a Finished event.
8425
+ assert_eq!(s.recv_body_server(stream, &mut [0; 100]), Err(Error::Done));
8426
+ assert_eq!(s.poll_server(), Err(Error::Done));
8427
+
8428
+ if !local_fin {
8429
+ assert_eq!(s.server.streams.len(), init_streams_server + 1);
8430
+
8431
+ s.send_body_server(stream, true).unwrap();
8432
+ }
8433
+
8434
+ assert_eq!(s.server.streams.len(), init_streams_server);
8005
8435
  }
8006
8436
 
8007
8437
  #[test]
@@ -8064,6 +8494,7 @@ mod tests {
8064
8494
  stream_id: stream,
8065
8495
  error_code: 42,
8066
8496
  final_size: 68,
8497
+ reliable_size: None,
8067
8498
  }];
8068
8499
 
8069
8500
  let pkt_type = crate::packet::Type::Short;