MCPcopy Index your code
hub / github.com/cetra3/mpart-async

github.com/cetra3/mpart-async @0.7.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.7.0 ↗ · + Follow
70 symbols 113 edges 7 files 22 documented · 31% updated 2y ago★ 333 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Rust Multipart Async

This crate allows the creation of client/server multipart streams for use with std futures.

Quick Usage

With clients, you want to create a MultipartRequest & add in your fields & files.

Hyper Client Example

Here is an example of how to use the client with hyper (cargo run --example hyper):

use hyper::{header::CONTENT_TYPE, Body, Client, Request};
use hyper::{service::make_service_fn, service::service_fn, Response, Server};
use mpart_async::client::MultipartRequest;

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[tokio::main]
async fn main() -> Result<(), Error> {
    //Setup a mock server to accept connections.
    setup_server();

    let client = Client::new();

    let mut mpart = MultipartRequest::default();

    mpart.add_field("foo", "bar");
    mpart.add_file("test", "Cargo.toml");

    let request = Request::post("http://localhost:3000")
        .header(
            CONTENT_TYPE,
            format!("multipart/form-data; boundary={}", mpart.get_boundary()),
        )
        .body(Body::wrap_stream(mpart))?;

    client.request(request).await?;

    Ok(())
}

fn setup_server() {
    let addr = ([127, 0, 0, 1], 3000).into();
    let make_svc = make_service_fn(|_conn| async { Ok::<_, Error>(service_fn(mock)) });
    let server = Server::bind(&addr).serve(make_svc);

    tokio::spawn(server);
}

async fn mock(_: Request<Body>) -> Result<Response<Body>, Error> {
    Ok(Response::new(Body::from("")))
}

Warp Server Example

Here is an example of using it with the warp server (cargo run --example warp):

use warp::Filter;

use bytes::Buf;
use futures::stream::TryStreamExt;
use futures::Stream;
use mime::Mime;
use mpart_async::server::MultipartStream;
use std::convert::Infallible;

#[tokio::main]
async fn main() {
    // Match any request and return hello world!
    let routes = warp::any()
        .and(warp::header::<Mime>("content-type"))
        .and(warp::body::stream())
        .and_then(mpart);

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}

async fn mpart(
    mime: Mime,
    body: impl Stream<Item = Result<impl Buf, warp::Error>> + Unpin,
) -> Result<impl warp::Reply, Infallible> {
    let boundary = mime.get_param("boundary").map(|v| v.to_string()).unwrap();

    let mut stream = MultipartStream::new(
        boundary,
        body.map_ok(|mut buf| buf.copy_to_bytes(buf.remaining())),
    );

    while let Ok(Some(mut field)) = stream.try_next().await {
        println!("Field received:{}", field.name().unwrap());
        if let Ok(filename) = field.filename() {
            println!("Field filename:{}", filename);
        }

        while let Ok(Some(bytes)) = field.try_next().await {
            println!("Bytes received:{}", bytes.len());
        }
    }

    Ok(format!("Thanks!\n"))
}

To interact with the server:

curl -F test=field -F file=@file.txt http://localhost:3030/

Core symbols most depended-on inside this repo

get_dispo_param
called by 13
src/server.rs
add_field
called by 5
src/client.rs
strip_utf8_prefix
called by 4
src/server.rs
add_stream
called by 4
src/client.rs
bytes_and_boundary
called by 3
benches/megabyte_parsing.rs
count_single_field_bytes
called by 3
benches/megabyte_parsing.rs
write_header
called by 3
src/client.rs
get_bytes
called by 3
src/client.rs

Shape

Function 34
Method 21
Class 10
Enum 5

Languages

Rust100%

Modules by API surface

src/server.rs31 symbols
src/client.rs23 symbols
benches/megabyte_parsing.rs7 symbols
src/filestream.rs4 symbols
examples/hyper.rs3 symbols
examples/warp.rs2 symbols

For agents

$ claude mcp add mpart-async \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page