async_http_codec/request/head/
mod.rs

1#[cfg(test)]
2mod test;
3
4use crate::internal::buffer_decode::{BufferDecode, BufferDecodeState};
5use crate::internal::buffer_write::{BufferWrite, BufferWriteState};
6use crate::internal::dec_helpers::request_head_parse;
7use crate::internal::enc_helpers::header_encode;
8use crate::internal::io_future::{IoFutureState, IoFutureWithOutputState};
9use futures::{AsyncRead, AsyncWrite};
10use http::request::Parts;
11use http::{HeaderMap, Method, Request, Uri, Version};
12use std::borrow::Cow;
13use std::io;
14
15#[derive(Clone, Debug)]
16pub struct RequestHead<'a> {
17    method: Method,
18    uri: Cow<'a, Uri>,
19    version: Version,
20    headers: Cow<'a, HeaderMap>,
21}
22
23impl<'a> RequestHead<'a> {
24    pub fn new(
25        method: Method,
26        uri: Cow<'a, Uri>,
27        version: Version,
28        headers: Cow<'a, HeaderMap>,
29    ) -> Self {
30        Self {
31            method,
32            uri,
33            version,
34            headers,
35        }
36    }
37    pub fn ref_parts(parts: &'a Parts) -> Self {
38        Self {
39            method: parts.method.clone(),
40            uri: Cow::Borrowed(&parts.uri),
41            version: parts.version,
42            headers: Cow::Borrowed(&parts.headers),
43        }
44    }
45    pub fn ref_request<B>(request: &'a Request<B>) -> Self {
46        Self {
47            method: request.method().clone(),
48            uri: Cow::Borrowed(&request.uri()),
49            version: request.version(),
50            headers: Cow::Borrowed(&request.headers()),
51        }
52    }
53    pub fn to_owned(self) -> RequestHead<'static> {
54        RequestHead {
55            method: self.method,
56            uri: Cow::Owned(self.uri.into_owned()),
57            version: self.version,
58            headers: Cow::Owned(self.headers.into_owned()),
59        }
60    }
61    pub fn to_vec(&self) -> io::Result<Vec<u8>> {
62        use std::io::Write;
63        let mut buffer = Vec::with_capacity(8192);
64        writeln!(buffer, "{} {} {:?}\r", self.method, &self.uri, self.version)?;
65        header_encode(&mut buffer, &self.headers)?;
66        Ok(buffer)
67    }
68    pub fn encode<IO: AsyncWrite + Unpin>(&self, io: IO) -> BufferWrite<IO> {
69        self.encode_state().into_future(io)
70    }
71    pub fn encode_state(&self) -> BufferWriteState {
72        BufferWriteState::new(self.to_vec())
73    }
74    pub fn decode<IO: AsyncRead + Unpin>(io: IO) -> BufferDecode<IO, Self> {
75        Self::decode_state().into_future(io)
76    }
77    pub fn decode_state() -> BufferDecodeState<Self> {
78        BufferDecodeState::new(8192, 128, &request_head_parse)
79    }
80    pub fn method(&self) -> Method {
81        self.method.clone()
82    }
83    pub fn uri(&self) -> &Uri {
84        self.uri.as_ref()
85    }
86    pub fn version(&self) -> Version {
87        self.version
88    }
89    pub fn headers(&self) -> &HeaderMap {
90        self.headers.as_ref()
91    }
92    pub fn method_mut(&mut self) -> &mut Method {
93        &mut self.method
94    }
95    pub fn uri_mut(&mut self) -> &mut Uri {
96        self.uri.to_mut()
97    }
98    pub fn version_mut(&mut self) -> &mut Version {
99        &mut self.version
100    }
101    pub fn headers_mut(&mut self) -> &mut HeaderMap {
102        self.headers.to_mut()
103    }
104}
105
106impl From<Parts> for RequestHead<'static> {
107    fn from(parts: Parts) -> Self {
108        Self {
109            method: parts.method.clone(),
110            uri: Cow::Owned(parts.uri),
111            version: parts.version,
112            headers: Cow::Owned(parts.headers),
113        }
114    }
115}
116
117impl<'a> From<RequestHead<'a>> for Parts {
118    fn from(head: RequestHead<'a>) -> Self {
119        let mut parts = Request::new(()).into_parts().0;
120        parts.method = head.method;
121        parts.uri = head.uri.into_owned();
122        parts.version = head.version;
123        parts.headers = head.headers.into_owned();
124        parts
125    }
126}
127
128impl<B> From<Request<B>> for RequestHead<'static> {
129    fn from(request: Request<B>) -> Self {
130        request.into_parts().0.into()
131    }
132}
133
134impl<'a> From<RequestHead<'a>> for Request<()> {
135    fn from(head: RequestHead<'a>) -> Self {
136        let parts: Parts = head.into();
137        Request::from_parts(parts, ())
138    }
139}