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