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
//! Utilities for working with Actix Web types.

// stuff in here comes in and out of usage
#![allow(dead_code)]

use std::{
    convert::Infallible,
    io,
    pin::Pin,
    task::{ready, Context, Poll},
};

use actix_http::{error::PayloadError, BoxedPayloadStream};
use actix_web::{dev, web::BufMut};
use futures_core::Stream;
use futures_util::StreamExt as _;
use local_channel::mpsc;

/// Returns an effectively cloned payload that supports streaming efficiently.
///
/// The cloned payload:
/// - yields identical chunks;
/// - does not poll ahead of the original;
/// - does not poll significantly slower than the original;
/// - receives an error signal if the original errors, but details are opaque to the copy.
///
/// If the payload is forked in one of the extractors used in a handler, then the original _must_ be
/// read in another extractor or else the request will hang.
pub fn fork_request_payload(orig_payload: &mut dev::Payload) -> dev::Payload {
    const TARGET: &str = concat!(module_path!(), "::fork_request_payload");

    let payload = orig_payload.take();

    let (tx, rx) = mpsc::channel();

    let proxy_stream: BoxedPayloadStream = Box::pin(payload.inspect(move |res| {
        match res {
            Ok(chunk) => {
                tracing::trace!(target: TARGET, "yielding {} byte chunk", chunk.len());
                tx.send(Ok(chunk.clone())).unwrap();
            }

            Err(err) => tx
                .send(Err(PayloadError::Io(io::Error::new(
                    io::ErrorKind::Other,
                    format!("error from original stream: {err}"),
                ))))
                .unwrap(),
        }
    }));

    tracing::trace!(target: TARGET, "creating proxy payload");
    *orig_payload = dev::Payload::from(proxy_stream);

    dev::Payload::Stream {
        payload: Box::pin(rx),
    }
}

/// An `io::Write`r that only requires mutable reference and assumes that there is space available
/// in the buffer for every write operation or that it can be extended implicitly (like
/// `bytes::BytesMut`, for example).
///
/// This is slightly faster (~10%) than `bytes::buf::Writer` in such cases because it does not
/// perform a remaining length check before writing.
pub(crate) struct MutWriter<'a, B>(pub(crate) &'a mut B);

impl<'a, B> MutWriter<'a, B> {
    pub fn get_ref(&self) -> &B {
        self.0
    }
}

impl<'a, B: BufMut> io::Write for MutWriter<'a, B> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.0.put_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

pin_project_lite::pin_project! {
    /// Converts stream with item `T` into `Result<T, Infallible>`.
    pub struct InfallibleStream<S> {
        #[pin]
        stream: S,
    }
}

impl<S> InfallibleStream<S> {
    /// Constructs new `InfallibleStream` stream.
    pub fn new(stream: S) -> Self {
        Self { stream }
    }
}

impl<S: Stream> Stream for InfallibleStream<S> {
    type Item = Result<S::Item, Infallible>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Poll::Ready(ready!(self.project().stream.poll_next(cx)).map(Ok))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.stream.size_hint()
    }
}

#[cfg(test)]
#[derive(Debug, Clone, Default)]
pub(crate) struct PollSeq<T> {
    seq: std::collections::VecDeque<T>,
}

#[cfg(test)]
mod poll_seq_impls {
    use std::collections::VecDeque;

    use futures_util::stream;

    use super::*;

    impl<T> PollSeq<T> {
        pub fn new(seq: VecDeque<T>) -> Self {
            Self { seq }
        }
    }

    impl<T> PollSeq<Poll<Option<T>>> {
        pub fn into_stream(mut self) -> impl Stream<Item = T> {
            stream::poll_fn(move |_cx| match self.seq.pop_front() {
                Some(item) => item,
                None => Poll::Ready(None),
            })
        }
    }

    impl<T, const N: usize> From<[T; N]> for PollSeq<T> {
        fn from(seq: [T; N]) -> Self {
            Self::new(VecDeque::from(seq))
        }
    }
}