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
use crate::{Body, BodyWriter};
use bytes::Bytes;
use conjure_error::Error;
use futures::channel::{mpsc, oneshot};
use futures::{pin_mut, Stream};
use hyper::HeaderMap;
use pin_project::pin_project;
use std::io::Cursor;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{error, fmt, mem};
use witchcraft_log::debug;
#[derive(Debug)]
pub struct BodyError(());
impl fmt::Display for BodyError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str("error writing body")
}
}
impl error::Error for BodyError {}
pub(crate) enum BodyPart {
Chunk(Bytes),
Done,
}
pub(crate) enum RawBodyInner {
Empty,
Single(Bytes),
Stream {
receiver: mpsc::Receiver<BodyPart>,
polled: Option<oneshot::Sender<()>>,
},
}
#[pin_project]
pub struct RawBody {
pub(crate) inner: RawBodyInner,
#[pin]
_p: PhantomPinned,
}
impl RawBody {
pub(crate) fn new<T>(body: Option<Pin<&mut T>>) -> (RawBody, Writer<'_, T>)
where
T: ?Sized + Body,
{
let body = match body {
Some(body) => body,
None => {
return (
RawBody {
inner: RawBodyInner::Empty,
_p: PhantomPinned,
},
Writer::Nop,
)
}
};
match body.full_body() {
Some(body) => (
RawBody {
inner: RawBodyInner::Single(body),
_p: PhantomPinned,
},
Writer::Nop,
),
None => {
let (body_sender, body_receiver) = mpsc::channel(1);
let (polled_sender, polled_receiver) = oneshot::channel();
(
RawBody {
inner: RawBodyInner::Stream {
receiver: body_receiver,
polled: Some(polled_sender),
},
_p: PhantomPinned,
},
Writer::Streaming {
polled: polled_receiver,
body,
sender: body_sender,
},
)
}
}
}
}
impl http_body::Body for RawBody {
type Data = Cursor<Bytes>;
type Error = BodyError;
fn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
let this = self.project();
match mem::replace(this.inner, RawBodyInner::Empty) {
RawBodyInner::Empty => Poll::Ready(None),
RawBodyInner::Single(chunk) => Poll::Ready(Some(Ok(Cursor::new(chunk)))),
RawBodyInner::Stream {
mut receiver,
mut polled,
} => {
if let Some(polled) = polled.take() {
let _ = polled.send(());
}
match Pin::new(&mut receiver).poll_next(cx) {
Poll::Ready(Some(BodyPart::Chunk(bytes))) => {
*this.inner = RawBodyInner::Stream { receiver, polled };
Poll::Ready(Some(Ok(Cursor::new(bytes))))
}
Poll::Ready(Some(BodyPart::Done)) => Poll::Ready(None),
Poll::Ready(None) => Poll::Ready(Some(Err(BodyError(())))),
Poll::Pending => {
*this.inner = RawBodyInner::Stream { receiver, polled };
Poll::Pending
}
}
}
}
}
fn poll_trailers(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
Poll::Ready(Ok(None))
}
fn is_end_stream(&self) -> bool {
matches!(self.inner, RawBodyInner::Empty)
}
}
pub(crate) enum Writer<'a, T>
where
T: ?Sized,
{
Nop,
Streaming {
polled: oneshot::Receiver<()>,
body: Pin<&'a mut T>,
sender: mpsc::Sender<BodyPart>,
},
}
impl<'a, T> Writer<'a, T>
where
T: ?Sized + Body,
{
pub async fn write(self) -> Result<(), Error> {
match self {
Writer::Nop => Ok(()),
Writer::Streaming {
polled,
body,
sender,
} => {
if polled.await.is_err() {
debug!("hyper hung up before polling request body");
return Ok(());
}
let writer = BodyWriter::new(sender);
pin_mut!(writer);
body.write(writer.as_mut()).await?;
writer.finish().await.map_err(Error::internal_safe)?;
Ok(())
}
}
}
}