#multipart-form #form-data #async #byte-stream #multipart #multipart-uploads

multer

Rust中用于解析multipart/form-data内容类型的异步解析器

16个稳定版本

3.1.0 2024年5月4日
3.0.0 2023年12月14日
2.1.0 2023年3月31日
2.0.4 2022年9月21日
1.2.2 2020年7月22日

#17 in 异步

Download history 256808/week @ 2024-05-02 257004/week @ 2024-05-09 290029/week @ 2024-05-16 297462/week @ 2024-05-23 307698/week @ 2024-05-30 309234/week @ 2024-06-06 307083/week @ 2024-06-13 308660/week @ 2024-06-20 337169/week @ 2024-06-27 277898/week @ 2024-07-04 290762/week @ 2024-07-11 290863/week @ 2024-07-18 308631/week @ 2024-07-25 302889/week @ 2024-08-01 336651/week @ 2024-08-08 357033/week @ 2024-08-15

1,362,403 每月下载量
用于 795 个crate(55直接)

MIT 许可证

73KB
1K SLoC

GitHub Actions Status crates.io Documentation MIT

multer-rs

Rust中用于解析multipart/form-data内容类型的异步解析器。

它接受一个 StreamBytes 作为源,因此它可以连接到任何异步Rust环境,例如任何异步服务器。

文档

安装

将此添加到您的 Cargo.toml

[dependencies]
multer = "2.0"

基本示例

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")
}

防止拒绝服务(DoS)攻击

此crate还提供了一些API,可以通过精细控制防止潜在的DoS攻击。建议在字段(尤其是文本字段)大小上添加一些限制,以防止DoS攻击耗尽服务器的内存。

示例

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(())
}

hyper.rs 服务器一起使用

一个使用 hyper.rs示例

有关更多示例,请访问 示例

贡献

您的PR和建议总是受欢迎的。

依赖关系

~4.5–6.5MB
~177K SLoC