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
use futures_lite::io::{self, AsyncRead, AsyncWrite};
use http_types::headers::{Headers, CONTENT_LENGTH};
use http_types::{Extensions, Method, Version};

use std::task::Poll;

use crate::Stopper;
use crate::{Conn, ReceivedBodyState};

pub struct Synthetic(Option<Vec<u8>>, usize);
impl AsyncWrite for Synthetic {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
        _buf: &[u8],
    ) -> std::task::Poll<io::Result<usize>> {
        Poll::Ready(Ok(0))
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }

    fn poll_close(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }
}

impl AsyncRead for Synthetic {
    fn poll_read(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        match &self.0 {
            Some(bytes) => {
                let bytes_left = bytes.len() - self.1;
                let bytes_to_read = bytes_left.min(buf.len());
                buf.copy_from_slice(&bytes[self.1..self.1 + bytes_to_read]);
                Poll::Ready(Ok(bytes_to_read))
            }
            None => Poll::Ready(Ok(0)),
        }
    }
}

impl Conn<Synthetic> {
    pub fn new_synthetic(method: Method, path: String, body: Option<Vec<u8>>) -> Self {
        let mut request_headers = Headers::new();
        request_headers.insert(
            CONTENT_LENGTH,
            body.as_ref()
                .map(|b| b.len())
                .unwrap_or_default()
                .to_string(),
        );

        Self {
            rw: Synthetic(body, 0),
            request_headers,
            response_headers: Headers::new(),
            path,
            method,
            status: None,
            version: Version::Http1_1,
            state: Extensions::new(),
            response_body: None,
            buffer: None,
            request_body_state: ReceivedBodyState::Start,
            secure: false,
            stopper: Stopper::new(),
        }
    }

    pub fn request_headers_mut(&mut self) -> &mut Headers {
        &mut self.request_headers
    }
}