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
use std::{
convert::Infallible,
pin::Pin,
task::{Context, Poll},
};
use bytes::{Buf, Bytes};
use http::HeaderMap;
use http_body::{Body, Frame};
use crate::util::BufList;
#[derive(Debug)]
pub struct Collected<B> {
bufs: BufList<B>,
trailers: Option<HeaderMap>,
}
impl<B: Buf> Collected<B> {
pub fn trailers(&self) -> Option<&HeaderMap> {
self.trailers.as_ref()
}
pub fn aggregate(self) -> impl Buf {
self.bufs
}
pub fn to_bytes(mut self) -> Bytes {
self.bufs.copy_to_bytes(self.bufs.remaining())
}
pub(crate) fn push_frame(&mut self, frame: Frame<B>) {
if frame.is_data() {
let data = frame.into_data().unwrap();
self.bufs.push(data);
} else if frame.is_trailers() {
let trailers = frame.into_trailers().unwrap();
if let Some(current) = &mut self.trailers {
current.extend(trailers.into_iter());
} else {
self.trailers = Some(trailers);
}
}
}
}
impl<B: Buf> Body for Collected<B> {
type Data = B;
type Error = Infallible;
fn poll_frame(
mut self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
let frame = if let Some(data) = self.bufs.pop() {
Frame::data(data)
} else if let Some(trailers) = self.trailers.take() {
Frame::trailers(trailers)
} else {
return Poll::Ready(None);
};
Poll::Ready(Some(Ok(frame)))
}
}
impl<B> Default for Collected<B> {
fn default() -> Self {
Self {
bufs: BufList::default(),
trailers: None,
}
}
}
impl<B> Unpin for Collected<B> {}
#[cfg(test)]
mod tests {
use std::convert::{Infallible, TryInto};
use futures_util::stream;
use crate::{BodyExt, Full, StreamBody};
use super::*;
#[tokio::test]
async fn full_body() {
let body = Full::new(&b"hello"[..]);
let buffered = body.collect().await.unwrap();
let mut buf = buffered.to_bytes();
assert_eq!(&buf.copy_to_bytes(buf.remaining())[..], &b"hello"[..]);
}
#[tokio::test]
async fn segmented_body() {
let bufs = [&b"hello"[..], &b"world"[..], &b"!"[..]];
let body = StreamBody::new(stream::iter(bufs.map(Frame::data).map(Ok::<_, Infallible>)));
let buffered = body.collect().await.unwrap();
let mut buf = buffered.to_bytes();
assert_eq!(&buf.copy_to_bytes(buf.remaining())[..], b"helloworld!");
}
#[tokio::test]
async fn delayed_segments() {
let one = stream::once(async { Ok::<_, Infallible>(Frame::data(&b"hello "[..])) });
let two = stream::once(async {
tokio::task::yield_now().await;
Ok::<_, Infallible>(Frame::data(&b"world!"[..]))
});
let stream = futures_util::StreamExt::chain(one, two);
let body = StreamBody::new(stream);
let buffered = body.collect().await.unwrap();
let mut buf = buffered.to_bytes();
assert_eq!(&buf.copy_to_bytes(buf.remaining())[..], b"hello world!");
}
#[tokio::test]
async fn trailers() {
let mut trailers = HeaderMap::new();
trailers.insert("this", "a trailer".try_into().unwrap());
let bufs = [
Frame::data(&b"hello"[..]),
Frame::data(&b"world!"[..]),
Frame::trailers(trailers.clone()),
];
let body = StreamBody::new(stream::iter(bufs.map(Ok::<_, Infallible>)));
let buffered = body.collect().await.unwrap();
assert_eq!(&trailers, buffered.trailers().unwrap());
let mut buf = buffered.to_bytes();
assert_eq!(&buf.copy_to_bytes(buf.remaining())[..], b"helloworld!");
}
}