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
use std::error::Error as StdError;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::{Buf, Bytes};
use futures::stream::Stream;
use http::header::HeaderMap;
use http_body::Body as HttpBody;
use crate::async_stream::AsyncStream;
pub struct Body {
inner: BodyType,
}
enum BodyType {
Bytes(Option<Bytes>),
AsyncStream(AsyncStream<Bytes, io::Error>),
Empty,
}
impl Body {
pub fn empty() -> Body {
Body {
inner: BodyType::Empty,
}
}
}
impl Stream for Body {
type Item = io::Result<Bytes>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
match self.inner {
BodyType::Bytes(ref mut strm) => Poll::Ready(strm.take().map(|b| Ok(b))),
BodyType::AsyncStream(ref mut strm) => {
let strm = Pin::new(strm);
strm.poll_next(cx)
},
BodyType::Empty => Poll::Ready(None),
}
}
}
impl HttpBody for Body {
type Data = Bytes;
type Error = io::Error;
fn poll_data(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Result<Self::Data, Self::Error>>> {
self.poll_next(cx)
}
fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context,
) -> Poll<Result<Option<HeaderMap>, Self::Error>>
{
Poll::Ready(Ok(None))
}
}
impl From<String> for Body {
fn from(t: String) -> Body {
Body {
inner: BodyType::Bytes(Some(Bytes::from(t))),
}
}
}
impl From<&str> for Body {
fn from(t: &str) -> Body {
Body {
inner: BodyType::Bytes(Some(Bytes::from(t.to_string()))),
}
}
}
impl From<Bytes> for Body {
fn from(t: Bytes) -> Body {
Body {
inner: BodyType::Bytes(Some(t)),
}
}
}
impl From<AsyncStream<Bytes, io::Error>> for Body {
fn from(s: AsyncStream<Bytes, io::Error>) -> Body {
Body {
inner: BodyType::AsyncStream(s),
}
}
}
use pin_project::pin_project;
#[pin_project]
pub struct StreamBody<B> {
#[pin]
body: B,
}
impl<ReqBody, ReqData, ReqError> HttpBody for StreamBody<ReqBody>
where
ReqData: Buf + Send,
ReqError: StdError + Send + Sync + 'static,
ReqBody: Stream<Item = Result<ReqData, ReqError>>,
{
type Data = ReqData;
type Error = ReqError;
fn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>>
{
let this = self.project();
this.body.poll_next(cx)
}
fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context,
) -> Poll<Result<Option<HeaderMap>, Self::Error>>
{
Poll::Ready(Ok(None))
}
}
impl<ReqBody, ReqData, ReqError> StreamBody<ReqBody>
where
ReqData: Buf + Send,
ReqError: StdError + Send + Sync + 'static,
ReqBody: Stream<Item = Result<ReqData, ReqError>>,
{
pub fn new(body: ReqBody) -> StreamBody<ReqBody> {
StreamBody { body }
}
}