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
//! re-export of [http] crate types.

pub use ::http::*;

use core::{
    borrow::{Borrow, BorrowMut},
    mem,
    pin::Pin,
    task::{Context, Poll},
};

use std::net::SocketAddr;

use futures_core::stream::Stream;
use pin_project_lite::pin_project;

/// Some often used header value.
#[allow(clippy::declare_interior_mutable_const)]
pub mod const_header_value {
    use ::http::header::HeaderValue;

    macro_rules! const_value {
            ($(($ident: ident, $expr: expr)), *) => {
                $(
                   pub const $ident: HeaderValue = HeaderValue::from_static($expr);
                )*
            }
        }

    const_value!(
        (TEXT, "text/plain"),
        (TEXT_UTF8, "text/plain; charset=utf-8"),
        (JSON, "application/json"),
        (APPLICATION_WWW_FORM_URLENCODED, "application/x-www-form-urlencoded"),
        (TEXT_HTML_UTF8, "text/html; charset=utf-8"),
        (GRPC, "application/grpc"),
        (WEBSOCKET, "websocket")
    );
}

/// Some often used header name.
#[allow(clippy::declare_interior_mutable_const)]
pub mod const_header_name {
    use ::http::header::HeaderName;

    macro_rules! const_name {
            ($(($ident: ident, $expr: expr)), *) => {
                $(
                   pub const $ident: HeaderName = HeaderName::from_static($expr);
                )*
            }
        }

    const_name!((PROTOCOL, "protocol"));
}

/// helper trait for converting a [Request] to [Response].
/// This is a memory optimization for re-use heap allocation and pass down the context data
/// inside [Extensions] from request to response.
///
/// # Example
/// ```rust
/// # use xitca_http::http::{Request, Response};
/// // arbitrary typed state inserted into request type.
/// #[derive(Clone)]
/// struct Foo;
///
/// fn into_response(mut req: Request<()>) -> Response<()> {
///     req.extensions_mut().insert(Foo); // insert Foo to request's extensions type map.
///     
///     // convert request into response in place with the same memory allocation.
///     use xitca_http::http::IntoResponse;
///     let res = req.into_response(());
///     
///     // the memory is re-used so Foo type is accessible from response's extensions type map.
///     assert!(res.extensions().get::<Foo>().is_some());
///
///     res
/// }
///
/// ```
pub trait IntoResponse<B, ResB> {
    fn into_response(self, body: B) -> Response<ResB>;

    fn as_response(&mut self, body: B) -> Response<ResB>
    where
        Self: Default,
    {
        mem::take(self).into_response(body)
    }
}

impl<ReqB, B, ResB> IntoResponse<B, ResB> for Request<ReqB>
where
    B: Into<ResB>,
{
    fn into_response(self, body: B) -> Response<ResB> {
        let (
            request::Parts {
                mut headers,
                extensions,
                ..
            },
            _,
        ) = self.into_parts();
        headers.clear();

        let mut res = Response::new(body.into());
        *res.headers_mut() = headers;
        *res.extensions_mut() = extensions;

        res
    }
}

#[cfg(feature = "router")]
use super::util::service::router::Params;

pin_project! {
    /// extension types for [Request]
    #[derive(Debug)]
    pub struct RequestExt<B> {
        #[pin]
        body: B,
        // http::Extensions is often brought up as an alternative for extended states but in general
        // xitca tries to be strongly typed when possible. runtime type casting is meant for library
        // consumer but not library itself.
        ext: Extension,
    }
}

impl<B> Clone for RequestExt<B>
where
    B: Clone,
{
    fn clone(&self) -> Self {
        Self {
            body: self.body.clone(),
            ext: Extension(Box::new(_Extension::clone(&*self.ext.0))),
        }
    }
}

// a separate extension type contain information can not be carried by http::Request. the goal is
// to keep extended info strongly typed and not depend on runtime type map of http::Extensions.
#[derive(Debug)]
pub(crate) struct Extension(Box<_Extension>);

impl Extension {
    pub(crate) fn new(addr: SocketAddr) -> Self {
        Self(Box::new(_Extension {
            addr,
            #[cfg(feature = "router")]
            params: Default::default(),
        }))
    }
}

#[derive(Clone, Debug)]
struct _Extension {
    addr: SocketAddr,
    #[cfg(feature = "router")]
    params: Params,
}

impl<B> RequestExt<B> {
    pub(crate) fn from_parts(body: B, ext: Extension) -> Self {
        Self { body, ext }
    }

    /// retrieve remote peer's socket address.
    ///
    /// # Default
    /// [std::net::Ipv4Addr::UNSPECIFIED] is used for representing peers that can't provide it's socket address.
    #[inline]
    pub fn socket_addr(&self) -> &SocketAddr {
        &self.ext.0.addr
    }

    /// exclusive version of [RequestExt::socket_addr]
    #[inline]
    pub fn socket_addr_mut(&mut self) -> &mut SocketAddr {
        &mut self.ext.0.addr
    }

    /// map body type of self to another type with given function closure.
    #[inline]
    pub fn map_body<F, B1>(self, func: F) -> RequestExt<B1>
    where
        F: FnOnce(B) -> B1,
    {
        RequestExt {
            body: func(self.body),
            ext: self.ext,
        }
    }

    /// replace body type of self with another type and return new type of Self and original body type
    /// in tuple.
    #[inline]
    pub fn replace_body<B1>(self, body: B1) -> (RequestExt<B1>, B) {
        let body_org = self.body;

        (RequestExt { body, ext: self.ext }, body_org)
    }
}

impl<B> Default for RequestExt<B>
where
    B: Default,
{
    fn default() -> Self {
        Self::from_parts(B::default(), Extension::new(crate::unspecified_socket_addr()))
    }
}

impl<B> Stream for RequestExt<B>
where
    B: Stream,
{
    type Item = B::Item;

    #[inline]
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.project().body.poll_next(cx)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.body.size_hint()
    }
}

impl<B> Borrow<SocketAddr> for RequestExt<B> {
    #[inline]
    fn borrow(&self) -> &SocketAddr {
        self.socket_addr()
    }
}

#[cfg(feature = "router")]
mod router {
    use super::*;

    impl<B> RequestExt<B> {
        /// retrieve shared reference of route [Params].
        #[inline]
        pub fn params(&self) -> &Params {
            &self.ext.0.params
        }

        /// retrieve exclusive reference of route [Params].
        #[inline]
        pub fn params_mut(&mut self) -> &mut Params {
            &mut self.ext.0.params
        }
    }

    impl<B> Borrow<Params> for RequestExt<B> {
        #[inline]
        fn borrow(&self) -> &Params {
            self.params()
        }
    }

    impl<B> BorrowMut<Params> for RequestExt<B> {
        #[inline]
        fn borrow_mut(&mut self) -> &mut Params {
            self.params_mut()
        }
    }
}

/// trait for Borrow &T from &Self.
/// used for foreign types (from xitca-http pov) that can be impl with [Borrow] trait.
pub trait BorrowReq<T> {
    fn borrow(&self) -> &T;
}

/// trait for Borrow &mut T from &mut Self.
/// used for foreign types (from xitca-http pov) that can be impl with [BorrowMut] trait.
pub trait BorrowReqMut<T> {
    fn borrow_mut(&mut self) -> &mut T;
}

impl<Ext> BorrowReq<Uri> for Request<Ext> {
    #[inline]
    fn borrow(&self) -> &Uri {
        self.uri()
    }
}

impl<Ext> BorrowReq<Method> for Request<Ext> {
    #[inline]
    fn borrow(&self) -> &Method {
        self.method()
    }
}

impl<Ext> BorrowReq<HeaderMap> for Request<Ext> {
    #[inline]
    fn borrow(&self) -> &HeaderMap {
        self.headers()
    }
}

impl<Ext> BorrowReq<Extensions> for Request<Ext> {
    #[inline]
    fn borrow(&self) -> &Extensions {
        self.extensions()
    }
}

impl<Ext> BorrowReqMut<Extensions> for Request<Ext> {
    #[inline]
    fn borrow_mut(&mut self) -> &mut Extensions {
        self.extensions_mut()
    }
}

impl<T, B> BorrowReq<T> for Request<RequestExt<B>>
where
    RequestExt<B>: Borrow<T>,
{
    #[inline]
    fn borrow(&self) -> &T {
        self.body().borrow()
    }
}

impl<T, B> BorrowReqMut<T> for Request<RequestExt<B>>
where
    RequestExt<B>: BorrowMut<T>,
{
    #[inline]
    fn borrow_mut(&mut self) -> &mut T {
        self.body_mut().borrow_mut()
    }
}