Skip to main content

Module files

Module files 

Source
Expand description

Serving files from disk

serve_file answers a request with a file: Content-Type from the extension, Last-Modified and a weak ETag, conditional requests (304), byte ranges (206, 416, If-Range) for resumable downloads and media seeking, and bodies streamed in chunks so large files never sit in memory. HEAD works through the router.

use ullun::files::{serve_file, FileOptions};
use ullun::prelude::*;

async fn report(req: Request) -> Result<Response> {
    // path chosen by the server, never built from client input
    serve_file(&req, "/var/data/report.pdf", &FileOptions::new().attachment("report.pdf")).await
}

The caller is responsible for the path: never join client-supplied names onto a directory without validating them (see App::serve_static, which does).

Structs§

FileOptions
Per-response settings

Functions§

content_type_for
Content-Type for a path, by extension (application/octet-stream if unknown)
http_date
HTTP date (IMF-fixdate), e.g. Sun, 06 Nov 1994 08:49:37 GMT
http_date_now
Current time as an HTTP date
iso8601
ISO 8601 / RFC 3339 UTC timestamp, e.g. 1994-11-06T08:49:37Z
parse_http_date
Parse an IMF-fixdate (the only format servers must generate)
serve_file
Serve path, honouring conditional and range headers of req