      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

    use super::{
        store, Buffer, Codec, Config, Counts, Frame, Prioritize, Prioritized, Store, Stream, StreamId,
        StreamIdOverflow, WindowSize,
    };
    use crate::codec::{RecvError, UserError};
    use crate::frame::{self, Reason};

    use bytes::Buf;
    use http;
    use std::task::{Context, Poll, Waker};
    use tokio::io::AsyncWrite;

    use std::io;

    /// Manages state transitions related to outbound frames.
    #[derive(Debug)]
    pub(super) struct Send {
        /// Stream identifier to use for next initialized stream.
        next_stream_id: Result<StreamId, StreamIdOverflow>,

        /// Any streams with a higher ID are ignored.
        ///
        /// This starts as MAX, but is lowered when a GOAWAY is received.
        ///
        /// > After sending a GOAWAY frame, the sender can discard frames for
        /// > streams initiated by the receiver with identifiers higher than
        /// > the identified last stream.
        max_stream_id: StreamId,

        /// Initial window size of locally initiated streams
        init_window_sz: WindowSize,

        /// Prioritization layer
        prioritize: Prioritize,

        is_push_enabled: bool,
    }

    /// A value to detect which public API has called `poll_reset`.
    #[derive(Debug)]
    pub(crate) enum PollReset {
        AwaitingHeaders,
        Streaming,
    }

    impl Send {
        /// Create a new `Send`
        pub fn new(config: &Config) -> Self {
            Send {
                init_window_sz: config.remote_init_window_sz,
                max_stream_id: StreamId::MAX,
                next_stream_id: Ok(config.local_next_stream_id),
                prioritize: Prioritize::new(config),
                is_push_enabled: true,
            }
        }

        /// Returns the initial send window size
        pub fn init_window_sz(&self) -> WindowSize {
            self.init_window_sz
        }

        pub fn open(&mut self) -> Result<StreamId, UserError> {
            let stream_id = self.ensure_next_stream_id()?;
            self.next_stream_id = stream_id.next_id();
            Ok(stream_id)
        }

        pub fn reserve_local(&mut self) -> Result<StreamId, UserError> {
            let stream_id = self.ensure_next_stream_id()?;
            self.next_stream_id = stream_id.next_id();
            Ok(stream_id)
        }

        fn check_headers(fields: &http::HeaderMap) -> Result<(), UserError> {
            // 8.1.2.2. Connection-Specific Header Fields
            if fields.contains_key(http::header::CONNECTION)
                || fields.contains_key(http::header::TRANSFER_ENCODING)
                || fields.contains_key(http::header::UPGRADE)
                || fields.contains_key("keep-alive")
                || fields.contains_key("proxy-connection")
            {
                tracing::debug!("illegal connection-specific headers found");
                return Err(UserError::MalformedHeaders);
            } else if let Some(te) = fields.get(http::header::TE) {
                if te != "trailers" {
                    tracing::debug!("illegal connection-specific headers found");
                    return Err(UserError::MalformedHeaders);
                }
            }
            Ok(())
        }

        pub fn send_push_promise<B>(
            &mut self,
            frame: frame::PushPromise,
            buffer: &mut Buffer<Frame<B>>,
            stream: &mut store::Ptr,
            task: &mut Option<Waker>,
        ) -> Result<(), UserError> {
            if !self.is_push_enabled {
                return Err(UserError::PeerDisabledServerPush);
            }

            tracing::trace!(
                "send_push_promise; frame={:?}; init_window={:?}",
                frame,
                self.init_window_sz
            );

            Self::check_headers(frame.fields())?;

            // Queue the frame for sending
            self.prioritize
                .queue_frame(frame.into(), buffer, stream, task);

            Ok(())
        }

        pub fn send_headers<B>(
            &mut self,
            frame: frame::Headers,
            buffer: &mut Buffer<Frame<B>>,
            stream: &mut store::Ptr,
            counts: &mut Counts,
            task: &mut Option<Waker>,
        ) -> Result<(), UserError> {
            tracing::trace!(
                "send_headers; frame={:?}; init_window={:?}",
                frame,
                self.init_window_sz
            );

            Self::check_headers(frame.fields())?;

            if frame.has_too_big_field() {
                return Err(UserError::HeaderTooBig);
            }

            let end_stream = frame.is_end_stream();

            // Update the state
            stream.state.send_open(end_stream)?;

            if counts.peer().is_local_init(frame.stream_id()) {
                // If we're waiting on a PushPromise anyway
                // handle potentially queueing the stream at that point
                if !stream.is_pending_push {
                    if counts.can_inc_num_send_streams() {
                        counts.inc_num_send_streams(stream);
                    } else {
                        self.prioritize.queue_open(stream);
                    }
                }
            }

            // Queue the frame for sending
            self.prioritize
                .queue_frame(frame.into(), buffer, stream, task);

            Ok(())
        }

        /// Send an explicit RST_STREAM frame
        pub fn send_reset<B>(
            &mut self,
            reason: Reason,
            buffer: &mut Buffer<Frame<B>>,
            stream: &mut store::Ptr,
            counts: &mut Counts,
            task: &mut Option<Waker>,
        ) {
            let is_reset = stream.state.is_reset();
            let is_closed = stream.state.is_closed();
            let is_empty = stream.pending_send.is_empty();

            tracing::trace!(
                "send_reset(..., reason={:?}, stream={:?}, ..., \
                 is_reset={:?}; is_closed={:?}; pending_send.is_empty={:?}; \
                 state={:?} \
                 ",
                reason,
                stream.id,
                is_reset,
                is_closed,
                is_empty,
                stream.state
            );

            if is_reset {
                // Don't double reset
                tracing::trace!(
                    " -> not sending RST_STREAM ({:?} is already reset)",
                    stream.id
                );
                return;
            }

            // Transition the state to reset no matter what.
            stream.state.set_reset(reason);

            // If closed AND the send queue is flushed, then the stream cannot be
            // reset explicitly, either. Implicit resets can still be queued.
            if is_closed && is_empty {
                tracing::trace!(
                    " -> not sending explicit RST_STREAM ({:?} was closed \
                     and send queue was flushed)",
                    stream.id
                );
                return;
            }

            // Clear all pending outbound frames.
            // Note that we don't call `self.recv_err` because we want to enqueue
            // the reset frame before transitioning the stream inside
            // `reclaim_all_capacity`.
            self.prioritize.clear_queue(buffer, stream);

            let frame = frame::Reset::new(stream.id, reason);

            tracing::trace!("send_reset -- queueing; frame={:?}", frame);
            self.prioritize
                .queue_frame(frame.into(), buffer, stream, task);
            self.prioritize.reclaim_all_capacity(stream, counts);
        }

        pub fn schedule_implicit_reset(
            &mut self,
            stream: &mut store::Ptr,
            reason: Reason,
            counts: &mut Counts,
            task: &mut Option<Waker>,
        ) {
            if stream.state.is_closed() {
                // Stream is already closed, nothing more to do
                return;
            }

            stream.state.set_scheduled_reset(reason);

            self.prioritize.reclaim_reserved_capacity(stream, counts);
            self.prioritize.schedule_send(stream, task);
        }

        pub fn send_data<B>(
            &mut self,
            frame: frame::Data<B>,
            buffer: &mut Buffer<Frame<B>>,
            stream: &mut store::Ptr,
            counts: &mut Counts,
            task: &mut Option<Waker>,
        ) -> Result<(), UserError>
        where
            B: Buf,
        {
            self.prioritize
                .send_data(frame, buffer, stream, counts, task)
        }

        pub fn send_trailers<B>(
            &mut self,
            frame: frame::Headers,
            buffer: &mut Buffer<Frame<B>>,
            stream: &mut store::Ptr,
            counts: &mut Counts,
            task: &mut Option<Waker>,
        ) -> Result<(), UserError> {
            // TODO: Should this logic be moved into state.rs?
            if !stream.state.is_send_streaming() {
                return Err(UserError::UnexpectedFrameType);
            }

            if frame.has_too_big_field() {
                return Err(UserError::HeaderTooBig);
            }

            stream.state.send_close();

            tracing::trace!("send_trailers -- queuing; frame={:?}", frame);
            self.prioritize
                .queue_frame(frame.into(), buffer, stream, task);

            // Release any excess capacity
            self.prioritize.reserve_capacity(0, stream, counts);

            Ok(())
        }

        pub fn poll_complete<T, B>(
            &mut self,
            cx: &mut Context,
            buffer: &mut Buffer<Frame<B>>,
            store: &mut Store,
            counts: &mut Counts,
            dst: &mut Codec<T, Prioritized<B>>,
        ) -> Poll<io::Result<()>>
        where
            T: AsyncWrite + Unpin,
            B: Buf,
        {
            self.prioritize
                .poll_complete(cx, buffer, store, counts, dst)
        }

        /// Request capacity to send data
        pub fn reserve_capacity(
            &mut self,
            capacity: WindowSize,
            stream: &mut store::Ptr,
            counts: &mut Counts,
        ) {
            self.prioritize.reserve_capacity(capacity, stream, counts)
        }

        pub fn poll_capacity(
            &mut self,
            cx: &Context,
            stream: &mut store::Ptr,
        ) -> Poll<Option<Result<WindowSize, UserError>>> {
            if !stream.state.is_send_streaming() {
                return Poll::Ready(None);
            }

            if !stream.send_capacity_inc {
                stream.wait_send(cx);
                return Poll::Pending;
            }

            stream.send_capacity_inc = false;

            Poll::Ready(Some(Ok(self.capacity(stream))))
        }

        /// Current available stream send capacity
        pub fn capacity(&self, stream: &mut store::Ptr) -> WindowSize {
            let available = stream.send_flow.available().as_size();
            let buffered = stream.buffered_send_data;

            if available <= buffered {
                0
            } else {
                available - buffered
            }
        }

        pub fn poll_reset(
            &self,
            cx: &Context,
            stream: &mut Stream,
            mode: PollReset,
        ) -> Poll<Result<Reason, crate::Error>> {
            match stream.state.ensure_reason(mode)? {
                Some(reason) => Poll::Ready(Ok(reason)),
                None => {
                    stream.wait_send(cx);
                    Poll::Pending
                }
            }
        }

        pub fn recv_connection_window_update(
            &mut self,
            frame: frame::WindowUpdate,
            store: &mut Store,
            counts: &mut Counts,
        ) -> Result<(), Reason> {
            self.prioritize
                .recv_connection_window_update(frame.size_increment(), store, counts)
        }

        pub fn recv_stream_window_update<B>(
            &mut self,
            sz: WindowSize,
            buffer: &mut Buffer<Frame<B>>,
            stream: &mut store::Ptr,
            counts: &mut Counts,
            task: &mut Option<Waker>,
        ) -> Result<(), Reason> {
            if let Err(e) = self.prioritize.recv_stream_window_update(sz, stream) {
                tracing::debug!("recv_stream_window_update !!; err={:?}", e);

                self.send_reset(Reason::FLOW_CONTROL_ERROR, buffer, stream, counts, task);

                return Err(e);
            }

            Ok(())
        }

        pub(super) fn recv_go_away(&mut self, last_stream_id: StreamId) -> Result<(), RecvError> {
            if last_stream_id > self.max_stream_id {
                // The remote endpoint sent a `GOAWAY` frame indicating a stream
                // that we never sent, or that we have already terminated on account
                // of previous `GOAWAY` frame. In either case, that is illegal.
                // (When sending multiple `GOAWAY`s, "Endpoints MUST NOT increase
                // the value they send in the last stream identifier, since the
                // peers might already have retried unprocessed requests on another
                // connection.")
                proto_err!(conn:
                    "recv_go_away: last_stream_id ({:?}) > max_stream_id ({:?})",
                    last_stream_id, self.max_stream_id,
                );
                return Err(RecvError::Connection(Reason::PROTOCOL_ERROR));
            }

            self.max_stream_id = last_stream_id;
            Ok(())
        }

        pub fn recv_err<B>(
            &mut self,
            buffer: &mut Buffer<Frame<B>>,
            stream: &mut store::Ptr,
            counts: &mut Counts,
        ) {
            // Clear all pending outbound frames
            self.prioritize.clear_queue(buffer, stream);
            self.prioritize.reclaim_all_capacity(stream, counts);
        }

        pub fn apply_remote_settings<B>(
            &mut self,
            settings: &frame::Settings,
            buffer: &mut Buffer<Frame<B>>,
            store: &mut Store,
            counts: &mut Counts,
            task: &mut Option<Waker>,
        ) -> Result<(), RecvError> {
            // Applies an update to the remote endpoint's initial window size.
            //
            // Per RFC 7540 §6.9.2:
            //
            // In addition to changing the flow-control window for streams that are
            // not yet active, a SETTINGS frame can alter the initial flow-control
            // window size for streams with active flow-control windows (that is,
            // streams in the "open" or "half-closed (remote)" state). When the
            // value of SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST adjust
            // the size of all stream flow-control windows that it maintains by the
            // difference between the new value and the old value.
            //
            // A change to `SETTINGS_INITIAL_WINDOW_SIZE` can cause the available
            // space in a flow-control window to become negative. A sender MUST
            // track the negative flow-control window and MUST NOT send new
            // flow-controlled frames until it receives WINDOW_UPDATE frames that
            // cause the flow-control window to become positive.
            if let Some(val) = settings.initial_window_size() {
                let old_val = self.init_window_sz;
                self.init_window_sz = val;

                if val < old_val {
                    // We must decrease the (remote) window on every open stream.
                    let dec = old_val - val;
                    tracing::trace!("decrementing all windows; dec={}", dec);

                    let mut total_reclaimed = 0;
                    store.for_each(|mut stream| {
                        let stream = &mut *stream;

                        stream.send_flow.dec_send_window(dec);

                        // It's possible that decreasing the window causes
                        // `window_size` (the stream-specific window) to fall below
                        // `available` (the portion of the connection-level window
                        // that we have allocated to the stream).
                        // In this case, we should take that excess allocation away
                        // and reassign it to other streams.
                        let window_size = stream.send_flow.window_size();
                        let available = stream.send_flow.available().as_size();
                        let reclaimed = if available > window_size {
                            // Drop down to `window_size`.
                            let reclaim = available - window_size;
                            stream.send_flow.claim_capacity(reclaim);
                            total_reclaimed += reclaim;
                            reclaim
                        } else {
                            0
                        };

                        tracing::trace!(
                            "decremented stream window; id={:?}; decr={}; reclaimed={}; flow={:?}",
                            stream.id,
                            dec,
                            reclaimed,
                            stream.send_flow
                        );

                        // TODO: Should this notify the producer when the capacity
                        // of a stream is reduced? Maybe it should if the capacity
                        // is reduced to zero, allowing the producer to stop work.

                        Ok::<_, RecvError>(())
                    })?;

                    self.prioritize
                        .assign_connection_capacity(total_reclaimed, store, counts);
                } else if val > old_val {
                    let inc = val - old_val;

                    store.for_each(|mut stream| {
                        self.recv_stream_window_update(inc, buffer, &mut stream, counts, task)
                            .map_err(RecvError::Connection)
                    })?;
                }
            }

            if let Some(val) = settings.is_push_enabled() {
                self.is_push_enabled = val
            }

            Ok(())
        }

        pub fn clear_queues(&mut self, store: &mut Store, counts: &mut Counts) {
            self.prioritize.clear_pending_capacity(store, counts);
            self.prioritize.clear_pending_send(store, counts);
            self.prioritize.clear_pending_open(store, counts);
        }

        pub fn ensure_not_idle(&self, id: StreamId) -> Result<(), Reason> {
            if let Ok(next) = self.next_stream_id {
                if id >= next {
                    return Err(Reason::PROTOCOL_ERROR);
                }
            }
            // if next_stream_id is overflowed, that's ok.

            Ok(())
        }

        pub fn ensure_next_stream_id(&self) -> Result<StreamId, UserError> {
            self.next_stream_id
                .map_err(|_| UserError::OverflowedStreamId)
        }

        pub fn may_have_created_stream(&self, id: StreamId) -> bool {
            if let Ok(next_id) = self.next_stream_id {
                // Peer::is_local_init should have been called beforehand
                debug_assert_eq!(id.is_server_initiated(), next_id.is_server_initiated(),);
                id < next_id
            } else {
                true
            }
        }
    }