turnloop-http 0.1.0-beta.2

Sans-I/O HTTP/1.1 and HTTP/2 client and server protocols
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
//! RFC 9113 connection/stream framing. Caller retains partial frames and acknowledges
//! writes. DATA is borrowed, flow-control credit is returned explicitly by the host.
use crate::{Error, Result, hpack, http1::Header};
use std::{collections::VecDeque, time::Instant};
pub const PREFACE: &[u8] = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
    Client,
    Server,
}
#[derive(Debug, Clone, Copy)]
pub struct Limits {
    pub frame_size: usize,
    pub header_block: usize,
    pub header_list: usize,
    pub continuations: usize,
    pub streams: usize,
}
impl Default for Limits {
    fn default() -> Self {
        Self {
            frame_size: 16384,
            header_block: 65536,
            header_list: 32768,
            continuations: 16,
            streams: 100,
        }
    }
}
/// SETTINGS parameters this endpoint sends: at most one value per identifier,
/// kept and serialised in ascending identifier order (the order Node writes
/// them in). An empty `Settings` is a valid, empty SETTINGS frame.
///
/// Identifiers this crate does not know are sent as given, so a host can
/// advertise extension settings; the named constants are the ones RFC 9113
/// section 6.5.2 and RFC 8441 define.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Settings {
    entries: Vec<(u16, u32)>,
}
impl Settings {
    pub const HEADER_TABLE_SIZE: u16 = 1;
    pub const ENABLE_PUSH: u16 = 2;
    pub const MAX_CONCURRENT_STREAMS: u16 = 3;
    pub const INITIAL_WINDOW_SIZE: u16 = 4;
    pub const MAX_FRAME_SIZE: u16 = 5;
    pub const MAX_HEADER_LIST_SIZE: u16 = 6;
    pub const ENABLE_CONNECT_PROTOCOL: u16 = 8;
    pub fn new() -> Self {
        Self::default()
    }
    /// The three limits [`Connection::new`] advertises.
    pub fn from_limits(limits: &Limits) -> Self {
        let mut settings = Self::new();
        settings
            .set(Self::MAX_CONCURRENT_STREAMS, limits.streams as u32)
            .set(Self::MAX_FRAME_SIZE, limits.frame_size as u32)
            .set(Self::MAX_HEADER_LIST_SIZE, limits.header_list as u32);
        settings
    }
    /// Set `id` to `value`, replacing any earlier value for it.
    pub fn set(&mut self, id: u16, value: u32) -> &mut Self {
        match self.entries.binary_search_by_key(&id, |e| e.0) {
            Ok(i) => self.entries[i].1 = value,
            Err(i) => self.entries.insert(i, (id, value)),
        }
        self
    }
    pub fn remove(&mut self, id: u16) -> Option<u32> {
        let i = self.entries.binary_search_by_key(&id, |e| e.0).ok()?;
        Some(self.entries.remove(i).1)
    }
    pub fn get(&self, id: u16) -> Option<u32> {
        let i = self.entries.binary_search_by_key(&id, |e| e.0).ok()?;
        Some(self.entries[i].1)
    }
    /// `(identifier, value)` pairs in ascending identifier order.
    pub fn iter(&self) -> impl Iterator<Item = (u16, u32)> + '_ {
        self.entries.iter().copied()
    }
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
    fn merge(&mut self, from: impl Iterator<Item = (u16, u32)>) {
        for (id, value) in from {
            self.set(id, value);
        }
    }
    fn encode(&self) -> Vec<u8> {
        let mut payload = Vec::with_capacity(self.entries.len() * 6);
        for (id, value) in self.iter() {
            payload.extend_from_slice(&id.to_be_bytes());
            payload.extend_from_slice(&value.to_be_bytes());
        }
        payload
    }
    /// Refuse what this endpoint cannot honour once the peer acts on it.
    fn validate(&self, role: Role) -> Result<()> {
        for (id, value) in self.iter() {
            let valid = match id {
                Self::HEADER_TABLE_SIZE => true,
                // Only a client may invite pushes (RFC 9113 section 6.5.2); a
                // server never sends a non-zero ENABLE_PUSH.
                Self::ENABLE_PUSH => match role {
                    Role::Client => value <= 1,
                    Role::Server => value == 0,
                },
                Self::INITIAL_WINDOW_SIZE => value <= 0x7fffffff,
                Self::MAX_FRAME_SIZE => (16384..=0xffffff).contains(&value),
                Self::ENABLE_CONNECT_PROTOCOL => value <= 1,
                _ => true,
            };
            if !valid {
                return Err(protocol("invalid SETTINGS value"));
            }
        }
        Ok(())
    }
}
/// The parameters of one SETTINGS frame the peer sent, borrowed from the input
/// exactly as they were on the wire: in frame order, duplicates and unknown
/// identifiers included. RFC 9113 section 6.5.3 processes them in that order,
/// so for a repeated identifier the last value is the one in force.
#[derive(Debug, Clone, Copy)]
pub struct SettingsFrame<'a> {
    payload: &'a [u8],
}
impl<'a> SettingsFrame<'a> {
    pub fn iter(&self) -> impl Iterator<Item = (u16, u32)> + 'a {
        self.payload
            .as_chunks::<6>()
            .0
            .iter()
            .map(|s| (u16::from_be_bytes([s[0], s[1]]), u32be(&s[2..])))
    }
    /// The value in force for `id` after this frame, if the frame carried it.
    pub fn get(&self, id: u16) -> Option<u32> {
        self.iter().filter(|e| e.0 == id).last().map(|e| e.1)
    }
    pub fn is_empty(&self) -> bool {
        self.payload.is_empty()
    }
    pub fn to_settings(&self) -> Settings {
        let mut settings = Settings::new();
        settings.merge(self.iter());
        settings
    }
}
#[derive(Debug, Clone, Copy)]
pub struct Frame<'a> {
    pub kind: u8,
    pub flags: u8,
    pub stream: u32,
    pub payload: &'a [u8],
}
fn error(code: &'static str, message: &'static str) -> Error {
    Error::new(code, message)
}
fn protocol(message: &'static str) -> Error {
    error("PROTOCOL_ERROR", message)
}
fn frame_error() -> Error {
    error("FRAME_SIZE_ERROR", "invalid frame size")
}
fn u32be(b: &[u8]) -> u32 {
    u32::from_be_bytes(b[..4].try_into().unwrap())
}
pub fn decode_frame(input: &[u8], max: usize) -> Result<Option<Frame<'_>>> {
    if input.len() < 9 {
        return Ok(None);
    }
    let len = ((input[0] as usize) << 16) | ((input[1] as usize) << 8) | input[2] as usize;
    if len > max {
        return Err(frame_error());
    }
    if input.len() < 9 + len {
        return Ok(None);
    }
    Ok(Some(Frame {
        kind: input[3],
        flags: input[4],
        stream: u32be(&input[5..9]) & 0x7fffffff,
        payload: &input[9..9 + len],
    }))
}
pub fn encode_frame(
    kind: u8,
    flags: u8,
    stream: u32,
    payload: &[u8],
    out: &mut Vec<u8>,
) -> Result<()> {
    if payload.len() > 0xffffff || stream > 0x7fffffff {
        return Err(frame_error());
    }
    let len = payload.len() as u32;
    out.extend_from_slice(&len.to_be_bytes()[1..]);
    out.push(kind);
    out.push(flags);
    out.extend_from_slice(&stream.to_be_bytes());
    out.extend_from_slice(payload);
    Ok(())
}
/// Which of the three header blocks an [`Event::Headers`] carries. The
/// connection already enforces the distinction, so a host does not have to
/// track `received_head` itself to tell them apart.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HeadersKind {
    /// The request head, or a final (>= 200) response head.
    Head,
    /// A 1xx response head. It never ends the stream, and a final head follows.
    Informational,
    /// A trailer block. It always ends the stream.
    Trailers,
}
#[derive(Debug)]
pub enum Event<'a> {
    /// The peer's SETTINGS, already applied and acknowledged.
    Settings(SettingsFrame<'a>),
    /// The peer acknowledged the oldest SETTINGS frame this endpoint still had
    /// outstanding; its parameters are returned here and are now in force.
    /// Acknowledgements arrive in the order the frames were sent (RFC 9113
    /// section 6.5.3), and the first is always for the frame the constructor
    /// queued, so a host that timestamps construction and each
    /// [`Connection::settings`] call can pair them in FIFO order to measure a
    /// round trip. No clock is read here.
    SettingsAck(Settings),
    Headers {
        stream: u32,
        headers: Vec<Header>,
        end_stream: bool,
        kind: HeadersKind,
    },
    Data {
        stream: u32,
        bytes: &'a [u8],
        end_stream: bool,
    },
    /// The server promised a response to the request `headers` (RFC 9113
    /// section 8.4), on `stream`, a stream this endpoint opened. `promised` is
    /// the server-initiated stream that will carry the response: its HEADERS
    /// and DATA arrive as ordinary events, and [`Connection::reset`] declines
    /// it. Only a client receives this, and only while it has not disabled push
    /// with ENABLE_PUSH = 0.
    PushPromise {
        stream: u32,
        promised: u32,
        headers: Vec<Header>,
    },
    /// The stream is over. It may name a stream the host never saw opened: a
    /// stream past the concurrent-stream limit is refused with
    /// RST_STREAM(REFUSED_STREAM) before any [`Event::Headers`] for it is
    /// produced (RFC 9113 section 5.1.2 requires a stream error there, and
    /// Node answers the same way — measured).
    Reset {
        stream: u32,
        code: u32,
    },
    /// The peer opened a stream after a graceful GOAWAY, above the last stream
    /// id that GOAWAY named, and **nothing has been sent for it**. RFC 9113
    /// section 6.8 makes such a stream simply "not processed" — the peer is
    /// expected to retry it on a new connection — and Node emits no frame at
    /// all here (measured against Node 26.5.1 with a raw peer: no RST_STREAM,
    /// no GOAWAY, the session stays alive and the request never reaches the
    /// application).
    ///
    /// The header block was decoded and discarded, because HPACK is connection
    /// state. The connection deliberately does not answer for the host: a frame
    /// it emitted could not be un-emitted, so a host that must match Node would
    /// have no way back. A host that wants to answer may call
    /// [`Connection::reset`].
    Unprocessed {
        stream: u32,
    },
    Goaway {
        last_stream: u32,
        code: u32,
        /// The opaque debug data (RFC 9113 section 6.8). `None` when the frame
        /// carried none - Node reports `undefined` there, not an empty buffer.
        /// The wire cannot express "present but empty", so this is never
        /// `Some` of an empty slice.
        debug: Option<&'a [u8]>,
    },
    Ping {
        ack: bool,
        data: [u8; 8],
    },
    WindowUpdate {
        stream: u32,
    },
}
/// One decode step: how much of `input` was consumed, and the event it
/// produced, if any. See the [step contract](crate#the-step-contract).
///
/// **`consumed` and `event` are independent.** All three shapes are normal:
///
/// | `consumed` | `event` | meaning |
/// |---|---|---|
/// | `0` | `None` | **stop.** A partial preface or a partial frame; read more input before calling again. |
/// | `> 0` | `None` | **keep going.** Progress with nothing for the host: the client preface, PRIORITY, an unknown frame type, or a frame the peer had in flight for a stream that is already gone. |
/// | `> 0` | `Some` | an event. In HTTP/2 an event always consumes; `consumed == 0` is only ever the stop case. |
///
/// So the loop condition is `consumed > 0 || event.is_some()`:
///
/// ```
/// # use turnloop_http::http2::{Connection, Limits, Role, PREFACE};
/// # let mut connection = Connection::new(Role::Server, Limits::default())?;
/// # let mut input = PREFACE.to_vec();
/// loop {
///     let step = connection.receive(&input)?;
///     let consumed = step.consumed;
///     let progressed = consumed > 0 || step.event.is_some();
///     if let Some(event) = step.event {
///         // DATA borrows `input`, so handle the event before draining.
///         let _ = event;
///     }
///     input.drain(..consumed);
///     if !progressed {
///         break; // read more bytes from the transport, then continue
///     }
/// }
/// # Ok::<(), turnloop_http::Error>(())
/// ```
///
/// Looping while `event.is_some()` instead **stalls at the client preface** —
/// the first step of every server connection, before a single frame is read.
/// Looping while input remains spins forever on a partial frame.
#[derive(Debug)]
pub struct Step<'a> {
    pub consumed: usize,
    pub event: Option<Event<'a>>,
}
/// What to do with a header block that does not belong to a live stream. Every
/// arm decodes the block first: HPACK is connection state, and a block that is
/// skipped desynchronises every block after it.
#[derive(Debug, Clone, Copy)]
enum Disposal {
    /// Past the concurrent-stream limit: answer RST_STREAM and report it.
    /// RFC 9113 section 5.1.2 requires a stream error here.
    Refuse(u32),
    /// Opened after our graceful GOAWAY: send nothing, report it, and let the
    /// host decide. See [`Event::Unprocessed`].
    Unprocessed,
    /// Frames the peer had in flight for a stream that is gone: discard in
    /// silence. The host already knows - it is the side that ended the stream.
    Discard,
}
#[derive(Debug)]
struct Stream {
    id: u32,
    send_window: i64,
    recv_window: i64,
    unreleased: u32,
    /// The peer reset this stream and its remaining connection credit was
    /// returned in bulk, so a late `release_capacity` for it is a no-op.
    credited: bool,
    local_end: bool,
    remote_end: bool,
    received_head: bool,
    sent_head: bool,
    recv_length: Option<u64>,
    received: u64,
    head_request: bool,
    recv_no_body: bool,
    send_no_body: bool,
    send_length: Option<u64>,
    sent: u64,
}
impl Stream {
    fn closed(&self) -> bool {
        self.local_end && self.remote_end
    }
}
pub struct Connection {
    role: Role,
    limits: Limits,
    streams: Vec<Stream>,
    next_id: u32,
    /// Highest remote stream id this connection has accepted **for processing**.
    /// GOAWAY names it, so a stream that was reported but not processed - one
    /// refused past the stream limit, or arriving after our own GOAWAY - must
    /// not advance it: RFC 9113 section 6.8 lets the peer retry everything above
    /// it on a new connection, and claiming one is processed loses that request.
    last_remote: u32,
    /// Highest remote stream id seen on the wire, processed or not. The
    /// monotonicity check and [`Connection::seen`] use this, so frames the peer
    /// had in flight for a stream we declined are still tolerated.
    highest_remote: u32,
    send_window: i64,
    recv_window: i64,
    initial_send: i64,
    peer_frame: usize,
    peer_streams: usize,
    preface: bool,
    settings_received: bool,
    /// SETTINGS frames sent and not yet acknowledged, oldest first. RFC 9113
    /// section 6.5.3 acknowledges them in order, so this is a queue: a second
    /// frame sent while one is outstanding is ordinary, not an error.
    settings_pending: VecDeque<Settings>,
    /// Every local parameter the peer has acknowledged, merged in order.
    local_settings: Settings,
    /// Every parameter the peer has sent, merged in order.
    remote_settings: Settings,
    /// The limits the acknowledged frames establish. `limits`, the ones
    /// enforced, is this relaxed by every outstanding frame: see
    /// `enforce_settings`.
    acked_limits: Limits,
    /// Our SETTINGS_INITIAL_WINDOW_SIZE as acknowledged, and as enforced.
    acked_recv_initial: i64,
    acked_table: usize,
    /// Whether this endpoint accepts PUSH_PROMISE: ENABLE_PUSH as acknowledged,
    /// loosened by every outstanding frame, defaulting to enabled.
    acked_push: bool,
    push_allowed: bool,
    recv_initial: i64,
    decoder: hpack::Decoder,
    encoder: hpack::Encoder,
    block: Vec<u8>,
    scratch: Vec<u8>,
    continuation: Option<(u32, bool, usize)>,
    /// What to do with the header block being assembled, when it does not
    /// belong to a live stream. HPACK is connection state, so such a block
    /// still has to advance the decoder before it is thrown away.
    disposal: Option<Disposal>,
    /// PINGs sent and not yet acknowledged. An acknowledgement with none
    /// outstanding answers nothing this endpoint sent.
    pings: usize,
    /// Whether an acknowledgement with no PING outstanding fails the connection.
    reject_unsolicited_ping_ack: bool,
    /// The PUSH_PROMISE whose header block is being assembled: the promised
    /// stream and whether the block is only to be decoded and dropped (the
    /// associated stream is gone).
    promise: Option<(u32, bool)>,
    output: Vec<u8>,
    output_pos: usize,
    draining: bool,
    failed: bool,
    settings_deadline: Option<Instant>,
}
impl Connection {
    /// A connection whose initial SETTINGS advertises `limits`: the
    /// concurrent-stream, frame-size and header-list limits, in that
    /// (ascending) identifier order.
    pub fn new(role: Role, limits: Limits) -> Result<Self> {
        Self::with_settings(role, limits, &Settings::from_limits(&limits))
    }
    /// A connection whose initial SETTINGS frame carries exactly `settings`,
    /// serialised in ascending identifier order. `Settings::new()` sends an
    /// empty frame, which is what Node's server sends.
    ///
    /// `limits` is what this endpoint enforces on its own. A parameter in
    /// `settings` that corresponds to a limit replaces it once the peer
    /// acknowledges the frame (and at once, if that relaxes it: the peer may
    /// act on the new value before its ack reaches us). A client that
    /// does not name ENABLE_PUSH leaves push enabled, as the protocol defaults
    /// it, and surfaces each promise as [`Event::PushPromise`].
    pub fn with_settings(role: Role, limits: Limits, settings: &Settings) -> Result<Self> {
        if !(16384..=0xffffff).contains(&limits.frame_size)
            || limits.streams == 0
            || limits.streams > u32::MAX as usize
        {
            return Err(protocol("invalid limits"));
        }
        settings.validate(role)?;
        let settings = settings.clone();
        let mut result = Self {
            role,
            limits,
            streams: Vec::with_capacity(limits.streams),
            next_id: if role == Role::Client { 1 } else { 2 },
            last_remote: 0,
            highest_remote: 0,
            send_window: 65535,
            recv_window: 65535,
            initial_send: 65535,
            peer_frame: 16384,
            peer_streams: usize::MAX,
            preface: role == Role::Client,
            settings_received: false,
            settings_pending: VecDeque::new(),
            local_settings: Settings::new(),
            remote_settings: Settings::new(),
            acked_limits: limits,
            acked_recv_initial: 65535,
            acked_table: 4096,
            acked_push: true,
            push_allowed: true,
            recv_initial: 65535,
            decoder: hpack::Decoder::new(4096, limits.header_list),
            encoder: hpack::Encoder::new(4096),
            block: Vec::new(),
            scratch: Vec::new(),
            continuation: None,
            disposal: None,
            pings: 0,
            reject_unsolicited_ping_ack: false,
            promise: None,
            output: Vec::new(),
            output_pos: 0,
            draining: false,
            failed: false,
            settings_deadline: None,
        };
        if role == Role::Client {
            result.output.extend_from_slice(PREFACE);
        }
        result.send_settings(settings)?;
        Ok(result)
    }
    /// Send a SETTINGS frame carrying `settings` to a live connection -
    /// Node's `session.settings()`. It may be called while earlier frames
    /// are still unacknowledged; each is acknowledged in turn with
    /// [`Event::SettingsAck`].
    ///
    /// A parameter that loosens what this endpoint enforces - a larger
    /// MAX_FRAME_SIZE, INITIAL_WINDOW_SIZE, MAX_HEADER_LIST_SIZE or
    /// MAX_CONCURRENT_STREAMS - applies at once, because the peer may act on
    /// it before its acknowledgement arrives. One that tightens applies when
    /// the peer acknowledges the frame (RFC 9113 section 6.5.3), so frames the
    /// peer sent under the old value are never treated as errors.
    ///
    /// Values this endpoint could not honour are refused before anything is
    /// sent: ENABLE_PUSH above 1 from a client, anything but 0 from a server, and the
    /// out-of-range values RFC 9113 section 6.5.2 forbids.
    pub fn settings(&mut self, settings: &Settings) -> Result<()> {
        if self.failed {
            return Err(protocol("failed connection"));
        }
        settings.validate(self.role)?;
        self.send_settings(settings.clone())
    }
    fn send_settings(&mut self, settings: Settings) -> Result<()> {
        let payload = settings.encode();
        if payload.len() > self.peer_frame {
            return Err(frame_error());
        }
        self.frame(4, 0, 0, &payload)?;
        self.settings_pending.push_back(settings);
        self.enforce_settings();
        Ok(())
    }
    /// Recompute the enforced limits: the acknowledged ones, loosened by every
    /// frame still outstanding. The peer may already be acting on any of them.
    fn enforce_settings(&mut self) {
        let mut limits = self.acked_limits;
        let mut recv_initial = self.acked_recv_initial;
        let mut table = self.acked_table;
        let mut push = self.acked_push;
        for settings in &self.settings_pending {
            for (id, value) in settings.iter() {
                let value = value as usize;
                match id {
                    Settings::MAX_CONCURRENT_STREAMS => limits.streams = limits.streams.max(value),
                    Settings::INITIAL_WINDOW_SIZE => recv_initial = recv_initial.max(value as i64),
                    Settings::MAX_FRAME_SIZE => limits.frame_size = limits.frame_size.max(value),
                    Settings::MAX_HEADER_LIST_SIZE => {
                        limits.header_list = limits.header_list.max(value)
                    }
                    Settings::HEADER_TABLE_SIZE => table = table.max(value),
                    Settings::ENABLE_PUSH => push |= value != 0,
                    _ => {}
                }
            }
        }
        // RFC 9113 section 6.9.2: a change to the initial window adjusts every
        // open stream's window by the difference.
        let delta = recv_initial - self.recv_initial;
        if delta != 0 {
            for s in &mut self.streams {
                s.recv_window += delta;
            }
            self.recv_initial = recv_initial;
        }
        self.decoder.max_list_size = limits.header_list;
        self.decoder.set_allowed(table);
        self.push_allowed = push;
        self.limits = limits;
    }
    /// What this endpoint enforces now, including the effect of SETTINGS it
    /// has sent (see [`settings`](Self::settings)).
    pub fn limits(&self) -> Limits {
        self.limits
    }
    /// Every parameter this endpoint has sent and the peer has acknowledged -
    /// Node's `session.localSettings`, less the defaults nobody sent.
    pub fn local_settings(&self) -> &Settings {
        &self.local_settings
    }
    /// Every parameter the peer has sent - Node's `session.remoteSettings`,
    /// less the defaults the peer did not send.
    pub fn remote_settings(&self) -> &Settings {
        &self.remote_settings
    }
    /// SETTINGS frames sent and not yet acknowledged.
    pub fn pending_settings(&self) -> usize {
        self.settings_pending.len()
    }
    /// Host-supplied deadline for the oldest outstanding SETTINGS frame's
    /// acknowledgement; no clock is sampled. Each acknowledgement clears it, so
    /// a host with further frames outstanding sets the next one after each
    /// [`Event::SettingsAck`].
    pub fn set_settings_deadline(&mut self, deadline: Option<Instant>) {
        self.settings_deadline = deadline;
    }
    pub fn next_timeout(&self) -> Option<Instant> {
        if !self.settings_pending.is_empty() && !self.failed {
            self.settings_deadline
        } else {
            None
        }
    }
    pub fn handle_timeout(&mut self, now: Instant) -> Option<Error> {
        if self.next_timeout().is_some_and(|d| d <= now) {
            self.failed = true;
            let mut payload = [0; 8];
            payload[..4].copy_from_slice(&self.last_remote.to_be_bytes());
            payload[7] = 4;
            self.frame(7, 0, 0, &payload).expect("fixed-size GOAWAY");
            Some(error(
                "SETTINGS_TIMEOUT",
                "SETTINGS acknowledgement timeout",
            ))
        } else {
            None
        }
    }
    /// Notify transport loss, then call `poll_failed_stream` until exhausted.
    pub fn eof(&mut self) {
        self.failed = true;
    }
    /// Exactly one terminal result for each still-open stream after connection failure.
    /// Fully ended/reset streams have already produced their terminal event.
    pub fn poll_failed_stream(&mut self) -> Option<u32> {
        if !self.failed {
            return None;
        }
        let stream = self.streams.iter_mut().find(|s| !s.remote_end)?;
        stream.local_end = true;
        stream.remote_end = true;
        Some(stream.id)
    }
    pub fn output(&self) -> &[u8] {
        &self.output[self.output_pos..]
    }
    pub fn consume_output(&mut self, n: usize) -> Result<()> {
        if n > self.output().len() {
            return Err(protocol("write acknowledgement exceeds output"));
        }
        self.output_pos += n;
        if self.output_pos == self.output.len() {
            self.output.clear();
            self.output_pos = 0;
        }
        Ok(())
    }
    fn frame(&mut self, kind: u8, flags: u8, id: u32, payload: &[u8]) -> Result<()> {
        encode_frame(kind, flags, id, payload, &mut self.output)
    }
    fn index(&self, id: u32) -> Result<usize> {
        self.streams
            .iter()
            .position(|s| s.id == id)
            .ok_or(protocol("unknown or idle stream"))
    }
    fn active(&self) -> usize {
        self.streams.iter().filter(|s| !s.closed()).count()
    }
    /// A stream id this connection has already opened, refused or finished
    /// with. Its record may be gone — dropped by `reset`, never created for a
    /// refused stream, or recycled — while the peer, not yet having seen the
    /// RST_STREAM or GOAWAY, still sends frames for it. RFC 9113 section 5.1
    /// requires those to be tolerated; a frame for an id that was never used is
    /// still a connection error.
    fn seen(&self, id: u32) -> bool {
        if id == 0 {
            false
        } else if (id % 2 == 1) == (self.role == Role::Client) {
            id < self.next_id
        } else {
            id <= self.highest_remote
        }
    }
    /// `STREAM_CLOSED` for a stream this connection has finished with, and the
    /// idle/unknown protocol error for one it has never used.
    fn gone(&self, id: u32) -> Error {
        if self.seen(id) {
            error("STREAM_CLOSED", "stream already terminated")
        } else {
            protocol("unknown or idle stream")
        }
    }
    /// Give back the connection-level credit a terminated stream still holds.
    /// RFC 9113 section 5.1: DATA the peer sent before it saw our RST_STREAM
    /// counts against the connection window either way, so the credit has to go
    /// back or the window shrinks for the life of the connection. No
    /// stream-level WINDOW_UPDATE: that stream is over.
    fn return_credit(&mut self, i: usize) -> Result<()> {
        let n = std::mem::take(&mut self.streams[i].unreleased);
        if n == 0 {
            return Ok(());
        }
        self.recv_window += n as i64;
        self.frame(8, 0, 0, &n.to_be_bytes())
    }
    /// Account DATA for a stream that is gone against the connection window and
    /// return the credit at once: the payload is discarded, so no host release
    /// will ever arrive for it.
    fn discard_data(&mut self, flow: i64) -> Result<()> {
        if flow > self.recv_window {
            return Err(error("FLOW_CONTROL_ERROR", "receive window exceeded"));
        }
        if flow == 0 {
            return Ok(());
        }
        self.frame(8, 0, 0, &(flow as u32).to_be_bytes())
    }
    fn add_stream(&mut self, id: u32) -> Result<usize> {
        if self.active() >= self.limits.streams {
            return Err(error("REFUSED_STREAM", "stream limit"));
        }
        let stream = Stream {
            id,
            send_window: self.initial_send,
            recv_window: self.recv_initial,
            unreleased: 0,
            credited: false,
            local_end: false,
            remote_end: false,
            received_head: false,
            sent_head: false,
            recv_length: None,
            received: 0,
            head_request: false,
            recv_no_body: false,
            send_no_body: false,
            send_length: None,
            sent: 0,
        };
        if let Some(i) = self
            .streams
            .iter()
            .position(|s| s.closed() && s.unreleased == 0)
        {
            self.streams[i] = stream;
            Ok(i)
        } else {
            if self.streams.len() >= self.limits.streams {
                return Err(error(
                    "REFUSED_STREAM",
                    "release consumed capacity before opening streams",
                ));
            }
            self.streams.push(stream);
            Ok(self.streams.len() - 1)
        }
    }
    pub fn open(&mut self, headers: &[Header], end_stream: bool) -> Result<u32> {
        if self.role != Role::Client || self.draining || self.failed {
            return Err(protocol("cannot open stream"));
        }
        if self.active() >= self.peer_streams {
            return Err(error("REFUSED_STREAM", "peer stream limit"));
        }
        validate_headers(headers, true, false)?;
        let id = self.next_id;
        self.next_id = self
            .next_id
            .checked_add(2)
            .filter(|n| *n <= 0x7fffffff)
            .ok_or(protocol("stream IDs exhausted"))?;
        self.add_stream(id)?;
        self.send_headers(id, headers, end_stream)?;
        Ok(id)
    }
    /// Promise a response to the request `headers` on `stream`, a request
    /// stream the client opened (RFC 9113 section 8.4). Returns the promised
    /// stream, which then carries the response through
    /// [`send_headers`](Self::send_headers) and [`send_data`](Self::send_data).
    /// Refused when the peer disabled push with ENABLE_PUSH = 0.
    pub fn push(&mut self, stream: u32, headers: &[Header]) -> Result<u32> {
        if self.role != Role::Server || self.draining || self.failed {
            return Err(protocol("cannot push"));
        }
        if self.remote_settings.get(Settings::ENABLE_PUSH) == Some(0) {
            return Err(protocol("push disabled by peer"));
        }
        let i = self.index(stream).map_err(|_| self.gone(stream))?;
        if self.streams[i].local_end {
            return Err(error("STREAM_CLOSED", "local stream closed"));
        }
        if self.active() >= self.peer_streams {
            return Err(error("REFUSED_STREAM", "peer stream limit"));
        }
        validate_headers(headers, true, false)?;
        let id = self.next_id;
        let next = id
            .checked_add(2)
            .filter(|n| *n <= 0x7fffffff)
            .ok_or(protocol("stream IDs exhausted"))?;
        self.scratch.clear();
        self.encoder.encode(headers, &mut self.scratch);
        let first = self.scratch.len().min(self.peer_frame - 4);
        let mut payload = id.to_be_bytes().to_vec();
        payload.extend_from_slice(&self.scratch[..first]);
        let j = self.add_stream(id)?;
        self.next_id = next;
        encode_frame(
            5,
            if first == self.scratch.len() { 4 } else { 0 },
            stream,
            &payload,
            &mut self.output,
        )?;
        let mut start = first;
        while start < self.scratch.len() {
            let end = (start + self.peer_frame).min(self.scratch.len());
            encode_frame(
                9,
                if end == self.scratch.len() { 4 } else { 0 },
                stream,
                &self.scratch[start..end],
                &mut self.output,
            )?;
            start = end;
        }
        let s = &mut self.streams[j];
        // The promised request has no body and the client sends nothing on it.
        s.remote_end = true;
        s.received_head = true;
        s.head_request = headers
            .iter()
            .any(|h| h.name == ":method" && h.value == b"HEAD");
        Ok(id)
    }
    pub fn send_headers(&mut self, id: u32, headers: &[Header], end_stream: bool) -> Result<()> {
        if self.failed {
            return Err(protocol("failed connection"));
        }
        let i = self.index(id).map_err(|_| self.gone(id))?;
        let s = &self.streams[i];
        if s.local_end {
            return Err(error("STREAM_CLOSED", "local stream closed"));
        }
        validate_headers(headers, self.role == Role::Client, s.sent_head)?;
        let informational = headers
            .iter()
            .find(|h| h.name == ":status")
            .is_some_and(|h| h.value.starts_with(b"1"));
        if informational && end_stream {
            return Err(protocol("informational END_STREAM"));
        }
        if s.sent_head && !end_stream {
            return Err(protocol("trailers must end stream"));
        }
        let no_body = self.role == Role::Server
            && (s.head_request
                || headers
                    .iter()
                    .any(|h| h.name == ":status" && matches!(h.value.as_slice(), b"204" | b"304")));
        let length = if s.sent_head {
            s.send_length
        } else if no_body {
            None
        } else {
            content_length(headers)?
        };
        if end_stream && length.is_some_and(|n| n != s.sent) {
            return Err(protocol("outgoing content-length mismatch"));
        }
        self.scratch.clear();
        self.encoder.encode(headers, &mut self.scratch);
        let count = self.scratch.len().max(1).div_ceil(self.peer_frame);
        for n in 0..count {
            let start = n * self.peer_frame;
            let end = (start + self.peer_frame).min(self.scratch.len());
            let flags =
                if n + 1 == count { 4 } else { 0 } | if n == 0 && end_stream { 1 } else { 0 };
            encode_frame(
                if n == 0 { 1 } else { 9 },
                flags,
                id,
                &self.scratch[start..end],
                &mut self.output,
            )?;
        }
        let s = &mut self.streams[i];
        s.sent_head |= !informational;
        s.send_no_body = no_body;
        s.send_length = length;
        s.local_end = end_stream;
        if self.role == Role::Client {
            s.head_request = headers
                .iter()
                .any(|h| h.name == ":method" && h.value == b"HEAD");
        }
        Ok(())
    }
    /// Returns accepted bytes (zero on flow-control stall). Host retains remainder.
    pub fn send_data(&mut self, id: u32, bytes: &[u8], end_stream: bool) -> Result<usize> {
        let i = self.index(id).map_err(|_| self.gone(id))?;
        let s = &self.streams[i];
        if self.failed || s.local_end || !s.sent_head {
            return Err(error("STREAM_CLOSED", "cannot send DATA"));
        }
        let n = bytes
            .len()
            .min(self.peer_frame)
            .min(self.send_window.max(0) as usize)
            .min(s.send_window.max(0) as usize);
        if n == 0 && !bytes.is_empty() {
            return Ok(0);
        }
        let end = end_stream && n == bytes.len();
        if s.send_no_body && !bytes.is_empty() {
            return Err(protocol("DATA on bodyless response"));
        }
        if s.send_length.is_some_and(|length| {
            s.sent + n as u64 > length || (end && s.sent + n as u64 != length)
        }) {
            return Err(protocol("outgoing content-length mismatch"));
        }
        self.frame(0, u8::from(end), id, &bytes[..n])?;
        self.send_window -= n as i64;
        let s = &mut self.streams[i];
        s.send_window -= n as i64;
        s.sent += n as u64;
        s.local_end = end;
        Ok(n)
    }
    /// Return `n` bytes of a stream's receive window to the peer; `n` may not
    /// exceed that stream's [`unreleased`](Self::unreleased) count.
    ///
    /// A terminated stream has already had its remaining credit returned in
    /// bulk, so a late release for one is a **no-op, not an error** — a host
    /// that buffers a body and releases when the application consumes it can
    /// always call this, whatever happened to the stream meanwhile. An id this
    /// connection has never used is still an error.
    pub fn release_capacity(&mut self, id: u32, n: u32) -> Result<()> {
        let Ok(i) = self.index(id) else {
            return if self.seen(id) {
                Ok(())
            } else {
                Err(protocol("unknown or idle stream"))
            };
        };
        if n == 0 || self.streams[i].credited {
            return Ok(());
        }
        let s = &mut self.streams[i];
        if n > s.unreleased {
            return Err(protocol("capacity exceeds received DATA"));
        }
        s.unreleased -= n;
        s.recv_window += n as i64;
        self.recv_window += n as i64;
        self.frame(8, 0, 0, &n.to_be_bytes())?;
        if !self.streams[i].remote_end {
            self.frame(8, 0, id, &n.to_be_bytes())?;
        }
        Ok(())
    }
    /// Received DATA bytes for `id` whose flow-control credit the host has not
    /// yet returned with [`release_capacity`](Self::release_capacity). `None`
    /// once the stream is gone, its credit having been returned in bulk.
    /// Padding never appears here: `receive` releases it as it is consumed.
    pub fn unreleased(&self, id: u32) -> Option<u32> {
        self.index(id).ok().map(|i| self.streams[i].unreleased)
    }
    /// Terminate a stream with RST_STREAM.
    ///
    /// The stream's outstanding connection-level credit is returned and its
    /// table slot is freed at once, so resetting a stream that still holds
    /// unreleased DATA — the ordinary "one stream errors while its siblings are
    /// live" case — costs the connection nothing. Frames the peer already had
    /// in flight for the stream are then ignored rather than failing the
    /// connection (RFC 9113 section 5.1).
    ///
    /// The frame is sent even when the stream has no record left — it may have
    /// been reset already, or refused before one existed, as after
    /// [`Event::Unprocessed`]. Encoding what the host asks for is this type's
    /// job; deciding not to is the host's. Only an id this connection has never
    /// used is an error.
    pub fn reset(&mut self, id: u32, code: u32) -> Result<()> {
        let Ok(i) = self.index(id) else {
            return if self.seen(id) {
                self.frame(3, 0, id, &code.to_be_bytes())
            } else {
                Err(protocol("unknown or idle stream"))
            };
        };
        self.return_credit(i)?;
        self.streams.swap_remove(i);
        self.frame(3, 0, id, &code.to_be_bytes())
    }
    /// RFC 9113 leaves a PING acknowledgement that answers nothing this
    /// endpoint sent without a required reaction, and by default it is only
    /// reported as [`Event::Ping`]. With `reject` set it fails the connection
    /// with GOAWAY(INTERNAL_ERROR), as Node does: no legitimate peer sends one.
    pub fn set_reject_unsolicited_ping_ack(&mut self, reject: bool) {
        self.reject_unsolicited_ping_ack = reject;
    }
    pub fn ping(&mut self, data: [u8; 8]) -> Result<()> {
        self.frame(6, 0, 0, &data)?;
        self.pings += 1;
        Ok(())
    }
    /// Graceful close: GOAWAY(NO_ERROR) naming the last stream this connection
    /// has processed, then drain. A stream the peer opens before it sees the
    /// frame does not fail the connection and is not answered: it arrives as
    /// [`Event::Unprocessed`] and the host decides.
    pub fn shutdown(&mut self) -> Result<()> {
        self.goaway(0, self.last_remote, &[])
    }
    /// GOAWAY with an explicit error code, last stream id and opaque debug
    /// data, the form [`shutdown`](Self::shutdown) cannot express. The
    /// connection starts draining either way.
    pub fn goaway(&mut self, code: u32, last_stream: u32, opaque: &[u8]) -> Result<()> {
        if last_stream > 0x7fffffff {
            return Err(protocol("invalid last stream"));
        }
        // The peer would answer a GOAWAY over its own SETTINGS_MAX_FRAME_SIZE
        // with FRAME_SIZE_ERROR, so refuse the opaque data here instead.
        if 8 + opaque.len() > self.peer_frame {
            return Err(frame_error());
        }
        self.draining = true;
        self.scratch.clear();
        self.scratch.extend_from_slice(&last_stream.to_be_bytes());
        self.scratch.extend_from_slice(&code.to_be_bytes());
        self.scratch.extend_from_slice(opaque);
        encode_frame(7, 0, 0, &self.scratch, &mut self.output)
    }
    pub fn is_drained(&self) -> bool {
        self.draining && self.active() == 0 && self.output().is_empty()
    }
    pub fn receive<'a>(&mut self, input: &'a [u8]) -> Result<Step<'a>> {
        if self.failed {
            return Err(protocol("failed connection"));
        }
        let result = self.receive_inner(input);
        if let Err(e) = &result {
            self.failed = true;
            let code = match e.code {
                "FLOW_CONTROL_ERROR" => 3,
                "INTERNAL_ERROR" => 2,
                "FRAME_SIZE_ERROR" => 6,
                "COMPRESSION_ERROR" => 9,
                "ENHANCE_YOUR_CALM" => 11,
                _ => 1,
            };
            let mut payload = [0; 8];
            payload[..4].copy_from_slice(&self.last_remote.to_be_bytes());
            payload[4..].copy_from_slice(&(code as u32).to_be_bytes());
            self.frame(7, 0, 0, &payload)?;
        }
        result
    }
    fn receive_inner<'a>(&mut self, input: &'a [u8]) -> Result<Step<'a>> {
        if !self.preface {
            let n = input.len().min(PREFACE.len());
            if input[..n] != PREFACE[..n] {
                return Err(protocol("invalid client preface"));
            }
            if n < PREFACE.len() {
                return Ok(Step {
                    consumed: 0,
                    event: None,
                });
            }
            self.preface = true;
            return Ok(Step {
                consumed: PREFACE.len(),
                event: None,
            });
        }
        let Some(f) = decode_frame(input, self.limits.frame_size)? else {
            return Ok(Step {
                consumed: 0,
                event: None,
            });
        };
        if !self.settings_received && (f.kind != 4 || f.flags & 1 != 0) {
            return Err(protocol("first frame must be SETTINGS"));
        }
        if let Some((id, _, _)) = self.continuation {
            if f.kind != 9 || f.stream != id {
                return Err(protocol("interleaved header block"));
            }
        } else if f.kind == 9 {
            return Err(protocol("unexpected CONTINUATION"));
        }
        let mut step = Step {
            consumed: 9 + f.payload.len(),
            event: None,
        };
        match f.kind {
            0 => {
                if f.stream == 0 {
                    return Err(protocol("DATA on connection"));
                }
                let Ok(i) = self.index(f.stream) else {
                    // In flight when the stream was refused or reset. RFC 9113
                    // section 5.1 tolerates it, and the bytes still count
                    // against the connection window, so return the credit.
                    if !self.seen(f.stream) {
                        return Err(protocol("unknown or idle stream"));
                    }
                    self.discard_data(f.payload.len() as i64)?;
                    return Ok(step);
                };
                let s = &mut self.streams[i];
                if s.remote_end || !s.received_head {
                    return Err(protocol("DATA in invalid stream state"));
                }
                let payload = unpadded(f)?;
                let flow = f.payload.len() as i64;
                if flow > self.recv_window || flow > s.recv_window {
                    return Err(error("FLOW_CONTROL_ERROR", "receive window exceeded"));
                }
                self.recv_window -= flow;
                s.recv_window -= flow;
                s.unreleased += flow as u32;
                s.received += payload.len() as u64;
                let end = f.flags & 1 != 0;
                if s.recv_no_body && !payload.is_empty() {
                    return Err(protocol("DATA on bodyless response"));
                }
                if s.recv_length
                    .is_some_and(|n| s.received > n || (end && n != s.received))
                {
                    return Err(protocol("content-length mismatch"));
                }
                s.remote_end = end;
                // Padding is consumed by the protocol, never charged to the application.
                let padding = f.payload.len() - payload.len();
                if padding > 0 {
                    self.release_capacity(f.stream, padding as u32)?;
                }
                step.event = Some(Event::Data {
                    stream: f.stream,
                    bytes: payload,
                    end_stream: end,
                });
            }
            1 => {
                if f.stream == 0 {
                    return Err(protocol("HEADERS on connection"));
                }
                self.disposal = None;
                if self.index(f.stream).is_err() {
                    if self.role != Role::Server {
                        // A response the peer already had in flight when we
                        // reset the stream. Its record is gone, so decode the
                        // block to keep HPACK in step and ignore it (RFC 9113
                        // section 5.1). Only a stream we opened ourselves is
                        // unambiguous here; anything else is idle.
                        if !self.seen(f.stream) {
                            return Err(protocol("invalid new stream"));
                        }
                        self.disposal = Some(Disposal::Discard);
                    } else if f.stream % 2 == 0 || f.stream <= self.highest_remote {
                        return Err(protocol("invalid new stream"));
                    } else {
                        self.highest_remote = f.stream;
                        // A stream this connection cannot accept never fails
                        // the connection. The two reasons answer differently,
                        // and both were measured against Node 26.5.1 with a raw
                        // peer rather than assumed:
                        //
                        // * after a graceful GOAWAY, the stream is above the id
                        //   that GOAWAY named and is simply not processed (RFC
                        //   9113 section 6.8). Node sends no frame at all, so
                        //   neither do we - the host is told and decides.
                        // * past the concurrent-stream limit, RFC 9113 section
                        //   5.1.2 requires a stream error, and Node answers
                        //   RST_STREAM(REFUSED_STREAM).
                        self.disposal = if self.draining {
                            Some(Disposal::Unprocessed)
                        } else {
                            match self.add_stream(f.stream) {
                                Ok(_) => {
                                    // Accepted for processing, so GOAWAY may name it.
                                    self.last_remote = f.stream;
                                    None
                                }
                                Err(e) if e.code == "REFUSED_STREAM" => Some(Disposal::Refuse(7)),
                                Err(e) => return Err(e),
                            }
                        };
                    }
                }
                let mut payload = unpadded(f)?;
                if f.flags & 32 != 0 {
                    if payload.len() < 5 {
                        return Err(frame_error());
                    }
                    if u32be(payload) & 0x7fffffff == f.stream {
                        return Err(protocol("stream depends on itself"));
                    }
                    payload = &payload[5..];
                }
                self.block.clear();
                self.append_block(payload)?;
                let end = f.flags & 1 != 0;
                if f.flags & 4 != 0 {
                    step.event = self.finish_block(f.stream, end)?;
                } else {
                    self.continuation = Some((f.stream, end, 0));
                }
            }
            9 => {
                let (id, end, count) = self.continuation.unwrap();
                if count >= self.limits.continuations {
                    return Err(error("ENHANCE_YOUR_CALM", "CONTINUATION limit"));
                }
                self.append_block(f.payload)?;
                if f.flags & 4 != 0 {
                    self.continuation = None;
                    step.event = self.finish_block(id, end)?;
                } else {
                    self.continuation = Some((id, end, count + 1));
                }
            }
            2 => {
                if f.stream == 0 {
                    return Err(protocol("PRIORITY on connection"));
                }
                if f.payload.len() != 5 {
                    return Err(frame_error());
                }
                if u32be(f.payload) & 0x7fffffff == f.stream {
                    return Err(protocol("stream depends on itself"));
                }
            }
            3 => {
                if f.stream == 0 {
                    return Err(protocol("RST_STREAM on connection"));
                }
                if f.payload.len() != 4 {
                    return Err(frame_error());
                }
                let Ok(i) = self.index(f.stream) else {
                    // Crossed resets, or a reset for a stream already refused.
                    if !self.seen(f.stream) {
                        return Err(protocol("unknown or idle stream"));
                    }
                    return Ok(step);
                };
                self.return_credit(i)?;
                let s = &mut self.streams[i];
                s.credited = true;
                s.remote_end = true;
                s.local_end = true;
                step.event = Some(Event::Reset {
                    stream: f.stream,
                    code: u32be(f.payload),
                });
            }
            4 => {
                if f.stream != 0 {
                    return Err(protocol("SETTINGS on stream"));
                }
                if f.flags & 1 != 0 {
                    if !f.payload.is_empty() {
                        return Err(frame_error());
                    }
                    let Some(acked) = self.settings_pending.pop_front() else {
                        return Err(protocol("unsolicited SETTINGS ack"));
                    };
                    for (id, value) in acked.iter() {
                        match id {
                            Settings::MAX_CONCURRENT_STREAMS => {
                                self.acked_limits.streams = value as usize
                            }
                            Settings::INITIAL_WINDOW_SIZE => self.acked_recv_initial = value as i64,
                            Settings::MAX_FRAME_SIZE => {
                                self.acked_limits.frame_size = value as usize
                            }
                            Settings::MAX_HEADER_LIST_SIZE => {
                                self.acked_limits.header_list = value as usize
                            }
                            Settings::HEADER_TABLE_SIZE => self.acked_table = value as usize,
                            Settings::ENABLE_PUSH => self.acked_push = value != 0,
                            _ => {}
                        }
                    }
                    self.local_settings.merge(acked.iter());
                    self.enforce_settings();
                    // The deadline was for this frame; the host arms the next.
                    self.settings_deadline = None;
                    step.event = Some(Event::SettingsAck(acked));
                } else {
                    if f.payload.len() % 6 != 0 {
                        return Err(frame_error());
                    }
                    for setting in f.payload.as_chunks::<6>().0 {
                        let id = u16::from_be_bytes(setting[..2].try_into().unwrap());
                        let value = u32be(&setting[2..]);
                        match id {
                            1 => self.encoder.set_table_size((value as usize).min(4096)),
                            2 => {
                                if match self.role {
                                    Role::Server => value > 1,
                                    Role::Client => value != 0,
                                } {
                                    return Err(protocol("invalid ENABLE_PUSH"));
                                }
                            }
                            3 => self.peer_streams = value as usize,
                            4 => {
                                if value > 0x7fffffff {
                                    return Err(error(
                                        "FLOW_CONTROL_ERROR",
                                        "invalid initial window",
                                    ));
                                }
                                let delta = value as i64 - self.initial_send;
                                for s in &mut self.streams {
                                    if s.send_window + delta > 0x7fffffff {
                                        return Err(error("FLOW_CONTROL_ERROR", "window overflow"));
                                    }
                                    s.send_window += delta;
                                }
                                self.initial_send = value as i64;
                            }
                            5 => {
                                if !(16384..=0xffffff).contains(&value) {
                                    return Err(protocol("invalid MAX_FRAME_SIZE"));
                                }
                                self.peer_frame = value as usize;
                            }
                            _ => {}
                        }
                    }
                    self.settings_received = true;
                    let settings = SettingsFrame { payload: f.payload };
                    self.remote_settings.merge(settings.iter());
                    self.frame(4, 1, 0, &[])?;
                    step.event = Some(Event::Settings(settings));
                }
            }
            5 => {
                if self.role != Role::Client || !self.push_allowed {
                    return Err(protocol("server push disabled"));
                }
                if f.stream == 0 {
                    return Err(protocol("PUSH_PROMISE on connection"));
                }
                let payload = unpadded(f)?;
                if payload.len() < 4 {
                    return Err(frame_error());
                }
                let promised = u32be(payload) & 0x7fffffff;
                if promised == 0 || !promised.is_multiple_of(2) || promised <= self.highest_remote {
                    return Err(protocol("invalid promised stream"));
                }
                // A promise rides on a stream we opened. One we have already
                // reset may still have a promise in flight: decode it to keep
                // HPACK in step and cancel the promised stream.
                let discard = match self.index(f.stream) {
                    Ok(i) if self.streams[i].remote_end => {
                        return Err(error("STREAM_CLOSED", "PUSH_PROMISE after END_STREAM"));
                    }
                    Ok(_) => false,
                    Err(_) if self.seen(f.stream) => true,
                    Err(_) => return Err(protocol("PUSH_PROMISE on idle stream")),
                };
                self.highest_remote = promised;
                self.disposal = None;
                self.block.clear();
                self.append_block(&payload[4..])?;
                self.promise = Some((promised, discard));
                if f.flags & 4 != 0 {
                    step.event = self.finish_block(f.stream, false)?;
                } else {
                    self.continuation = Some((f.stream, false, 0));
                }
            }
            6 => {
                if f.stream != 0 {
                    return Err(protocol("PING on stream"));
                }
                if f.payload.len() != 8 {
                    return Err(frame_error());
                }
                let data = f.payload.try_into().unwrap();
                let ack = f.flags & 1 != 0;
                if !ack {
                    self.frame(6, 1, 0, f.payload)?;
                } else if self.pings > 0 {
                    self.pings -= 1;
                } else if self.reject_unsolicited_ping_ack {
                    // Node answers GOAWAY(INTERNAL_ERROR) and drops the
                    // connection (measured against a raw peer).
                    return Err(error("INTERNAL_ERROR", "unsolicited PING acknowledgement"));
                }
                step.event = Some(Event::Ping { ack, data });
            }
            7 => {
                if f.stream != 0 {
                    return Err(protocol("GOAWAY on stream"));
                }
                if f.payload.len() < 8 {
                    return Err(frame_error());
                }
                self.draining = true;
                step.event = Some(Event::Goaway {
                    last_stream: u32be(f.payload) & 0x7fffffff,
                    code: u32be(&f.payload[4..]),
                    debug: Some(&f.payload[8..]).filter(|d| !d.is_empty()),
                });
            }
            8 => {
                if f.payload.len() != 4 {
                    return Err(frame_error());
                }
                if f.stream != 0 && self.index(f.stream).is_err() {
                    // Credit for a stream that is already gone. RFC 9113
                    // section 6.9 requires this to be tolerated.
                    if !self.seen(f.stream) {
                        return Err(protocol("unknown or idle stream"));
                    }
                    return Ok(step);
                }
                let n = (u32be(f.payload) & 0x7fffffff) as i64;
                if n == 0 {
                    if f.stream != 0 {
                        self.reset(f.stream, 1)?;
                        step.event = Some(Event::Reset {
                            stream: f.stream,
                            code: 1,
                        });
                        return Ok(step);
                    }
                    return Err(protocol("zero WINDOW_UPDATE"));
                }
                let window = if f.stream == 0 {
                    &mut self.send_window
                } else {
                    let i = self.index(f.stream)?;
                    &mut self.streams[i].send_window
                };
                if *window + n > 0x7fffffff {
                    if f.stream != 0 {
                        self.reset(f.stream, 3)?;
                        step.event = Some(Event::Reset {
                            stream: f.stream,
                            code: 3,
                        });
                        return Ok(step);
                    }
                    return Err(error("FLOW_CONTROL_ERROR", "window overflow"));
                }
                *window += n;
                step.event = Some(Event::WindowUpdate { stream: f.stream });
            }
            _ => {}
        }
        Ok(step)
    }
    fn append_block(&mut self, bytes: &[u8]) -> Result<()> {
        if bytes.len() > self.limits.header_block.saturating_sub(self.block.len()) {
            return Err(error("ENHANCE_YOUR_CALM", "compressed header block limit"));
        }
        self.block.extend_from_slice(bytes);
        Ok(())
    }
    /// Complete a header block: the stream's own, or one being thrown away.
    fn finish_block(&mut self, id: u32, end: bool) -> Result<Option<Event<'static>>> {
        if let Some((promised, discard)) = self.promise.take() {
            return self.finish_promise(id, promised, discard);
        }
        let Some(disposal) = self.disposal.take() else {
            return self.finish_headers(id, end).map(Some);
        };
        // Decode before discarding: HPACK state belongs to the connection, not
        // the stream, and a skipped block desynchronises every block after it.
        let mut headers = Vec::new();
        self.decoder.decode(&self.block, &mut headers)?;
        match disposal {
            Disposal::Refuse(code) => {
                self.frame(3, 0, id, &code.to_be_bytes())?;
                Ok(Some(Event::Reset { stream: id, code }))
            }
            Disposal::Unprocessed => Ok(Some(Event::Unprocessed { stream: id })),
            Disposal::Discard => Ok(None),
        }
    }
    /// Complete a PUSH_PROMISE: reserve the promised stream, or decline it.
    fn finish_promise(
        &mut self,
        id: u32,
        promised: u32,
        discard: bool,
    ) -> Result<Option<Event<'static>>> {
        let mut headers = Vec::new();
        self.decoder.decode(&self.block, &mut headers)?;
        if discard {
            self.frame(3, 0, promised, &8u32.to_be_bytes())?;
            return Ok(None);
        }
        validate_headers(&headers, true, false)?;
        match self.add_stream(promised) {
            Ok(i) => {
                let s = &mut self.streams[i];
                // The client never sends on a pushed stream.
                s.local_end = true;
                s.head_request = headers
                    .iter()
                    .any(|h| h.name == ":method" && h.value == b"HEAD");
                self.last_remote = promised;
                Ok(Some(Event::PushPromise {
                    stream: id,
                    promised,
                    headers,
                }))
            }
            Err(e) if e.code == "REFUSED_STREAM" => {
                self.frame(3, 0, promised, &7u32.to_be_bytes())?;
                Ok(Some(Event::Reset {
                    stream: promised,
                    code: 7,
                }))
            }
            Err(e) => Err(e),
        }
    }
    fn finish_headers(&mut self, id: u32, end: bool) -> Result<Event<'static>> {
        let mut headers = Vec::new();
        self.decoder.decode(&self.block, &mut headers)?;
        let i = self.index(id)?;
        let s = &mut self.streams[i];
        if s.remote_end {
            return Err(error("STREAM_CLOSED", "headers after END_STREAM"));
        }
        validate_headers(&headers, self.role == Role::Server, s.received_head)?;
        let informational = headers
            .iter()
            .find(|h| h.name == ":status")
            .is_some_and(|h| h.value.starts_with(b"1"));
        if informational && end {
            return Err(protocol("informational END_STREAM"));
        }
        if s.received_head && !end {
            return Err(protocol("trailers without END_STREAM"));
        }
        let kind = if informational {
            HeadersKind::Informational
        } else if s.received_head {
            HeadersKind::Trailers
        } else {
            HeadersKind::Head
        };
        if !s.received_head && !informational {
            s.recv_no_body = self.role == Role::Client
                && (s.head_request
                    || headers.iter().any(|h| {
                        h.name == ":status" && matches!(h.value.as_slice(), b"204" | b"304")
                    }));
            s.recv_length = if s.recv_no_body {
                None
            } else {
                content_length(&headers)?
            };
            if self.role == Role::Server {
                s.head_request = headers
                    .iter()
                    .any(|h| h.name == ":method" && h.value == b"HEAD");
            }
            s.received_head = true;
        }
        if end && s.recv_length.is_some_and(|n| n != s.received) {
            return Err(protocol("content-length mismatch"));
        }
        s.remote_end = end;
        Ok(Event::Headers {
            stream: id,
            headers,
            end_stream: end,
            kind,
        })
    }
}
fn unpadded(f: Frame<'_>) -> Result<&[u8]> {
    if f.flags & 8 == 0 {
        return Ok(f.payload);
    }
    let pad = *f.payload.first().ok_or_else(frame_error)? as usize;
    if pad >= f.payload.len() {
        return Err(protocol("invalid padding"));
    }
    Ok(&f.payload[1..f.payload.len() - pad])
}
fn content_length(headers: &[Header]) -> Result<Option<u64>> {
    let mut found = None;
    for h in headers {
        if h.name == "content-length" {
            if found.is_some() || h.value.is_empty() || !h.value.iter().all(u8::is_ascii_digit) {
                return Err(protocol("invalid content-length"));
            }
            found = Some(
                h.value
                    .iter()
                    .try_fold(0u64, |n, b| {
                        n.checked_mul(10)?.checked_add((b - b'0') as u64)
                    })
                    .ok_or(protocol("content-length overflow"))?,
            );
        }
    }
    Ok(found)
}
fn validate_headers(headers: &[Header], request: bool, trailers: bool) -> Result<()> {
    let mut regular = false;
    let mut pseudo = [false; 5];
    for h in headers {
        if h.name.is_empty()
            || h.name.bytes().any(|b| b.is_ascii_uppercase())
            || h.value.iter().any(|b| matches!(b, 0 | 10 | 13))
            || h.value.first().is_some_and(|b| matches!(b, b' ' | b'\t'))
            || h.value.last().is_some_and(|b| matches!(b, b' ' | b'\t'))
        {
            return Err(protocol("invalid header field"));
        }
        if h.name.starts_with(':') {
            if regular || trailers {
                return Err(protocol("pseudo-header ordering"));
            }
            let index = match h.name.as_str() {
                ":method" if request => 0,
                ":scheme" if request => 1,
                ":path" if request => 2,
                ":authority" if request => 3,
                ":status" if !request => 4,
                _ => return Err(protocol("invalid pseudo-header")),
            };
            if pseudo[index] {
                return Err(protocol("duplicate pseudo-header"));
            }
            pseudo[index] = true;
        } else {
            regular = true;
            if !crate::http1::valid_token(h.name.as_bytes()) {
                return Err(protocol("invalid header name"));
            }
            if matches!(
                h.name.as_str(),
                "connection" | "proxy-connection" | "keep-alive" | "transfer-encoding" | "upgrade"
            ) || h.name == "te" && h.value != b"trailers"
            {
                return Err(protocol("connection-specific field"));
            }
        }
    }
    if !trailers {
        if request {
            let connect = headers
                .iter()
                .any(|h| h.name == ":method" && h.value == b"CONNECT");
            if !pseudo[0]
                || if connect {
                    !pseudo[3] || pseudo[1] || pseudo[2]
                } else {
                    !pseudo[1]
                        || !pseudo[2]
                        || headers
                            .iter()
                            .any(|h| h.name == ":path" && h.value.is_empty())
                }
            {
                return Err(protocol("missing request pseudo-header"));
            }
        } else {
            let Some(status) = headers.iter().find(|h| h.name == ":status") else {
                return Err(protocol("missing status"));
            };
            if status.value.len() != 3
                || !status.value.iter().all(u8::is_ascii_digit)
                || status.value.as_slice() < b"100".as_slice()
                || status.value == b"101"
            {
                return Err(protocol("invalid status"));
            }
        }
    }
    if trailers && headers.iter().any(|h| h.name == "content-length") {
        return Err(protocol("content-length in trailers"));
    }
    content_length(headers)?;
    Ok(())
}