ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

ExtractorsBuilt-in ExtractorsValidationCustom ExtractorsMultipart

Protocols

Integrations

Operating

Reference

Extractors

Multipart

Read file uploads part by part, under a server body limit and a tighter per-route one.

Multipart reads a multipart/form-data body one part at a time, so a handler never holds the whole upload. The example is examples/19-http-uploads.

use ulo_http::{Bytes, CallError, Created, ErrorKind, KB, MaxBody, Multipart, Path};

impl UploadController {
    /// Stores every part that carries a file name and answers where the first one went.
    async fn store(&self, mut parts: Multipart) -> Result<Created<&'static str>, BoxError> {
        let mut first = None;
        while let Some(field) = parts.next_field().await? {
            let Some(name) = field.file_name().map(str::to_owned) else { continue };
            let content = field.bytes().await?;
            self.files.stored.lock().unwrap().insert(name.clone(), content);
            first.get_or_insert(name);
        }
        let Some(name) = first else {
            return Err(CallError::new(ErrorKind::BadRequest, "the form carried no file").into());
        };
        Ok(Created::at(format!("/files/{name}"), "stored"))
    }
}

#[handlers]
impl UploadController {
    #[ulo_http::post("/files")]
    async fn upload(&self, parts: Multipart) -> Result<Created<&'static str>, BoxError> {
        self.store(parts).await
    }
}

next_field yields the next part, None after the last. A part is ulo_http::multipart::Field, with file_name(), bytes() and the other multer accessors; ulo_http::multipart::Error is the error type of a failed read. Created::at(location, body) answers 201 with a Location header.

Limits

The server (ulo_http_hyper::Server) sets a body limit, and a route can override it with #[meta(MaxBody(..))]:

/// What a body may weigh unless its route says otherwise.
pub const SERVER_LIMIT: u64 = KB;
/// The tighter limit on `/avatars`.
pub const AVATAR_LIMIT: u64 = 256;

pub fn server(endpoint: &str) -> Server {
    Server::new(endpoint).max_body(SERVER_LIMIT)
}

#[ulo_http::post("/avatars")]
#[meta(MaxBody(AVATAR_LIMIT))]
async fn avatar(&self, parts: Multipart) -> Result<Created<&'static str>, BoxError> {
    self.store(parts).await
}

KB and MB are byte constants for the builder, as in .max_body(2 * MB). The server's limit is 2 MiB unless set. The route's limit replaces the server's for that route.

tests/uploads.rs asserts the answers:

RequestAnswer
a form with a file under the limit201, Location: /files/a.txt, the bytes read back unchanged
a body over the server limit413, and nothing stored
the same body to /files and /avatars201 on /files, 413 on /avatars
a form under the route limit, boundaries included201
a body that breaks the boundary400
a form with no file400, with the handler's message
a body that is not multipart/form-data415

A declared Content-Length over the limit is refused as 413 before any part is read. A body that turns out longer than the limit while parts are read fails the next_field or bytes call with a multer::Error, and a handler that returns it with ? answers 413 as well. Any other multer::Error returned that way, such as a broken boundary, answers 400.

Where the limit is read

MaxBody is HTTP metadata and fails to compile on a handler of another transport. The same limit applies to Json, Form, Bytes and BodyStream. See Limits and timeouts for the other server settings.

Custom Extractors

Implement FromCall to give handlers a parameter type of your own.

HTTP Servers

The two ways to serve an Ulo application over HTTP, a server crate that owns the sockets or a host framework that does, and how to choose between them.

On this page

LimitsWhere the limit is read