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
use crate::{
    after_send::AfterSend, http_config::DEFAULT_CONFIG, received_body::ReceivedBodyState,
    transport::Transport, Conn, Headers, KnownHeaderName, Method, StateSet, Stopper, Version,
};
use futures_lite::io::{AsyncRead, AsyncWrite, Cursor, Result};
use std::{
    pin::Pin,
    task::{Context, Poll},
    time::Instant,
};

/**
Synthetic represents a simple transport that contains fixed
content. This is exclusively useful for testing or for server
implementations that are not read from an io connection, such as a
faas function, in which the entire body may be available immediately
on invocation.
*/
#[derive(Debug)]
pub struct Synthetic {
    data: Cursor<Vec<u8>>,
    closed: bool,
}

impl AsyncRead for Synthetic {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize>> {
        let Synthetic { data, closed } = &mut *self;
        if *closed {
            Poll::Ready(Ok(0))
        } else {
            match Pin::new(data).poll_read(cx, buf) {
                Poll::Ready(Ok(0)) => Poll::Pending,
                other => other,
            }
        }
    }
}

impl Synthetic {
    /// the length of this synthetic transport's body
    pub fn len(&self) -> Option<usize> {
        // this is as such for semver-compatibility with a previous interface
        match self.data.get_ref().len() {
            0 => None,
            n => Some(n),
        }
    }

    /// predicate to determine if this synthetic contains no content
    pub fn is_empty(&self) -> bool {
        self.data.get_ref().is_empty()
    }

    /// close this connection
    pub fn close(&mut self) {
        self.closed = true;
    }
}

impl Transport for Synthetic {}

impl AsyncWrite for Synthetic {
    fn poll_write(self: Pin<&mut Self>, _cx: &mut Context<'_>, _buf: &[u8]) -> Poll<Result<usize>> {
        Poll::Ready(Ok(0))
    }

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

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

impl From<Cursor<Vec<u8>>> for Synthetic {
    fn from(data: Cursor<Vec<u8>>) -> Self {
        Self {
            data,
            closed: false,
        }
    }
}

impl From<Vec<u8>> for Synthetic {
    fn from(v: Vec<u8>) -> Self {
        Cursor::new(v).into()
    }
}

impl From<&[u8]> for Synthetic {
    fn from(v: &[u8]) -> Self {
        v.to_owned().into()
    }
}

impl From<String> for Synthetic {
    fn from(v: String) -> Self {
        v.into_bytes().into()
    }
}

impl From<&str> for Synthetic {
    fn from(v: &str) -> Self {
        v.as_bytes().into()
    }
}

impl From<()> for Synthetic {
    fn from((): ()) -> Self {
        Vec::new().into()
    }
}

impl From<Option<Vec<u8>>> for Synthetic {
    fn from(v: Option<Vec<u8>>) -> Self {
        v.unwrap_or_default().into()
    }
}

impl Conn<Synthetic> {
    /**
    Construct a new synthetic conn with provided method, path, and body.
    ```rust
    # use trillium_http::{Method, Conn};
    let conn = Conn::new_synthetic(Method::Get, "/", "hello");
    assert_eq!(conn.method(), Method::Get);
    assert_eq!(conn.path(), "/");
    ```
    */
    pub fn new_synthetic(
        method: Method,
        path: impl Into<String>,
        body: impl Into<Synthetic>,
    ) -> Self {
        let transport = body.into();
        let mut request_headers = Headers::new();
        request_headers.insert(
            KnownHeaderName::ContentLength,
            transport.len().unwrap_or_default().to_string(),
        );

        Self {
            transport,
            request_headers,
            response_headers: Headers::new(),
            path: path.into(),
            method,
            status: None,
            version: Version::Http1_1,
            state: StateSet::new(),
            response_body: None,
            buffer: Vec::with_capacity(DEFAULT_CONFIG.request_buffer_initial_len).into(),
            request_body_state: ReceivedBodyState::Start,
            secure: false,
            stopper: Stopper::new(),
            after_send: AfterSend::default(),
            start_time: Instant::now(),
            peer_ip: None,
            http_config: DEFAULT_CONFIG,
        }
    }

    /// simulate closing the transport
    pub fn close(&mut self) {
        self.transport.close();
    }

    /**
    Replaces the synthetic body. This is intended for testing use.
     */
    pub fn replace_body(&mut self, body: impl Into<Synthetic>) {
        let transport = body.into();
        self.request_headers_mut().insert(
            KnownHeaderName::ContentLength,
            transport.len().unwrap_or_default().to_string(),
        );
        self.transport = transport;
        self.request_body_state = ReceivedBodyState::default();
    }
}