Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multipart_async_stream"
version = "0.1.1"
version = "0.2.2"
edition = "2024"
license = "MIT OR Apache-2.0"
description = "An easy-to-use, efficient, and asynchronous multipart stream parser."
Expand All @@ -14,12 +14,14 @@ categories = [
]

[dependencies]
async-iterator = "2.3.0"
bytes = "1.10.1"
constcat = "0.6.1"
futures-util = { version = "0.3.31", features = ["tokio-io"] }
http = "1.3.1"
httparse = "1.10.1"
memchr = "2.7.5"
thiserror = "2.0.15"
thiserror = "2.0.16"

[dev-dependencies]
reqwest = { version = "0.12.23", features = ["stream"] }
Expand Down
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
# Multipart Stream

![alt text](https://img.shields.io/crates/v/multipart_async_stream.svg)


![alt text](https://docs.rs/multipart_async_stream/badge.svg)


![alt text](https://github.com/OpenTritium/multipart_stream/actions/workflows/ci.yaml/badge.svg)
![alt text](https://img.shields.io/crates/v/multipart_async_stream.svg) ![alt text](https://docs.rs/multipart_async_stream/badge.svg) ![alt text](https://github.com/OpenTritium/multipart_stream/actions/workflows/ci.yaml/badge.svg)

This library is designed as an adapter for `futures_util::TryStream`, allowing for easy parsing of an incoming byte stream (such as from an HTTP response) and splitting it into multiple parts (`Part`). It is especially useful for handling `multipart/byteranges` HTTP responses.

A common use case is sending an HTTP Range request to a server and then parsing the resulting `multipart/byteranges` response body.
The example below demonstrates how to use reqwest to download multiple ranges of a file and parse the individual parts using multipart_stream.

```rust
use http::header::CONTENT_TYPE;
use multipart_async_stream::{LendingIterator, MultipartStream, TryStreamExt, header::CONTENT_TYPE};

#[tokio::main]
async fn main() {
const URL: &str = "https://mat1.gtimg.com/pingjs/ext2020/newom/build/static/images/new_logo.png";
let client = reqwest::Client::new();
let response = client
.get(URL)
.header("Range", "bytes=0-32,64-128")
.send()
.await.unwrap();
let response = client.get(URL).header("Range", "bytes=0-31,64-127").send().await.unwrap();
let boundary = response
.headers()
.get(CONTENT_TYPE)
Expand All @@ -33,9 +23,23 @@ async fn main() {
.and_then(|s| s.split("boundary=").nth(1))
.map(|s| s.trim().as_bytes().to_vec().into_boxed_slice());
let s = response.bytes_stream();
let mut m = multipart_async_stream::MultipartStream::new(s, &boundary.unwrap());
while let Ok(x) = m.try_next().await {
println!("Part: {x:?}");
let mut m = MultipartStream::new(s, &boundary.unwrap());

while let Some(Ok(part)) = m.next().await {
println!("{:?}", part.headers());
let mut body = part.body();
while let Ok(Some(b)) = body.try_next().await {
println!("{:?}", b);
}
}
}
```

The output of the program above is:

```bash
{"content-type": "image/png", "content-range": "bytes 0-31/10845"}
b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR\0\0\0\xf4\0\0\0B\x08\x06\0\0\0`\xbc\xfb"
{"content-type": "image/png", "content-range": "bytes 64-127/10845"}
b"L:com.adobe.xmp\0\0\0\0\0<?xpacket begin=\"\xef\xbb\xbf\" id=\"W5M0MpCehiHzreSzNT"
```
13 changes: 9 additions & 4 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use http::header::CONTENT_TYPE;
use multipart_async_stream::{LendingIterator, MultipartStream, TryStreamExt, header::CONTENT_TYPE};

#[tokio::main]
async fn main() {
Expand All @@ -13,8 +13,13 @@ async fn main() {
.and_then(|s| s.split("boundary=").nth(1))
.map(|s| s.trim().as_bytes().to_vec().into_boxed_slice());
let s = response.bytes_stream();
let mut m = multipart_async_stream::MultipartStream::new(s, &boundary.unwrap());
while let Ok(x) = m.try_next().await {
println!("Part: {x:?}");
let mut m = MultipartStream::new(s, &boundary.unwrap());

while let Some(Ok(part)) = m.next().await {
println!("{:?}", part.headers());
let mut body = part.body();
while let Ok(Some(b)) = body.try_next().await {
println!("{:?}", b);
}
}
}
32 changes: 23 additions & 9 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions fuzz/fuzz_targets/parse_stream.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![no_main]

use bytes::Bytes;
use futures_util::{TryStream, stream};
use futures_util::stream;
use libfuzzer_sys::fuzz_target;
use multipart_async_stream::MultipartStream;
use multipart_async_stream::{LendingIterator, MultipartStream, TryStream, TryStreamExt};
use std::convert::Infallible;
use tokio::runtime::Builder;

Expand All @@ -26,9 +25,9 @@ fn do_fuzz(data: &[u8]) {

rt.block_on(async {
let s = create_stream_from_chunks(body, 32);
let mut stream = MultipartStream::new(s, boundary);
let mut m = MultipartStream::new(s, boundary);
loop {
if stream.try_next().await.is_err() {
if m.next().await.is_some_and(|result| result.is_err()) {
break;
}
}
Expand Down
Loading
Loading