All Projects → rousan → multer-rs

rousan / multer-rs

Licence: MIT license
An async parser for multipart/form-data content-type in Rust

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to multer-rs

Android Upload Service
Easily upload files (Multipart/Binary/FTP out of the box) in the background with progress notification. Support for persistent upload requests, customizations and custom plugins.
Stars: ✭ 2,593 (+2278.9%)
Mutual labels:  multipart-uploads, multipart-formdata
pipedream
Easy multipart uploads for Amazon S3, DigitalOcean Spaces and S3-compatible services
Stars: ✭ 17 (-84.4%)
Mutual labels:  multipart-uploads
streaming-form-data
Streaming parser for multipart/form-data written in Cython
Stars: ✭ 112 (+2.75%)
Mutual labels:  multipart-formdata
MultipartUploadBundle
Symfony multipart/related/mixed/alternative content type handler (rfc1341).
Stars: ✭ 21 (-80.73%)
Mutual labels:  multipart-uploads
vuejs-uploader
Vue multipart file uploader
Stars: ✭ 62 (-43.12%)
Mutual labels:  multipart-uploads
laravel-uppy-s3-multipart-upload
Multipart Uploads using Laravel, AWS S3, and Uppy
Stars: ✭ 30 (-72.48%)
Mutual labels:  multipart-uploads
rustypaste
A minimal file upload/pastebin service.
Stars: ✭ 102 (-6.42%)
Mutual labels:  multipart-uploads
aws-s3-multipart-upload
Example AWS S3 Multipart upload with aws-sdk for Go - Retries for failing parts
Stars: ✭ 34 (-68.81%)
Mutual labels:  multipart-uploads
cpplipa
C++ library package
Stars: ✭ 17 (-84.4%)
Mutual labels:  multipart-formdata
nativescript-http
The best way to do HTTP requests in NativeScript, a drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data support and certificate pinning
Stars: ✭ 32 (-70.64%)
Mutual labels:  multipart-formdata
rocket-multipart-form-data
This crate provides a multipart parser for the Rocket framework.
Stars: ✭ 34 (-68.81%)
Mutual labels:  multipart-formdata

Github Actions Status crates.io Documentation MIT

multer-rs

An async parser for multipart/form-data content-type in Rust.

It accepts a Stream of Bytes as a source, so that It can be plugged into any async Rust environment e.g. any async server.

Docs

Install

Add this to your Cargo.toml:

[dependencies]
multer = "2.0"

Basic Example

use bytes::Bytes;
use futures::stream::Stream;
// Import multer types.
use multer::Multipart;
use std::convert::Infallible;
use futures::stream::once;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Generate a byte stream and the boundary from somewhere e.g. server request body.
    let (stream, boundary) = get_byte_stream_from_somewhere().await;

    // Create a `Multipart` instance from that byte stream and the boundary.
    let mut multipart = Multipart::new(stream, boundary);

    // Iterate over the fields, use `next_field()` to get the next field.
    while let Some(mut field) = multipart.next_field().await? {
        // Get field name.
        let name = field.name();
        // Get the field's filename if provided in "Content-Disposition" header.
        let file_name = field.file_name();

        println!("Name: {:?}, File Name: {:?}", name, file_name);

        // Process the field data chunks e.g. store them in a file.
        while let Some(chunk) = field.chunk().await? {
            // Do something with field chunk.
            println!("Chunk: {:?}", chunk);
        }
    }

    Ok(())
}

// Generate a byte stream and the boundary from somewhere e.g. server request body.
async fn get_byte_stream_from_somewhere() -> (impl Stream<Item = Result<Bytes, Infallible>>, &'static str) {
    let data = "--X-BOUNDARY\r\nContent-Disposition: form-data; name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n";
    let stream = once(async move { Result::<Bytes, Infallible>::Ok(Bytes::from(data)) });
    
    (stream, "X-BOUNDARY")
}

Prevent Denial of Service (DoS) Attacks

This crate also provides some APIs to prevent potential DoS attacks with fine grained control. It's recommended to add some constraints on field (specially text field) size to prevent DoS attacks exhausting the server's memory.

An example:

use multer::{Multipart, Constraints, SizeLimit};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create some constraints to be applied to the fields to prevent DoS attack.
    let constraints = Constraints::new()
         // We only accept `my_text_field` and `my_file_field` fields,
         // For any unknown field, we will throw an error.
         .allowed_fields(vec!["my_text_field", "my_file_field"])
         .size_limit(
             SizeLimit::new()
                 // Set 15mb as size limit for the whole stream body.
                 .whole_stream(15 * 1024 * 1024)
                 // Set 10mb as size limit for all fields.
                 .per_field(10 * 1024 * 1024)
                 // Set 30kb as size limit for our text field only.
                 .for_field("my_text_field", 30 * 1024),
         );

    // Create a `Multipart` instance from a stream and the constraints.
    let mut multipart = Multipart::with_constraints(some_stream, "X-BOUNDARY", constraints);

    while let Some(field) = multipart.next_field().await.unwrap() {
        let content = field.text().await.unwrap();
        assert_eq!(content, "abcd");
    } 
   
    Ok(())
}

Usage with hyper.rs server

An example showing usage with hyper.rs.

For more examples, please visit examples.

Contributing

Your PRs and suggestions are always welcome.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].