forked from mozilla/neqo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencoder.rs
More file actions
1684 lines (1418 loc) · 58.6 KB
/
Copy pathencoder.rs
File metadata and controls
1684 lines (1418 loc) · 58.6 KB
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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::{
cmp::min,
collections::VecDeque,
fmt::{self, Display, Formatter},
};
use neqo_common::{qdebug, qerror, qlog::Qlog, qtrace, Header};
use neqo_transport::{Connection, Error as TransportError, StreamId};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
use crate::{
decoder_instructions::{DecoderInstruction, DecoderInstructionReader},
encoder_instructions::EncoderInstruction,
header_block::HeaderEncoder,
qlog,
reader::ReceiverConnWrapper,
stats::Stats,
table::{HeaderTable, LookupResult, ADDITIONAL_TABLE_ENTRY_SIZE},
Error, Res, Settings,
};
pub const QPACK_UNI_STREAM_TYPE_ENCODER: u64 = 0x2;
#[derive(Debug, PartialEq)]
enum LocalStreamState {
NoStream,
Uninitialized(StreamId),
Initialized(StreamId),
}
impl LocalStreamState {
pub const fn stream_id(&self) -> Option<StreamId> {
match self {
Self::NoStream => None,
Self::Uninitialized(stream_id) | Self::Initialized(stream_id) => Some(*stream_id),
}
}
}
#[derive(Debug)]
pub struct Encoder {
table: HeaderTable,
max_table_size: u64,
max_entries: u64,
instruction_reader: DecoderInstructionReader,
local_stream: LocalStreamState,
max_blocked_streams: u16,
// Remember header blocks that are referring to dynamic table.
// There can be multiple header blocks in one stream, headers, trailer, push stream request,
// etc. This HashMap maps a stream ID to a list of header blocks. Each header block is a
// list of referenced dynamic table entries.
unacked_header_blocks: HashMap<StreamId, VecDeque<HashSet<u64>>>,
blocked_stream_cnt: u16,
use_huffman: bool,
next_capacity: Option<u64>,
stats: Stats,
}
impl Encoder {
#[must_use]
pub fn new(qpack_settings: &Settings, use_huffman: bool) -> Self {
Self {
table: HeaderTable::new(true),
max_table_size: qpack_settings.max_table_size_encoder,
max_entries: 0,
instruction_reader: DecoderInstructionReader::new(),
local_stream: LocalStreamState::NoStream,
max_blocked_streams: 0,
unacked_header_blocks: HashMap::default(),
blocked_stream_cnt: 0,
use_huffman,
next_capacity: None,
stats: Stats::default(),
}
}
/// This function is use for setting encoders table max capacity. The value is received as
/// a `SETTINGS_QPACK_MAX_TABLE_CAPACITY` setting parameter.
///
/// # Errors
///
/// `EncoderStream` if value is too big.
/// `ChangeCapacity` if table capacity cannot be reduced.
pub fn set_max_capacity(&mut self, cap: u64) -> Res<()> {
if cap > (1 << 30) - 1 {
return Err(Error::EncoderStream);
}
if cap == self.table.capacity() {
return Ok(());
}
qdebug!(
"[{self}] Set max capacity to new capacity:{cap} old:{} max_table_size={}",
self.table.capacity(),
self.max_table_size,
);
let new_cap = min(self.max_table_size, cap);
// we also set our table to the max allowed.
self.change_capacity(new_cap);
Ok(())
}
/// This function is use for setting encoders max blocked streams. The value is received as
/// a `SETTINGS_QPACK_BLOCKED_STREAMS` setting parameter.
///
/// # Errors
///
/// `EncoderStream` if value is too big.
pub fn set_max_blocked_streams(&mut self, blocked_streams: u64) -> Res<()> {
self.max_blocked_streams = u16::try_from(blocked_streams).or(Err(Error::EncoderStream))?;
Ok(())
}
/// Reads decoder instructions.
///
/// # Errors
///
/// May return: `ClosedCriticalStream` if stream has been closed or `DecoderStream`
/// in case of any other transport error.
pub fn receive(&mut self, conn: &mut Connection, stream_id: StreamId) -> Res<()> {
self.read_instructions(conn, stream_id)
.map_err(|e| map_error(&e))
}
fn read_instructions(&mut self, conn: &mut Connection, stream_id: StreamId) -> Res<()> {
qdebug!("[{self}] read a new instruction");
loop {
let mut recv = ReceiverConnWrapper::new(conn, stream_id);
match self.instruction_reader.read_instructions(&mut recv) {
Ok(instruction) => self.call_instruction(instruction, conn.qlog_mut())?,
Err(Error::NeedMoreData) => break Ok(()),
Err(e) => break Err(e),
}
}
}
fn recalculate_blocked_streams(&mut self) {
let acked_inserts_cnt = self.table.get_acked_inserts_cnt();
self.blocked_stream_cnt = 0;
#[expect(
clippy::iter_over_hash_type,
reason = "OK to loop over unACKed blocks in an undefined order."
)]
for hb_list in self.unacked_header_blocks.values_mut() {
debug_assert!(!hb_list.is_empty());
if hb_list.iter().flatten().any(|e| *e >= acked_inserts_cnt) {
self.blocked_stream_cnt += 1;
}
}
}
fn insert_count_instruction(&mut self, increment: u64) -> Res<()> {
self.table
.increment_acked(increment)
.map_err(|_| Error::DecoderStream)?;
self.recalculate_blocked_streams();
Ok(())
}
fn header_ack(&mut self, stream_id: StreamId) {
self.stats.header_acks_recv += 1;
let mut new_acked = self.table.get_acked_inserts_cnt();
if let Some(hb_list) = self.unacked_header_blocks.get_mut(&stream_id) {
if let Some(ref_list) = hb_list.pop_back() {
#[expect(
clippy::iter_over_hash_type,
reason = "OK to loop over unACKed blocks in an undefined order."
)]
for iter in ref_list {
self.table.remove_ref(iter);
if iter >= new_acked {
new_acked = iter + 1;
}
}
} else {
debug_assert!(false, "We should have at least one header block");
}
if hb_list.is_empty() {
self.unacked_header_blocks.remove(&stream_id);
}
}
if new_acked > self.table.get_acked_inserts_cnt() {
self.insert_count_instruction(new_acked - self.table.get_acked_inserts_cnt())
.expect("This should neve happen");
}
}
fn stream_cancellation(&mut self, stream_id: StreamId) {
self.stats.stream_cancelled_recv += 1;
let mut was_blocker = false;
if let Some(mut hb_list) = self.unacked_header_blocks.remove(&stream_id) {
debug_assert!(!hb_list.is_empty());
while let Some(ref_list) = hb_list.pop_front() {
#[expect(
clippy::iter_over_hash_type,
reason = "OK to loop over unACKed blocks in an undefined order."
)]
for iter in ref_list {
self.table.remove_ref(iter);
was_blocker = was_blocker || (iter >= self.table.get_acked_inserts_cnt());
}
}
}
if was_blocker {
debug_assert!(self.blocked_stream_cnt > 0);
self.blocked_stream_cnt -= 1;
}
}
fn call_instruction(&mut self, instruction: DecoderInstruction, qlog: &Qlog) -> Res<()> {
qdebug!("[{self}] call instruction {instruction:?}");
match instruction {
DecoderInstruction::InsertCountIncrement { increment } => {
qlog::qpack_read_insert_count_increment_instruction(
qlog,
increment,
&increment.to_be_bytes(),
);
self.insert_count_instruction(increment)
}
DecoderInstruction::HeaderAck { stream_id } => {
self.header_ack(stream_id);
Ok(())
}
DecoderInstruction::StreamCancellation { stream_id } => {
self.stream_cancellation(stream_id);
Ok(())
}
DecoderInstruction::NoInstruction => Ok(()),
}
}
/// Inserts a new entry into a table and sends the corresponding instruction to a peer. An entry
/// is added only if it is possible to send the corresponding instruction immediately, i.e.
/// the encoder stream is not blocked by the flow control (or stream internal buffer(this is
/// very unlikely)).
///
/// # Errors
///
/// `EncoderStreamBlocked` if the encoder stream is blocked by the flow control.
/// `DynamicTableFull` if the dynamic table does not have enough space for the entry.
/// The function can return transport errors: `InvalidStreamId`, `InvalidInput` and
/// `FinalSizeError`.
///
/// # Panics
///
/// When the insertion fails (it should not).
pub fn send_and_insert(
&mut self,
conn: &mut Connection,
name: &[u8],
value: &[u8],
) -> Res<u64> {
qdebug!("[{self}] insert {name:?} {value:?}");
let entry_size = name.len() + value.len() + ADDITIONAL_TABLE_ENTRY_SIZE;
if !self.table.insert_possible(entry_size) {
return Err(Error::DynamicTableFull);
}
let mut buf = neqo_common::Encoder::default();
EncoderInstruction::InsertWithNameLiteral { name, value }
.marshal(&mut buf, self.use_huffman);
let stream_id = self.local_stream.stream_id().ok_or(Error::Internal)?;
let sent = conn
.stream_send_atomic(stream_id, buf.as_ref())
.map_err(|e| map_stream_send_atomic_error(&e))?;
if !sent {
return Err(Error::EncoderStreamBlocked);
}
self.stats.dynamic_table_inserts += 1;
match self.table.insert(name, value) {
Ok(inx) => Ok(inx),
Err(e) => {
debug_assert!(false);
Err(e)
}
}
}
fn change_capacity(&mut self, value: u64) {
qdebug!("[{self}] change capacity: {value}");
self.next_capacity = Some(value);
}
fn maybe_send_change_capacity(
&mut self,
conn: &mut Connection,
stream_id: StreamId,
) -> Res<()> {
if let Some(cap) = self.next_capacity {
// Check if it is possible to reduce the capacity, e.g. if enough space can be made free
// for the reduction.
if cap < self.table.capacity() && !self.table.can_evict_to(cap) {
return Err(Error::DynamicTableFull);
}
let mut buf = neqo_common::Encoder::default();
EncoderInstruction::Capacity { value: cap }.marshal(&mut buf, self.use_huffman);
if !conn.stream_send_atomic(stream_id, buf.as_ref())? {
return Err(Error::EncoderStreamBlocked);
}
if self.table.set_capacity(cap).is_err() {
debug_assert!(
false,
"can_evict_to should have checked and make sure this operation is possible"
);
return Err(Error::Internal);
}
self.max_entries = cap / 32;
self.next_capacity = None;
}
Ok(())
}
/// Sends any qpack encoder instructions.
///
/// # Errors
///
/// returns `EncoderStream` in case of an error.
pub fn send_encoder_updates(&mut self, conn: &mut Connection) -> Res<()> {
match self.local_stream {
LocalStreamState::NoStream => {
qerror!("Send call but there is no stream yet");
Ok(())
}
LocalStreamState::Uninitialized(stream_id) => {
let mut buf = neqo_common::Encoder::default();
buf.encode_varint(QPACK_UNI_STREAM_TYPE_ENCODER);
if !conn.stream_send_atomic(stream_id, buf.as_ref())? {
return Err(Error::EncoderStreamBlocked);
}
self.local_stream = LocalStreamState::Initialized(stream_id);
self.maybe_send_change_capacity(conn, stream_id)
}
LocalStreamState::Initialized(stream_id) => {
self.maybe_send_change_capacity(conn, stream_id)
}
}
}
fn is_stream_blocker(&self, stream_id: StreamId) -> bool {
self.unacked_header_blocks
.get(&stream_id)
.is_some_and(|hb_list| {
debug_assert!(!hb_list.is_empty());
hb_list
.iter()
.flatten()
.max()
.is_some_and(|max_ref| *max_ref >= self.table.get_acked_inserts_cnt())
})
}
/// Encodes headers
///
/// # Errors
///
/// `ClosedCriticalStream` if the encoder stream is closed.
/// `InternalError` if an unexpected error occurred.
///
/// # Panics
///
/// If there is a programming error.
pub fn encode_header_block(
&mut self,
conn: &mut Connection,
h: &[Header],
stream_id: StreamId,
) -> HeaderEncoder {
qdebug!("[{self}] encoding headers");
// Try to send capacity instructions if present.
// This code doesn't try to deal with errors, it just tries
// to write to the encoder stream AND if it can't uses
// literal instructions.
// The errors can be:
// 1) `EncoderStreamBlocked` - this is an error that can occur.
// 2) `InternalError` - this is unexpected error.
// 3) `ClosedCriticalStream` - this is error that should close the HTTP/3 session.
// The last 2 errors are ignored here and will be picked up
// by the main loop.
let mut encoder_blocked = self.send_encoder_updates(conn).is_err();
let mut encoded_h =
HeaderEncoder::new(self.table.base(), self.use_huffman, self.max_entries);
let stream_is_blocker = self.is_stream_blocker(stream_id);
let can_block = self.blocked_stream_cnt < self.max_blocked_streams || stream_is_blocker;
let mut ref_entries = HashSet::default();
for iter in h {
let name = iter.name().as_bytes().to_vec();
let value = iter.value().as_bytes().to_vec();
qtrace!("encoding {name:x?} {value:x?}");
if let Some(LookupResult {
index,
static_table,
value_matches,
}) = self.table.lookup(&name, &value, can_block)
{
qtrace!(
"[{self}] found a {} entry, value-match={value_matches}",
if static_table { "static" } else { "dynamic" }
);
if value_matches {
if static_table {
encoded_h.encode_indexed_static(index);
} else {
encoded_h.encode_indexed_dynamic(index);
}
} else {
encoded_h.encode_literal_with_name_ref(static_table, index, &value);
}
if !static_table && ref_entries.insert(index) {
self.table.add_ref(index);
}
} else if can_block && !encoder_blocked {
// Insert using an InsertWithNameLiteral instruction. This entry name does not match
// any name in the tables therefore we cannot use any other
// instruction.
if let Ok(index) = self.send_and_insert(conn, &name, &value) {
encoded_h.encode_indexed_dynamic(index);
ref_entries.insert(index);
self.table.add_ref(index);
} else {
// This code doesn't try to deal with errors, it just tries
// to write to the encoder stream AND if it can't uses
// literal instructions.
// The errors can be:
// 1) `EncoderStreamBlocked` - this is an error that can occur.
// 2) `DynamicTableFull` - this is an error that can occur.
// 3) `InternalError` - this is unexpected error.
// 4) `ClosedCriticalStream` - this is error that should close the HTTP/3
// session.
// The last 2 errors are ignored here and will be picked up
// by the main loop.
// As soon as one of the instructions cannot be written or the table is full, do
// not try again.
encoder_blocked = true;
encoded_h.encode_literal_with_name_literal(&name, &value);
}
} else {
encoded_h.encode_literal_with_name_literal(&name, &value);
}
}
encoded_h.encode_header_block_prefix();
if !stream_is_blocker {
// The streams was not a blocker, check if the stream is a blocker now.
if let Some(max_ref) = ref_entries.iter().max() {
if *max_ref >= self.table.get_acked_inserts_cnt() {
debug_assert!(self.blocked_stream_cnt <= self.max_blocked_streams);
self.blocked_stream_cnt += 1;
}
}
}
if !ref_entries.is_empty() {
self.unacked_header_blocks
.entry(stream_id)
.or_default()
.push_front(ref_entries);
self.stats.dynamic_table_references += 1;
}
encoded_h
}
/// Encoder stream has been created. Add the stream id.
///
/// # Panics
///
/// If a stream has already been added.
pub fn add_send_stream(&mut self, stream_id: StreamId) {
if self.local_stream == LocalStreamState::NoStream {
self.local_stream = LocalStreamState::Uninitialized(stream_id);
} else {
panic!("Adding multiple local streams");
}
}
#[must_use]
pub fn stats(&self) -> Stats {
self.stats.clone()
}
#[must_use]
pub const fn local_stream_id(&self) -> Option<StreamId> {
self.local_stream.stream_id()
}
#[cfg(test)]
const fn blocked_stream_cnt(&self) -> u16 {
self.blocked_stream_cnt
}
}
impl Display for Encoder {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "QPack")
}
}
fn map_error(err: &Error) -> Error {
if *err == Error::ClosedCriticalStream {
Error::ClosedCriticalStream
} else {
Error::DecoderStream
}
}
fn map_stream_send_atomic_error(err: &TransportError) -> Error {
match err {
TransportError::InvalidStreamId | TransportError::FinalSize => Error::ClosedCriticalStream,
_ => {
debug_assert!(false, "Unexpected error");
Error::Internal
}
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use neqo_transport::{ConnectionParameters, StreamId, StreamType};
use test_fixture::{
default_client, default_server, handshake, new_server, now, CountingConnectionIdGenerator,
DEFAULT_ALPN,
};
use super::{Connection, Encoder, Error, Header, Res};
use crate::Settings;
struct TestEncoder {
encoder: Encoder,
send_stream_id: StreamId,
recv_stream_id: StreamId,
conn: Connection,
peer_conn: Connection,
}
impl TestEncoder {
pub fn change_capacity(&mut self, capacity: u64) -> Res<()> {
self.encoder.set_max_capacity(capacity)?;
// We will try to really change the table only when we send the change capacity
// instruction.
self.encoder.send_encoder_updates(&mut self.conn)
}
pub fn insert(&mut self, header: &[u8], value: &[u8], inst: &[u8]) {
let res = self.encoder.send_and_insert(&mut self.conn, header, value);
assert!(res.is_ok());
self.send_instructions(inst);
}
pub fn encode_header_block(
&mut self,
stream_id: StreamId,
headers: &[Header],
expected_encoding: &[u8],
inst: &[u8],
) {
let buf = self
.encoder
.encode_header_block(&mut self.conn, headers, stream_id);
assert_eq!(buf.as_ref(), expected_encoding);
self.send_instructions(inst);
}
pub fn send_instructions(&mut self, encoder_instruction: &[u8]) {
self.encoder.send_encoder_updates(&mut self.conn).unwrap();
let out = self.conn.process_output(now());
let out2 = self.peer_conn.process(out.dgram(), now());
drop(self.conn.process(out2.dgram(), now()));
let mut buf = [0_u8; 100];
let (amount, fin) = self
.peer_conn
.stream_recv(self.send_stream_id, &mut buf)
.unwrap();
assert!(!fin);
assert_eq!(buf[..amount], encoder_instruction[..]);
}
}
fn connect_generic(huffman: bool, max_data: Option<u64>) -> TestEncoder {
let mut conn = default_client();
let mut peer_conn = max_data.map_or_else(default_server, |max| {
new_server::<CountingConnectionIdGenerator>(
DEFAULT_ALPN,
ConnectionParameters::default()
.max_stream_data(StreamType::UniDi, true, max)
.max_stream_data(StreamType::BiDi, true, max)
.max_stream_data(StreamType::BiDi, false, max),
)
});
handshake(&mut conn, &mut peer_conn);
// create a stream
let recv_stream_id = peer_conn.stream_create(StreamType::UniDi).unwrap();
let send_stream_id = conn.stream_create(StreamType::UniDi).unwrap();
// create an encoder
let mut encoder = Encoder::new(
&Settings {
max_table_size_encoder: 1500,
max_table_size_decoder: 0,
max_blocked_streams: 0,
},
huffman,
);
encoder.add_send_stream(send_stream_id);
TestEncoder {
encoder,
send_stream_id,
recv_stream_id,
conn,
peer_conn,
}
}
fn connect(huffman: bool) -> TestEncoder {
connect_generic(huffman, None)
}
fn connect_flow_control(max_data: u64) -> TestEncoder {
connect_generic(true, Some(max_data))
}
fn recv_instruction(encoder: &mut TestEncoder, decoder_instruction: &[u8]) {
encoder
.peer_conn
.stream_send(encoder.recv_stream_id, decoder_instruction)
.unwrap();
let out = encoder.peer_conn.process_output(now());
drop(encoder.conn.process(out.dgram(), now()));
assert!(encoder
.encoder
.read_instructions(&mut encoder.conn, encoder.recv_stream_id)
.is_ok());
}
const CAP_INSTRUCTION_200: &[u8] = &[0x02, 0x3f, 0xa9, 0x01];
const CAP_INSTRUCTION_60: &[u8] = &[0x02, 0x3f, 0x1d];
const CAP_INSTRUCTION_1000: &[u8] = &[0x02, 0x3f, 0xc9, 0x07];
const CAP_INSTRUCTION_1500: &[u8] = &[0x02, 0x3f, 0xbd, 0x0b];
const HEADER_CONTENT_LENGTH: &[u8] = &[
0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68,
];
const VALUE_1: &[u8] = &[0x31, 0x32, 0x33, 0x34];
const VALUE_2: &[u8] = &[0x31, 0x32, 0x33, 0x34, 0x35];
// HEADER_CONTENT_LENGTH and VALUE_1 encoded by instruction insert_with_name_literal.
const HEADER_CONTENT_LENGTH_VALUE_1_NAME_LITERAL: &[u8] = &[
0x4e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68,
0x04, 0x31, 0x32, 0x33, 0x34,
];
// HEADER_CONTENT_LENGTH and VALUE_2 encoded by instruction insert_with_name_literal.
const HEADER_CONTENT_LENGTH_VALUE_2_NAME_LITERAL: &[u8] = &[
0x4e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68,
0x05, 0x31, 0x32, 0x33, 0x34, 0x35,
];
// Indexed Header Field that refers to the first entry in the dynamic table.
const ENCODE_INDEXED_REF_DYNAMIC: &[u8] = &[0x02, 0x00, 0x80];
const STREAM_1: StreamId = StreamId::new(1);
const STREAM_2: StreamId = StreamId::new(2);
const HEADER_ACK_STREAM_ID_1: &[u8] = &[0x81];
const HEADER_ACK_STREAM_ID_2: &[u8] = &[0x82];
const STREAM_CANCELED_ID_1: &[u8] = &[0x41];
// test insert_with_name_literal which fails because there is not enough space in the table
#[test]
fn insert_with_name_literal_1() {
let mut encoder = connect(false);
// insert "content-length: 1234
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_1);
assert_eq!(Error::DynamicTableFull, res.unwrap_err());
encoder.send_instructions(&[0x02]);
}
// test insert_with_name_literal - succeeds
#[test]
fn insert_with_name_literal_2() {
let mut encoder = connect(false);
assert!(encoder.encoder.set_max_capacity(200).is_ok());
// test the change capacity instruction.
encoder.send_instructions(CAP_INSTRUCTION_200);
// insert "content-length: 1234
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_1);
assert!(res.is_ok());
encoder.send_instructions(HEADER_CONTENT_LENGTH_VALUE_1_NAME_LITERAL);
}
#[test]
fn change_capacity() {
let mut encoder = connect(false);
assert!(encoder.encoder.set_max_capacity(200).is_ok());
encoder.send_instructions(CAP_INSTRUCTION_200);
}
struct TestElement {
pub headers: Vec<Header>,
pub header_block: &'static [u8],
pub encoder_inst: &'static [u8],
}
#[test]
fn header_block_encoder_non() {
let test_cases: [TestElement; 6] = [
// test a header with ref to static - encode_indexed
TestElement {
headers: vec![Header::new(":method", "GET")],
header_block: &[0x00, 0x00, 0xd1],
encoder_inst: &[],
},
// test encode_literal_with_name_ref
TestElement {
headers: vec![Header::new(":path", "/somewhere")],
header_block: &[
0x00, 0x00, 0x51, 0x0a, 0x2f, 0x73, 0x6f, 0x6d, 0x65, 0x77, 0x68, 0x65, 0x72,
0x65,
],
encoder_inst: &[],
},
// test adding a new header and encode_post_base_index, also test
// fix_header_block_prefix
TestElement {
headers: vec![Header::new("my-header", "my-value")],
header_block: &[0x02, 0x80, 0x10],
encoder_inst: &[
0x49, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x08, 0x6d, 0x79,
0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65,
],
},
// test encode_indexed with a ref to dynamic table.
TestElement {
headers: vec![Header::new("my-header", "my-value")],
header_block: ENCODE_INDEXED_REF_DYNAMIC,
encoder_inst: &[],
},
// test encode_literal_with_name_ref.
TestElement {
headers: vec![Header::new("my-header", "my-value2")],
header_block: &[
0x02, 0x00, 0x40, 0x09, 0x6d, 0x79, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x32,
],
encoder_inst: &[],
},
// test multiple headers
TestElement {
headers: vec![
Header::new(":method", "GET"),
Header::new(":path", "/somewhere"),
Header::new(":authority", "example.com"),
Header::new(":scheme", "https"),
],
header_block: &[
0x00, 0x01, 0xd1, 0x51, 0x0a, 0x2f, 0x73, 0x6f, 0x6d, 0x65, 0x77, 0x68, 0x65,
0x72, 0x65, 0x50, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
0x6f, 0x6d, 0xd7,
],
encoder_inst: &[],
},
];
let mut encoder = connect(false);
encoder.encoder.set_max_blocked_streams(100).unwrap();
encoder.encoder.set_max_capacity(200).unwrap();
// test the change capacity instruction.
encoder.send_instructions(CAP_INSTRUCTION_200);
for t in &test_cases {
let buf = encoder
.encoder
.encode_header_block(&mut encoder.conn, &t.headers, STREAM_1);
assert_eq!(buf.as_ref(), t.header_block);
encoder.send_instructions(t.encoder_inst);
}
}
#[test]
fn header_block_encoder_huffman() {
let test_cases: [TestElement; 6] = [
// test a header with ref to static - encode_indexed
TestElement {
headers: vec![Header::new(":method", "GET")],
header_block: &[0x00, 0x00, 0xd1],
encoder_inst: &[],
},
// test encode_literal_with_name_ref
TestElement {
headers: vec![Header::new(":path", "/somewhere")],
header_block: &[
0x00, 0x00, 0x51, 0x87, 0x61, 0x07, 0xa4, 0xbe, 0x27, 0x2d, 0x85,
],
encoder_inst: &[],
},
// test adding a new header and encode_post_base_index, also test
// fix_header_block_prefix
TestElement {
headers: vec![Header::new("my-header", "my-value")],
header_block: &[0x02, 0x80, 0x10],
encoder_inst: &[
0x67, 0xa7, 0xd2, 0xd3, 0x94, 0x72, 0x16, 0xcf, 0x86, 0xa7, 0xd2, 0xdd, 0xc7,
0x45, 0xa5,
],
},
// test encode_indexed with a ref to dynamic table.
TestElement {
headers: vec![Header::new("my-header", "my-value")],
header_block: ENCODE_INDEXED_REF_DYNAMIC,
encoder_inst: &[],
},
// test encode_literal_with_name_ref.
TestElement {
headers: vec![Header::new("my-header", "my-value2")],
header_block: &[
0x02, 0x00, 0x40, 0x87, 0xa7, 0xd2, 0xdd, 0xc7, 0x45, 0xa5, 0x17,
],
encoder_inst: &[],
},
// test multiple headers
TestElement {
headers: vec![
Header::new(":method", "GET"),
Header::new(":path", "/somewhere"),
Header::new(":authority", "example.com"),
Header::new(":scheme", "https"),
],
header_block: &[
0x00, 0x01, 0xd1, 0x51, 0x87, 0x61, 0x07, 0xa4, 0xbe, 0x27, 0x2d, 0x85, 0x50,
0x88, 0x2f, 0x91, 0xd3, 0x5d, 0x05, 0x5c, 0x87, 0xa7, 0xd7,
],
encoder_inst: &[],
},
];
let mut encoder = connect(true);
encoder.encoder.set_max_blocked_streams(100).unwrap();
encoder.encoder.set_max_capacity(200).unwrap();
// test the change capacity instruction.
encoder.send_instructions(CAP_INSTRUCTION_200);
for t in &test_cases {
let buf = encoder
.encoder
.encode_header_block(&mut encoder.conn, &t.headers, STREAM_1);
assert_eq!(buf.as_ref(), t.header_block);
encoder.send_instructions(t.encoder_inst);
}
}
// Test inserts block on waiting for an insert count increment.
#[test]
fn insertion_blocked_on_insert_count_feedback() {
let mut encoder = connect(false);
encoder.encoder.set_max_capacity(60).unwrap();
// test the change capacity instruction.
encoder.send_instructions(CAP_INSTRUCTION_60);
// insert "content-length: 1234
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_1);
assert!(res.is_ok());
encoder.send_instructions(HEADER_CONTENT_LENGTH_VALUE_1_NAME_LITERAL);
// insert "content-length: 12345 which will fail because the entry in the table cannot be
// evicted.
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_2);
assert!(res.is_err());
encoder.send_instructions(&[]);
// receive an insert count increment.
recv_instruction(&mut encoder, &[0x01]);
// insert "content-length: 12345 again it will succeed.
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_2);
assert!(res.is_ok());
encoder.send_instructions(HEADER_CONTENT_LENGTH_VALUE_2_NAME_LITERAL);
}
// Test inserts block on waiting for ACKs
// test the table insertion is blocked:
// 0 - waiting for a header ack
// 2 - waiting for a stream cancel.
fn test_insertion_blocked_on_waiting_for_header_ack_or_stream_cancel(wait: u8) {
let mut encoder = connect(false);
assert!(encoder.encoder.set_max_capacity(60).is_ok());
// test the change capacity instruction.
encoder.send_instructions(CAP_INSTRUCTION_60);
// insert "content-length: 1234
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_1);
assert!(res.is_ok());
encoder.send_instructions(HEADER_CONTENT_LENGTH_VALUE_1_NAME_LITERAL);
// receive an insert count increment.
recv_instruction(&mut encoder, &[0x01]);
// send a header block
let buf = encoder.encoder.encode_header_block(
&mut encoder.conn,
&[Header::new("content-length", "1234")],
STREAM_1,
);
assert_eq!(buf.as_ref(), ENCODE_INDEXED_REF_DYNAMIC);
encoder.send_instructions(&[]);
// insert "content-length: 12345 which will fail because the entry in the table cannot be
// evicted
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_2);
assert!(res.is_err());
encoder.send_instructions(&[]);
if wait == 0 {
// receive a header_ack.
recv_instruction(&mut encoder, HEADER_ACK_STREAM_ID_1);
} else {
// receive a stream canceled
recv_instruction(&mut encoder, STREAM_CANCELED_ID_1);
}
// insert "content-length: 12345 again it will succeed.
let res =
encoder
.encoder
.send_and_insert(&mut encoder.conn, HEADER_CONTENT_LENGTH, VALUE_2);
assert!(res.is_ok());
encoder.send_instructions(HEADER_CONTENT_LENGTH_VALUE_2_NAME_LITERAL);
}
#[test]
fn header_ack() {
test_insertion_blocked_on_waiting_for_header_ack_or_stream_cancel(0);
}
#[test]
fn stream_canceled() {
test_insertion_blocked_on_waiting_for_header_ack_or_stream_cancel(1);
}
fn assert_is_index_to_dynamic(buf: &[u8]) {
assert_eq!(buf[2] & 0xc0, 0x80);
}
fn assert_is_index_to_dynamic_post(buf: &[u8]) {
assert_eq!(buf[2] & 0xf0, 0x10);
}