1use super::Error;
28use super::Result;
29
30use super::frame;
31
32pub const HTTP3_CONTROL_STREAM_TYPE_ID: u64 = 0x0;
33pub const HTTP3_PUSH_STREAM_TYPE_ID: u64 = 0x1;
34pub const QPACK_ENCODER_STREAM_TYPE_ID: u64 = 0x2;
35pub const QPACK_DECODER_STREAM_TYPE_ID: u64 = 0x3;
36
37const MAX_STATE_BUF_SIZE: usize = (1 << 24) - 1;
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40pub enum Type {
41 Control,
42 Request,
43 Push,
44 QpackEncoder,
45 QpackDecoder,
46 Unknown,
47}
48
49impl Type {
50 #[cfg(feature = "qlog")]
51 pub fn to_qlog(self) -> qlog::events::h3::H3StreamType {
52 match self {
53 Type::Control => qlog::events::h3::H3StreamType::Control,
54 Type::Request => qlog::events::h3::H3StreamType::Request,
55 Type::Push => qlog::events::h3::H3StreamType::Push,
56 Type::QpackEncoder => qlog::events::h3::H3StreamType::QpackEncode,
57 Type::QpackDecoder => qlog::events::h3::H3StreamType::QpackDecode,
58 Type::Unknown => qlog::events::h3::H3StreamType::Unknown,
59 }
60 }
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64pub enum State {
65 StreamType,
67
68 FrameType,
70
71 FramePayloadLen,
73
74 FramePayload,
76
77 Data,
79
80 PushId,
82
83 QpackInstruction,
85
86 Drain,
88
89 Finished,
91}
92
93impl Type {
94 pub fn deserialize(v: u64) -> Result<Type> {
95 match v {
96 HTTP3_CONTROL_STREAM_TYPE_ID => Ok(Type::Control),
97 HTTP3_PUSH_STREAM_TYPE_ID => Ok(Type::Push),
98 QPACK_ENCODER_STREAM_TYPE_ID => Ok(Type::QpackEncoder),
99 QPACK_DECODER_STREAM_TYPE_ID => Ok(Type::QpackDecoder),
100
101 _ => Ok(Type::Unknown),
102 }
103 }
104}
105
106#[derive(Debug)]
121pub struct Stream {
122 id: u64,
124
125 ty: Option<Type>,
127
128 state: State,
130
131 state_buf: Vec<u8>,
133
134 state_len: usize,
136
137 state_off: usize,
141
142 frame_type: Option<u64>,
144
145 is_local: bool,
147
148 remote_initialized: bool,
150
151 local_initialized: bool,
153
154 data_event_triggered: bool,
156
157 last_priority_update: Option<Vec<u8>>,
159
160 headers_received_count: usize,
162
163 data_received: bool,
165
166 trailers_sent: bool,
168
169 trailers_received: bool,
171}
172
173impl Stream {
174 pub fn new(id: u64, is_local: bool) -> Stream {
179 let (ty, state) = if crate::stream::is_bidi(id) {
180 (Some(Type::Request), State::FrameType)
183 } else {
184 (None, State::StreamType)
186 };
187
188 Stream {
189 id,
190 ty,
191
192 state,
193
194 state_buf: vec![0; 16],
196
197 state_len: 1,
200 state_off: 0,
201
202 frame_type: None,
203
204 is_local,
205 remote_initialized: false,
206 local_initialized: false,
207
208 data_event_triggered: false,
209
210 last_priority_update: None,
211
212 headers_received_count: 0,
213
214 data_received: false,
215
216 trailers_sent: false,
217 trailers_received: false,
218 }
219 }
220
221 pub fn ty(&self) -> Option<Type> {
222 self.ty
223 }
224
225 pub fn state(&self) -> State {
226 self.state
227 }
228
229 pub fn set_ty(&mut self, ty: Type) -> Result<()> {
231 assert_eq!(self.state, State::StreamType);
232
233 self.ty = Some(ty);
234
235 let state = match ty {
236 Type::Control | Type::Request => State::FrameType,
237
238 Type::Push => State::PushId,
239
240 Type::QpackEncoder | Type::QpackDecoder => {
241 self.remote_initialized = true;
242
243 State::QpackInstruction
244 },
245
246 Type::Unknown => State::Drain,
247 };
248
249 self.state_transition(state, 1, true)?;
250
251 Ok(())
252 }
253
254 pub fn set_push_id(&mut self, _id: u64) -> Result<()> {
256 assert_eq!(self.state, State::PushId);
257
258 self.state_transition(State::FrameType, 1, true)?;
261
262 Ok(())
263 }
264
265 pub fn set_frame_type(&mut self, ty: u64) -> Result<()> {
267 assert_eq!(self.state, State::FrameType);
268
269 match self.ty {
271 Some(Type::Control) => {
272 match (ty, self.remote_initialized) {
276 (frame::SETTINGS_FRAME_TYPE_ID, false) =>
278 self.remote_initialized = true,
279
280 (_, false) => return Err(Error::MissingSettings),
283
284 (frame::SETTINGS_FRAME_TYPE_ID, true) =>
286 return Err(Error::FrameUnexpected),
287
288 (frame::DATA_FRAME_TYPE_ID, true) =>
291 return Err(Error::FrameUnexpected),
292
293 (frame::HEADERS_FRAME_TYPE_ID, true) =>
294 return Err(Error::FrameUnexpected),
295
296 (frame::PUSH_PROMISE_FRAME_TYPE_ID, true) =>
297 return Err(Error::FrameUnexpected),
298
299 (_, true) => (),
301 }
302 },
303
304 Some(Type::Request) => {
305 if !self.is_local {
312 match (ty, self.remote_initialized) {
313 (frame::HEADERS_FRAME_TYPE_ID, false) => {
314 self.remote_initialized = true;
315 },
316
317 (frame::DATA_FRAME_TYPE_ID, false) =>
318 return Err(Error::FrameUnexpected),
319
320 (frame::HEADERS_FRAME_TYPE_ID, true) => {
321 if self.trailers_received {
322 return Err(Error::FrameUnexpected);
323 }
324
325 if self.data_received {
326 self.trailers_received = true;
327 }
328 },
329
330 (frame::DATA_FRAME_TYPE_ID, true) => {
331 if self.trailers_received {
332 return Err(Error::FrameUnexpected);
333 }
334
335 self.data_received = true;
336 },
337
338 (frame::CANCEL_PUSH_FRAME_TYPE_ID, _) =>
339 return Err(Error::FrameUnexpected),
340
341 (frame::SETTINGS_FRAME_TYPE_ID, _) =>
342 return Err(Error::FrameUnexpected),
343
344 (frame::GOAWAY_FRAME_TYPE_ID, _) =>
345 return Err(Error::FrameUnexpected),
346
347 (frame::MAX_PUSH_FRAME_TYPE_ID, _) =>
348 return Err(Error::FrameUnexpected),
349
350 _ => (),
353 }
354 }
355 },
356
357 Some(Type::Push) => {
358 match ty {
359 frame::CANCEL_PUSH_FRAME_TYPE_ID =>
361 return Err(Error::FrameUnexpected),
362
363 frame::SETTINGS_FRAME_TYPE_ID =>
364 return Err(Error::FrameUnexpected),
365
366 frame::PUSH_PROMISE_FRAME_TYPE_ID =>
367 return Err(Error::FrameUnexpected),
368
369 frame::GOAWAY_FRAME_TYPE_ID =>
370 return Err(Error::FrameUnexpected),
371
372 frame::MAX_PUSH_FRAME_TYPE_ID =>
373 return Err(Error::FrameUnexpected),
374
375 _ => (),
376 }
377 },
378
379 _ => return Err(Error::FrameUnexpected),
380 }
381
382 self.frame_type = Some(ty);
383
384 self.state_transition(State::FramePayloadLen, 1, true)?;
385
386 Ok(())
387 }
388
389 pub fn frame_type(&self) -> Option<u64> {
391 self.frame_type
392 }
393
394 pub fn set_frame_payload_len(&mut self, len: u64) -> Result<()> {
396 assert_eq!(self.state, State::FramePayloadLen);
397
398 if matches!(self.ty, Some(Type::Control | Type::Request | Type::Push)) {
400 let (state, resize) = match self.frame_type {
401 Some(frame::DATA_FRAME_TYPE_ID) => (State::Data, false),
402
403 Some(
406 frame::GOAWAY_FRAME_TYPE_ID |
407 frame::PUSH_PROMISE_FRAME_TYPE_ID |
408 frame::CANCEL_PUSH_FRAME_TYPE_ID |
409 frame::MAX_PUSH_FRAME_TYPE_ID,
410 ) => {
411 if len == 0 {
412 return Err(Error::FrameError);
413 }
414
415 (State::FramePayload, true)
416 },
417
418 _ => (State::FramePayload, true),
419 };
420
421 self.state_transition(state, len as usize, resize)?;
422
423 return Ok(());
424 }
425
426 Err(Error::InternalError)
427 }
428
429 pub fn try_fill_buffer(
435 &mut self, conn: &mut crate::Connection,
436 ) -> Result<()> {
437 if self.state_buffer_complete() {
439 return Ok(());
440 }
441
442 let buf = &mut self.state_buf[self.state_off..self.state_len];
443
444 let read = match conn.stream_recv(self.id, buf) {
445 Ok((len, fin)) => {
446 if fin &&
448 matches!(
449 self.ty,
450 Some(Type::Control) |
451 Some(Type::QpackEncoder) |
452 Some(Type::QpackDecoder)
453 )
454 {
455 super::close_conn_critical_stream(conn)?;
456 }
457
458 len
459 },
460
461 Err(e @ crate::Error::StreamReset(_)) => {
462 if matches!(
464 self.ty,
465 Some(Type::Control) |
466 Some(Type::QpackEncoder) |
467 Some(Type::QpackDecoder)
468 ) {
469 super::close_conn_critical_stream(conn)?;
470 }
471
472 return Err(e.into());
473 },
474
475 Err(e) => {
476 if e == crate::Error::Done {
478 self.reset_data_event();
479 }
480
481 return Err(e.into());
482 },
483 };
484
485 trace!(
486 "{} read {} bytes on stream {}",
487 conn.trace_id(),
488 read,
489 self.id,
490 );
491
492 self.state_off += read;
493
494 if !self.state_buffer_complete() {
495 self.reset_data_event();
496
497 return Err(Error::Done);
498 }
499
500 Ok(())
501 }
502
503 pub fn initialize_local(&mut self) {
505 self.local_initialized = true
506 }
507
508 pub fn local_initialized(&self) -> bool {
510 self.local_initialized
511 }
512
513 pub fn increment_headers_received(&mut self) {
514 self.headers_received_count =
515 self.headers_received_count.saturating_add(1);
516 }
517
518 pub fn headers_received_count(&self) -> usize {
519 self.headers_received_count
520 }
521
522 pub fn mark_trailers_sent(&mut self) {
523 self.trailers_sent = true;
524 }
525
526 pub fn trailers_sent(&self) -> bool {
527 self.trailers_sent
528 }
529
530 #[cfg(test)]
535 fn try_fill_buffer_for_tests(
536 &mut self, stream: &mut std::io::Cursor<Vec<u8>>,
537 ) -> Result<()> {
538 if self.state_buffer_complete() {
540 return Ok(());
541 }
542
543 let buf = &mut self.state_buf[self.state_off..self.state_len];
544
545 let read = std::io::Read::read(stream, buf).unwrap();
546
547 self.state_off += read;
548
549 if !self.state_buffer_complete() {
550 return Err(Error::Done);
551 }
552
553 Ok(())
554 }
555
556 pub fn try_consume_varint(&mut self) -> Result<u64> {
558 if self.state_off == 1 {
559 self.state_len = octets::varint_parse_len(self.state_buf[0]);
560 self.state_buf.resize(self.state_len, 0);
561 }
562
563 if !self.state_buffer_complete() {
566 return Err(Error::Done);
567 }
568
569 let varint = octets::Octets::with_slice(&self.state_buf).get_varint()?;
570
571 Ok(varint)
572 }
573
574 pub fn try_consume_frame(&mut self) -> Result<(frame::Frame, u64)> {
578 self.reset_data_event();
580
581 let payload_len = self.state_len as u64;
582
583 let frame = frame::Frame::from_bytes(
585 self.frame_type.unwrap(),
586 payload_len,
587 &self.state_buf,
588 )?;
589
590 self.state_transition(State::FrameType, 1, true)?;
591
592 Ok((frame, payload_len))
593 }
594
595 pub fn try_consume_data(
597 &mut self, conn: &mut crate::Connection, out: &mut [u8],
598 ) -> Result<(usize, bool)> {
599 let left = std::cmp::min(out.len(), self.state_len - self.state_off);
600
601 let (len, fin) = match conn.stream_recv(self.id, &mut out[..left]) {
602 Ok(v) => v,
603
604 Err(e) => {
605 if e == crate::Error::Done {
607 self.reset_data_event();
608 }
609
610 return Err(e.into());
611 },
612 };
613
614 self.state_off += len;
615
616 if !conn.stream_readable(self.id) {
618 self.reset_data_event();
619 }
620
621 if self.state_buffer_complete() {
622 self.state_transition(State::FrameType, 1, true)?;
623 }
624
625 Ok((len, fin))
626 }
627
628 pub fn finished(&mut self) {
630 let _ = self.state_transition(State::Finished, 0, false);
631 }
632
633 #[cfg(test)]
638 fn try_consume_data_for_tests(
639 &mut self, stream: &mut std::io::Cursor<Vec<u8>>, out: &mut [u8],
640 ) -> Result<usize> {
641 let left = std::cmp::min(out.len(), self.state_len - self.state_off);
642
643 let len = std::io::Read::read(stream, &mut out[..left]).unwrap();
644
645 self.state_off += len;
646
647 if self.state_buffer_complete() {
648 self.state_transition(State::FrameType, 1, true)?;
649 }
650
651 Ok(len)
652 }
653
654 pub fn try_trigger_data_event(&mut self) -> bool {
659 if self.data_event_triggered {
660 return false;
661 }
662
663 self.data_event_triggered = true;
664
665 true
666 }
667
668 fn reset_data_event(&mut self) {
670 self.data_event_triggered = false;
671 }
672
673 pub fn set_last_priority_update(&mut self, priority_update: Option<Vec<u8>>) {
675 self.last_priority_update = priority_update;
676 }
677
678 pub fn take_last_priority_update(&mut self) -> Option<Vec<u8>> {
680 self.last_priority_update.take()
681 }
682
683 pub fn has_last_priority_update(&self) -> bool {
685 self.last_priority_update.is_some()
686 }
687
688 fn state_buffer_complete(&self) -> bool {
690 self.state_off == self.state_len
691 }
692
693 fn state_transition(
696 &mut self, new_state: State, expected_len: usize, resize: bool,
697 ) -> Result<()> {
698 if resize {
701 if expected_len > MAX_STATE_BUF_SIZE {
705 return Err(Error::ExcessiveLoad);
706 }
707
708 self.state_buf.resize(expected_len, 0);
709 }
710
711 self.state = new_state;
712 self.state_off = 0;
713 self.state_len = expected_len;
714
715 Ok(())
716 }
717}
718
719#[cfg(test)]
720mod tests {
721 use crate::h3::frame::*;
722
723 use super::*;
724
725 fn open_uni(b: &mut octets::OctetsMut, ty: u64) -> Result<Stream> {
726 let stream = Stream::new(2, false);
727 assert_eq!(stream.state, State::StreamType);
728
729 b.put_varint(ty)?;
730
731 Ok(stream)
732 }
733
734 fn parse_uni(
735 stream: &mut Stream, ty: u64, cursor: &mut std::io::Cursor<Vec<u8>>,
736 ) -> Result<()> {
737 stream.try_fill_buffer_for_tests(cursor)?;
738
739 let stream_ty = stream.try_consume_varint()?;
740 assert_eq!(stream_ty, ty);
741 stream.set_ty(Type::deserialize(stream_ty).unwrap())?;
742
743 Ok(())
744 }
745
746 fn parse_skip_frame(
747 stream: &mut Stream, cursor: &mut std::io::Cursor<Vec<u8>>,
748 ) -> Result<()> {
749 stream.try_fill_buffer_for_tests(cursor)?;
751
752 let frame_ty = stream.try_consume_varint()?;
753
754 stream.set_frame_type(frame_ty)?;
755 assert_eq!(stream.state, State::FramePayloadLen);
756
757 stream.try_fill_buffer_for_tests(cursor)?;
759
760 let frame_payload_len = stream.try_consume_varint()?;
761 stream.set_frame_payload_len(frame_payload_len)?;
762 assert_eq!(stream.state, State::FramePayload);
763
764 stream.try_fill_buffer_for_tests(cursor)?;
766
767 stream.try_consume_frame()?;
768 assert_eq!(stream.state, State::FrameType);
769
770 Ok(())
771 }
772
773 #[test]
774 fn control_good() {
776 let mut d = vec![42; 40];
777 let mut b = octets::OctetsMut::with_slice(&mut d);
778
779 let raw_settings = vec![
780 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0),
781 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
782 (SETTINGS_QPACK_BLOCKED_STREAMS, 0),
783 ];
784
785 let frame = Frame::Settings {
786 max_field_section_size: Some(0),
787 qpack_max_table_capacity: Some(0),
788 qpack_blocked_streams: Some(0),
789 connect_protocol_enabled: None,
790 h3_datagram: None,
791 grease: None,
792 additional_settings: None,
793 raw: Some(raw_settings),
794 };
795
796 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
797 frame.to_bytes(&mut b).unwrap();
798
799 let mut cursor = std::io::Cursor::new(d);
800
801 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
802 .unwrap();
803 assert_eq!(stream.state, State::FrameType);
804
805 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
807
808 let frame_ty = stream.try_consume_varint().unwrap();
809 assert_eq!(frame_ty, frame::SETTINGS_FRAME_TYPE_ID);
810
811 stream.set_frame_type(frame_ty).unwrap();
812 assert_eq!(stream.state, State::FramePayloadLen);
813
814 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
816
817 let frame_payload_len = stream.try_consume_varint().unwrap();
818 assert_eq!(frame_payload_len, 6);
819 stream.set_frame_payload_len(frame_payload_len).unwrap();
820 assert_eq!(stream.state, State::FramePayload);
821
822 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
824
825 assert_eq!(stream.try_consume_frame(), Ok((frame, 6)));
826 assert_eq!(stream.state, State::FrameType);
827 }
828
829 #[test]
830 fn control_empty_settings() {
832 let mut d = vec![42; 40];
833 let mut b = octets::OctetsMut::with_slice(&mut d);
834
835 let frame = Frame::Settings {
836 max_field_section_size: None,
837 qpack_max_table_capacity: None,
838 qpack_blocked_streams: None,
839 connect_protocol_enabled: None,
840 h3_datagram: None,
841 grease: None,
842 additional_settings: None,
843 raw: Some(vec![]),
844 };
845
846 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
847 frame.to_bytes(&mut b).unwrap();
848
849 let mut cursor = std::io::Cursor::new(d);
850
851 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
852 .unwrap();
853 assert_eq!(stream.state, State::FrameType);
854
855 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
857
858 let frame_ty = stream.try_consume_varint().unwrap();
859 assert_eq!(frame_ty, frame::SETTINGS_FRAME_TYPE_ID);
860
861 stream.set_frame_type(frame_ty).unwrap();
862 assert_eq!(stream.state, State::FramePayloadLen);
863
864 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
866
867 let frame_payload_len = stream.try_consume_varint().unwrap();
868 assert_eq!(frame_payload_len, 0);
869 stream.set_frame_payload_len(frame_payload_len).unwrap();
870 assert_eq!(stream.state, State::FramePayload);
871
872 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
874
875 assert_eq!(stream.try_consume_frame(), Ok((frame, 0)));
876 assert_eq!(stream.state, State::FrameType);
877 }
878
879 #[test]
880 fn control_bad_multiple_settings() {
882 let mut d = vec![42; 40];
883 let mut b = octets::OctetsMut::with_slice(&mut d);
884
885 let raw_settings = vec![
886 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0),
887 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
888 (SETTINGS_QPACK_BLOCKED_STREAMS, 0),
889 ];
890
891 let frame = frame::Frame::Settings {
892 max_field_section_size: Some(0),
893 qpack_max_table_capacity: Some(0),
894 qpack_blocked_streams: Some(0),
895 connect_protocol_enabled: None,
896 h3_datagram: None,
897 grease: None,
898 additional_settings: None,
899 raw: Some(raw_settings),
900 };
901
902 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
903 frame.to_bytes(&mut b).unwrap();
904 frame.to_bytes(&mut b).unwrap();
905
906 let mut cursor = std::io::Cursor::new(d);
907
908 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
909 .unwrap();
910 assert_eq!(stream.state, State::FrameType);
911
912 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
914
915 let frame_ty = stream.try_consume_varint().unwrap();
916 assert_eq!(frame_ty, frame::SETTINGS_FRAME_TYPE_ID);
917
918 stream.set_frame_type(frame_ty).unwrap();
919 assert_eq!(stream.state, State::FramePayloadLen);
920
921 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
923
924 let frame_payload_len = stream.try_consume_varint().unwrap();
925 assert_eq!(frame_payload_len, 6);
926 stream.set_frame_payload_len(frame_payload_len).unwrap();
927 assert_eq!(stream.state, State::FramePayload);
928
929 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
931
932 assert_eq!(stream.try_consume_frame(), Ok((frame, 6)));
933 assert_eq!(stream.state, State::FrameType);
934
935 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
937
938 let frame_ty = stream.try_consume_varint().unwrap();
939 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::FrameUnexpected));
940 }
941
942 #[test]
943 fn control_bad_late_settings() {
945 let mut d = vec![42; 40];
946 let mut b = octets::OctetsMut::with_slice(&mut d);
947
948 let goaway = frame::Frame::GoAway { id: 0 };
949
950 let raw_settings = vec![
951 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0),
952 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
953 (SETTINGS_QPACK_BLOCKED_STREAMS, 0),
954 ];
955
956 let settings = frame::Frame::Settings {
957 max_field_section_size: Some(0),
958 qpack_max_table_capacity: Some(0),
959 qpack_blocked_streams: Some(0),
960 connect_protocol_enabled: None,
961 h3_datagram: None,
962 grease: None,
963 additional_settings: None,
964 raw: Some(raw_settings),
965 };
966
967 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
968 goaway.to_bytes(&mut b).unwrap();
969 settings.to_bytes(&mut b).unwrap();
970
971 let mut cursor = std::io::Cursor::new(d);
972
973 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
974 .unwrap();
975 assert_eq!(stream.state, State::FrameType);
976
977 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
979
980 let frame_ty = stream.try_consume_varint().unwrap();
981 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::MissingSettings));
982 }
983
984 #[test]
985 fn control_bad_frame() {
987 let mut d = vec![42; 40];
988 let mut b = octets::OctetsMut::with_slice(&mut d);
989
990 let header_block = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
991 let hdrs = frame::Frame::Headers { header_block };
992
993 let raw_settings = vec![
994 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0),
995 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
996 (SETTINGS_QPACK_BLOCKED_STREAMS, 0),
997 (33, 33),
998 ];
999
1000 let settings = frame::Frame::Settings {
1001 max_field_section_size: Some(0),
1002 qpack_max_table_capacity: Some(0),
1003 qpack_blocked_streams: Some(0),
1004 connect_protocol_enabled: None,
1005 h3_datagram: None,
1006 grease: None,
1007 additional_settings: None,
1008 raw: Some(raw_settings),
1009 };
1010
1011 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
1012 settings.to_bytes(&mut b).unwrap();
1013 hdrs.to_bytes(&mut b).unwrap();
1014
1015 let mut cursor = std::io::Cursor::new(d);
1016
1017 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
1018 .unwrap();
1019 assert_eq!(stream.state, State::FrameType);
1020
1021 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1023
1024 let frame_ty = stream.try_consume_varint().unwrap();
1025 stream.set_frame_type(frame_ty).unwrap();
1026
1027 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1028
1029 let frame_payload_len = stream.try_consume_varint().unwrap();
1030 stream.set_frame_payload_len(frame_payload_len).unwrap();
1031
1032 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1033
1034 assert!(stream.try_consume_frame().is_ok());
1035
1036 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1038
1039 let frame_ty = stream.try_consume_varint().unwrap();
1040 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::FrameUnexpected));
1041 }
1042
1043 #[test]
1044 fn request_no_data() {
1045 let mut stream = Stream::new(0, false);
1046
1047 assert_eq!(stream.ty, Some(Type::Request));
1048 assert_eq!(stream.state, State::FrameType);
1049
1050 assert_eq!(stream.try_consume_varint(), Err(Error::Done));
1051 }
1052
1053 #[test]
1054 fn request_good() {
1055 let mut stream = Stream::new(0, false);
1056
1057 let mut d = vec![42; 128];
1058 let mut b = octets::OctetsMut::with_slice(&mut d);
1059
1060 let header_block = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1061 let payload = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1062 let hdrs = frame::Frame::Headers { header_block };
1063 let data = frame::Frame::Data {
1064 payload: payload.clone(),
1065 };
1066
1067 hdrs.to_bytes(&mut b).unwrap();
1068 data.to_bytes(&mut b).unwrap();
1069
1070 let mut cursor = std::io::Cursor::new(d);
1071
1072 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1074
1075 let frame_ty = stream.try_consume_varint().unwrap();
1076 assert_eq!(frame_ty, frame::HEADERS_FRAME_TYPE_ID);
1077
1078 stream.set_frame_type(frame_ty).unwrap();
1079 assert_eq!(stream.state, State::FramePayloadLen);
1080
1081 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1083
1084 let frame_payload_len = stream.try_consume_varint().unwrap();
1085 assert_eq!(frame_payload_len, 12);
1086
1087 stream.set_frame_payload_len(frame_payload_len).unwrap();
1088 assert_eq!(stream.state, State::FramePayload);
1089
1090 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1092
1093 assert_eq!(stream.try_consume_frame(), Ok((hdrs, 12)));
1094 assert_eq!(stream.state, State::FrameType);
1095
1096 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1098
1099 let frame_ty = stream.try_consume_varint().unwrap();
1100 assert_eq!(frame_ty, frame::DATA_FRAME_TYPE_ID);
1101
1102 stream.set_frame_type(frame_ty).unwrap();
1103 assert_eq!(stream.state, State::FramePayloadLen);
1104
1105 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1107
1108 let frame_payload_len = stream.try_consume_varint().unwrap();
1109 assert_eq!(frame_payload_len, 12);
1110
1111 stream.set_frame_payload_len(frame_payload_len).unwrap();
1112 assert_eq!(stream.state, State::Data);
1113
1114 let mut recv_buf = vec![0; payload.len()];
1116 assert_eq!(
1117 stream.try_consume_data_for_tests(&mut cursor, &mut recv_buf),
1118 Ok(payload.len())
1119 );
1120 assert_eq!(payload, recv_buf);
1121
1122 assert_eq!(stream.state, State::FrameType);
1123 }
1124
1125 #[test]
1126 fn push_good() {
1127 let mut d = vec![42; 128];
1128 let mut b = octets::OctetsMut::with_slice(&mut d);
1129
1130 let header_block = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1131 let payload = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1132 let hdrs = frame::Frame::Headers { header_block };
1133 let data = frame::Frame::Data {
1134 payload: payload.clone(),
1135 };
1136
1137 let mut stream = open_uni(&mut b, HTTP3_PUSH_STREAM_TYPE_ID).unwrap();
1138 b.put_varint(1).unwrap();
1139 hdrs.to_bytes(&mut b).unwrap();
1140 data.to_bytes(&mut b).unwrap();
1141
1142 let mut cursor = std::io::Cursor::new(d);
1143
1144 parse_uni(&mut stream, HTTP3_PUSH_STREAM_TYPE_ID, &mut cursor).unwrap();
1145 assert_eq!(stream.state, State::PushId);
1146
1147 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1149
1150 let push_id = stream.try_consume_varint().unwrap();
1151 assert_eq!(push_id, 1);
1152
1153 stream.set_push_id(push_id).unwrap();
1154 assert_eq!(stream.state, State::FrameType);
1155
1156 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1158
1159 let frame_ty = stream.try_consume_varint().unwrap();
1160 assert_eq!(frame_ty, frame::HEADERS_FRAME_TYPE_ID);
1161
1162 stream.set_frame_type(frame_ty).unwrap();
1163 assert_eq!(stream.state, State::FramePayloadLen);
1164
1165 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1167
1168 let frame_payload_len = stream.try_consume_varint().unwrap();
1169 assert_eq!(frame_payload_len, 12);
1170
1171 stream.set_frame_payload_len(frame_payload_len).unwrap();
1172 assert_eq!(stream.state, State::FramePayload);
1173
1174 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1176
1177 assert_eq!(stream.try_consume_frame(), Ok((hdrs, 12)));
1178 assert_eq!(stream.state, State::FrameType);
1179
1180 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1182
1183 let frame_ty = stream.try_consume_varint().unwrap();
1184 assert_eq!(frame_ty, frame::DATA_FRAME_TYPE_ID);
1185
1186 stream.set_frame_type(frame_ty).unwrap();
1187 assert_eq!(stream.state, State::FramePayloadLen);
1188
1189 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1191
1192 let frame_payload_len = stream.try_consume_varint().unwrap();
1193 assert_eq!(frame_payload_len, 12);
1194
1195 stream.set_frame_payload_len(frame_payload_len).unwrap();
1196 assert_eq!(stream.state, State::Data);
1197
1198 let mut recv_buf = vec![0; payload.len()];
1200 assert_eq!(
1201 stream.try_consume_data_for_tests(&mut cursor, &mut recv_buf),
1202 Ok(payload.len())
1203 );
1204 assert_eq!(payload, recv_buf);
1205
1206 assert_eq!(stream.state, State::FrameType);
1207 }
1208
1209 #[test]
1210 fn grease() {
1211 let mut d = vec![42; 20];
1212 let mut b = octets::OctetsMut::with_slice(&mut d);
1213
1214 let mut stream = open_uni(&mut b, 33).unwrap();
1215
1216 let mut cursor = std::io::Cursor::new(d);
1217
1218 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1220
1221 let stream_ty = stream.try_consume_varint().unwrap();
1222 assert_eq!(stream_ty, 33);
1223 stream
1224 .set_ty(Type::deserialize(stream_ty).unwrap())
1225 .unwrap();
1226 assert_eq!(stream.state, State::Drain);
1227 }
1228
1229 #[test]
1230 fn data_before_headers() {
1231 let mut stream = Stream::new(0, false);
1232
1233 let mut d = vec![42; 128];
1234 let mut b = octets::OctetsMut::with_slice(&mut d);
1235
1236 let data = frame::Frame::Data {
1237 payload: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
1238 };
1239
1240 data.to_bytes(&mut b).unwrap();
1241
1242 let mut cursor = std::io::Cursor::new(d);
1243
1244 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1246
1247 let frame_ty = stream.try_consume_varint().unwrap();
1248 assert_eq!(frame_ty, frame::DATA_FRAME_TYPE_ID);
1249
1250 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::FrameUnexpected));
1251 }
1252
1253 #[test]
1254 fn additional_headers() {
1255 let mut stream = Stream::new(0, false);
1256
1257 let mut d = vec![42; 128];
1258 let mut b = octets::OctetsMut::with_slice(&mut d);
1259
1260 let header_block = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1261 let payload = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1262 let info_hdrs = frame::Frame::Headers {
1263 header_block: header_block.clone(),
1264 };
1265 let non_info_hdrs = frame::Frame::Headers {
1266 header_block: header_block.clone(),
1267 };
1268 let trailers = frame::Frame::Headers { header_block };
1269 let data = frame::Frame::Data {
1270 payload: payload.clone(),
1271 };
1272
1273 info_hdrs.to_bytes(&mut b).unwrap();
1274 non_info_hdrs.to_bytes(&mut b).unwrap();
1275 data.to_bytes(&mut b).unwrap();
1276 trailers.to_bytes(&mut b).unwrap();
1277
1278 let mut cursor = std::io::Cursor::new(d);
1279
1280 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1282
1283 let frame_ty = stream.try_consume_varint().unwrap();
1284 assert_eq!(frame_ty, frame::HEADERS_FRAME_TYPE_ID);
1285
1286 stream.set_frame_type(frame_ty).unwrap();
1287 assert_eq!(stream.state, State::FramePayloadLen);
1288
1289 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1291
1292 let frame_payload_len = stream.try_consume_varint().unwrap();
1293 assert_eq!(frame_payload_len, 12);
1294
1295 stream.set_frame_payload_len(frame_payload_len).unwrap();
1296 assert_eq!(stream.state, State::FramePayload);
1297
1298 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1300
1301 assert_eq!(stream.try_consume_frame(), Ok((info_hdrs, 12)));
1302 assert_eq!(stream.state, State::FrameType);
1303
1304 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1306
1307 let frame_ty = stream.try_consume_varint().unwrap();
1308 assert_eq!(frame_ty, frame::HEADERS_FRAME_TYPE_ID);
1309
1310 stream.set_frame_type(frame_ty).unwrap();
1311 assert_eq!(stream.state, State::FramePayloadLen);
1312
1313 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1315
1316 let frame_payload_len = stream.try_consume_varint().unwrap();
1317 assert_eq!(frame_payload_len, 12);
1318
1319 stream.set_frame_payload_len(frame_payload_len).unwrap();
1320 assert_eq!(stream.state, State::FramePayload);
1321
1322 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1324
1325 assert_eq!(stream.try_consume_frame(), Ok((non_info_hdrs, 12)));
1326 assert_eq!(stream.state, State::FrameType);
1327
1328 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1330
1331 let frame_ty = stream.try_consume_varint().unwrap();
1332 assert_eq!(frame_ty, frame::DATA_FRAME_TYPE_ID);
1333
1334 stream.set_frame_type(frame_ty).unwrap();
1335 assert_eq!(stream.state, State::FramePayloadLen);
1336
1337 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1339
1340 let frame_payload_len = stream.try_consume_varint().unwrap();
1341 assert_eq!(frame_payload_len, 12);
1342
1343 stream.set_frame_payload_len(frame_payload_len).unwrap();
1344 assert_eq!(stream.state, State::Data);
1345
1346 let mut recv_buf = vec![0; payload.len()];
1348 assert_eq!(
1349 stream.try_consume_data_for_tests(&mut cursor, &mut recv_buf),
1350 Ok(payload.len())
1351 );
1352 assert_eq!(payload, recv_buf);
1353
1354 assert_eq!(stream.state, State::FrameType);
1355
1356 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1358
1359 let frame_ty = stream.try_consume_varint().unwrap();
1360 assert_eq!(frame_ty, frame::HEADERS_FRAME_TYPE_ID);
1361
1362 stream.set_frame_type(frame_ty).unwrap();
1363 assert_eq!(stream.state, State::FramePayloadLen);
1364
1365 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1367
1368 let frame_payload_len = stream.try_consume_varint().unwrap();
1369 assert_eq!(frame_payload_len, 12);
1370
1371 stream.set_frame_payload_len(frame_payload_len).unwrap();
1372 assert_eq!(stream.state, State::FramePayload);
1373
1374 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1376
1377 assert_eq!(stream.try_consume_frame(), Ok((trailers, 12)));
1378 assert_eq!(stream.state, State::FrameType);
1379 }
1380
1381 #[test]
1382 fn zero_length_goaway() {
1383 let mut d = vec![42; 128];
1384 let mut b = octets::OctetsMut::with_slice(&mut d);
1385
1386 let frame = Frame::Settings {
1387 max_field_section_size: None,
1388 qpack_max_table_capacity: None,
1389 qpack_blocked_streams: None,
1390 connect_protocol_enabled: None,
1391 h3_datagram: None,
1392 grease: None,
1393 additional_settings: None,
1394 raw: Some(vec![]),
1395 };
1396
1397 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
1398 frame.to_bytes(&mut b).unwrap();
1399
1400 b.put_varint(frame::GOAWAY_FRAME_TYPE_ID).unwrap();
1402 b.put_varint(0).unwrap();
1403
1404 let mut cursor = std::io::Cursor::new(d);
1405
1406 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
1407 .unwrap();
1408
1409 parse_skip_frame(&mut stream, &mut cursor).unwrap();
1411
1412 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1414 let frame_ty = stream.try_consume_varint().unwrap();
1415 assert_eq!(frame_ty, frame::GOAWAY_FRAME_TYPE_ID);
1416
1417 stream.set_frame_type(frame_ty).unwrap();
1418 assert_eq!(stream.state, State::FramePayloadLen);
1419
1420 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1422 let frame_payload_len = stream.try_consume_varint().unwrap();
1423 assert_eq!(
1424 Err(Error::FrameError),
1425 stream.set_frame_payload_len(frame_payload_len)
1426 );
1427 }
1428
1429 #[test]
1430 fn zero_length_push_promise() {
1431 let mut d = vec![42; 128];
1432 let mut b = octets::OctetsMut::with_slice(&mut d);
1433
1434 let mut stream = Stream::new(0, false);
1435
1436 assert_eq!(stream.ty, Some(Type::Request));
1437 assert_eq!(stream.state, State::FrameType);
1438
1439 b.put_varint(frame::PUSH_PROMISE_FRAME_TYPE_ID).unwrap();
1441 b.put_varint(0).unwrap();
1442
1443 let mut cursor = std::io::Cursor::new(d);
1444
1445 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1447 let frame_ty = stream.try_consume_varint().unwrap();
1448 assert_eq!(frame_ty, frame::PUSH_PROMISE_FRAME_TYPE_ID);
1449
1450 stream.set_frame_type(frame_ty).unwrap();
1451 assert_eq!(stream.state, State::FramePayloadLen);
1452
1453 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1455 let frame_payload_len = stream.try_consume_varint().unwrap();
1456 assert_eq!(
1457 Err(Error::FrameError),
1458 stream.set_frame_payload_len(frame_payload_len)
1459 );
1460 }
1461
1462 #[test]
1463 fn zero_length_cancel_push() {
1464 let mut d = vec![42; 128];
1465 let mut b = octets::OctetsMut::with_slice(&mut d);
1466
1467 let frame = Frame::Settings {
1468 max_field_section_size: None,
1469 qpack_max_table_capacity: None,
1470 qpack_blocked_streams: None,
1471 connect_protocol_enabled: None,
1472 h3_datagram: None,
1473 grease: None,
1474 additional_settings: None,
1475 raw: Some(vec![]),
1476 };
1477
1478 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
1479 frame.to_bytes(&mut b).unwrap();
1480
1481 b.put_varint(frame::CANCEL_PUSH_FRAME_TYPE_ID).unwrap();
1483 b.put_varint(0).unwrap();
1484
1485 let mut cursor = std::io::Cursor::new(d);
1486
1487 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
1488 .unwrap();
1489
1490 parse_skip_frame(&mut stream, &mut cursor).unwrap();
1492
1493 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1495 let frame_ty = stream.try_consume_varint().unwrap();
1496 assert_eq!(frame_ty, frame::CANCEL_PUSH_FRAME_TYPE_ID);
1497
1498 stream.set_frame_type(frame_ty).unwrap();
1499 assert_eq!(stream.state, State::FramePayloadLen);
1500
1501 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1503 let frame_payload_len = stream.try_consume_varint().unwrap();
1504 assert_eq!(
1505 Err(Error::FrameError),
1506 stream.set_frame_payload_len(frame_payload_len)
1507 );
1508 }
1509
1510 #[test]
1511 fn zero_length_max_push_id() {
1512 let mut d = vec![42; 128];
1513 let mut b = octets::OctetsMut::with_slice(&mut d);
1514
1515 let frame = Frame::Settings {
1516 max_field_section_size: None,
1517 qpack_max_table_capacity: None,
1518 qpack_blocked_streams: None,
1519 connect_protocol_enabled: None,
1520 h3_datagram: None,
1521 grease: None,
1522 additional_settings: None,
1523 raw: Some(vec![]),
1524 };
1525
1526 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap();
1527 frame.to_bytes(&mut b).unwrap();
1528
1529 b.put_varint(frame::MAX_PUSH_FRAME_TYPE_ID).unwrap();
1531 b.put_varint(0).unwrap();
1532
1533 let mut cursor = std::io::Cursor::new(d);
1534
1535 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor)
1536 .unwrap();
1537
1538 parse_skip_frame(&mut stream, &mut cursor).unwrap();
1540
1541 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1543 let frame_ty = stream.try_consume_varint().unwrap();
1544 assert_eq!(frame_ty, frame::MAX_PUSH_FRAME_TYPE_ID);
1545
1546 stream.set_frame_type(frame_ty).unwrap();
1547 assert_eq!(stream.state, State::FramePayloadLen);
1548
1549 stream.try_fill_buffer_for_tests(&mut cursor).unwrap();
1551 let frame_payload_len = stream.try_consume_varint().unwrap();
1552 assert_eq!(
1553 Err(Error::FrameError),
1554 stream.set_frame_payload_len(frame_payload_len)
1555 );
1556 }
1557}