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
//! Defines the interface for the session-level management of HTTP/2
//! communication. This is effectively an API that allows hooking into an
//! HTTP/2 connection in order to handle events arising on the connection.
//!
//! The module also provides a default implementation for some of the traits.
use std::marker::PhantomData;
use std::collections::HashMap;
use std::error::Error;
use std::io::Read;
use std::io::Cursor;
use std::iter::FromIterator;
use http::{StreamId, OwnedHeader, Header, HttpResult, ErrorCode, HttpError, ConnectionError};
use http::frame::{HttpSetting, PingFrame};
use http::connection::HttpConnection;

/// A trait that defines the interface between an `HttpConnection` and the higher-levels that use
/// it. Essentially, it allows the `HttpConnection` to pass information onto those higher levels
/// through a well-defined interface.
///
/// These methods are effectively a set of callbacks that the `HttpConnection` invokes when the
/// corresponding events arise on the HTTP/2 connection (i.e. frame stream).
pub trait Session {
    /// Notifies the `Session` that a new data chunk has arrived on the
    /// connection for a particular stream. Only the raw data is passed
    /// to the callback (all padding is already discarded by the connection).
    fn new_data_chunk(&mut self,
                      stream_id: StreamId,
                      data: &[u8],
                      conn: &mut HttpConnection)
                      -> HttpResult<()>;
    /// Notifies the `Session` that headers have arrived for a particular
    /// stream. The given list of headers is already decoded by the connection.
    /// TODO: The Session should be notified separately for every header that is decoded.
    fn new_headers<'n, 'v>(&mut self,
                           stream_id: StreamId,
                           headers: Vec<Header<'n, 'v>>,
                           conn: &mut HttpConnection)
                           -> HttpResult<()>;
    /// Notifies the `Session` that a particular stream got closed by the peer.
    fn end_of_stream(&mut self, stream_id: StreamId, conn: &mut HttpConnection) -> HttpResult<()>;
    /// Notifies the `Session` that a particular stream was reset by the peer and provides the
    /// reason behind it.
    fn rst_stream(&mut self,
                  stream_id: StreamId,
                  error_code: ErrorCode,
                  conn: &mut HttpConnection)
                  -> HttpResult<()>;
    /// Notifies the `Session` that the peer has sent a new set of settings. The session itself is
    /// responsible for acknowledging the receipt of the settings.
    fn new_settings(&mut self,
                    settings: Vec<HttpSetting>,
                    conn: &mut HttpConnection)
                    -> HttpResult<()>;

    /// Notifies the `Session` that a PING request has been received. The session itself is
    /// responsible for replying with an ACK.
    fn on_ping(&mut self, ping: &PingFrame, conn: &mut HttpConnection) -> HttpResult<()>;

    /// Notifies the `Session` that a PING acknowledgement has been received.
    fn on_pong(&mut self, ping: &PingFrame, conn: &mut HttpConnection) -> HttpResult<()>;

    /// Notifies the `Session` that the peer has sent a GOAWAY frame, indicating that the
    /// connection is terminated.
    ///
    /// The default implementation simply maps the error into an appropriate
    /// HttpError::PeerConnectionError struct.
    ///
    /// Concrete `Session` implementations can override this in order to, for example, figure out
    /// which streams can be safely retried (based on the last processed stream id).
    fn on_goaway(&mut self,
                 _last_stream_id: StreamId,
                 error_code: ErrorCode,
                 debug_data: Option<&[u8]>,
                 _conn: &mut HttpConnection)
                 -> HttpResult<()> {
        Err(HttpError::PeerConnectionError(ConnectionError {
            error_code: error_code,
            debug_data: debug_data.map(|data| data.to_vec()),
        }))
    }
}

/// A newtype for an iterator over `Stream`s saved in a `SessionState`.
///
/// Allows `SessionState` implementations to return iterators over its session without being forced
/// to declare them as associated types.
pub struct StreamIter<'a, S: Stream + 'a>(Box<Iterator<Item = (&'a StreamId, &'a mut S)> + 'a>);

impl<'a, S> Iterator for StreamIter<'a, S>
    where S: Stream + 'a
{
    type Item = (&'a StreamId, &'a mut S);

    #[inline]
    fn next(&mut self) -> Option<(&'a StreamId, &'a mut S)> {
        self.0.next()
    }
}

/// A trait defining a set of methods for accessing and influencing an HTTP/2 session's state.
///
/// This trait is tightly coupled to a `Stream`-based session layer implementation. Particular
/// implementations are additionally tightly coupled to one particular `Stream` implementation.
///
/// # Note
///
/// Clients built on top of the raw `HttpConnection` + `Session` can still exist without using
/// this trait; however, it is included for convenience, as most session implementations *will*
/// want to keep track of similar things in the session's state.
pub trait SessionState {
    /// The type of the `Stream` that the `SessionState` manages.
    type Stream: Stream;

    /// Inserts the given `Stream` into the session's state, starting to track it.
    /// The `SessionState` should assign it the next available outgoing stream ID.
    fn insert_outgoing(&mut self, stream: Self::Stream) -> StreamId;
    /// Inserts the given `Stream` into the session's state, considering it an incoming
    /// stream.
    /// TODO(mlalic): Allow the exact error to propagate out.
    fn insert_incoming(&mut self, id: StreamId, stream: Self::Stream) -> Result<(), ()>;
    /// Returns a reference to a `Stream` with the given `StreamId`, if it is found in the current
    /// session.
    fn get_stream_ref(&self, stream_id: StreamId) -> Option<&Self::Stream>;
    /// Returns a mutable reference to a `Stream` with the given `StreamId`, if it is found in the
    /// current session.
    fn get_stream_mut(&mut self, stream_id: StreamId) -> Option<&mut Self::Stream>;
    /// Removes the stream with the given `StreamId` from the session. If the stream was found in
    /// the session, it is returned in the result.
    fn remove_stream(&mut self, stream_id: StreamId) -> Option<Self::Stream>;

    /// Returns an iterator over the streams currently found in the session.
    fn iter(&mut self) -> StreamIter<Self::Stream>;

    /// The number of streams tracked by this state object
    fn len(&self) -> usize;

    /// Returns all streams that are closed and tracked by the session state.
    ///
    /// The streams are moved out of the session state.
    ///
    /// The default implementations relies on the `iter` implementation to find the closed streams
    /// first and then calls `remove_stream` on all of them.
    fn get_closed(&mut self) -> Vec<Self::Stream> {
        let ids: Vec<StreamId> = self.iter()
                                     .filter_map(|(id, s)| {
                                         if s.is_closed() {
                                             Some(*id)
                                         } else {
                                             None
                                         }
                                     })
                                     .collect();
        FromIterator::from_iter(ids.into_iter().map(|i| self.remove_stream(i).unwrap()))
    }
}

/// A phantom type for the `DefaultSessionState` struct that indicates that the struct should be
/// geared for a client session.
pub struct Client;
/// A phantom type for the `DefaultSessionState` struct that indicates that the struct should be
/// geared for a server session.
pub struct Server;

/// The simple enum indicates the parity of an integer. Used by the `DefaultSessionState`
/// implementation to make sure that server and client sessions only accept stream IDs with the
/// correct parity (for clients outgoing stream IDs must be odd and incoming even, while for
/// servers it is the other way around).
#[derive(Debug, Clone, Copy, PartialEq)]
enum Parity {
    Even,
    Odd,
}

impl Parity {
    /// Returns the parity of the given `StreamId`.
    fn of(stream_id: StreamId) -> Parity {
        match stream_id % 2 {
            0 => Parity::Even,
            1 => Parity::Odd,
            _ => unreachable!(),
        }
    }
}

/// An implementation of the `SessionState` trait that tracks the active streams in a `HashMap`,
/// mapping the stream ID to the concrete `Stream` instance.
pub struct DefaultSessionState<T, S>
    where S: Stream
{
    /// All streams that the session state is currently aware of.
    streams: HashMap<StreamId, S>,
    /// The next available ID for outgoing streams.
    next_stream_id: StreamId,
    /// The parity bit for outgoing connections. Client-initiated connections must always be
    /// odd-numbered, while server-initiated ones should be even. Therefore, the parity bit
    /// is `Odd` for clients' session state and `Even` for servers'.
    /// TODO It'd be better to use an associated constant to type T instead, but
    ///      `associated_consts` is feature gated for now.
    outgoing_parity: Parity,
    /// Indicates whether the session state is maintained for a client or a server session.
    _server_or_client: PhantomData<T>,
}

impl<T, S> DefaultSessionState<T, S>
    where S: Stream
{
    /// A helper function that returns `true` iff the given `StreamId` is a valid ID for an
    /// incoming stream, depending on whether the session is that of a client or a server.
    fn validate_incoming_parity(&self, stream_id: StreamId) -> bool {
        // The parity of incoming connections should be different than the parity of outgoing ones.
        Parity::of(stream_id) != self.outgoing_parity
    }
}

impl<S> DefaultSessionState<Client, S>
    where S: Stream
{
    /// Creates a new `DefaultSessionState` for a client session with no known streams.
    pub fn new() -> DefaultSessionState<Client, S> {
        DefaultSessionState {
            streams: HashMap::new(),
            next_stream_id: 1,
            outgoing_parity: Parity::Odd,
            _server_or_client: PhantomData,
        }
    }
}

impl<S> DefaultSessionState<Server, S>
    where S: Stream
{
    /// Creates a new `DefaultSessionState` for a server session with no known streams.
    pub fn new() -> DefaultSessionState<Server, S> {
        DefaultSessionState {
            streams: HashMap::new(),
            next_stream_id: 2,
            outgoing_parity: Parity::Even,
            _server_or_client: PhantomData,
        }
    }
}

/// Create a new `DefaultSessionState` for a client session.
/// This function is a workaround required due to
/// [rust-lang/rust#29023](https://github.com/rust-lang/rust/issues/29023).
pub fn default_client_state<S: Stream>() -> DefaultSessionState<Client, S> {
    DefaultSessionState::<Client, S>::new()
}
/// Create a new `DefaultSessionState` for a server session.
/// This function is a workaround required due to
/// [rust-lang/rust#29023](https://github.com/rust-lang/rust/issues/29023).
pub fn default_server_state<S: Stream>() -> DefaultSessionState<Server, S> {
    DefaultSessionState::<Server, S>::new()
}

impl<T, S> SessionState for DefaultSessionState<T, S>
    where S: Stream
{
    type Stream = S;

    fn insert_outgoing(&mut self, stream: Self::Stream) -> StreamId {
        let id = self.next_stream_id;
        self.streams.insert(id, stream);
        self.next_stream_id += 2;
        id
    }

    fn insert_incoming(&mut self, stream_id: StreamId, stream: Self::Stream) -> Result<(), ()> {
        if self.validate_incoming_parity(stream_id) {
            // TODO(mlalic): Assert that the stream IDs are monotonically increasing!
            self.streams.insert(stream_id, stream);
            Ok(())
        } else {
            Err(())
        }
    }

    #[inline]
    fn get_stream_ref(&self, stream_id: StreamId) -> Option<&Self::Stream> {
        self.streams.get(&stream_id)
    }
    #[inline]
    fn get_stream_mut(&mut self, stream_id: StreamId) -> Option<&mut Self::Stream> {
        self.streams.get_mut(&stream_id)
    }

    #[inline]
    fn remove_stream(&mut self, stream_id: StreamId) -> Option<Self::Stream> {
        self.streams.remove(&stream_id)
    }

    #[inline]
    fn iter(&mut self) -> StreamIter<S> {
        StreamIter(Box::new(self.streams.iter_mut()))
    }

    /// Number of currently active streams
    #[inline]
    fn len(&self) -> usize {
        self.streams.len()
    }
}

/// The enum represents all the states that an HTTP/2 stream can be found in.
///
/// Corresponds to [section 5.1.](http://http2.github.io/http2-spec/#rfc.section.5.1) of the spec.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum StreamState {
    Idle,
    ReservedLocal,
    ReservedRemote,
    Open,
    HalfClosedRemote,
    HalfClosedLocal,
    Closed,
}

/// The enum represents errors that can be returned from the `Stream::get_data_chunk` method.
#[derive(Debug)]
pub enum StreamDataError {
    /// Indicates that the stream cannot provide any data, since it is closed for further writes
    /// from the peer's side.
    Closed,
    /// A different error while trying to obtain the data chunk. Wraps a boxed `Error` impl.
    Other(Box<Error + Send + Sync>),
}

impl<E> From<E> for StreamDataError
    where E: Error + Send + Sync + 'static
{
    fn from(err: E) -> StreamDataError {
        StreamDataError::Other(Box::new(err))
    }
}

/// The enum represents the successful completion of the `Stream::get_data_chunk` method.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum StreamDataChunk {
    /// A data chunk of the given size, after which more chunks can follow.
    Chunk(usize),
    /// The chunk was the last one that the stream will ever write.
    Last(usize),
    /// No data currently available, but the stream isn't closed yet
    Unavailable,
}

/// A trait representing a single HTTP/2 stream. An HTTP/2 connection multiplexes a number of
/// streams.
///
/// The trait defines which operations need to be implemented by a type that should
/// be usable as an HTTP/2 stream. By implementing this trait, clients can implement only
/// stream-level logic, such as how the received data should be handled, or which data should be
/// sent to the peer, without having to worry about the lower-level details of session and
/// connection management (e.g. handling raw frames or tracking stream status).
pub trait Stream {
    /// Handle a new data chunk that has arrived for the stream.
    fn new_data_chunk(&mut self, data: &[u8]);
    /// Set headers for a stream. A stream is only allowed to have one set of
    /// headers.
    fn set_headers<'n, 'v>(&mut self, headers: Vec<Header<'n, 'v>>);
    /// Sets the stream state to the newly provided state.
    fn set_state(&mut self, state: StreamState);

    /// Invoked when the session detects that the peer has reset the stream (i.e. sent a RST_STREAM
    /// frame for this stream).
    ///
    /// The default implementation simply closes the stream, discarding the provided error_code.
    /// Concrete `Stream` implementations can override this.
    fn on_rst_stream(&mut self, _error_code: ErrorCode) {
        self.close();
    }

    /// Places the next data chunk that should be written onto the stream into the given buffer.
    ///
    /// # Returns
    ///
    /// The returned variant of the `StreamDataChunk` enum can indicate that the returned chunk is
    /// the last one that the stream can write (the `StreamDataChunk::Last` variant).
    ///
    /// It can also indicate that the stream currently does not have any data that could be
    /// written, but it isn't closed yet, implying that at a later time some data might become
    /// available for writing (the `StreamDataChunk::Unavailable` variant).
    ///
    /// The `StreamDataChunk::Chunk` indicates that the chunk of the given length has been placed
    /// into the buffer and that more data might follow on the stream.
    fn get_data_chunk(&mut self, buf: &mut [u8]) -> Result<StreamDataChunk, StreamDataError>;

    /// Returns the current state of the stream.
    fn state(&self) -> StreamState;

    /// Transitions the stream state to closed. After this, the stream is considered to be closed
    /// for any further reads or writes.
    fn close(&mut self) {
        self.set_state(StreamState::Closed);
    }
    /// Updates the `Stream` status to indicate that it is closed locally.
    ///
    /// If the stream is closed on the remote end, then it is fully closed after this call.
    fn close_local(&mut self) {
        let next = match self.state() {
            StreamState::HalfClosedRemote => StreamState::Closed,
            _ => StreamState::HalfClosedLocal,
        };
        self.set_state(next);
    }
    /// Updates the `Stream` status to indicate that it is closed on the remote peer's side.
    ///
    /// If the stream is also locally closed, then it is fully closed after this call.
    fn close_remote(&mut self) {
        let next = match self.state() {
            StreamState::HalfClosedLocal => StreamState::Closed,
            _ => StreamState::HalfClosedRemote,
        };
        self.set_state(next);
    }
    /// Returns whether the stream is closed.
    ///
    /// A stream is considered to be closed iff its state is set to `Closed`.
    fn is_closed(&self) -> bool {
        self.state() == StreamState::Closed
    }
    /// Returns whether the stream is closed locally.
    fn is_closed_local(&self) -> bool {
        match self.state() {
            StreamState::HalfClosedLocal | StreamState::Closed => true,
            _ => false,
        }
    }
    /// Returns whether the remote peer has closed the stream. This includes a fully closed stream.
    fn is_closed_remote(&self) -> bool {
        match self.state() {
            StreamState::HalfClosedRemote | StreamState::Closed => true,
            _ => false,
        }
    }
}

/// An implementation of the `Stream` trait that saves all headers and data
/// in memory.
///
/// Stores its outgoing data as a `Vec<u8>`.
#[derive(Clone)]
pub struct DefaultStream {
    /// The ID of the stream, if already assigned by the connection.
    pub stream_id: Option<StreamId>,
    /// The headers associated with the stream (i.e. the response headers)
    pub headers: Option<Vec<Header<'static, 'static>>>,
    /// The body of the stream (i.e. the response body)
    pub body: Vec<u8>,
    /// The current stream state.
    pub state: StreamState,
    /// The outgoing data associated to the stream. The `Cursor` points into the `Vec` at the
    /// position where the data has been sent out.
    data: Option<Cursor<Vec<u8>>>,
}

impl DefaultStream {
    /// Create a new `DefaultStream`, where the ID is not yet assigned.
    pub fn new() -> DefaultStream {
        DefaultStream {
            stream_id: None,
            headers: None,
            body: Vec::new(),
            state: StreamState::Open,
            data: None,
        }
    }

    /// Create a new `DefaultStream` with the given ID.
    pub fn with_id(stream_id: StreamId) -> DefaultStream {
        DefaultStream {
            stream_id: Some(stream_id),
            headers: None,
            body: Vec::new(),
            state: StreamState::Open,
            data: None,
        }
    }

    /// Sets the outgoing data of the stream to the given `Vec`.
    ///
    /// Any previously associated (and perhaps unwritten) data is discarded.
    #[inline]
    pub fn set_full_data(&mut self, data: Vec<u8>) {
        self.data = Some(Cursor::new(data));
    }
}

impl Stream for DefaultStream {
    fn new_data_chunk(&mut self, data: &[u8]) {
        self.body.extend(data.to_vec().into_iter());
    }

    fn set_headers<'n, 'v>(&mut self, headers: Vec<Header<'n, 'v>>) {
        let new_headers = headers.into_iter().map(|h| {
            let owned: OwnedHeader = h.into();
            owned.into()
        });

        self.headers = match self.headers.take() {
            Some(mut x) => {
                x.extend(new_headers);
                Some(x)
            }
            None => Some(new_headers.collect()),
        };
    }

    fn set_state(&mut self, state: StreamState) {
        self.state = state;
    }

    fn state(&self) -> StreamState {
        self.state
    }

    fn get_data_chunk(&mut self, buf: &mut [u8]) -> Result<StreamDataChunk, StreamDataError> {
        if self.is_closed_local() {
            return Err(StreamDataError::Closed);
        }
        let chunk = match self.data.as_mut() {
            // No data associated to the stream, but it's open => nothing available for writing
            None => StreamDataChunk::Unavailable,
            Some(d) => {
                let read = try!(d.read(buf));
                if (d.position() as usize) == d.get_ref().len() {
                    StreamDataChunk::Last(read)
                } else {
                    StreamDataChunk::Chunk(read)
                }
            }
        };
        // Transition the stream state to locally closed if we've extracted the final data chunk.
        if let StreamDataChunk::Last(_) = chunk {
            self.close_local()
        }

        Ok(chunk)
    }
}

#[cfg(test)]
mod tests {
    use super::{Stream, DefaultSessionState, DefaultStream, StreamDataChunk, StreamDataError,
                SessionState, Parity};
    use super::Client as ClientMarker;
    use super::Server as ServerMarker;
    use http::{ErrorCode, Header};
    use http::tests::common::TestStream;

    /// Checks that the `Parity` struct indeed works as advertised.
    #[test]
    fn test_parity_sanity_check() {
        assert_eq!(Parity::of(1), Parity::Odd);
        assert_eq!(Parity::of(2), Parity::Even);
        assert_eq!(Parity::of(301), Parity::Odd);
        assert_eq!(Parity::of(418), Parity::Even);
    }

    /// Tests that the `DefaultSessionState` when instantiated in client-mode correctly assigns
    /// stream IDs.
    #[test]
    fn test_default_session_state_client() {
        let mut state = DefaultSessionState::<ClientMarker, TestStream>::new();
        // Outgoing streams are odd-numbered...
        assert_eq!(state.insert_outgoing(TestStream::new()), 1);
        assert_eq!(state.insert_outgoing(TestStream::new()), 3);
        // ...while incoming are only allowed to be even-numbered.
        assert!(state.insert_incoming(2, TestStream::new()).is_ok());
        assert!(state.insert_incoming(3, TestStream::new()).is_err());
    }

    /// Tests that the `DefaultSessionState` when instantiated in server-mode correctly assigns
    /// stream IDs.
    #[test]
    fn test_default_session_state_server() {
        let mut state = DefaultSessionState::<ServerMarker, TestStream>::new();
        // Outgoing streams are even-numbered...
        assert_eq!(state.insert_outgoing(TestStream::new()), 2);
        assert_eq!(state.insert_outgoing(TestStream::new()), 4);
        // ...while incoming are only allowed to be odd-numbered.
        assert!(state.insert_incoming(2, TestStream::new()).is_err());
        assert!(state.insert_incoming(3, TestStream::new()).is_ok());
    }

    /// Tests for the `DefaultSessionState` implementation of the `SessionState` trait.
    #[test]
    fn test_default_session_state() {
        fn new_mock_state() -> DefaultSessionState<ClientMarker, TestStream> {
            DefaultSessionState::<ClientMarker, _>::new()
        }

        {
            // Test insert
            let mut state = new_mock_state();
            let assigned_id = state.insert_outgoing(TestStream::new());
            assert_eq!(assigned_id, 1);
        }
        {
            // Test remove: known stream ID
            let mut state = new_mock_state();
            let id = state.insert_outgoing(TestStream::new());

            let _ = state.remove_stream(id).unwrap();
        }
        {
            // Test remove: unknown stream ID
            let mut state = new_mock_state();
            state.insert_outgoing(TestStream::new());

            assert!(state.remove_stream(101).is_none());
        }
        {
            // Test get stream -- unknown ID
            let mut state = new_mock_state();
            state.insert_outgoing(TestStream::new());
            assert!(state.get_stream_ref(3).is_none());
        }
        {
            // Test iterate
            let mut state = new_mock_state();
            state.insert_outgoing(TestStream::new());
            state.insert_outgoing(TestStream::new());
            state.insert_outgoing(TestStream::new());

            let mut stream_ids: Vec<_> = state.iter().map(|(&id, _)| id).collect();
            stream_ids.sort();

            assert_eq!(vec![1, 3, 5], stream_ids);
        }
        {
            // Test iterate on an empty state
            let mut state = new_mock_state();

            assert_eq!(state.iter().collect::<Vec<_>>().len(), 0);
        }
        {
            // Test `get_closed`
            let mut state = new_mock_state();
            state.insert_outgoing(TestStream::new());
            state.insert_outgoing(TestStream::new());
            state.insert_outgoing(TestStream::new());
            // Close some streams now
            state.get_stream_mut(1).unwrap().close();
            state.get_stream_mut(5).unwrap().close();

            let closed = state.get_closed();

            // Only one stream left
            assert_eq!(state.streams.len(), 1);
            // Both of the closed streams extracted into the `closed` Vec.
            assert_eq!(closed.len(), 2);
        }
    }

    /// Tests that the `DefaultStream` provides the correct data when its `get_data_chunk` method
    /// is called.
    #[test]
    fn test_default_stream_get_data() {
        // The buffer that will be used in upcoming tests
        let mut buf = Vec::with_capacity(2);
        unsafe {
            buf.set_len(2);
        }

        {
            // A newly open stream has no available data.
            let mut stream = DefaultStream::new();
            let res = stream.get_data_chunk(&mut buf).ok().unwrap();
            assert_eq!(res, StreamDataChunk::Unavailable);
        }
        {
            // A closed stream returns an error
            let mut stream = DefaultStream::new();
            stream.close();
            let res = stream.get_data_chunk(&mut buf).err().unwrap();
            assert!(match res {
                StreamDataError::Closed => true,
                _ => false,
            });
        }
        {
            // A locally closed stream returns an error
            let mut stream = DefaultStream::new();
            stream.close_local();
            let res = stream.get_data_chunk(&mut buf).err().unwrap();
            assert!(match res {
                StreamDataError::Closed => true,
                _ => false,
            });
        }
        {
            let mut stream = DefaultStream::new();
            stream.set_full_data(vec![1, 2, 3, 4]);

            // A stream with data returns the first full chunk
            let res = stream.get_data_chunk(&mut buf).ok().unwrap();
            assert_eq!(res, StreamDataChunk::Chunk(2));
            assert_eq!(buf, vec![1, 2]);

            // Now it returns the last chunk with the correct indicator
            let res = stream.get_data_chunk(&mut buf).ok().unwrap();
            assert_eq!(res, StreamDataChunk::Last(2));
            assert_eq!(buf, vec![3, 4]);

            // Further calls indicate that the stream is now closed locally
            let res = stream.get_data_chunk(&mut buf).err().unwrap();
            assert!(match res {
                StreamDataError::Closed => true,
                _ => false,
            });
        }
        {
            let mut stream = DefaultStream::new();
            stream.set_full_data(vec![1, 2, 3, 4, 5]);

            let res = stream.get_data_chunk(&mut buf).ok().unwrap();
            assert_eq!(res, StreamDataChunk::Chunk(2));
            assert_eq!(buf, vec![1, 2]);

            let res = stream.get_data_chunk(&mut buf).ok().unwrap();
            assert_eq!(res, StreamDataChunk::Chunk(2));
            assert_eq!(buf, vec![3, 4]);

            let res = stream.get_data_chunk(&mut buf).ok().unwrap();
            assert_eq!(res, StreamDataChunk::Last(1));
            assert_eq!(&buf[..1], &vec![5][..]);
        }
        {
            // Empty data
            let mut stream = DefaultStream::new();
            stream.set_full_data(vec![]);

            let res = stream.get_data_chunk(&mut buf).ok().unwrap();
            assert_eq!(res, StreamDataChunk::Last(0));
        }
    }

    #[test]
    fn test_default_stream_get_data_after_rst() {
        let mut buf = vec![0; 2];
        let mut stream = DefaultStream::new();
        stream.set_full_data(vec![1, 2, 3, 4, 5]);

        let res = stream.get_data_chunk(&mut buf).ok().unwrap();
        assert_eq!(res, StreamDataChunk::Chunk(2));
        assert_eq!(buf, vec![1, 2]);

        // Now signal the stream that it's been reset.
        stream.on_rst_stream(ErrorCode::Cancel);
        // The stream no longer provides data, as there's no point in sending any once it is fully
        // closed on both ends for whatever reason.
        assert!(match stream.get_data_chunk(&mut buf) {
            Err(StreamDataError::Closed) => true,
            _ => false,
        });
    }

    #[test]
    /// test_second_header_call will ensure that if headers are called twice in one stream (such as
    /// to set trailers) both results will be added to the stream's headers.
    fn test_second_header_call() {
        let mut stream = DefaultStream::new();

        let headers1 = vec![Header::new(b"Foo", b"Bar")];
        let headers2 = vec![Header::new(b"Baz", b"Bop")];

        stream.set_headers(headers1);
        stream.set_headers(headers2);

        assert!(stream.headers.is_some());
        let headers = stream.headers.unwrap();

        assert_eq!(headers.len(), 2);
        // These assert checks are ugly, but they make the borrow checker happy.
        assert_eq!(headers[0].clone().name.into_owned(), b"Foo");
        assert_eq!(headers[1].clone().name.into_owned(), b"Baz");

        assert_eq!(headers[0].clone().value.into_owned(), b"Bar");
        assert_eq!(headers[1].clone().value.into_owned(), b"Bop");
    }
}