Skip to content

Commit 97f0b3a

Browse files
committed
Expose a Form::into_stream() method on async multipart forms.
The async equivalent of the change in seanmonstar#2524. An example use case is compressing multipart form data with zstd. The entire contents of the payload have to be compressed together, which requires accessing the contents of the Form. The existing stream functionality mostly implements what we need, but just isn't publicly accessible. This PR adds a pub method for it.
1 parent 8b8fdd2 commit 97f0b3a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/async_impl/multipart.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,21 @@ impl Form {
140140
}
141141

142142
/// Consume this instance and transform into an instance of Body for use in a request.
143-
pub(crate) fn stream(mut self) -> Body {
143+
pub(crate) fn stream(self) -> Body {
144144
if self.inner.fields.is_empty() {
145145
return Body::empty();
146146
}
147147

148+
Body::stream(self.into_stream())
149+
}
150+
151+
/// Produce a stream of the bytes in this `Form`, consuming it.
152+
pub fn into_stream(mut self) -> impl Stream<Item = Result<Bytes, crate::Error>> + Send + Sync {
153+
if self.inner.fields.is_empty() {
154+
let empty_stream: Pin<Box<dyn Stream<Item = Result<Bytes, crate::Error>> + Send + Sync>> = Box::pin(futures_util::stream::empty());
155+
return empty_stream;
156+
}
157+
148158
// create initial part to init reduce chain
149159
let (name, part) = self.inner.fields.remove(0);
150160
let start = Box::pin(self.part_stream(name, part))
@@ -161,7 +171,7 @@ impl Form {
161171
let last = stream::once(future::ready(Ok(
162172
format!("--{}--\r\n", self.boundary()).into()
163173
)));
164-
Body::stream(stream.chain(last))
174+
Box::pin(stream.chain(last))
165175
}
166176

167177
/// Generate a hyper::Body stream for a single Part instance of a Form request.

0 commit comments

Comments
 (0)