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
use std::{
    cell::UnsafeCell,
    convert::Infallible,
    io::{Cursor, Read},
    task::{ready, Poll},
};

use bytes::{Bytes, BytesMut};
use flate2::{
    read::{GzDecoder, GzEncoder, ZlibDecoder, ZlibEncoder},
    Compression,
};
use futures_core::Future;
use monoio::buf::IoBuf;
use monoio_compat::box_future::MaybeArmedBoxFuture;
use smallvec::SmallVec;

use super::error::{EncodeDecodeError, HttpError};
use crate::{
    common::{request::Request, response::Response},
    h1::payload::Payload,
    h2::RecvStream,
};

const SUPPORTED_ENCODINGS: [&str; 3] = ["gzip", "br", "deflate"];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamHint {
    None,
    Fixed,
    Stream,
}

pub trait Body {
    type Data: IoBuf;
    type Error;

    fn next_data(&mut self) -> impl Future<Output = Option<Result<Self::Data, Self::Error>>>;
    fn stream_hint(&self) -> StreamHint;
}

impl Body for () {
    type Data = Bytes;
    type Error = Infallible;

    async fn next_data(&mut self) -> Option<Result<Self::Data, Self::Error>> {
        Some(Ok(Bytes::new()))
    }

    fn stream_hint(&self) -> StreamHint {
        StreamHint::Fixed
    }
}

impl<T: Body> Body for &mut T {
    type Data = T::Data;
    type Error = T::Error;

    #[inline]
    fn next_data(&mut self) -> impl Future<Output = Option<Result<Self::Data, Self::Error>>> {
        (**self).next_data()
    }

    #[inline]
    fn stream_hint(&self) -> StreamHint {
        (**self).stream_hint()
    }
}

pub type Chunks = SmallVec<[Bytes; 16]>;

pub trait BodyExt: Body {
    /// Consumes body and return continous memory
    fn bytes(self) -> impl Future<Output = Result<Bytes, Self::Error>>;
    /// Return bytes array
    fn chunks(self) -> impl Future<Output = Result<Chunks, Self::Error>>;
}

pub trait BodyEncodeExt: BodyExt {
    type EncodeDecodeError;

    /// Consumes body and returns decodec content
    fn decode_content(
        self,
        content_encoding: String,
    ) -> impl Future<Output = Result<Bytes, Self::EncodeDecodeError>>;
    /// Consumes body and returns encoded content
    fn encode_content(
        self,
        accept_encoding: String,
    ) -> impl Future<Output = Result<Bytes, Self::EncodeDecodeError>>;
}

impl<T> BodyExt for T
where
    T: Body<Data = Bytes>,
{
    async fn bytes(mut self) -> Result<Bytes, Self::Error> {
        match self.stream_hint() {
            StreamHint::None => Ok(Bytes::new()),
            StreamHint::Fixed => self.next_data().await.unwrap_or(Ok(Bytes::new())),
            StreamHint::Stream => {
                let mut data = BytesMut::new();
                while let Some(chunk) = self.next_data().await {
                    data.extend_from_slice(&chunk?);
                }
                Ok(data.freeze())
            }
        }
    }

    async fn chunks(mut self) -> Result<Chunks, Self::Error> {
        match self.stream_hint() {
            StreamHint::None => Ok(Chunks::new()),
            StreamHint::Fixed => {
                let mut chunks = Chunks::new();
                if let Some(b) = self.next_data().await {
                    chunks.push(b?);
                }
                Ok(chunks)
            }
            StreamHint::Stream => {
                let mut chunks = Chunks::new();
                while let Some(chunk) = self.next_data().await {
                    chunks.push(chunk?);
                }
                Ok(chunks)
            }
        }
    }
}

impl<T: BodyExt> BodyEncodeExt for T {
    type EncodeDecodeError = EncodeDecodeError<T::Error>;
    async fn decode_content(self, encoding: String) -> Result<Bytes, Self::EncodeDecodeError> {
        let buf = self.bytes().await.map_err(EncodeDecodeError::Http)?;
        match encoding.as_str() {
            "gzip" => {
                let mut decoder = GzDecoder::new(buf.as_ref());
                let mut decompressed_data = Vec::new();
                decoder.read_to_end(&mut decompressed_data)?;
                Ok(bytes::Bytes::from(decompressed_data))
            }
            "deflate" => {
                let mut decoder = ZlibDecoder::new(buf.as_ref());
                let mut decompressed_data = Vec::new();
                decoder.read_to_end(&mut decompressed_data)?;
                Ok(bytes::Bytes::from(decompressed_data))
            }
            "br" => {
                let mut decoder = brotli::Decompressor::new(buf.as_ref(), 4096);
                let mut decompressed_data = Vec::new();
                decoder.read_to_end(&mut decompressed_data)?;
                Ok(bytes::Bytes::from(decompressed_data))
            }
            _ => {
                // Unsupported or no encoding, return original data
                Ok(buf)
            }
        }
    }

    async fn encode_content(
        self,
        accept_encoding: String,
    ) -> Result<Bytes, Self::EncodeDecodeError> {
        let buf = self.bytes().await.map_err(EncodeDecodeError::Http)?;
        let accepted_encodings: Vec<String> = accept_encoding
            .split(',')
            .map(|s| s.trim().to_string())
            .collect();

        // Find the first supported encoding from the accepted encodings
        let selected_encoding = accepted_encodings
            .iter()
            .find(|&encoding| SUPPORTED_ENCODINGS.contains(&encoding.as_str()))
            .cloned()
            .unwrap_or_else(|| "identity".to_string());

        let encoded_data = match selected_encoding.as_str() {
            "gzip" => {
                let mut encoder = GzEncoder::new(buf.as_ref(), Compression::best());
                let mut compressed_data = Vec::new();
                encoder.read_to_end(&mut compressed_data)?;
                compressed_data
            }
            "deflate" => {
                let mut encoder = ZlibEncoder::new(buf.as_ref(), Compression::best());
                let mut compressed_data = Vec::new();
                encoder.read_to_end(&mut compressed_data)?;
                compressed_data
            }
            "br" => {
                let mut encoder = brotli::CompressorReader::new(Cursor::new(buf), 4096, 11, 22);
                let mut compressed_data = Vec::new();
                encoder.read_to_end(&mut compressed_data)?;
                compressed_data
            }
            _ => {
                // Unsupported or no encoding, return original data
                buf.to_vec()
            }
        };

        Ok(bytes::Bytes::from(encoded_data))
    }
}

#[derive(Debug)]
pub enum HttpBody {
    Ready(Option<Bytes>),
    H1(Payload),
    H2(RecvStream),
}

impl HttpBody {
    pub async fn to_ready(&mut self) -> Result<Option<Bytes>, HttpError> {
        match self.stream_hint() {
            StreamHint::None => Ok(None),
            StreamHint::Fixed => match self.next_data().await {
                None => {
                    *self = Self::Ready(None);
                    Ok(None)
                }
                Some(chunk) => {
                    let bytes = chunk?;
                    *self = Self::Ready(Some(bytes.clone()));
                    Ok(Some(bytes))
                }
            },
            StreamHint::Stream => {
                let mut data = BytesMut::new();
                while let Some(chunk) = self.next_data().await {
                    data.extend_from_slice(&chunk?);
                }
                let bytes = data.freeze();
                *self = Self::Ready(Some(bytes.clone()));
                Ok(Some(bytes))
            }
        }
    }

    #[inline]
    pub fn is_ready(&self) -> bool {
        matches!(self, Self::Ready(_))
    }

    #[inline]
    pub fn ready_data(&self) -> Option<Bytes> {
        match self {
            Self::Ready(b) => b.clone(),
            _ => None,
        }
    }
}

impl From<Payload> for HttpBody {
    #[inline]
    fn from(p: Payload) -> Self {
        Self::H1(p)
    }
}

impl From<RecvStream> for HttpBody {
    #[inline]
    fn from(p: RecvStream) -> Self {
        Self::H2(p)
    }
}

impl Default for HttpBody {
    #[inline]
    fn default() -> Self {
        Self::Ready(None)
    }
}

impl HttpBody {
    pub fn request<T>(req: Request<T>) -> Request<Self>
    where
        Self: From<T>,
    {
        let (parts, body) = req.into_parts();
        #[cfg(feature = "logging")]
        tracing::debug!("Request {parts:?}");
        Request::from_parts(parts, body.into())
    }

    pub fn response<T>(req: Response<T>) -> Response<Self>
    where
        Self: From<T>,
    {
        let (parts, body) = req.into_parts();
        #[cfg(feature = "logging")]
        tracing::debug!("Response {parts:?}");
        Response::from_parts(parts, body.into())
    }
}

impl Body for HttpBody {
    type Data = Bytes;
    type Error = HttpError;

    async fn next_data(&mut self) -> Option<Result<Self::Data, Self::Error>> {
        match self {
            Self::Ready(b) => b.take().map(Result::Ok),
            Self::H1(ref mut p) => p.next_data().await,
            Self::H2(ref mut p) => p.next_data().await.map(|r| r.map_err(HttpError::from)),
        }
    }

    fn stream_hint(&self) -> StreamHint {
        match self {
            Self::Ready(Some(_)) => StreamHint::Fixed,
            Self::Ready(None) => StreamHint::None,
            Self::H1(ref p) => p.stream_hint(),
            Self::H2(ref p) => p.stream_hint(),
        }
    }
}

pub trait FixedBody: Body {
    fn fixed_body(data: Option<Bytes>) -> Self;
}

impl FixedBody for HttpBody {
    fn fixed_body(data: Option<Bytes>) -> Self {
        Self::Ready(data)
    }
}

#[derive(Debug)]
pub struct HttpBodyStream {
    inner: UnsafeCell<HttpBody>,
    stream_fut: MaybeArmedBoxFuture<Option<Result<Bytes, HttpError>>>,
}

impl HttpBodyStream {
    pub fn new(inner: HttpBody) -> Self {
        let stream_fut = MaybeArmedBoxFuture::new(async move { Some(Ok(Bytes::new())) });
        Self {
            inner: UnsafeCell::new(inner),
            stream_fut,
        }
    }
}

unsafe impl Send for HttpBodyStream {}

impl From<HttpBody> for HttpBodyStream {
    fn from(b: HttpBody) -> Self {
        Self::new(b)
    }
}

impl futures_core::Stream for HttpBodyStream {
    type Item = Result<Bytes, HttpError>;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context,
    ) -> std::task::Poll<Option<Self::Item>> {
        if !self.stream_fut.armed() {
            let body_stream = unsafe { &mut *(self.inner.get()) };
            self.stream_fut.arm_future(body_stream.next_data());
        }

        Poll::Ready(ready!(self.stream_fut.poll(cx)))
    }
}